Changeset View
Changeset View
Standalone View
Standalone View
applets/kicker/plugin/simplefavoritesmodel.cpp
- This file was moved from applets/kicker/plugin/favoritesmodel.cpp.
Show All 11 Lines | |||||
12 | * GNU General Public License for more details. * | 12 | * GNU General Public License for more details. * | ||
13 | * * | 13 | * * | ||
14 | * You should have received a copy of the GNU General Public License * | 14 | * You should have received a copy of the GNU General Public License * | ||
15 | * along with this program; if not, write to the * | 15 | * along with this program; if not, write to the * | ||
16 | * Free Software Foundation, Inc., * | 16 | * Free Software Foundation, Inc., * | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * | 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * | ||
18 | ***************************************************************************/ | 18 | ***************************************************************************/ | ||
19 | 19 | | |||
20 | #include "favoritesmodel.h" | 20 | #include "simplefavoritesmodel.h" | ||
21 | #include "appentry.h" | 21 | #include "appentry.h" | ||
22 | #include "contactentry.h" | 22 | #include "contactentry.h" | ||
23 | #include "fileentry.h" | 23 | #include "fileentry.h" | ||
24 | #include "systementry.h" | 24 | #include "systementry.h" | ||
25 | #include "actionlist.h" | 25 | #include "actionlist.h" | ||
26 | 26 | | |||
27 | #include <KLocalizedString> | 27 | #include <KLocalizedString> | ||
28 | 28 | | |||
29 | FavoritesModel::FavoritesModel(QObject *parent) : AbstractModel(parent) | 29 | SimpleFavoritesModel::SimpleFavoritesModel(QObject *parent) : AbstractModel(parent) | ||
30 | , m_enabled(true) | 30 | , m_enabled(true) | ||
31 | , m_maxFavorites(-1) | 31 | , m_maxFavorites(-1) | ||
32 | , m_dropPlaceholderIndex(-1) | 32 | , m_dropPlaceholderIndex(-1) | ||
33 | { | 33 | { | ||
34 | } | 34 | } | ||
35 | 35 | | |||
36 | FavoritesModel::~FavoritesModel() | 36 | SimpleFavoritesModel::~SimpleFavoritesModel() | ||
37 | { | 37 | { | ||
38 | qDeleteAll(m_entryList); | 38 | qDeleteAll(m_entryList); | ||
39 | } | 39 | } | ||
40 | 40 | | |||
41 | QString FavoritesModel::description() const | 41 | QString SimpleFavoritesModel::description() const | ||
42 | { | 42 | { | ||
43 | return i18n("Favorites"); | 43 | return i18n("Favorites"); | ||
44 | } | 44 | } | ||
45 | 45 | | |||
46 | QVariant FavoritesModel::data(const QModelIndex& index, int role) const | 46 | QVariant SimpleFavoritesModel::data(const QModelIndex& index, int role) const | ||
47 | { | 47 | { | ||
48 | if (!index.isValid() || index.row() >= rowCount()) { | 48 | if (!index.isValid() || index.row() >= rowCount()) { | ||
49 | return QVariant(); | 49 | return QVariant(); | ||
50 | } | 50 | } | ||
51 | 51 | | |||
52 | if (index.row() == m_dropPlaceholderIndex) { | 52 | if (index.row() == m_dropPlaceholderIndex) { | ||
53 | if (role == Kicker::IsDropPlaceholderRole) { | 53 | if (role == Kicker::IsDropPlaceholderRole) { | ||
54 | return true; | 54 | return true; | ||
Show All 24 Lines | 78 | } else if (role == Kicker::HasActionListRole) { | |||
79 | return entry->hasActions(); | 79 | return entry->hasActions(); | ||
80 | } else if (role == Kicker::ActionListRole) { | 80 | } else if (role == Kicker::ActionListRole) { | ||
81 | return entry->actions(); | 81 | return entry->actions(); | ||
82 | } | 82 | } | ||
83 | 83 | | |||
84 | return QVariant(); | 84 | return QVariant(); | ||
85 | } | 85 | } | ||
86 | 86 | | |||
87 | int FavoritesModel::rowCount(const QModelIndex& parent) const | 87 | int SimpleFavoritesModel::rowCount(const QModelIndex& parent) const | ||
88 | { | 88 | { | ||
89 | return parent.isValid() ? 0 : m_entryList.count() + (m_dropPlaceholderIndex != -1 ? 1 : 0); | 89 | return parent.isValid() ? 0 : m_entryList.count() + (m_dropPlaceholderIndex != -1 ? 1 : 0); | ||
90 | } | 90 | } | ||
91 | 91 | | |||
92 | bool FavoritesModel::trigger(int row, const QString &actionId, const QVariant &argument) | 92 | bool SimpleFavoritesModel::trigger(int row, const QString &actionId, const QVariant &argument) | ||
93 | { | 93 | { | ||
94 | if (row < 0 || row >= m_entryList.count()) { | 94 | if (row < 0 || row >= m_entryList.count()) { | ||
95 | return false; | 95 | return false; | ||
96 | } | 96 | } | ||
97 | 97 | | |||
98 | return m_entryList.at(row)->run(actionId, argument); | 98 | return m_entryList.at(row)->run(actionId, argument); | ||
99 | } | 99 | } | ||
100 | 100 | | |||
101 | bool FavoritesModel::enabled() const | 101 | bool SimpleFavoritesModel::enabled() const | ||
102 | { | 102 | { | ||
103 | return m_enabled; | 103 | return m_enabled; | ||
104 | } | 104 | } | ||
105 | 105 | | |||
106 | void FavoritesModel::setEnabled(bool enable) | 106 | void SimpleFavoritesModel::setEnabled(bool enable) | ||
107 | { | 107 | { | ||
108 | if (m_enabled != enable) { | 108 | if (m_enabled != enable) { | ||
109 | m_enabled = enable; | 109 | m_enabled = enable; | ||
110 | 110 | | |||
111 | emit enabledChanged(); | 111 | emit enabledChanged(); | ||
112 | } | 112 | } | ||
113 | } | 113 | } | ||
114 | 114 | | |||
115 | QStringList FavoritesModel::favorites() const | 115 | QStringList SimpleFavoritesModel::favorites() const | ||
116 | { | 116 | { | ||
117 | return m_favorites; | 117 | return m_favorites; | ||
118 | } | 118 | } | ||
119 | 119 | | |||
120 | void FavoritesModel::setFavorites(const QStringList& favorites) | 120 | void SimpleFavoritesModel::setFavorites(const QStringList& favorites) | ||
121 | { | 121 | { | ||
122 | QStringList _favorites(favorites); | 122 | QStringList _favorites(favorites); | ||
123 | _favorites.removeDuplicates(); | 123 | _favorites.removeDuplicates(); | ||
124 | 124 | | |||
125 | if (_favorites != m_favorites) { | 125 | if (_favorites != m_favorites) { | ||
126 | m_favorites = _favorites; | 126 | m_favorites = _favorites; | ||
127 | refresh(); | 127 | refresh(); | ||
128 | } | 128 | } | ||
129 | } | 129 | } | ||
130 | 130 | | |||
131 | int FavoritesModel::maxFavorites() const | 131 | int SimpleFavoritesModel::maxFavorites() const | ||
132 | { | 132 | { | ||
133 | return m_maxFavorites; | 133 | return m_maxFavorites; | ||
134 | } | 134 | } | ||
135 | 135 | | |||
136 | void FavoritesModel::setMaxFavorites(int max) | 136 | void SimpleFavoritesModel::setMaxFavorites(int max) | ||
137 | { | 137 | { | ||
138 | if (m_maxFavorites != max) | 138 | if (m_maxFavorites != max) | ||
139 | { | 139 | { | ||
140 | m_maxFavorites = max; | 140 | m_maxFavorites = max; | ||
141 | 141 | | |||
142 | if (m_maxFavorites != -1 && m_favorites.count() > m_maxFavorites) { | 142 | if (m_maxFavorites != -1 && m_favorites.count() > m_maxFavorites) { | ||
143 | refresh(); | 143 | refresh(); | ||
144 | } | 144 | } | ||
145 | 145 | | |||
146 | emit maxFavoritesChanged(); | 146 | emit maxFavoritesChanged(); | ||
147 | } | 147 | } | ||
148 | } | 148 | } | ||
149 | 149 | | |||
150 | bool FavoritesModel::isFavorite(const QString &id) const | 150 | bool SimpleFavoritesModel::isFavorite(const QString &id) const | ||
151 | { | 151 | { | ||
152 | return m_favorites.contains(id); | 152 | return m_favorites.contains(id); | ||
153 | } | 153 | } | ||
154 | 154 | | |||
155 | void FavoritesModel::addFavorite(const QString &id, int index) | 155 | void SimpleFavoritesModel::addFavorite(const QString &id, int index) | ||
156 | { | 156 | { | ||
157 | if (!m_enabled || id.isEmpty()) { | 157 | if (!m_enabled || id.isEmpty()) { | ||
158 | return; | 158 | return; | ||
159 | } | 159 | } | ||
160 | 160 | | |||
161 | if (m_maxFavorites != -1 && m_favorites.count() == m_maxFavorites) { | 161 | if (m_maxFavorites != -1 && m_favorites.count() == m_maxFavorites) { | ||
162 | return; | 162 | return; | ||
163 | } | 163 | } | ||
Show All 15 Lines | |||||
179 | m_favorites.insert(insertIndex, entry->id()); | 179 | m_favorites.insert(insertIndex, entry->id()); | ||
180 | 180 | | |||
181 | endInsertRows(); | 181 | endInsertRows(); | ||
182 | 182 | | |||
183 | emit countChanged(); | 183 | emit countChanged(); | ||
184 | emit favoritesChanged(); | 184 | emit favoritesChanged(); | ||
185 | } | 185 | } | ||
186 | 186 | | |||
187 | void FavoritesModel::removeFavorite(const QString &id) | 187 | void SimpleFavoritesModel::removeFavorite(const QString &id) | ||
188 | { | 188 | { | ||
189 | if (!m_enabled || id.isEmpty()) { | 189 | if (!m_enabled || id.isEmpty()) { | ||
190 | return; | 190 | return; | ||
191 | } | 191 | } | ||
192 | 192 | | |||
193 | int index = m_favorites.indexOf(id); | 193 | int index = m_favorites.indexOf(id); | ||
194 | 194 | | |||
195 | if (index != -1) { | 195 | if (index != -1) { | ||
196 | setDropPlaceholderIndex(-1); | 196 | setDropPlaceholderIndex(-1); | ||
197 | 197 | | |||
198 | beginRemoveRows(QModelIndex(), index, index); | 198 | beginRemoveRows(QModelIndex(), index, index); | ||
199 | 199 | | |||
200 | delete m_entryList[index]; | 200 | delete m_entryList[index]; | ||
201 | m_entryList.removeAt(index); | 201 | m_entryList.removeAt(index); | ||
202 | m_favorites.removeAt(index); | 202 | m_favorites.removeAt(index); | ||
203 | 203 | | |||
204 | endRemoveRows(); | 204 | endRemoveRows(); | ||
205 | 205 | | |||
206 | emit countChanged(); | 206 | emit countChanged(); | ||
207 | emit favoritesChanged(); | 207 | emit favoritesChanged(); | ||
208 | } | 208 | } | ||
209 | } | 209 | } | ||
210 | 210 | | |||
211 | void FavoritesModel::moveRow(int from, int to) | 211 | void SimpleFavoritesModel::moveRow(int from, int to) | ||
212 | { | 212 | { | ||
213 | if (from >= m_favorites.count() || to >= m_favorites.count()) { | 213 | if (from >= m_favorites.count() || to >= m_favorites.count()) { | ||
214 | return; | 214 | return; | ||
215 | } | 215 | } | ||
216 | 216 | | |||
217 | if (from == to) { | 217 | if (from == to) { | ||
218 | return; | 218 | return; | ||
219 | } | 219 | } | ||
Show All 9 Lines | 227 | if (ok) { | |||
229 | m_favorites.move(from, to); | 229 | m_favorites.move(from, to); | ||
230 | 230 | | |||
231 | endMoveRows(); | 231 | endMoveRows(); | ||
232 | 232 | | |||
233 | emit favoritesChanged(); | 233 | emit favoritesChanged(); | ||
234 | } | 234 | } | ||
235 | } | 235 | } | ||
236 | 236 | | |||
237 | int FavoritesModel::dropPlaceholderIndex() const | 237 | int SimpleFavoritesModel::dropPlaceholderIndex() const | ||
238 | { | 238 | { | ||
239 | return m_dropPlaceholderIndex; | 239 | return m_dropPlaceholderIndex; | ||
240 | } | 240 | } | ||
241 | 241 | | |||
242 | void FavoritesModel::setDropPlaceholderIndex(int index) | 242 | void SimpleFavoritesModel::setDropPlaceholderIndex(int index) | ||
243 | { | 243 | { | ||
244 | if (index == -1 && m_dropPlaceholderIndex != -1) { | 244 | if (index == -1 && m_dropPlaceholderIndex != -1) { | ||
245 | beginRemoveRows(QModelIndex(), m_dropPlaceholderIndex, m_dropPlaceholderIndex); | 245 | beginRemoveRows(QModelIndex(), m_dropPlaceholderIndex, m_dropPlaceholderIndex); | ||
246 | 246 | | |||
247 | m_dropPlaceholderIndex = index; | 247 | m_dropPlaceholderIndex = index; | ||
248 | 248 | | |||
249 | endRemoveRows(); | 249 | endRemoveRows(); | ||
250 | 250 | | |||
Show All 14 Lines | 260 | } else if (m_dropPlaceholderIndex != index) { | |||
265 | if (ok) { | 265 | if (ok) { | ||
266 | m_dropPlaceholderIndex = index; | 266 | m_dropPlaceholderIndex = index; | ||
267 | 267 | | |||
268 | endMoveRows(); | 268 | endMoveRows(); | ||
269 | } | 269 | } | ||
270 | } | 270 | } | ||
271 | } | 271 | } | ||
272 | 272 | | |||
273 | AbstractModel *FavoritesModel::favoritesModel() | 273 | AbstractModel *SimpleFavoritesModel::favoritesModel() | ||
274 | { | 274 | { | ||
275 | return this; | 275 | return this; | ||
276 | } | 276 | } | ||
277 | 277 | | |||
278 | void FavoritesModel::refresh() | 278 | void SimpleFavoritesModel::refresh() | ||
279 | { | 279 | { | ||
280 | beginResetModel(); | 280 | beginResetModel(); | ||
281 | 281 | | |||
282 | setDropPlaceholderIndex(-1); | 282 | setDropPlaceholderIndex(-1); | ||
283 | 283 | | |||
284 | int oldCount = m_entryList.count(); | 284 | int oldCount = m_entryList.count(); | ||
285 | 285 | | |||
286 | qDeleteAll(m_entryList); | 286 | qDeleteAll(m_entryList); | ||
Show All 22 Lines | |||||
309 | 309 | | |||
310 | if (oldCount != m_entryList.count()) { | 310 | if (oldCount != m_entryList.count()) { | ||
311 | emit countChanged(); | 311 | emit countChanged(); | ||
312 | } | 312 | } | ||
313 | 313 | | |||
314 | emit favoritesChanged(); | 314 | emit favoritesChanged(); | ||
315 | } | 315 | } | ||
316 | 316 | | |||
317 | AbstractEntry *FavoritesModel::favoriteFromId(const QString &id) | 317 | AbstractEntry *SimpleFavoritesModel::favoriteFromId(const QString &id) | ||
318 | { | 318 | { | ||
319 | const QUrl url(id); | 319 | const QUrl url(id); | ||
320 | const QString &s = url.scheme(); | 320 | const QString &s = url.scheme(); | ||
321 | 321 | | |||
322 | if ((s.isEmpty() && id.contains(QStringLiteral(".desktop"))) || s == QStringLiteral("preferred")) { | 322 | if ((s.isEmpty() && id.contains(QStringLiteral(".desktop"))) || s == QStringLiteral("preferred")) { | ||
323 | return new AppEntry(this, id); | 323 | return new AppEntry(this, id); | ||
324 | } else if (s == QStringLiteral("ktp")) { | 324 | } else if (s == QStringLiteral("ktp")) { | ||
325 | return new ContactEntry(this, id); | 325 | return new ContactEntry(this, id); | ||
326 | } else if (url.isValid() && !url.scheme().isEmpty()) { | 326 | } else if (url.isValid() && !url.scheme().isEmpty()) { | ||
327 | return new FileEntry(this, url); | 327 | return new FileEntry(this, url); | ||
328 | } else { | 328 | } else { | ||
329 | return new SystemEntry(this, id); | 329 | return new SystemEntry(this, id); | ||
330 | } | 330 | } | ||
331 | 331 | | |||
332 | return nullptr; | 332 | return nullptr; | ||
333 | } | 333 | } |