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

0001 // SPDX-FileCopyrightText: 2004 - 2014 Jesper K. Pedersen <jesper.pedersen@kdab.com>
0002 // SPDX-FileCopyrightText: 2006 Tuomas Suutari <tuomas@nepnep.net>
0003 // SPDX-FileCopyrightText: 2007 Dirk Mueller <mueller@kde.org>
0004 // SPDX-FileCopyrightText: 2013 - 2024 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
0005 // SPDX-FileCopyrightText: 2014 - 2022 Tobias Leupold <tl@stonemx.de>
0006 //
0007 // SPDX-License-Identifier: GPL-2.0-or-later
0008 
0009 #ifndef CATEGORY_H
0010 #define CATEGORY_H
0011 
0012 #include "ImageDate.h"
0013 
0014 #include <KIconLoader>
0015 #include <QDate>
0016 #include <QExplicitlySharedDataPointer>
0017 #include <QObject>
0018 #include <QString>
0019 
0020 class QImage;
0021 class QPixmap;
0022 namespace DB
0023 {
0024 class CategoryItem;
0025 class TagInfo;
0026 
0027 struct CountWithRange {
0028     uint count = 0;
0029     ImageDate range {};
0030     void add(const ImageDate &date)
0031     {
0032         count++;
0033         range.extendTo(date);
0034     }
0035 };
0036 
0037 /**
0038    This class stores information about categories (People/Places/Events)
0039 */
0040 class Category : public QObject, public QSharedData
0041 {
0042     Q_OBJECT
0043 
0044 public:
0045     enum ViewType { TreeView, ///< TreeView with generic items
0046                     ThumbedTreeView, ///< TreeView with user thumbnails
0047                     IconView, ///< IconView with generic items
0048                     ThumbedIconView }; ///< IconView with user thumbnails
0049     enum CategoryType { PlainCategory,
0050                         FolderCategory,
0051                         MediaTypeCategory,
0052                         TokensCategory };
0053 
0054     Category(const QString &name, const QString &icon, ViewType type, int thumbnailSize, bool show, bool positionable = false);
0055 
0056     QString name() const;
0057     void setName(const QString &name);
0058 
0059     bool positionable() const;
0060     void setPositionable(bool positionable);
0061 
0062     QString iconName() const;
0063     void setIconName(const QString &name);
0064     QPixmap icon(int size = 22, KIconLoader::States state = KIconLoader::DefaultState) const;
0065 
0066     ViewType viewType() const;
0067     void setViewType(ViewType type);
0068 
0069     int thumbnailSize() const;
0070     void setThumbnailSize(int size);
0071 
0072     /**
0073      * @brief doShow tells if the Category should be shown as part of the info text.
0074      * @return
0075      */
0076     bool doShow() const;
0077     void setDoShow(bool b);
0078 
0079     CategoryType type() const;
0080     void setType(CategoryType t);
0081 
0082     bool isSpecialCategory() const;
0083 
0084     QStringList items() const;
0085     QStringList itemsInclCategories() const;
0086     QExplicitlySharedDataPointer<CategoryItem> itemsCategories() const;
0087     void addOrReorderItems(const QStringList &items);
0088     void setItems(const QStringList &items);
0089     void removeItem(const QString &item);
0090     /**
0091      * @brief renameItem renames an existing tag from the old name to a new name.
0092      * The \c newValue string is trimmed to avoid leading or trailing whitespace.
0093      * @param oldValue
0094      * @param newValue
0095      */
0096     void renameItem(const QString &oldValue, const QString &newValue);
0097     /**
0098      * @brief addItem adds a new tag with the given name to the category.
0099      * The \c item string is trimmed to avoid leading or trailing whitespace.
0100      * @param item
0101      */
0102     void addItem(const QString &item);
0103     /**
0104      * @brief itemForName returns a TagInfo for a given tag.
0105      * In contrast to the usual tag representation as a QString, a TagInfo maintains a connection to the Category and is notified of tag renaming and deletion.
0106      *
0107      * @param item the name of the tag
0108      * @return a TagInfo object for the given tag name, or a \c nullptr if the tag does not exist.
0109      */
0110     DB::TagInfo *itemForName(const QString &tag);
0111 
0112     QPixmap categoryImage(const QString &category, const QString &, int width, int height) const;
0113     void setCategoryImage(const QString &category, const QString &, const QImage &image);
0114     QString fileForCategoryImage(const QString &category, QString member) const;
0115 
0116     QDate birthDate(const QString &item) const;
0117     void setBirthDate(const QString &item, const QDate &birthDate);
0118 
0119     int idForName(const QString &name) const;
0120     /**
0121      * @brief initIdMap is needed when writing categories into the XML file.
0122      * FIXME: make private and add CategoryCollection as friend class
0123      */
0124     void initIdMap();
0125     /**
0126      * @brief setIdMapping sets the mapping from id to name and vice versa.
0127      * The id must be a positive value.
0128      * @param name
0129      * @param id > 0
0130      */
0131     void setIdMapping(const QString &name, int id);
0132     /**
0133      * @brief addZeroMapping allows adding of category names with id 0.
0134      * This id is not allowed normally, but can happen in corrupted index.xml files.
0135      * @param name
0136      */
0137     void addZeroMapping(const QString &name);
0138     QString nameForId(int id) const;
0139     /**
0140      * @brief namesForIdZero returns all names for id 0.
0141      * Obviously, this is not how ids usually work.
0142      * The only time when this makes sense is when reading a damaged index.xml file that is to be repaired.
0143      * After loading the database is complete, the mapping between id and name must always 1:1!
0144      * @return
0145      */
0146     QStringList namesForIdZero() const;
0147     /**
0148      * @brief clearNullIds clears the IdMapping for tags with id=0.
0149      * Category names with id 0 can only happen when loading a corrupted database file.
0150      */
0151     void clearNullIds();
0152 
0153     bool shouldSave();
0154     void setShouldSave(bool b);
0155 
0156 private:
0157     QString m_name;
0158     QString m_icon;
0159     bool m_show;
0160     ViewType m_type;
0161     int m_thumbnailSize;
0162     bool m_positionable;
0163 
0164     CategoryType m_categoryType;
0165     QStringList m_items;
0166     QMap<QString, int> m_idMap;
0167     QMap<int, QString> m_nameMap;
0168     QMap<QString, QDate> m_birthDates;
0169     QStringList m_namesWithIdZero;
0170 
0171     bool m_shouldSave;
0172 
0173 Q_SIGNALS:
0174     void changed();
0175     void itemRenamed(const QString &oldName, const QString &newName);
0176     void itemRemoved(const QString &name);
0177 };
0178 
0179 }
0180 
0181 #endif /* CATEGORY_H */
0182 
0183 // vi:expandtab:tabstop=4 shiftwidth=4: