File indexing completed on 2024-04-28 15:39:55

0001 // SPDX-FileCopyrightText: 2003-2023 The KPhotoAlbum Development Team
0002 //
0003 // SPDX-License-Identifier: GPL-2.0-or-later
0004 
0005 #include "GlobalCategorySortOrder.h"
0006 #include "CategoryCollection.h"
0007 #include "ImageDB.h"
0008 #include <QDebug>
0009 #include <algorithm>
0010 #include <set>
0011 
0012 namespace DB
0013 {
0014 
0015 void GlobalCategorySortOrder::pushToFront(const QString &category, const QString &value)
0016 {
0017     const Item item { category, value };
0018     m_sortOrder.removeAll(item);
0019     m_sortOrder.push_front(item);
0020 }
0021 
0022 QList<GlobalCategorySortOrder::Item> GlobalCategorySortOrder::modifiedSortOrder()
0023 {
0024     return m_sortOrder;
0025 }
0026 
0027 QList<GlobalCategorySortOrder::Item> GlobalCategorySortOrder::completeSortOrder()
0028 {
0029     QList<Item> result;
0030     result.append(m_sortOrder);
0031 
0032     // This is the set of all those categories already kept at the front.
0033     // We have it as a set to allow fast lookup when going through all categories
0034     // and appending them (as those found here should not be appended)
0035     QSet<Item> currentSet(m_sortOrder.cbegin(), m_sortOrder.cend());
0036 
0037     // To make it possible to remove entries no longer present in the categories
0038     // (either because they were removed or renamed, or gone some other magic way),
0039     // we initial copy all categories from m_sortOrder, and then remove them as we see them
0040     // while populating from each category
0041     QSet<Item> unknownSet = currentSet;
0042 
0043     const auto categories = DB::ImageDB::instance()->categoryCollection()->categories();
0044     for (const auto &category : categories) {
0045         if (!category->isSpecialCategory() || category->type() == DB::Category::TokensCategory) {
0046             auto items = category->items();
0047 
0048             for (const auto &categoryItem : qAsConst(items)) {
0049                 const Item item { category->name(), categoryItem };
0050                 unknownSet.remove(item);
0051                 if (!currentSet.contains(item))
0052                     result.append(item);
0053             }
0054         }
0055     }
0056 
0057     for (const auto &item : unknownSet) {
0058         result.removeAll(item);
0059     }
0060 
0061     return result;
0062 }
0063 
0064 bool operator==(const GlobalCategorySortOrder::Item &x, const GlobalCategorySortOrder::Item &y)
0065 {
0066     return x.category == y.category && x.item == y.item;
0067 }
0068 
0069 size_t qHash(const GlobalCategorySortOrder::Item &item)
0070 {
0071     return qHash(item.category) + qHash(item.item);
0072 }
0073 
0074 } // namespace DB