refactor sessiondialog a bit

- made code more local by moving clicked slot to lambda
 - use class initializations
 - move default value inside the constructor to reduce arguments

Change-Id: I98735a0b4c69ea538ed1af6133ef1054039a1b11
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
Tim Jenssen
2017-05-29 13:45:19 +02:00
parent 76185b3b09
commit 89639b0bf3
3 changed files with 11 additions and 17 deletions

View File

@@ -78,21 +78,23 @@ void SessionValidator::fixup(QString &input) const
input = copy; input = copy;
} }
SessionNameInputDialog::SessionNameInputDialog(const QStringList &sessions, QWidget *parent) SessionNameInputDialog::SessionNameInputDialog(QWidget *parent)
: QDialog(parent), m_usedSwitchTo(false) : QDialog(parent)
{ {
auto hlayout = new QVBoxLayout(this); auto hlayout = new QVBoxLayout(this);
auto label = new QLabel(tr("Enter the name of the session:"), this); auto label = new QLabel(tr("Enter the name of the session:"), this);
hlayout->addWidget(label); hlayout->addWidget(label);
m_newSessionLineEdit = new QLineEdit(this); m_newSessionLineEdit = new QLineEdit(this);
m_newSessionLineEdit->setValidator(new SessionValidator(this, sessions)); m_newSessionLineEdit->setValidator(new SessionValidator(this, SessionManager::sessions()));
hlayout->addWidget(m_newSessionLineEdit); hlayout->addWidget(m_newSessionLineEdit);
auto buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this); auto buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
buttons->button(QDialogButtonBox::Ok)->setText(tr("&Create")); buttons->button(QDialogButtonBox::Ok)->setText(tr("&Create"));
m_switchToButton = buttons->addButton(tr("Create and &Open"), QDialogButtonBox::AcceptRole); m_switchToButton = buttons->addButton(tr("Create and &Open"), QDialogButtonBox::AcceptRole);
connect(m_switchToButton, &QPushButton::clicked, [this]() {
m_usedSwitchTo = true;
});
connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject); connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
connect(buttons, &QDialogButtonBox::clicked, this, &SessionNameInputDialog::clicked);
hlayout->addWidget(buttons); hlayout->addWidget(buttons);
setLayout(hlayout); setLayout(hlayout);
} }
@@ -107,12 +109,6 @@ QString SessionNameInputDialog::value() const
return m_newSessionLineEdit->text(); return m_newSessionLineEdit->text();
} }
void SessionNameInputDialog::clicked(QAbstractButton *button)
{
if (button == m_switchToButton)
m_usedSwitchTo = true;
}
bool SessionNameInputDialog::isSwitchToRequested() const bool SessionNameInputDialog::isSwitchToRequested() const
{ {
return m_usedSwitchTo; return m_usedSwitchTo;

View File

@@ -59,18 +59,16 @@ class SessionNameInputDialog : public QDialog
Q_OBJECT Q_OBJECT
public: public:
explicit SessionNameInputDialog(const QStringList &sessions, QWidget *parent = nullptr); explicit SessionNameInputDialog(QWidget *parent = nullptr);
void setValue(const QString &value); void setValue(const QString &value);
QString value() const; QString value() const;
bool isSwitchToRequested() const; bool isSwitchToRequested() const;
private: private:
void clicked(QAbstractButton *button); QLineEdit *m_newSessionLineEdit = nullptr;
QPushButton *m_switchToButton = nullptr;
QLineEdit *m_newSessionLineEdit; bool m_usedSwitchTo = false;
QPushButton *m_switchToButton;
bool m_usedSwitchTo;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -224,7 +224,7 @@ void SessionModel::switchToSession(const QString &session)
void SessionModel::runNewSessionDialog(const QString &suggestedName, std::function<void(const QString &)> createSession) void SessionModel::runNewSessionDialog(const QString &suggestedName, std::function<void(const QString &)> createSession)
{ {
SessionNameInputDialog newSessionInputDialog(SessionManager::sessions(), nullptr); SessionNameInputDialog newSessionInputDialog;
newSessionInputDialog.setWindowTitle(tr("New Session Name")); newSessionInputDialog.setWindowTitle(tr("New Session Name"));
newSessionInputDialog.setValue(suggestedName); newSessionInputDialog.setValue(suggestedName);