Qt Utilities  5.13.0
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 Dialogs {
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 *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  switch (role) {
72  case Qt::DisplayRole:
73  return m_categories.at(index.row())->displayName();
74  case Qt::DecorationRole: {
75  const QIcon &icon = m_categories.at(index.row())->icon();
76  if (!icon.isNull()) {
77  return icon.pixmap(
78 #ifdef QT_UTILITIES_GUI_QTWIDGETS
79  QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize)
80 #else
81  QSize(32, 32)
82 #endif
83  );
84  }
85  }
86  }
87  }
88  return QVariant();
89 }
90 
94 void OptionCategoryModel::categoryChangedName()
95 {
96  if (OptionCategory *senderCategory = qobject_cast<OptionCategory *>(QObject::sender())) {
97  for (int i = 0, end = m_categories.size(); i < end; ++i) {
98  if (senderCategory == m_categories.at(i)) {
99  QModelIndex index = this->index(i);
100  emit dataChanged(index, index, QVector<int>() << Qt::DisplayRole);
101  }
102  }
103  }
104 }
105 
109 void OptionCategoryModel::categoryChangedIcon()
110 {
111  if (OptionCategory *senderCategory = qobject_cast<OptionCategory *>(QObject::sender())) {
112  for (int i = 0, end = m_categories.size(); i < end; ++i) {
113  if (senderCategory == m_categories.at(i)) {
114  QModelIndex index = this->index(i);
115  emit dataChanged(index, index, QVector<int>() << Qt::DecorationRole);
116  }
117  }
118  }
119 }
120 } // namespace Dialogs
The OptionCategory class wraps associated option pages.
OptionCategory * category(const QModelIndex &index) const
Returns the category for the specified model index.
const QList< OptionCategory * > & categories() const
Returns the categories.
OptionCategoryModel(QObject *parent=nullptr)
Constructs an option category model.
Provides common dialogs such as AboutDialog, EnterPasswordDialog and SettingsDialog.
Definition: dialogutils.h:12
void setCategories(const QList< OptionCategory * > categories)
Sets the categories for the model.
void iconChanged()
Emitted when the icon changed.
void displayNameChanged()
Emitted when the display name changed.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
virtual ~OptionCategoryModel() override
Destroys the option category model.
int rowCount(const QModelIndex &parent=QModelIndex()) const override