Debian Codegraph @

settingsdialog/optioncategorymodel.cpp

raw symbols SCIP
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
11 namespace QtUtilities {
12
13 /*!
14 * \class OptionCategoryModel
15 * \brief The OptionCategoryModel class is used by SettingsDialog to store and
16 * display option categories.
17 */
18
19 /*!
20 * \brief Constructs an option category model.
21 */
22 OptionCategoryModel::OptionCategoryModel(QObject *parent)
23 : QAbstractListModel(parent)
24 {
25 }
26
27 /*!
28 * \brief Constructs an option category model with the specified \a categories.
29 * \remarks The model takes ownership over the given categories.
30 */
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
40 /*!
41 * \brief Destroys the option category model.
42 */
43 OptionCategoryModel::~OptionCategoryModel()
44 {
45 }
46
47 /*!
48 * \brief Sets the \a categories for the model.
49 *
50 * The model takes ownership over the given \a categories.
51 */
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
70 QVariant 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
94 /*!
95 * \brief Handles the change of name of a category.
96 */
97 void 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
111 /*!
112 * \brief Handles the a changed icon of a category.
113 */
114 void 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