Commit e4327f77 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Allow to edit when we double click

parent 2a0bfbfc
Pipeline #51195 failed with stage
in 8 minutes and 30 seconds
......@@ -28,6 +28,7 @@ GroupManagementWidget::GroupManagementWidget(QWidget *parent)
mainLayout->addWidget(mListWidget);
mListWidget->setSelectionMode(QAbstractItemView::MultiSelection);
mListWidget->setContextMenuPolicy(Qt::CustomContextMenu);
connect(mListWidget, &QListWidget::itemDoubleClicked, this, &GroupManagementWidget::renameGroup);
connect(mListWidget, &QListWidget::customContextMenuRequested, this, &GroupManagementWidget::slotCustomContextMenu);
init();
}
......@@ -36,6 +37,27 @@ GroupManagementWidget::~GroupManagementWidget()
{
}
void GroupManagementWidget::renameGroup(QListWidgetItem *item)
{
if (!item) {
return;
}
const QString fullPath = item->data(FullPathRole).toString();
QFile f(fullPath);
const QString newName = QInputDialog::getText(this, i18n("Rename Group"), i18n("New Name:"));
const QString newNameTrimmed = newName.trimmed();
if (!newNameTrimmed.isEmpty()) {
const QString newFullPath{LoadGroupMenu::defaultWritableGroupPath() + QLatin1Char('/') + newNameTrimmed};
if (!f.rename(newFullPath)) {
KMessageBox::error(this, i18n("Impossible to rename group as \'%1\'", newNameTrimmed), i18n("Rename Group"));
} else {
item->setText(newNameTrimmed);
item->setData(FullPathRole, newFullPath);
Q_EMIT groupsChanged();
}
}
}
void GroupManagementWidget::slotCustomContextMenu()
{
const auto items = mListWidget->selectedItems();
......@@ -44,20 +66,7 @@ void GroupManagementWidget::slotCustomContextMenu()
if (mListWidget->selectedItems().count() == 1) {
const auto item = items.at(0);
menu.addAction(QIcon::fromTheme(QStringLiteral("edit")), i18n("Rename Groups"), this, [this, item]() {
const QString fullPath = item->data(FullPathRole).toString();
QFile f(fullPath);
const QString newName = QInputDialog::getText(this, i18n("Rename Group"), i18n("New Name:"));
const QString newNameTrimmed = newName.trimmed();
if (!newNameTrimmed.isEmpty()) {
const QString newFullPath{LoadGroupMenu::defaultWritableGroupPath() + QLatin1Char('/') + newNameTrimmed};
if (!f.rename(newFullPath)) {
KMessageBox::error(this, i18n("Impossible to rename group as \'%1\'", newNameTrimmed), i18n("Rename Group"));
} else {
item->setText(newNameTrimmed);
item->setData(FullPathRole, newFullPath);
Q_EMIT groupsChanged();
}
}
renameGroup(item);
});
menu.addSeparator();
}
......
......@@ -12,6 +12,7 @@
#include "libkdebugsettings_private_export.h"
class QListWidget;
class QListWidgetItem;
class LIBKDEBUGSETTINGS_EXPORT_TEST_EXPORT GroupManagementWidget : public QWidget
{
Q_OBJECT
......@@ -29,6 +30,7 @@ private:
enum { FullPathRole = Qt::UserRole + 1 };
void init();
QListWidget *const mListWidget;
void renameGroup(QListWidgetItem *item);
};
#endif // GROUPMANAGEMENTWIDGET_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment