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,7 +67,9 @@ 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()) {
return QVariant();
}
switch (role) { switch (role) {
case Qt::DisplayRole: case Qt::DisplayRole:
return m_categories.at(index.row())->displayName(); return m_categories.at(index.row())->displayName();
@ -84,7 +86,6 @@ QVariant OptionCategoryModel::data(const QModelIndex &index, int role) const
} }
} }
} }
}
return QVariant(); return QVariant();
} }
@ -93,14 +94,16 @@ 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());
if (!senderCategory) {
return;
}
for (int i = 0, end = m_categories.size(); i < end; ++i) { for (int i = 0, end = m_categories.size(); i < end; ++i) {
if (senderCategory == m_categories.at(i)) { if (senderCategory == m_categories.at(i)) {
QModelIndex index = this->index(i); QModelIndex index = this->index(i);
emit dataChanged(index, index, QVector<int>({ Qt::DisplayRole })); emit dataChanged(index, index, QVector<int>({ Qt::DisplayRole }));
} }
} }
}
} }
/*! /*!
@ -108,13 +111,15 @@ 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());
if (!senderCategory) {
return;
}
for (int i = 0, end = m_categories.size(); i < end; ++i) { for (int i = 0, end = m_categories.size(); i < end; ++i) {
if (senderCategory == m_categories.at(i)) { if (senderCategory == m_categories.at(i)) {
QModelIndex index = this->index(i); QModelIndex index = this->index(i);
emit dataChanged(index, index, QVector<int>({ Qt::DecorationRole })); emit dataChanged(index, index, QVector<int>({ Qt::DecorationRole }));
} }
} }
}
} }
} // namespace QtUtilities } // namespace QtUtilities