Changeset View
Changeset View
Standalone View
Standalone View
kcms/icons/iconsmodel.cpp
Show All 20 Lines | |||||
21 | * You should have received a copy of the GNU General Public License | 21 | * You should have received a copy of the GNU General Public License | ||
22 | * along with this program; if not, write to the Free Software | 22 | * along with this program; if not, write to the Free Software | ||
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
24 | */ | 24 | */ | ||
25 | 25 | | |||
26 | #include "iconsmodel.h" | 26 | #include "iconsmodel.h" | ||
27 | 27 | | |||
28 | #include <QFileIconProvider> | 28 | #include <QFileIconProvider> | ||
29 | 29 | | |||
crossi: include not used | |||||
30 | #include <KIconTheme> | 30 | #include <KIconTheme> | ||
31 | 31 | | |||
32 | IconsModel::IconsModel(QObject *parent) : QAbstractListModel(parent) | 32 | #include "iconssettings.h" | ||
33 | | ||||
34 | IconsModel::IconsModel(IconsSettings *iconsSettings, QObject *parent) | ||||
35 | : QAbstractListModel(parent) | ||||
36 | , m_settings(iconsSettings) | ||||
33 | { | 37 | { | ||
34 | 38 | | |||
35 | } | 39 | } | ||
36 | 40 | | |||
37 | IconsModel::~IconsModel() = default; | 41 | IconsModel::~IconsModel() = default; | ||
38 | 42 | | |||
39 | int IconsModel::rowCount(const QModelIndex &parent) const | 43 | int IconsModel::rowCount(const QModelIndex &parent) const | ||
40 | { | 44 | { | ||
Show All 36 Lines | 77 | if (role == PendingDeletionRole) { | |||
77 | 81 | | |||
78 | if (item.pendingDeletion != pendingDeletion) { | 82 | if (item.pendingDeletion != pendingDeletion) { | ||
79 | item.pendingDeletion = pendingDeletion; | 83 | item.pendingDeletion = pendingDeletion; | ||
80 | emit dataChanged(index, index, {PendingDeletionRole}); | 84 | emit dataChanged(index, index, {PendingDeletionRole}); | ||
81 | 85 | | |||
82 | // move to the next non-pending theme | 86 | // move to the next non-pending theme | ||
83 | const auto nonPending = match(index, PendingDeletionRole, false); | 87 | const auto nonPending = match(index, PendingDeletionRole, false); | ||
84 | if (!nonPending.isEmpty()) { | 88 | if (!nonPending.isEmpty()) { | ||
85 | setSelectedTheme(nonPending.first().data(ThemeNameRole).toString()); | 89 | m_settings->setTheme(nonPending.first().data(ThemeNameRole).toString()); | ||
86 | } | 90 | } | ||
87 | 91 | | |||
88 | emit pendingDeletionsChanged(); | 92 | emit pendingDeletionsChanged(); | ||
89 | return true; | 93 | return true; | ||
90 | } | 94 | } | ||
91 | } | 95 | } | ||
92 | 96 | | |||
93 | return false; | 97 | return false; | ||
94 | } | 98 | } | ||
95 | 99 | | |||
96 | QHash<int, QByteArray> IconsModel::roleNames() const | 100 | QHash<int, QByteArray> IconsModel::roleNames() const | ||
97 | { | 101 | { | ||
98 | return { | 102 | return { | ||
99 | {Qt::DisplayRole, QByteArrayLiteral("display")}, | 103 | {Qt::DisplayRole, QByteArrayLiteral("display")}, | ||
100 | {DescriptionRole, QByteArrayLiteral("description")}, | 104 | {DescriptionRole, QByteArrayLiteral("description")}, | ||
101 | {ThemeNameRole, QByteArrayLiteral("themeName")}, | 105 | {ThemeNameRole, QByteArrayLiteral("themeName")}, | ||
102 | {RemovableRole, QByteArrayLiteral("removable")}, | 106 | {RemovableRole, QByteArrayLiteral("removable")}, | ||
103 | {PendingDeletionRole, QByteArrayLiteral("pendingDeletion")} | 107 | {PendingDeletionRole, QByteArrayLiteral("pendingDeletion")} | ||
104 | }; | 108 | }; | ||
105 | } | 109 | } | ||
106 | 110 | | |||
107 | QString IconsModel::selectedTheme() const | | |||
108 | { | | |||
109 | return m_selectedTheme; | | |||
110 | } | | |||
111 | | ||||
112 | void IconsModel::setSelectedTheme(const QString &theme) | | |||
113 | { | | |||
114 | if (m_selectedTheme == theme) { | | |||
115 | return; | | |||
116 | } | | |||
117 | | ||||
118 | const bool firstTime = m_selectedTheme.isNull(); | | |||
119 | m_selectedTheme = theme; | | |||
120 | | ||||
121 | if (!firstTime) { | | |||
122 | emit selectedThemeChanged(); | | |||
123 | } | | |||
124 | emit selectedThemeIndexChanged(); | | |||
125 | } | | |||
126 | | ||||
127 | int IconsModel::selectedThemeIndex() const | | |||
128 | { | | |||
129 | auto it = std::find_if(m_data.begin(), m_data.end(), [this](const IconsModelData &item) { | | |||
130 | return item.themeName == m_selectedTheme; | | |||
131 | }); | | |||
132 | | ||||
133 | if (it != m_data.end()) { | | |||
134 | return std::distance(m_data.begin(), it); | | |||
135 | } | | |||
136 | | ||||
137 | return -1; | | |||
138 | } | | |||
139 | | ||||
140 | void IconsModel::load() | 111 | void IconsModel::load() | ||
141 | { | 112 | { | ||
142 | beginResetModel(); | 113 | beginResetModel(); | ||
143 | 114 | | |||
144 | const int oldCount = m_data.count(); | | |||
145 | | ||||
146 | m_data.clear(); | 115 | m_data.clear(); | ||
147 | 116 | | |||
148 | const QStringList themes = KIconTheme::list(); | 117 | const QStringList themes = KIconTheme::list(); | ||
149 | 118 | | |||
150 | m_data.reserve(themes.count()); | 119 | m_data.reserve(themes.count()); | ||
151 | 120 | | |||
152 | for (const QString &themeName : themes) { | 121 | for (const QString &themeName : themes) { | ||
153 | KIconTheme theme(themeName); | 122 | KIconTheme theme(themeName); | ||
Show All 16 Lines | |||||
170 | m_data.append(item); | 139 | m_data.append(item); | ||
171 | } | 140 | } | ||
172 | 141 | | |||
173 | std::sort(m_data.begin(), m_data.end(), [](const IconsModelData &a, const IconsModelData &b) { | 142 | std::sort(m_data.begin(), m_data.end(), [](const IconsModelData &a, const IconsModelData &b) { | ||
174 | return a.display < b.display; | 143 | return a.display < b.display; | ||
175 | }); | 144 | }); | ||
176 | 145 | | |||
177 | endResetModel(); | 146 | endResetModel(); | ||
178 | | ||||
179 | // an item might have been added before the currently selected one | | |||
180 | if (oldCount != m_data.count()) { | | |||
181 | emit selectedThemeIndexChanged(); | | |||
182 | } | | |||
183 | } | 147 | } | ||
184 | 148 | | |||
185 | QStringList IconsModel::pendingDeletions() const | 149 | QStringList IconsModel::pendingDeletions() const | ||
186 | { | 150 | { | ||
187 | QStringList pendingDeletions; | 151 | QStringList pendingDeletions; | ||
188 | 152 | | |||
189 | for (const auto &item : m_data) { | 153 | for (const auto &item : m_data) { | ||
190 | if (item.pendingDeletion) { | 154 | if (item.pendingDeletion) { | ||
Show All 17 Lines |
include not used