Improve coding style in optioncategorymodel.cpp

This commit is contained in:
Martchus 2019-08-10 22:03:13 +02:00
parent e03b619b58
commit 5cb0d801ba
1 changed files with 31 additions and 26 deletions

View File

@ -52,7 +52,7 @@ void OptionCategoryModel::setCategories(const QList<OptionCategory *> &categorie
beginResetModel(); beginResetModel();
qDeleteAll(m_categories); qDeleteAll(m_categories);
m_categories = categories; m_categories = categories;
for (OptionCategory *category : m_categories) { for (OptionCategory *const category : m_categories) {
category->setParent(this); category->setParent(this);
connect(category, &OptionCategory::displayNameChanged, this, &OptionCategoryModel::categoryChangedName); connect(category, &OptionCategory::displayNameChanged, this, &OptionCategoryModel::categoryChangedName);
connect(category, &OptionCategory::iconChanged, this, &OptionCategoryModel::categoryChangedIcon); 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 QVariant OptionCategoryModel::data(const QModelIndex &index, int role) const
{ {
if (index.isValid() && index.row() < m_categories.size()) { if (!index.isValid() || index.row() >= m_categories.size()) {
switch (role) { return QVariant();
case Qt::DisplayRole: }
return m_categories.at(index.row())->displayName(); switch (role) {
case Qt::DecorationRole: { case Qt::DisplayRole:
const QIcon &icon = m_categories.at(index.row())->icon(); return m_categories.at(index.row())->displayName();
if (!icon.isNull()) { case Qt::DecorationRole: {
return icon.pixmap( const QIcon &icon = m_categories.at(index.row())->icon();
if (!icon.isNull()) {
return icon.pixmap(
#ifdef QT_UTILITIES_GUI_QTWIDGETS #ifdef QT_UTILITIES_GUI_QTWIDGETS
QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize) QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize)
#else #else
QSize(32, 32) QSize(32, 32)
#endif #endif
); );
}
}
} }
} }
}
return QVariant(); return QVariant();
} }
@ -93,12 +94,14 @@ QVariant OptionCategoryModel::data(const QModelIndex &index, int role) const
*/ */
void OptionCategoryModel::categoryChangedName() void OptionCategoryModel::categoryChangedName()
{ {
if (OptionCategory *senderCategory = qobject_cast<OptionCategory *>(QObject::sender())) { const auto *const senderCategory = qobject_cast<const OptionCategory *>(QObject::sender());
for (int i = 0, end = m_categories.size(); i < end; ++i) { if (!senderCategory) {
if (senderCategory == m_categories.at(i)) { return;
QModelIndex index = this->index(i); }
emit dataChanged(index, index, QVector<int>({ Qt::DisplayRole })); 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<int>({ Qt::DisplayRole }));
} }
} }
} }
@ -108,12 +111,14 @@ void OptionCategoryModel::categoryChangedName()
*/ */
void OptionCategoryModel::categoryChangedIcon() void OptionCategoryModel::categoryChangedIcon()
{ {
if (OptionCategory *senderCategory = qobject_cast<OptionCategory *>(QObject::sender())) { const auto *const senderCategory = qobject_cast<const OptionCategory *>(QObject::sender());
for (int i = 0, end = m_categories.size(); i < end; ++i) { if (!senderCategory) {
if (senderCategory == m_categories.at(i)) { return;
QModelIndex index = this->index(i); }
emit dataChanged(index, index, QVector<int>({ Qt::DecorationRole })); 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<int>({ Qt::DecorationRole }));
} }
} }
} }