From 7bfd7f586385be19c3ccb38615ee26e5af2ef38f Mon Sep 17 00:00:00 2001 From: Mike Gelfand Date: Sat, 29 Apr 2017 23:11:57 +0300 Subject: [PATCH] Avoid declaring variables inside conditional expressions (Qt client) --- qt/PrefsDialog.cc | 135 +++++++++++++++++++++++++++++++--------------- 1 file changed, 91 insertions(+), 44 deletions(-) diff --git a/qt/PrefsDialog.cc b/qt/PrefsDialog.cc index bc0eec063..f36af7b29 100644 --- a/qt/PrefsDialog.cc +++ b/qt/PrefsDialog.cc @@ -51,17 +51,51 @@ namespace { -char const* PREF_KEY("pref-key"); - -void setPrefKey(QObject* object, int key) +class PreferenceWidget { - object->setProperty(PREF_KEY, key); -} + static char const* const PREF_KEY; -int getPrefKey(QObject const* object) -{ - return object->property(PREF_KEY).toInt(); -} +public: + explicit PreferenceWidget(QObject* object) : + m_object(object) + { + } + + template + bool is() const + { + return qobject_cast(m_object) != nullptr; + } + + template + T const* as() const + { + assert(is()); + return static_cast(m_object); + } + + template + T* as() + { + assert(is()); + return static_cast(m_object); + } + + void setPrefKey(int key) + { + m_object->setProperty(PREF_KEY, key); + } + + int getPrefKey() const + { + return m_object->property(PREF_KEY).toInt(); + } + +private: + QObject* const m_object; +}; + +char const* const PreferenceWidget::PREF_KEY = "pref-key"; int qtDayToTrDay(int day) { @@ -129,33 +163,35 @@ QString qtDayName(int day) bool PrefsDialog::updateWidgetValue(QWidget* widget, int prefKey) { - if (auto w = qobject_cast(widget)) + PreferenceWidget prefWidget(widget); + + if (prefWidget.is()) { - w->setChecked(myPrefs.getBool(prefKey)); + prefWidget.as()->setChecked(myPrefs.getBool(prefKey)); } - else if (auto w = qobject_cast(widget)) + else if (prefWidget.is()) { - w->setValue(myPrefs.getInt(prefKey)); + prefWidget.as()->setValue(myPrefs.getInt(prefKey)); } - else if (auto w = qobject_cast(widget)) + else if (prefWidget.is()) { - w->setValue(myPrefs.getDouble(prefKey)); + prefWidget.as()->setValue(myPrefs.getDouble(prefKey)); } - else if (auto w = qobject_cast(widget)) + else if (prefWidget.is()) { - w->setTime(QTime(0, 0).addSecs(myPrefs.getInt(prefKey) * 60)); + prefWidget.as()->setTime(QTime(0, 0).addSecs(myPrefs.getInt(prefKey) * 60)); } - else if (auto w = qobject_cast(widget)) + else if (prefWidget.is()) { - w->setText(myPrefs.getString(prefKey)); + prefWidget.as()->setText(myPrefs.getString(prefKey)); } - else if (auto w = qobject_cast(widget)) + else if (prefWidget.is()) { - w->setPath(myPrefs.getString(prefKey)); + prefWidget.as()->setPath(myPrefs.getString(prefKey)); } - else if (auto w = qobject_cast(widget)) + else if (prefWidget.is()) { - w->setPath(myPrefs.getString(prefKey)); + prefWidget.as()->setPath(myPrefs.getString(prefKey)); } else { @@ -167,27 +203,29 @@ bool PrefsDialog::updateWidgetValue(QWidget* widget, int prefKey) void PrefsDialog::linkWidgetToPref(QWidget* widget, int prefKey) { - setPrefKey(widget, prefKey); + PreferenceWidget prefWidget(widget); + + prefWidget.setPrefKey(prefKey); updateWidgetValue(widget, prefKey); myWidgets.insert(prefKey, widget); - if (widget->inherits("QCheckBox")) + if (prefWidget.is()) { connect(widget, SIGNAL(toggled(bool)), SLOT(checkBoxToggled(bool))); } - else if (widget->inherits("QTimeEdit")) + else if (prefWidget.is()) { connect(widget, SIGNAL(editingFinished()), SLOT(timeEditingFinished())); } - else if (widget->inherits("QLineEdit")) + else if (prefWidget.is()) { connect(widget, SIGNAL(editingFinished()), SLOT(lineEditingFinished())); } - else if (widget->inherits("PathButton")) + else if (prefWidget.is()) { connect(widget, SIGNAL(pathChanged(QString)), SLOT(pathChanged(QString))); } - else if (widget->inherits("QAbstractSpinBox")) + else if (prefWidget.is()) { connect(widget, SIGNAL(editingFinished()), SLOT(spinBoxEditingFinished())); } @@ -195,51 +233,60 @@ void PrefsDialog::linkWidgetToPref(QWidget* widget, int prefKey) void PrefsDialog::checkBoxToggled(bool checked) { - if (auto c = qobject_cast(sender())) + PreferenceWidget const prefWidget(sender()); + + if (prefWidget.is()) { - setPref(getPrefKey(c), checked); + setPref(prefWidget.getPrefKey(), checked); } } void PrefsDialog::spinBoxEditingFinished() { - QObject const* spin = sender(); - int const key = getPrefKey(spin); + PreferenceWidget const prefWidget(sender()); - if (auto e = qobject_cast(spin)) + if (prefWidget.is()) { - setPref(key, e->value()); + setPref(prefWidget.getPrefKey(), prefWidget.as()->value()); } - else if (auto e = qobject_cast(spin)) + else if (prefWidget.is()) { - setPref(key, e->value()); + setPref(prefWidget.getPrefKey(), prefWidget.as()->value()); } } void PrefsDialog::timeEditingFinished() { - if (auto e = qobject_cast(sender())) + PreferenceWidget const prefWidget(sender()); + + if (prefWidget.is()) { - setPref(getPrefKey(e), QTime(0, 0).secsTo(e->time()) / 60); + setPref(prefWidget.getPrefKey(), QTime(0, 0).secsTo(prefWidget.as()->time()) / 60); } } void PrefsDialog::lineEditingFinished() { - if (auto e = qobject_cast(sender())) + PreferenceWidget const prefWidget(sender()); + + if (prefWidget.is()) { - if (e->isModified()) + QLineEdit const* const lineEdit = prefWidget.as(); + + if (lineEdit->isModified()) { - setPref(getPrefKey(e), e->text()); + setPref(prefWidget.getPrefKey(), lineEdit->text()); } } } void PrefsDialog::pathChanged(QString const& path) { - if (auto b = qobject_cast(sender())) + PreferenceWidget const prefWidget(sender()); + + if (prefWidget.is()) { - setPref(getPrefKey(b), path); + setPref(prefWidget.getPrefKey(), path); } }