Qt Utilities: settingsdialog/optioncategorymodel.cpp Source File

Qt Utilities 6.21.1 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.
1#include "./optioncategorymodel.h"
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
18
22OptionCategoryModel::OptionCategoryModel(QObject *parent)
23 : QAbstractListModel(parent)
24{
25}
26
31 OptionCategoryModel::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
43OptionCategoryModel::~OptionCategoryModel()
44{
45}
46
52 void 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
65 int 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
QtUtilities::OptionCategoryModel::category
OptionCategory * category(const QModelIndex &index) const
Returns the category for the specified model index.
Definition optioncategorymodel.h:52
QtUtilities::OptionCategoryModel::~OptionCategoryModel
~OptionCategoryModel() override
Destroys the option category model.
Definition optioncategorymodel.cpp:43
QtUtilities::OptionCategoryModel::OptionCategoryModel
OptionCategoryModel(QObject *parent=nullptr)
Constructs an option category model.
Definition optioncategorymodel.cpp:22
QtUtilities::OptionCategoryModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Definition optioncategorymodel.cpp:70
QtUtilities::OptionCategoryModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition optioncategorymodel.cpp:65
QtUtilities::OptionCategoryModel::categories
QList< OptionCategory * > categories
Definition optioncategorymodel.h:16
QtUtilities::OptionCategoryModel::setCategories
void setCategories(const QList< OptionCategory * > &categories)
Sets the categories for the model.
Definition optioncategorymodel.cpp:52
QtUtilities::OptionCategory
The OptionCategory class wraps associated option pages.
Definition optioncategory.h:18
QtUtilities::OptionCategory::displayNameChanged
void displayNameChanged(const QString &displayName)
Emitted when the display name changed.
QtUtilities::OptionCategory::iconChanged
void iconChanged(const QIcon &icon)
Emitted when the icon changed.
QtUtilities
!
Definition trylocker.h:8
optioncategory.h
optioncategorymodel.h