Verified Commit ace70fca authored by Daniel Vrátil's avatar Daniel Vrátil 🤖
Browse files

Cleanup and simplify RenameFavoriteDialog

* make it a private include
* wrap it in Akonadi namespace
* use UI file for UI
* simplify code
parent 0fd20954
......@@ -52,6 +52,7 @@ set(akonadiwidgets_UI
controlprogressindicator.ui
erroroverlay.ui
manageaccountwidget.ui
renamefavoritedialog.ui
selftestdialog.ui
subscriptiondialog.ui
tageditwidget.ui
......
......@@ -17,69 +17,33 @@
02110-1301, USA.
*/
#include "renamefavoritedialog.h"
#include <QLabel>
#include <QLineEdit>
#include "renamefavoritedialog_p.h"
#include <KLocalizedString>
#include <QVBoxLayout>
#include <QDialogButtonBox>
#include <QPushButton>
RenameFavoriteDialog::RenameFavoriteDialog(const QString &caption, const QString &text, const QString &value, const QString &defaultName, QWidget *parent)
using namespace Akonadi;
RenameFavoriteDialog::RenameFavoriteDialog(const QString &value, const QString &defaultName, QWidget *parent)
: QDialog(parent)
, m_defaultName(defaultName)
{
setWindowTitle(caption);
setModal(true);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
m_label = new QLabel(text, this);
m_label->setWordWrap(true);
mainLayout->addWidget(m_label);
m_lineEdit = new QLineEdit(value, this);
m_lineEdit->setClearButtonEnabled(true);
mainLayout->addWidget(m_lineEdit);
m_lineEdit->setFocus();
m_label->setBuddy(m_lineEdit);
mainLayout->addStretch();
connect(m_lineEdit, &QLineEdit::textChanged, this, &RenameFavoriteDialog::slotEditTextChanged);
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
mOkButton = buttonBox->button(QDialogButtonBox::Ok);
QPushButton *defaultButtonName = new QPushButton(i18n("Default Name"), this);
buttonBox->addButton(defaultButtonName, QDialogButtonBox::ActionRole);
connect(defaultButtonName, &QPushButton::clicked, this, &RenameFavoriteDialog::slotDefaultName);
mOkButton->setDefault(true);
mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
connect(buttonBox, &QDialogButtonBox::accepted, this, &RenameFavoriteDialog::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &RenameFavoriteDialog::reject);
mainLayout->addWidget(buttonBox);
slotEditTextChanged(value);
setMinimumWidth(350);
}
RenameFavoriteDialog::~RenameFavoriteDialog()
{
}
void RenameFavoriteDialog::slotDefaultName()
{
m_lineEdit->setText(m_defaultName);
}
void RenameFavoriteDialog::slotEditTextChanged(const QString &text)
{
mOkButton->setEnabled(!text.trimmed().isEmpty());
ui.setupUi(this);
connect(ui.lineEdit, &QLineEdit::textChanged,
this, [this](const QString &text) {
ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!text.trimmed().isEmpty());
});
connect(ui.buttonBox, &QDialogButtonBox::accepted, this, &RenameFavoriteDialog::accept);
connect(ui.buttonBox, &QDialogButtonBox::rejected, this, &RenameFavoriteDialog::reject);
connect(ui.buttonBox->button(QDialogButtonBox::RestoreDefaults), &QPushButton::clicked,
this, [this]() { ui.lineEdit->setText(m_defaultName); });
ui.lineEdit->setText(value);
}
QString RenameFavoriteDialog::newName() const
{
return m_lineEdit->text();
return ui.lineEdit->text();
}
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>RenameFavoriteDialog</class>
<widget class="QDialog" name="RenameFavoriteDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>108</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>350</width>
<height>0</height>
</size>
</property>
<property name="windowTitle">
<string>Rename Favorite</string>
</property>
<property name="modal">
<bool>true</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Name:</string>
</property>
<property name="buddy">
<cstring>lineEdit</cstring>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit"/>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::RestoreDefaults</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
......@@ -21,31 +21,25 @@
#define RENAMEFAVORITEDIALOG_H
#include <QDialog>
#include "collection.h"
class QLabel;
class QLineEdit;
class QPushButton;
#include "ui_renamefavoritedialog.h"
namespace Akonadi
{
class RenameFavoriteDialog : public QDialog
{
Q_OBJECT
public:
explicit RenameFavoriteDialog(const QString &caption, const QString &text, const QString &value, const QString &defaultName, QWidget *parent);
~RenameFavoriteDialog();
explicit RenameFavoriteDialog(const QString &value, const QString &defaultName, QWidget *parent);
Q_REQUIRED_RESULT QString newName() const;
protected:
protected Q_SLOTS:
void slotEditTextChanged(const QString &text);
void slotDefaultName();
private:
QString m_defaultName;
QLabel *m_label = nullptr;
QLineEdit *m_lineEdit = nullptr;
QPushButton *mOkButton = nullptr;
Ui::RenameFavoriteDialog ui;
};
}
#endif /* RENAMEFAVORITEDIALOG_H */
......@@ -38,7 +38,7 @@
#include "specialcollectionattribute.h"
#include "collectionpropertiesdialog.h"
#include "subscriptiondialog.h"
#include "renamefavoritedialog.h"
#include "renamefavoritedialog_p.h"
#include "trashjob.h"
#include "trashrestorejob.h"
#include "entitydeletedattribute.h"
......@@ -1041,7 +1041,10 @@ public:
const Collection collection = index.data(CollectionModel::CollectionRole).value<Collection>();
Q_ASSERT(collection.isValid());
QPointer<RenameFavoriteDialog> dlg(new RenameFavoriteDialog(contextText(StandardActionManager::RenameFavoriteCollection, StandardActionManager::DialogTitle), contextText(StandardActionManager::RenameFavoriteCollection, StandardActionManager::DialogText), favoritesModel->favoriteLabel(collection), collection.displayName(), parentWidget));
QPointer<RenameFavoriteDialog> dlg(new RenameFavoriteDialog(
favoritesModel->favoriteLabel(collection),
collection.displayName(),
parentWidget));
if (dlg->exec() == QDialog::Accepted) {
favoritesModel->setFavoriteLabel(collection, dlg->newName());
}
......
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