Changeset View
Changeset View
Standalone View
Standalone View
src/core/models/favoritecollectionsmodel.cpp
Show First 20 Lines • Show All 89 Lines • ▼ Show 20 Line(s) | 43 | public: | |||
---|---|---|---|---|---|
90 | * Stuff changed (e.g. new rows inserted into sorted model), reload everything. | 90 | * Stuff changed (e.g. new rows inserted into sorted model), reload everything. | ||
91 | */ | 91 | */ | ||
92 | void reload() | 92 | void reload() | ||
93 | { | 93 | { | ||
94 | //don't clear the selection model here. Otherwise we mess up the users selection as collections get removed and re-inserted. | 94 | //don't clear the selection model here. Otherwise we mess up the users selection as collections get removed and re-inserted. | ||
95 | for (const Collection::Id &collectionId : qAsConst(collectionIds)) { | 95 | for (const Collection::Id &collectionId : qAsConst(collectionIds)) { | ||
96 | insertIfAvailable(collectionId); | 96 | insertIfAvailable(collectionId); | ||
97 | } | 97 | } | ||
98 | //TODO remove what's no longer here | 98 | // If a favorite folder was removed then surely it's gone from the selection model, so no need to do anything about that. | ||
99 | } | 99 | } | ||
100 | 100 | | |||
101 | void rowsInserted(const QModelIndex &parent, int begin, int end) | 101 | void rowsInserted(const QModelIndex &parent, int begin, int end) | ||
102 | { | 102 | { | ||
103 | for (int row = begin; row <= end; row++) { | 103 | for (int row = begin; row <= end; row++) { | ||
104 | const QModelIndex child = q->sourceModel()->index(row, 0, parent); | 104 | const QModelIndex child = q->sourceModel()->index(row, 0, parent); | ||
105 | if (!child.isValid()) { | 105 | if (!child.isValid()) { | ||
106 | continue; | 106 | continue; | ||
▲ Show 20 Lines • Show All 147 Lines • ▼ Show 20 Line(s) | |||||
254 | FavoriteCollectionsModel *const q; | 254 | FavoriteCollectionsModel *const q; | ||
255 | 255 | | |||
256 | QList<Collection::Id> collectionIds; | 256 | QList<Collection::Id> collectionIds; | ||
257 | QSet<Collection::Id> referencedCollections; | 257 | QSet<Collection::Id> referencedCollections; | ||
258 | QHash<qint64, QString> labelMap; | 258 | QHash<qint64, QString> labelMap; | ||
259 | KConfigGroup configGroup; | 259 | KConfigGroup configGroup; | ||
260 | }; | 260 | }; | ||
261 | 261 | | |||
262 | /* Implementation note: | ||||
263 | * | ||||
264 | * We use KSelectionProxyModel in order to make a flat list of selected folders from the folder tree. | ||||
265 | * | ||||
266 | * Attempts to use QSortFilterProxyModel / KRecursiveFilterProxyModel make code somewhat simpler, | ||||
267 | * but don't work since we then get a filtered tree, not a flat list. Stacking a KDescendantsProxyModel | ||||
268 | * on top would likely remove explicitly selected parents when one of their child is selected too. | ||||
269 | */ | ||||
270 | | ||||
262 | FavoriteCollectionsModel::FavoriteCollectionsModel(QAbstractItemModel *source, const KConfigGroup &group, QObject *parent) | 271 | FavoriteCollectionsModel::FavoriteCollectionsModel(QAbstractItemModel *source, const KConfigGroup &group, QObject *parent) | ||
263 | : Akonadi::SelectionProxyModel(new QItemSelectionModel(source, parent), parent) | 272 | : KSelectionProxyModel(new QItemSelectionModel(source, parent), parent) | ||
264 | , d(new Private(group, this)) | 273 | , d(new Private(group, this)) | ||
265 | { | 274 | { | ||
266 | //This should only be a KRecursiveFilterProxyModel, but remains a SelectionProxyModel for backwards compatibility. | | |||
267 | // We therefore disable what we anyways don't want (the referencing is handled separately). | | |||
268 | disconnect(this, SIGNAL(rootIndexAdded(QModelIndex)), this, SLOT(rootIndexAdded(QModelIndex))); | | |||
269 | disconnect(this, SIGNAL(rootIndexAboutToBeRemoved(QModelIndex)), this, SLOT(rootIndexAboutToBeRemoved(QModelIndex))); | | |||
270 | | ||||
271 | setSourceModel(source); | 275 | setSourceModel(source); | ||
272 | setFilterBehavior(ExactSelection); | 276 | setFilterBehavior(ExactSelection); | ||
273 | 277 | | |||
274 | d->loadConfig(); | 278 | d->loadConfig(); | ||
275 | //React to various changes in the source model | 279 | //React to various changes in the source model | ||
276 | connect(source, SIGNAL(modelReset()), this, SLOT(reload())); | 280 | connect(source, SIGNAL(modelReset()), this, SLOT(reload())); | ||
277 | connect(source, SIGNAL(layoutChanged()), this, SLOT(reload())); | 281 | connect(source, SIGNAL(layoutChanged()), this, SLOT(reload())); | ||
278 | connect(source, &QAbstractItemModel::rowsInserted, this, [this](const QModelIndex &parent, int begin, int end) { d->rowsInserted(parent, begin, end); }); | 282 | connect(source, &QAbstractItemModel::rowsInserted, this, [this](const QModelIndex &parent, int begin, int end) { d->rowsInserted(parent, begin, end); }); | ||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Line(s) | 358 | if (index.isValid() && index.column() == 0 && | |||
357 | if (newLabel.isEmpty()) { | 361 | if (newLabel.isEmpty()) { | ||
358 | return false; | 362 | return false; | ||
359 | } | 363 | } | ||
360 | const QModelIndex sourceIndex = mapToSource(index); | 364 | const QModelIndex sourceIndex = mapToSource(index); | ||
361 | const Collection collection = sourceModel()->data(sourceIndex, EntityTreeModel::CollectionRole).value<Collection>(); | 365 | const Collection collection = sourceModel()->data(sourceIndex, EntityTreeModel::CollectionRole).value<Collection>(); | ||
362 | setFavoriteLabel(collection, newLabel); | 366 | setFavoriteLabel(collection, newLabel); | ||
363 | return true; | 367 | return true; | ||
364 | } | 368 | } | ||
365 | return Akonadi::SelectionProxyModel::setData(index, value, role); | 369 | return KSelectionProxyModel::setData(index, value, role); | ||
366 | } | 370 | } | ||
367 | 371 | | |||
368 | QString Akonadi::FavoriteCollectionsModel::favoriteLabel(const Akonadi::Collection &collection) | 372 | QString Akonadi::FavoriteCollectionsModel::favoriteLabel(const Akonadi::Collection &collection) | ||
369 | { | 373 | { | ||
370 | if (!collection.isValid()) { | 374 | if (!collection.isValid()) { | ||
371 | return QString(); | 375 | return QString(); | ||
372 | } | 376 | } | ||
373 | return d->labelForCollection(collection.id()); | 377 | return d->labelForCollection(collection.id()); | ||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Line(s) | 422 | #endif | |||
430 | } | 434 | } | ||
431 | return true; | 435 | return true; | ||
432 | } | 436 | } | ||
433 | return false; | 437 | return false; | ||
434 | } | 438 | } | ||
435 | 439 | | |||
436 | QStringList FavoriteCollectionsModel::mimeTypes() const | 440 | QStringList FavoriteCollectionsModel::mimeTypes() const | ||
437 | { | 441 | { | ||
438 | QStringList mts = Akonadi::SelectionProxyModel::mimeTypes(); | 442 | QStringList mts = KSelectionProxyModel::mimeTypes(); | ||
439 | if (!mts.contains(QStringLiteral("text/uri-list"))) { | 443 | if (!mts.contains(QStringLiteral("text/uri-list"))) { | ||
440 | mts.append(QStringLiteral("text/uri-list")); | 444 | mts.append(QStringLiteral("text/uri-list")); | ||
441 | } | 445 | } | ||
442 | return mts; | 446 | return mts; | ||
443 | } | 447 | } | ||
444 | 448 | | |||
445 | Qt::ItemFlags FavoriteCollectionsModel::flags(const QModelIndex &index) const | 449 | Qt::ItemFlags FavoriteCollectionsModel::flags(const QModelIndex &index) const | ||
446 | { | 450 | { | ||
447 | Qt::ItemFlags fs = Akonadi::SelectionProxyModel::flags(index); | 451 | Qt::ItemFlags fs = KSelectionProxyModel::flags(index); | ||
448 | if (!index.isValid()) { | 452 | if (!index.isValid()) { | ||
449 | fs |= Qt::ItemIsDropEnabled; | 453 | fs |= Qt::ItemIsDropEnabled; | ||
450 | } | 454 | } | ||
451 | return fs; | 455 | return fs; | ||
452 | } | 456 | } | ||
453 | 457 | | |||
454 | void FavoriteCollectionsModel::pasteJobDone(KJob *job) | 458 | void FavoriteCollectionsModel::pasteJobDone(KJob *job) | ||
455 | { | 459 | { | ||
456 | if (job->error()) { | 460 | if (job->error()) { | ||
457 | qCDebug(AKONADICORE_LOG) << job->errorString(); | 461 | qCDebug(AKONADICORE_LOG) << job->errorString(); | ||
458 | } | 462 | } | ||
459 | } | 463 | } | ||
460 | 464 | | |||
461 | #include "moc_favoritecollectionsmodel.cpp" | 465 | #include "moc_favoritecollectionsmodel.cpp" |