File indexing completed on 2025-01-19 03:57:35

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 29-07-2013
0007  * Description : Sort settings for showfoto item infos
0008  *
0009  * SPDX-FileCopyrightText: 2013 by Mohamed_Anwer <m_dot_anwer at gmx dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #ifndef SHOW_FOTO_ITEM_SORT_SETTINGS_H
0016 #define SHOW_FOTO_ITEM_SORT_SETTINGS_H
0017 
0018 // Qt includes
0019 
0020 #include <QCollator>
0021 
0022 // Local includes
0023 
0024 #include "showfotoiteminfo.h"
0025 
0026 namespace ShowFoto
0027 {
0028 
0029 class ShowfotoItemSortSettings
0030 {
0031 public:
0032 
0033     enum SortOrder
0034     {
0035         AscendingOrder  = Qt::AscendingOrder,
0036         DescendingOrder = Qt::DescendingOrder,
0037         DefaultOrder                            ///< sort order depends on the chosen sort role
0038     };
0039 
0040     enum CategorizationMode
0041     {
0042         NoCategories,
0043         CategoryByFolder,
0044         CategoryByFormat
0045     };
0046 
0047     enum SortRole
0048     {
0049         SortByCreationDate,
0050         SortByFileName,
0051         SortByFileSize
0052     };
0053 
0054 public:
0055 
0056     ShowfotoItemSortSettings();
0057     ~ShowfotoItemSortSettings();
0058 
0059     bool operator==(const ShowfotoItemSortSettings& other) const;
0060 
0061     /**
0062      * Compares the categories of left and right ShowfotoItemInfos.
0063      * It returns -1 if the left ShowfotoItemInfo is less than right, and 0 if both fall
0064      * in the same category, and 1 if the left ShowfotoItemInfo is greater than right.
0065      * Adheres to set categorization mode and current category sort order.
0066      */
0067     int compareCategories(const ShowfotoItemInfo& left, const ShowfotoItemInfo& right) const;
0068 
0069 
0070     /**
0071      * Returns true if left is less than right.
0072      * Adheres to current sort role and sort order.
0073      */
0074     bool lessThan(const ShowfotoItemInfo& left, const ShowfotoItemInfo& right) const;
0075 
0076     /**
0077      * Returns true if left QVariant is less than right.
0078      * Adheres to current sort role and sort order.
0079      */
0080     bool lessThan(const QVariant& left, const QVariant& right) const;
0081 
0082     /**
0083      * Compares the showfotoItemInfos left and right.
0084      * Return -1 if left is less than right, 1 if left is greater than right,
0085      * and 0 if left equals right comparing the current sort role's value.
0086      * Adheres to set sort role and sort order.
0087      */
0088     int compare(const ShowfotoItemInfo& left, const ShowfotoItemInfo& right) const;
0089 
0090     /// --- Categories ---------------
0091 
0092     void setCategorizationMode(CategorizationMode mode);
0093     void setCategorizationSortOrder(SortOrder order);
0094 
0095     bool isCategorized() const
0096     {
0097         return (categorizationMode >= CategoryByFolder);
0098     }
0099 
0100     /// --- Showfoto Items Sorting ---------------
0101 
0102     void setSortRole(SortRole role);
0103     void setSortOrder(SortOrder order);
0104 
0105     int  compare(const ShowfotoItemInfo& left, const ShowfotoItemInfo& right, SortRole sortRole) const;
0106 
0107     static Qt::SortOrder defaultSortOrderForCategorizationMode(CategorizationMode mode);
0108     static Qt::SortOrder defaultSortOrderForSortRole(SortRole role);
0109 
0110     /**
0111      * Returns a < b if sortOrder is Ascending, or b < a if order is descending
0112      */
0113     template <typename T>
0114     static inline bool lessThanByOrder(const T& a, const T& b, Qt::SortOrder sortOrder)
0115     {
0116         if (sortOrder == Qt::AscendingOrder)
0117         {
0118             return (a < b);
0119         }
0120         else
0121         {
0122             return (b < a);
0123         }
0124     }
0125 
0126     /**
0127      * Returns the usual compare result of -1, 0, or 1 for lessThan, equals and greaterThan.
0128      */
0129     template <typename T>
0130     static inline int compareValue(const T& a, const T &b)
0131     {
0132         if (a == b)
0133         {
0134             return 0;
0135         }
0136 
0137         if (a > b)
0138         {
0139             return 1;
0140         }
0141 
0142         return (-1);
0143     }
0144 
0145     /**
0146      * Takes a typical result from a compare method (0 is equal, -1 is less than, 1 is greater than)
0147      * and applies the given sort order to it.
0148      */
0149     static inline int compareByOrder(int compareResult, Qt::SortOrder sortOrder)
0150     {
0151         if (sortOrder == Qt::AscendingOrder)
0152         {
0153             return compareResult;
0154         }
0155 
0156         return (- compareResult);
0157     }
0158 
0159     template <typename T>
0160     static inline int compareByOrder(const T& a, const T& b, Qt::SortOrder sortOrder)
0161     {
0162         return compareByOrder(compareValue(a, b), sortOrder);
0163     }
0164 
0165     /**
0166      * Compares the two string by natural comparison and adheres to given sort order
0167      */
0168     static inline int naturalCompare(const QString& a,
0169                                      const QString& b,
0170                                      Qt::SortOrder sortOrder,
0171                                      Qt::CaseSensitivity caseSensitive = Qt::CaseSensitive)
0172     {
0173         QCollator collator;
0174         collator.setNumericMode(true);
0175         collator.setIgnorePunctuation(false);
0176         collator.setCaseSensitivity(caseSensitive);
0177 
0178         return (compareByOrder(collator.compare(a, b), sortOrder));
0179     }
0180 
0181 public:
0182 
0183     CategorizationMode   categorizationMode;
0184     SortOrder            categorizationSortOrder;
0185 
0186     /// Only Ascending or Descending, never be DefaultOrder
0187     Qt::SortOrder        currentCategorizationSortOrder;
0188     Qt::CaseSensitivity  categorizationCaseSensitivity;
0189 
0190     SortOrder            sortOrder;
0191     SortRole             sortRole;
0192 
0193     Qt::SortOrder        currentSortOrder;
0194     Qt::CaseSensitivity  sortCaseSensitivity;
0195 };
0196 
0197 } // namespace ShowFoto
0198 
0199 #endif // SHOW_FOTO_ITEM_SORT_SETTINGS_H