Qt Utilities 6.14.3
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
Loading...
Searching...
No Matches
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#include <utility>
10
11namespace QtUtilities {
12
23 : QAbstractListModel(parent)
24{
25}
26
31OptionCategoryModel::OptionCategoryModel(const QList<OptionCategory *> &categories, QObject *parent)
32 : QAbstractListModel(parent)
33 , m_categories(categories)
34{
35 for (OptionCategory *category : std::as_const(m_categories)) {
36 category->setParent(this);
37 }
38}
39
46
52void OptionCategoryModel::setCategories(const QList<OptionCategory *> &categories)
53{
54 beginResetModel();
55 qDeleteAll(m_categories);
56 m_categories = categories;
57 for (OptionCategory *const category : std::as_const(m_categories)) {
58 category->setParent(this);
59 connect(category, &OptionCategory::displayNameChanged, this, &OptionCategoryModel::categoryChangedName);
60 connect(category, &OptionCategory::iconChanged, this, &OptionCategoryModel::categoryChangedIcon);
61 }
62 endResetModel();
63}
64
65int OptionCategoryModel::rowCount(const QModelIndex &parent) const
66{
67 return parent.isValid() ? 0 : static_cast<int>(m_categories.size());
68}
69
70QVariant OptionCategoryModel::data(const QModelIndex &index, int role) const
71{
72 if (!index.isValid() || index.row() >= m_categories.size()) {
73 return QVariant();
74 }
75 switch (role) {
76 case Qt::DisplayRole:
77 return m_categories.at(index.row())->displayName();
78 case Qt::DecorationRole: {
79 const QIcon &icon = m_categories.at(index.row())->icon();
80 if (!icon.isNull()) {
81 return icon.pixmap(
82#ifdef QT_UTILITIES_GUI_QTWIDGETS
83 QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize)
84#else
85 QSize(32, 32)
86#endif
87 );
88 }
89 }
90 }
91 return QVariant();
92}
93
97void OptionCategoryModel::categoryChangedName()
98{
99 const auto *const senderCategory = qobject_cast<const OptionCategory *>(QObject::sender());
100 if (!senderCategory) {
101 return;
102 }
103 for (int i = 0, end = static_cast<int>(m_categories.size()); i < end; ++i) {
104 if (senderCategory == m_categories.at(i)) {
105 QModelIndex index = this->index(i);
106 emit dataChanged(index, index, QVector<int>({ Qt::DisplayRole }));
107 }
108 }
109}
110
114void OptionCategoryModel::categoryChangedIcon()
115{
116 const auto *const senderCategory = qobject_cast<const OptionCategory *>(QObject::sender());
117 if (!senderCategory) {
118 return;
119 }
120 for (int i = 0, end = static_cast<int>(m_categories.size()); i < end; ++i) {
121 if (senderCategory == m_categories.at(i)) {
122 QModelIndex index = this->index(i);
123 emit dataChanged(index, index, QVector<int>({ Qt::DecorationRole }));
124 }
125 }
126}
127} // 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.