From 5cb0d801ba2f7133f990df70a3204a7b087187dc Mon Sep 17 00:00:00 2001 From: Martchus Date: Sat, 10 Aug 2019 22:03:13 +0200 Subject: [PATCH] Improve coding style in optioncategorymodel.cpp --- settingsdialog/optioncategorymodel.cpp | 57 ++++++++++++++------------ 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/settingsdialog/optioncategorymodel.cpp b/settingsdialog/optioncategorymodel.cpp index 7a52053..34a5ecb 100644 --- a/settingsdialog/optioncategorymodel.cpp +++ b/settingsdialog/optioncategorymodel.cpp @@ -52,7 +52,7 @@ void OptionCategoryModel::setCategories(const QList &categorie beginResetModel(); qDeleteAll(m_categories); m_categories = categories; - for (OptionCategory *category : m_categories) { + for (OptionCategory *const category : m_categories) { category->setParent(this); connect(category, &OptionCategory::displayNameChanged, this, &OptionCategoryModel::categoryChangedName); connect(category, &OptionCategory::iconChanged, this, &OptionCategoryModel::categoryChangedIcon); @@ -67,24 +67,25 @@ int OptionCategoryModel::rowCount(const QModelIndex &parent) const QVariant OptionCategoryModel::data(const QModelIndex &index, int role) const { - if (index.isValid() && index.row() < m_categories.size()) { - switch (role) { - case Qt::DisplayRole: - return m_categories.at(index.row())->displayName(); - case Qt::DecorationRole: { - const QIcon &icon = m_categories.at(index.row())->icon(); - if (!icon.isNull()) { - return icon.pixmap( + if (!index.isValid() || index.row() >= m_categories.size()) { + return QVariant(); + } + switch (role) { + case Qt::DisplayRole: + return m_categories.at(index.row())->displayName(); + case Qt::DecorationRole: { + const QIcon &icon = m_categories.at(index.row())->icon(); + if (!icon.isNull()) { + return icon.pixmap( #ifdef QT_UTILITIES_GUI_QTWIDGETS - QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize) + QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize) #else - QSize(32, 32) + QSize(32, 32) #endif - ); - } - } + ); } } + } return QVariant(); } @@ -93,12 +94,14 @@ QVariant OptionCategoryModel::data(const QModelIndex &index, int role) const */ void OptionCategoryModel::categoryChangedName() { - if (OptionCategory *senderCategory = qobject_cast(QObject::sender())) { - for (int i = 0, end = m_categories.size(); i < end; ++i) { - if (senderCategory == m_categories.at(i)) { - QModelIndex index = this->index(i); - emit dataChanged(index, index, QVector({ Qt::DisplayRole })); - } + const auto *const senderCategory = qobject_cast(QObject::sender()); + if (!senderCategory) { + return; + } + for (int i = 0, end = m_categories.size(); i < end; ++i) { + if (senderCategory == m_categories.at(i)) { + QModelIndex index = this->index(i); + emit dataChanged(index, index, QVector({ Qt::DisplayRole })); } } } @@ -108,12 +111,14 @@ void OptionCategoryModel::categoryChangedName() */ void OptionCategoryModel::categoryChangedIcon() { - if (OptionCategory *senderCategory = qobject_cast(QObject::sender())) { - for (int i = 0, end = m_categories.size(); i < end; ++i) { - if (senderCategory == m_categories.at(i)) { - QModelIndex index = this->index(i); - emit dataChanged(index, index, QVector({ Qt::DecorationRole })); - } + const auto *const senderCategory = qobject_cast(QObject::sender()); + if (!senderCategory) { + return; + } + for (int i = 0, end = m_categories.size(); i < end; ++i) { + if (senderCategory == m_categories.at(i)) { + QModelIndex index = this->index(i); + emit dataChanged(index, index, QVector({ Qt::DecorationRole })); } } }