1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2009-05-31
* Description : Sort settings for use with ItemFilterModel
*
* SPDX-FileCopyrightText: 2009 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#pragma once
// Qt includes
#include <QString>
#include <QVariant>
// Local includes
#include "digikam_export.h"
#include "itemsortcollator.h"
namespace Digikam
{
class ItemInfo;
class FaceTagsIface;
namespace DatabaseFields
{
class Set;
}
class DIGIKAM_DATABASE_EXPORT ItemSortSettings
{
public:
enum SortOrder
{
AscendingOrder = Qt::AscendingOrder,
DescendingOrder = Qt::DescendingOrder,
DefaultOrder ///< sort order depends on the chosen sort role
};
enum CategorizationMode
{
NoCategories, ///< categorization switched off
OneCategory, ///< all items in one global category
CategoryByAlbum,
CategoryByFormat,
CategoryByMonth,
CategoryByFaces
};
enum SortRole
{
// Note: For legacy reasons, the order of the first five entries must remain unchanged
SortByFileName,
SortByFilePath,
SortByCreationDate,
SortByModificationDate,
SortByFileSize,
SortByRating,
SortByImageSize, ///< pixel number
SortByAspectRatio, ///< width / height * 100000
SortByFaces, ///< count of unconfirmed faces
SortBySimilarity,
SortByManualOrderAndName,
SortByManualOrderAndDate
};
public:
ItemSortSettings() = default;
bool operator==(const ItemSortSettings& other) const;
/**
* Compares the categories of left and right.
* Return -1 if left is less than right, 0 if both fall in the same category,
* and 1 if left is greater than right.
* Adheres to set categorization mode and current category sort order.
* Face passed in to allow Categorization by Faces. Pass in an empty
* Face if not needed.
*/
int compareCategories(const ItemInfo& left,
const ItemInfo& right,
const FaceTagsIface& leftFace,
const FaceTagsIface& rightFace) const;
/**
* Returns true if left is less than right.
* Adheres to current sort role and sort order.
*/
bool lessThan(const ItemInfo& left, const ItemInfo& right) const;
/**
* Compares the ItemInfos left and right.
* Return -1 if left is less than right, 1 if left is greater than right,
* and 0 if left equals right comparing the current sort role's value.
* Adheres to set sort role and sort order.
*/
int compare(const ItemInfo& left, const ItemInfo& right) const;
/**
* Returns true if left QVariant is less than right.
* Adheres to current sort role and sort order.
* Use for extraValue, if necessary.
*/
bool lessThan(const QVariant& left, const QVariant& right) const;
void setSortRole(SortRole role);
void setSortOrder(SortOrder order);
void setStringTypeNatural(bool natural);
public:
/// --- Categories ---
void setCategorizationMode(CategorizationMode mode);
void setCategorizationSortOrder(SortOrder order);
bool isCategorized() const
{
return (categorizationMode >= CategoryByAlbum);
}
public:
/// --- Image Sorting ---
int compare(const ItemInfo& left, const ItemInfo& right, SortRole sortRole) const;
static Qt::SortOrder defaultSortOrderForCategorizationMode(CategorizationMode mode);
static Qt::SortOrder defaultSortOrderForSortRole(SortRole role);
public:
/// --- Change notification ---
/**
* Returns database fields a change in which would affect the current sorting.
*/
DatabaseFields::Set watchFlags() const;
public:
/// --- Utilities ---
/**
* Returns a < b if sortOrder is Ascending, or b < a if order is descending.
*/
template <typename T>
static inline bool lessThanByOrder(const T& a, const T& b, Qt::SortOrder sortOrder)
{
if (sortOrder == Qt::AscendingOrder)
{
return (a < b);
}
return (b < a);
}
/**
* Returns the usual compare result of -1, 0, or 1 for lessThan, equals and greaterThan.
*/
template <typename T>
static inline int compareValue(const T& a, const T& b)
{
if (a == b)
{
return 0;
}
if (a > b)
{
return 1;
}
return (-1);
}
/**
* Takes a typical result from a compare method (0 is equal, -1 is less than, 1 is greater than)
* and applies the given sort order to it.
*/
static inline int compareByOrder(int compareResult, Qt::SortOrder sortOrder)
{
if (sortOrder == Qt::AscendingOrder)
{
return compareResult;
}
return (- compareResult);
}
template <typename T>
static inline int compareByOrder(const T& a, const T& b, Qt::SortOrder sortOrder)
{
return compareByOrder(compareValue(a, b), sortOrder);
}
/**
* Compares the two string by natural comparison and adheres to given sort order
*/
static inline int naturalCompare(const QString& a,
const QString& b,
Qt::SortOrder sortOrder,
Qt::CaseSensitivity caseSensitive = Qt::CaseSensitive,
bool natural = true)
{
ItemSortCollator* const sorter = ItemSortCollator::instance();<--- Variable 'sorter' can be declared as pointer to const
return compareByOrder(sorter->itemCompare(a, b, caseSensitive, natural), sortOrder);
}
public:
CategorizationMode categorizationMode = NoCategories;
SortOrder categorizationSortOrder = DefaultOrder;
/// Only Ascending or Descending, never DefaultOrder
Qt::SortOrder currentCategorizationSortOrder = Qt::AscendingOrder;
Qt::CaseSensitivity categorizationCaseSensitivity = Qt::CaseSensitive;
SortRole sortRole = SortByFileName;
SortOrder sortOrder = DefaultOrder;
bool strTypeNatural = true;
Qt::SortOrder currentSortOrder = Qt::AscendingOrder;
Qt::CaseSensitivity sortCaseSensitivity = Qt::CaseSensitive;
};
} // namespace Digikam
|