Qt Utilities  5.6.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 GUI_QTWIDGETS
5 # include <QApplication>
6 # include <QStyle>
7 #endif
8 
9 namespace Dialogs {
10 
20  QAbstractListModel(parent)
21 {}
22 
27 OptionCategoryModel::OptionCategoryModel(const QList<OptionCategory *> &categories, QObject *parent) :
28  QAbstractListModel(parent),
29  m_categories(categories)
30 {
31  for(OptionCategory *category : m_categories) {
32  category->setParent(this);
33  }
34 }
35 
40 {}
41 
47 void OptionCategoryModel::setCategories(const QList<OptionCategory *> categories)
48 {
49  beginResetModel();
50  qDeleteAll(m_categories);
51  m_categories = categories;
52  for(OptionCategory *category : m_categories) {
53  category->setParent(this);
54  connect(category, &OptionCategory::displayNameChanged, this, &OptionCategoryModel::categoryChangedName);
55  connect(category, &OptionCategory::iconChanged, this, &OptionCategoryModel::categoryChangedIcon);
56  }
57  endResetModel();
58 }
59 
60 int OptionCategoryModel::rowCount(const QModelIndex &parent) const
61 {
62  return parent.isValid() ? 0 : m_categories.size();
63 }
64 
65 QVariant OptionCategoryModel::data(const QModelIndex &index, int role) const
66 {
67  if(index.isValid() && index.row() < m_categories.size()) {
68  switch (role) {
69  case Qt::DisplayRole:
70  return m_categories.at(index.row())->displayName();
71  case Qt::DecorationRole: {
72  const QIcon &icon = m_categories.at(index.row())->icon();
73  if(!icon.isNull()) {
74  return icon.pixmap(
75 #ifdef GUI_QTWIDGETS
76  QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize)
77 #else
78  QSize(32, 32)
79 #endif
80  );
81  }
82  }
83  }
84  }
85  return QVariant();
86 }
87 
91 void OptionCategoryModel::categoryChangedName()
92 {
93  if(OptionCategory *senderCategory = qobject_cast<OptionCategory *>(QObject::sender())) {
94  for(int i = 0, end = m_categories.size(); i < end; ++i) {
95  if(senderCategory == m_categories.at(i)) {
96  QModelIndex index = this->index(i);
97  emit dataChanged(index, index, QVector<int>() << Qt::DisplayRole);
98  }
99  }
100  }
101 }
102 
106 void OptionCategoryModel::categoryChangedIcon()
107 {
108  if(OptionCategory *senderCategory = qobject_cast<OptionCategory *>(QObject::sender())) {
109  for(int i = 0, end = m_categories.size(); i < end; ++i) {
110  if(senderCategory == m_categories.at(i)) {
111  QModelIndex index = this->index(i);
112  emit dataChanged(index, index, QVector<int>() << Qt::DecorationRole);
113  }
114  }
115  }
116 }
117 
118 }
The OptionCategory class wraps associated option pages.
void setCategories(const QList< OptionCategory *> categories)
Sets the categories for the model.
OptionCategory * category(const QModelIndex &index) const
Returns the category for the specified model index.
int rowCount(const QModelIndex &parent=QModelIndex()) const
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
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
void iconChanged()
Emitted when the icon changed.
virtual ~OptionCategoryModel()
Destroys the option category model.
void displayNameChanged()
Emitted when the display name changed.