Utils: Modernize SavedActions

Smaller visible interface.

Change-Id: I5b4a875b208e1202b46780dbe7f91e881a17becb
Reviewed-by: Eike Ziller <eike.ziller@theqtcompany.com>
This commit is contained in:
hjk
2015-05-29 16:51:31 +02:00
parent 32bcc3dc5f
commit 2aacb89ec5
2 changed files with 42 additions and 82 deletions

View File

@@ -43,7 +43,7 @@
#include <QSpinBox> #include <QSpinBox>
#include <QTextEdit> #include <QTextEdit>
using namespace Utils; namespace Utils {
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// //
@@ -229,37 +229,50 @@ void SavedAction::connectWidget(QWidget *widget, ApplyMode applyMode)
QTC_ASSERT(!m_widget, QTC_ASSERT(!m_widget,
qDebug() << "ALREADY CONNECTED: " << widget << m_widget << toString(); return); qDebug() << "ALREADY CONNECTED: " << widget << m_widget << toString(); return);
m_widget = widget; m_widget = widget;
m_applyMode = applyMode;
if (QCheckBox *button = qobject_cast<QCheckBox *>(widget)) { if (auto button = qobject_cast<QCheckBox *>(widget)) {
if (!m_dialogText.isEmpty()) if (!m_dialogText.isEmpty())
button->setText(m_dialogText); button->setText(m_dialogText);
button->setChecked(m_value.toBool()); button->setChecked(m_value.toBool());
if (applyMode == ImmediateApply) {
connect(button, &QCheckBox::clicked, connect(button, &QCheckBox::clicked,
this, &SavedAction::checkableButtonClicked); this, [this, button] { setValue(button->isChecked()); });
} else if (QSpinBox *spinBox = qobject_cast<QSpinBox *>(widget)) { }
} else if (auto spinBox = qobject_cast<QSpinBox *>(widget)) {
spinBox->setValue(m_value.toInt()); spinBox->setValue(m_value.toInt());
if (applyMode == ImmediateApply) {
connect(spinBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), connect(spinBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged),
this, &SavedAction::spinBoxValueChanged); this, [this, spinBox]() { setValue(spinBox->value()); });
} else if (QLineEdit *lineEdit = qobject_cast<QLineEdit *>(widget)) { }
} else if (auto lineEdit = qobject_cast<QLineEdit *>(widget)) {
lineEdit->setText(m_value.toString()); lineEdit->setText(m_value.toString());
if (applyMode == ImmediateApply) {
connect(lineEdit, &QLineEdit::editingFinished, connect(lineEdit, &QLineEdit::editingFinished,
this, &SavedAction::lineEditEditingFinished); this, [this, lineEdit] { setValue(lineEdit->text()); });
} else if (PathChooser *pathChooser = qobject_cast<PathChooser *>(widget)) { }
} else if (auto pathChooser = qobject_cast<PathChooser *>(widget)) {
pathChooser->setPath(m_value.toString()); pathChooser->setPath(m_value.toString());
connect(pathChooser, &PathChooser::editingFinished, if (applyMode == ImmediateApply) {
this, &SavedAction::pathChooserEditingFinished); auto finished = [this, pathChooser] { setValue(pathChooser->path()); };
connect(pathChooser, &PathChooser::browsingFinished, connect(pathChooser, &PathChooser::editingFinished, this, finished);
this, &SavedAction::pathChooserEditingFinished); connect(pathChooser, &PathChooser::browsingFinished, this, finished);
} else if (QGroupBox *groupBox = qobject_cast<QGroupBox *>(widget)) { }
} else if (auto groupBox = qobject_cast<QGroupBox *>(widget)) {
if (!groupBox->isCheckable()) if (!groupBox->isCheckable())
qDebug() << "connectWidget to non-checkable group box" << widget << toString(); qDebug() << "connectWidget to non-checkable group box" << widget << toString();
groupBox->setChecked(m_value.toBool()); groupBox->setChecked(m_value.toBool());
connect(groupBox, &QGroupBox::toggled, this, &SavedAction::groupBoxToggled); if (applyMode == ImmediateApply) {
} else if (QTextEdit *textEdit = qobject_cast<QTextEdit *>(widget)) { connect(groupBox, &QGroupBox::toggled,
this, [this, groupBox] { setValue(QVariant(groupBox->isChecked())); });
}
} else if (auto textEdit = qobject_cast<QTextEdit *>(widget)) {
textEdit->setPlainText(m_value.toString()); textEdit->setPlainText(m_value.toString());
connect(textEdit, &QTextEdit::textChanged, this, &SavedAction::textEditTextChanged); if (applyMode == ImmediateApply) {
} else if (PathListEditor *editor = qobject_cast<PathListEditor *>(widget)) { connect(textEdit, &QTextEdit::textChanged,
this, [this, textEdit] { setValue(textEdit->toPlainText()); });
}
} else if (auto editor = qobject_cast<PathListEditor *>(widget)) {
editor->setPathList(m_value.toStringList()); editor->setPathList(m_value.toStringList());
} else { } else {
qDebug() << "Cannot connect widget " << widget << toString(); qDebug() << "Cannot connect widget " << widget << toString();
@@ -282,64 +295,24 @@ void SavedAction::disconnectWidget()
void SavedAction::apply(QSettings *s) void SavedAction::apply(QSettings *s)
{ {
if (QCheckBox *button = qobject_cast<QCheckBox *>(m_widget)) if (auto button = qobject_cast<QCheckBox *>(m_widget))
setValue(button->isChecked()); setValue(button->isChecked());
else if (QLineEdit *lineEdit = qobject_cast<QLineEdit *>(m_widget)) else if (auto lineEdit = qobject_cast<QLineEdit *>(m_widget))
setValue(lineEdit->text()); setValue(lineEdit->text());
else if (QSpinBox *spinBox = qobject_cast<QSpinBox *>(m_widget)) else if (auto spinBox = qobject_cast<QSpinBox *>(m_widget))
setValue(spinBox->value()); setValue(spinBox->value());
else if (PathChooser *pathChooser = qobject_cast<PathChooser *>(m_widget)) else if (auto pathChooser = qobject_cast<PathChooser *>(m_widget))
setValue(pathChooser->path()); setValue(pathChooser->path());
else if (const QGroupBox *groupBox = qobject_cast<QGroupBox *>(m_widget)) else if (auto groupBox = qobject_cast<QGroupBox *>(m_widget))
setValue(groupBox->isChecked()); setValue(groupBox->isChecked());
else if (const QTextEdit *textEdit = qobject_cast<QTextEdit *>(m_widget)) else if (auto textEdit = qobject_cast<QTextEdit *>(m_widget))
setValue(textEdit->toPlainText()); setValue(textEdit->toPlainText());
else if (const PathListEditor *editor = qobject_cast<PathListEditor *>(m_widget)) else if (auto editor = qobject_cast<PathListEditor *>(m_widget))
setValue(editor->pathList()); setValue(editor->pathList());
if (s) if (s)
writeSettings(s); writeSettings(s);
} }
void SavedAction::checkableButtonClicked(bool)
{
QCheckBox *button = qobject_cast<QCheckBox *>(sender());
QTC_ASSERT(button, return);
if (m_applyMode == ImmediateApply)
setValue(button->isChecked());
}
void SavedAction::lineEditEditingFinished()
{
QLineEdit *lineEdit = qobject_cast<QLineEdit *>(sender());
QTC_ASSERT(lineEdit, return);
if (m_applyMode == ImmediateApply)
setValue(lineEdit->text());
}
void SavedAction::spinBoxValueChanged(int value)
{
QSpinBox *spinBox = qobject_cast<QSpinBox *>(sender());
QTC_ASSERT(spinBox, return);
if (m_applyMode == ImmediateApply)
setValue(value);
}
void SavedAction::pathChooserEditingFinished()
{
PathChooser *pathChooser = qobject_cast<PathChooser *>(sender());
QTC_ASSERT(pathChooser, return);
if (m_applyMode == ImmediateApply)
setValue(pathChooser->path());
}
void SavedAction::textEditTextChanged()
{
QTextEdit *textEdit = qobject_cast<QTextEdit *>(sender());
QTC_ASSERT(textEdit, return);
if (m_applyMode == ImmediateApply)
setValue(textEdit->toPlainText());
}
/* /*
Default text to be used in labels if this SavedAction is Default text to be used in labels if this SavedAction is
used in a settings dialog. used in a settings dialog.
@@ -360,13 +333,6 @@ void SavedAction::setDialogText(const QString &dialogText)
m_dialogText = dialogText; m_dialogText = dialogText;
} }
void SavedAction::groupBoxToggled(bool checked)
{
if (m_applyMode == ImmediateApply)
setValue(QVariant(checked));
}
void SavedAction::actionTriggered(bool) void SavedAction::actionTriggered(bool)
{ {
if (isCheckable()) if (isCheckable())
@@ -410,3 +376,4 @@ void SavedActionSet::finish()
action->disconnectWidget(); action->disconnectWidget();
} }
} // namespace Utils

View File

@@ -82,13 +82,7 @@ signals:
void valueChanged(const QVariant &newValue); void valueChanged(const QVariant &newValue);
private: private:
void checkableButtonClicked(bool);
void lineEditEditingFinished();
void pathChooserEditingFinished();
void actionTriggered(bool); void actionTriggered(bool);
void spinBoxValueChanged(int);
void groupBoxToggled(bool checked);
void textEditTextChanged();
QVariant m_value; QVariant m_value;
QVariant m_defaultValue; QVariant m_defaultValue;
@@ -96,7 +90,6 @@ private:
QString m_settingsGroup; QString m_settingsGroup;
QString m_dialogText; QString m_dialogText;
QWidget *m_widget; QWidget *m_widget;
ApplyMode m_applyMode;
}; };
class QTCREATOR_UTILS_EXPORT SavedActionSet class QTCREATOR_UTILS_EXPORT SavedActionSet