Qt Utilities  6.4.1
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
optioncategorymodel.cpp
Go to the documentation of this file.
2 #include "./optioncategory.h"
3 
4 #ifdef QT_UTILITIES_GUI_QTWIDGETS
5 #include <QApplication>
6 #include <QStyle>
7 #endif
8 
9 namespace QtUtilities {
10 
21  : QAbstractListModel(parent)
22 {
23 }
24 
29 OptionCategoryModel::OptionCategoryModel(const QList<OptionCategory *> &categories, QObject *parent)
30  : QAbstractListModel(parent)
31  , m_categories(categories)
32 {
33  for (OptionCategory *category : m_categories) {
34  category->setParent(this);
35  }
36 }
37 
42 {
43 }
44 
50 void OptionCategoryModel::setCategories(const QList<OptionCategory *> &categories)
51 {
52  beginResetModel();
53  qDeleteAll(m_categories);
54  m_categories = categories;
55  for (OptionCategory *const category : m_categories) {
56  category->setParent(this);
57  connect(category, &OptionCategory::displayNameChanged, this, &OptionCategoryModel::categoryChangedName);
58  connect(category, &OptionCategory::iconChanged, this, &OptionCategoryModel::categoryChangedIcon);
59  }
60  endResetModel();
61 }
62 
63 int OptionCategoryModel::rowCount(const QModelIndex &parent) const
64 {
65  return parent.isValid() ? 0 : m_categories.size();
66 }
67 
68 QVariant OptionCategoryModel::data(const QModelIndex &index, int role) const
69 {
70  if (!index.isValid() || index.row() >= m_categories.size()) {
71  return QVariant();
72  }
73  switch (role) {
74  case Qt::DisplayRole:
75  return m_categories.at(index.row())->displayName();
76  case Qt::DecorationRole: {
77  const QIcon &icon = m_categories.at(index.row())->icon();
78  if (!icon.isNull()) {
79  return icon.pixmap(
80 #ifdef QT_UTILITIES_GUI_QTWIDGETS
81  QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize)
82 #else
83  QSize(32, 32)
84 #endif
85  );
86  }
87  }
88  }
89  return QVariant();
90 }
91 
95 void OptionCategoryModel::categoryChangedName()
96 {
97  const auto *const senderCategory = qobject_cast<const OptionCategory *>(QObject::sender());
98  if (!senderCategory) {
99  return;
100  }
101  for (int i = 0, end = m_categories.size(); i < end; ++i) {
102  if (senderCategory == m_categories.at(i)) {
103  QModelIndex index = this->index(i);
104  emit dataChanged(index, index, QVector<int>({ Qt::DisplayRole }));
105  }
106  }
107 }
108 
112 void OptionCategoryModel::categoryChangedIcon()
113 {
114  const auto *const senderCategory = qobject_cast<const OptionCategory *>(QObject::sender());
115  if (!senderCategory) {
116  return;
117  }
118  for (int i = 0, end = m_categories.size(); i < end; ++i) {
119  if (senderCategory == m_categories.at(i)) {
120  QModelIndex index = this->index(i);
121  emit dataChanged(index, index, QVector<int>({ Qt::DecorationRole }));
122  }
123  }
124 }
125 } // namespace QtUtilities
OptionCategory * category(const QModelIndex &index) const
Returns the category for the specified model index.
~OptionCategoryModel() override
Destroys the option category model.
OptionCategoryModel(QObject *parent=nullptr)
Constructs an option category model.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QList< OptionCategory * > categories
void setCategories(const QList< OptionCategory * > &categories)
Sets the categories for the model.
The OptionCategory class wraps associated option pages.
void displayNameChanged(const QString &displayName)
Emitted when the display name changed.
void iconChanged(const QIcon &icon)
Emitted when the icon changed.