Debian Codegraph @

core/showfoto/thumbbar/showfotoitemmodel.h

raw symbols SCIP
1 /* ============================================================
2 *
3 * This file is a part of digiKam project
4 * https://www.digikam.org
5 *
6 * Date : 2013-07-05
7 * Description : Qt model for Showfoto entries
8 *
9 * SPDX-FileCopyrightText: 2013 by Mohamed_Anwer <m_dot_anwer at gmx dot com>
10 *
11 * SPDX-License-Identifier: GPL-2.0-or-later
12 *
13 * ============================================================ */
14
15 #pragma once
16
17 // Qt includes
18
19 #include <QAbstractListModel>
20 #include <QUrl>
21
22 // Local includes
23
24 #include "dragdropimplementations.h"
25 #include "abstractitemdragdrophandler.h"
26 #include "showfotoiteminfo.h"
27
28 using namespace Digikam;
29
30 namespace ShowFoto
31 {
32
33 typedef QPair<int, int> IntPair;
34
35 class ShowfotoItemModel : public QAbstractListModel,
36 public DragDropModelImplementation
37 {
38 Q_OBJECT
39
40 public:
41
42 enum ShowfotoItemModelRoles
43 {
44 /**
45 * An ShowfotoItemModel* pointer to this model
46 */
47 ShowfotoItemModelPointerRole = Qt::UserRole,
48 ShowfotoItemModelInternalId = Qt::UserRole + 1,
49
50 /**
51 * Returns a thumbnail pixmap. May be implemented by subclasses.
52 * Returns either a valid pixmap or a null QVariant.
53 */
54 ThumbnailRole = Qt::UserRole + 2,
55
56 /**
57 * Return (optional) extraData field
58 */
59 ExtraDataRole = Qt::UserRole + 3,
60
61 /**
62 * Returns the number of duplicate indexes for the same image id
63 */
64 ExtraDataDuplicateCount = Qt::UserRole + 6,
65
66 FilterModelRoles = Qt::UserRole + 100
67 };
68
69 public:
70
71 explicit ShowfotoItemModel(QObject* const parent);
72 ~ShowfotoItemModel() override;
73
74 /**
75 * If a cache is kept, lookup by file path is fast,
76 * without a cache it is O(n). Default is false.
77 */
78 void setKeepsFileUrlCache(bool keepCache);
79
80 /**
81 * Returns the ShowfotoItemInfo object, reference from the underlying data pointed to by the index.
82 * For ShowfotoItemInfo and ShowfotoItemInfoId If the index is not valid they will return a null ShowfotoItemInfo, and 0
83 * respectively, ShowfotoItemInfoRef must not be called with an invalid index as it will crash.
84 */
85 ShowfotoItemInfo showfotoItemInfo(const QModelIndex& index) const;
86 ShowfotoItemInfo& showfotoItemInfoRef(const QModelIndex& index) const;
87 qlonglong showfotoItemId(const QModelIndex& index) const;
88 ShowfotoItemInfoList showfotoItemInfos(const QList<QModelIndex>& indexes) const;
89 QList<qlonglong> showfotoItemIds(const QList<QModelIndex>& indexes) const;
90
91 /**
92 * Returns the ShowfotoItemInfo object, reference from the underlying data of
93 * the given row (parent is the invalid QModelIndex, column is 0).
94 * Note that ShowfotoItemInfoRef must not be called with an invalid index as it will crash.
95 */
96 ShowfotoItemInfo showfotoItemInfo(int row) const;
97 ShowfotoItemInfo& showfotoItemInfoRef(int row) const;
98 qlonglong showfotoItemId(int row) const;
99
100 /**
101 * Return the index of a given ShowfotoItemInfo, if it exists in the model.
102 */
103 QModelIndex indexForShowfotoItemInfo(const ShowfotoItemInfo& info) const;
104 QList<QModelIndex> indexesForShowfotoItemInfo(const ShowfotoItemInfo& info) const;
105 QModelIndex indexForShowfotoItemId(qlonglong id) const;
106 QList<QModelIndex> indexesForShowfotoItemId(qlonglong id) const;
107
108 /**
109 * Returns the index or ShowfotoItemInfo object from the underlying data for
110 * the given file url. In case of multiple occurrences of the same file, the simpler
111 * overrides returns any one found first, use the QList methods to retrieve all occurrences.
112 */
113 QModelIndex indexForUrl(const QUrl& fileUrl) const;
114 QList<QModelIndex> indexesForUrl(const QUrl& fileUrl) const;
115 ShowfotoItemInfo showfotoItemInfo(const QUrl& fileUrl) const;
116 QList<ShowfotoItemInfo> showfotoItemInfos(const QUrl& fileUrl) const;
117
118 void addShowfotoItemInfo(const ShowfotoItemInfo& info);
119 void addShowfotoItemInfos(const QList<ShowfotoItemInfo>& infos);
120
121 /**
122 * Clears the ShowfotoItemInfos and resets the model.
123 */
124 void clearShowfotoItemInfos();
125
126 /**
127 * addShowfotoItemInfo() is asynchronous if a prepocessor is set.
128 * This method first adds the info, synchronously.
129 * Only afterwards, the preprocessor will have the opportunity to process it.
130 * This method also bypasses any incremental updates.
131 */
132 void addShowfotoItemInfoSynchronously(const ShowfotoItemInfo& info);
133 void addShowfotoItemInfosSynchronously(const QList<ShowfotoItemInfo>& infos);
134
135 /**
136 * Clears and adds infos.
137 */
138 void setShowfotoItemInfos(const QList<ShowfotoItemInfo>& infos);
139
140 QList<ShowfotoItemInfo> showfotoItemInfos() const;
141 QList<qlonglong> showfotoItemIds() const;
142 QList<ShowfotoItemInfo> uniqueShowfotoItemInfos() const;
143
144 bool hasImage(qlonglong id) const;
145 bool hasImage(const ShowfotoItemInfo& info) const;
146
147 bool isEmpty() const;
148
149 /**
150 * Remove the given infos or indexes directly from the model.
151 */
152 void removeIndex(const QModelIndex& index);
153 void removeIndexs(const QList<QModelIndex>& indexes);
154 void removeShowfotoItemInfo(const ShowfotoItemInfo& info);
155 void removeShowfotoItemInfos(const QList<ShowfotoItemInfo>& infos);
156
157 int numberOfIndexesForShowfotoItemInfo(const ShowfotoItemInfo& info) const;
158 int numberOfIndexesForShowfotoItemId(qlonglong id) const;
159
160 /**
161 * Retrieve the ShowfotoItemInfo object from the data() function of the given index
162 * The index may be from a QSortFilterProxyModel as long as an ShowfotoItemModel is at the end.
163 */
164 static ShowfotoItemInfo retrieveShowfotoItemInfo(const QModelIndex& index);
165 static qlonglong retrieveShowfotoItemId(const QModelIndex& index);
166
167 /**
168 * QAbstractListModel implementations
169 */
170 int rowCount(const QModelIndex& parent) const override;
171 QVariant data(const QModelIndex& index, int role) const override;
172 QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
173 Qt::ItemFlags flags(const QModelIndex& index) const override;
174 QModelIndex index(int row, int column, const QModelIndex& parent) const override;
175
176 /**
177 * DragDrop methods
178 */
179 DECLARE_MODEL_DRAG_DROP_METHODS
180
181 /**
182 * Enable sending of itemInfosAboutToBeRemoved and itemsInfosRemoved signals.
183 * Default: false
184 */
185 void setSendRemovalSignals(bool send);
186
187 Q_SIGNALS:
188
189 /**
190 * Informs that ItemInfos will be added to the model.
191 * This signal is sent before the model data is changed and views are informed.
192 */
193 void itemInfosAboutToBeAdded(const QList<ShowfotoItemInfo>& infos);
194
195 /**
196 * Informs that ItemInfos have been added to the model.
197 * This signal is sent after the model data is changed and views are informed.
198 */
199 void itemInfosAdded(const QList<ShowfotoItemInfo>& infos);
200
201 /**
202 * Informs that ShowfotoItemInfos will be removed from the model.
203 * This signal is sent before the model data is changed and views are informed.
204 * Note: You need to explicitly enable sending of this signal. It is not sent
205 * in clearShowfotoItemInfos().
206 */
207 void itemInfosAboutToBeRemoved(const QList<ShowfotoItemInfo>& infos);
208
209 /**
210 * Informs that ShowfotoItemInfos have been removed from the model.
211 * This signal is sent after the model data is changed and views are informed.
212 * Note: You need to explicitly enable sending of this signal. It is not sent
213 * in clearShowfotoItemInfos().
214 */
215 void itemInfosRemoved(const QList<ShowfotoItemInfo>& infos);
216
217 /**
218 * Connect to this signal only if you are the current preprocessor.
219 */
220 void preprocess(const QList<ShowfotoItemInfo>& infos);
221 void processAdded(const QList<ShowfotoItemInfo>& infos);
222
223 /**
224 * Signals that the model is right now ready to start an incremental refresh.
225 * This is guaranteed only for the scope of emitting this signal.
226 */
227 void readyForIncrementalRefresh();
228
229 /**
230 * Signals that the model has finished currently with all scheduled
231 * refreshing, full or incremental, and all preprocessing.
232 * The model is in polished, clean situation right now.
233 */
234 void allRefreshingFinished();
235
236 public Q_SLOTS:
237
238 void reAddShowfotoItemInfos(const ShowfotoItemInfoList& infos);
239 void reAddingFinished();
240 void slotFileDeleted(const QString& folder, const QString& file, bool status);
241 void slotFileUploaded(const ShowfotoItemInfo& info);
242
243 protected:
244
245 /**
246 * As soon as the model is ready to start an incremental refresh, the signal
247 * readyForIncrementalRefresh() will be emitted. The signal will be emitted inline
248 * if the model is ready right now.
249 */
250 void requestIncrementalRefresh();
251
252 /**
253 * Starts an incremental refresh operation. You shall only call this method from a slot
254 * connected to readyForIncrementalRefresh(). To initiate an incremental refresh,
255 * call requestIncrementalRefresh().
256 */
257 void startIncrementalRefresh();
258 void finishIncrementalRefresh();
259
260 void emitDataChangedForAll();
261 void emitDataChangedForSelections(const QItemSelection& selection);
262
263 /**
264 * Called when the internal storage is cleared.
265 */
266 virtual void showfotoItemInfosCleared() {};
267
268 /**
269 * Called before rowsAboutToBeRemoved
270 */
271 virtual void showfotoItemInfosAboutToBeRemoved(int /*begin*/, int /*end*/) {};
272
273 private:
274
275 void appendInfos(const QList<ShowfotoItemInfo>& infos);
276 void publiciseInfos(const QList<ShowfotoItemInfo>& infos);
277 /*
278 void cleanSituationChecks();
279 */
280 void removeRowPairs(const QList<QPair<int, int> >& toRemove);
281 /*
282 void removeRowPairsWithCheck(const QList<QPair<int, int> >& toRemove);
283 */
284 static QList<IntPair> toContiguousPairs(const QList<int>& unsorted);
285
286 public:
287
288 /// @note Declared public because it's used in ItemModelIncrementalUpdater class
289 class Private;
290
291 private:
292
293 /// @note disabled
294 ShowfotoItemModel(const ShowfotoItemModel&) = delete;
295 ShowfotoItemModel& operator=(const ShowfotoItemModel&) = delete;
296
297 private:
298
299 Private* const d = nullptr;
300 };
301
302 } // namespace ShowFoto
303
304 Q_DECLARE_METATYPE(ShowFoto::ShowfotoItemModel*)