]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemmodelrolesupdater.h
Merge branch 'release/20.08' into master
[dolphin.git] / src / kitemviews / kfileitemmodelrolesupdater.h
1 /*
2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #ifndef KFILEITEMMODELROLESUPDATER_H
8 #define KFILEITEMMODELROLESUPDATER_H
9
10 #include "dolphin_export.h"
11 #include "kitemviews/kitemmodelbase.h"
12
13 #include <KFileItem>
14 #include <config-baloo.h>
15
16 #include <QObject>
17 #include <QSet>
18 #include <QSize>
19 #include <QStringList>
20
21 class KDirectoryContentsCounter;
22 class KFileItemModel;
23 class QPixmap;
24 class QTimer;
25 class KOverlayIconPlugin;
26
27 namespace KIO {
28 class PreviewJob;
29 }
30
31 #ifdef HAVE_BALOO
32 namespace Baloo
33 {
34 class FileMonitor;
35 }
36 #include <Baloo/IndexerConfig>
37 #endif
38
39 /**
40 * @brief Resolves expensive roles asynchronously and applies them to the KFileItemModel.
41 *
42 * KFileItemModel only resolves roles that are inexpensive like e.g. the file name or
43 * the permissions. Creating previews or determining the MIME-type can be quite expensive
44 * and KFileItemModelRolesUpdater takes care to update such roles asynchronously.
45 *
46 * To prevent a huge CPU and I/O load, these roles are not updated for all
47 * items, but only for the visible items, some items around the visible area,
48 * and the items on the first and last pages of the view. This is a compromise
49 * that aims to minimize the risk that the user sees items with unknown icons
50 * in the view when scrolling or pressing Home or End.
51 *
52 * Determining the roles is done in several phases:
53 *
54 * 1. If the sort role is "slow", it is determined for all items. If this
55 * cannot be finished synchronously in 200 ms, the remaining items are
56 * handled asynchronously by \a resolveNextSortRole().
57 *
58 * 2. The function startUpdating(), which is called if either the sort role
59 * has been successfully determined for all items, or items are inserted
60 * in the view, or the visible items might have changed because items
61 * were removed or moved, tries to determine the icons for all visible
62 * items synchronously for 200 ms. Then:
63 *
64 * (a) If previews are disabled, icons and all other roles are determined
65 * asynchronously for the interesting items. This is done by the
66 * function \a resolveNextPendingRoles().
67 *
68 * (b) If previews are enabled, a \a KIO::PreviewJob is started that loads
69 * the previews for the interesting items. At the same time, the icons
70 * for these items are determined asynchronously as fast as possible
71 * by \a resolveNextPendingRoles(). This minimizes the risk that the
72 * user sees "unknown" icons when scrolling before the previews have
73 * arrived.
74 *
75 * 3. Finally, the entire process is repeated for any items that might have
76 * changed in the mean time.
77 */
78 class DOLPHIN_EXPORT KFileItemModelRolesUpdater : public QObject
79 {
80 Q_OBJECT
81
82 public:
83 explicit KFileItemModelRolesUpdater(KFileItemModel* model, QObject* parent = nullptr);
84 ~KFileItemModelRolesUpdater() override;
85
86 void setIconSize(const QSize& size);
87 QSize iconSize() const;
88
89 /**
90 * Sets the range of items that are visible currently. The roles
91 * of visible items are resolved first.
92 */
93 void setVisibleIndexRange(int index, int count);
94
95 void setMaximumVisibleItems(int count);
96
97 /**
98 * If \a show is set to true, the "iconPixmap" role will be filled with a preview
99 * of the file. If \a show is false the MIME type icon will be used for the "iconPixmap"
100 * role.
101 */
102 void setPreviewsShown(bool show);
103 bool previewsShown() const;
104
105 /**
106 * If enabled a small preview gets upscaled to the icon size in case where
107 * the icon size is larger than the preview. Per default enlarging is
108 * enabled.
109 */
110 void setEnlargeSmallPreviews(bool enlarge);
111 bool enlargeSmallPreviews() const;
112
113 /**
114 * If \a paused is set to true the asynchronous resolving of roles will be paused.
115 * State changes during pauses like changing the icon size or the preview-shown
116 * will be remembered and handled after unpausing.
117 */
118 void setPaused(bool paused);
119 bool isPaused() const;
120
121 /**
122 * Sets the roles that should be resolved asynchronously.
123 */
124 void setRoles(const QSet<QByteArray>& roles);
125 QSet<QByteArray> roles() const;
126
127 /**
128 * Sets the list of enabled thumbnail plugins that are used for previews.
129 * Per default all plugins enabled in the KConfigGroup "PreviewSettings"
130 * are used.
131 *
132 * For a list of available plugins, call KServiceTypeTrader::self()->query("ThumbCreator").
133 *
134 * @see enabledPlugins
135 */
136 void setEnabledPlugins(const QStringList& list);
137
138 /**
139 * Returns the list of enabled thumbnail plugins.
140 * @see setEnabledPlugins
141 */
142 QStringList enabledPlugins() const;
143
144 /**
145 * Sets the maximum file size of local files for which
146 * previews will be generated (if enabled). A value of 0
147 * indicates no file size limit.
148 * Per default the value from KConfigGroup "PreviewSettings"
149 * MaximumSize is used, 0 otherwise.
150 * @param size
151 */
152 void setLocalFileSizePreviewLimit(qlonglong size);
153 qlonglong localFileSizePreviewLimit() const;
154
155 private slots:
156 void slotItemsInserted(const KItemRangeList& itemRanges);
157 void slotItemsRemoved(const KItemRangeList& itemRanges);
158 void slotItemsMoved(const KItemRange& itemRange, const QList<int> &movedToIndexes);
159 void slotItemsChanged(const KItemRangeList& itemRanges,
160 const QSet<QByteArray>& roles);
161 void slotSortRoleChanged(const QByteArray& current,
162 const QByteArray& previous);
163
164 /**
165 * Is invoked after a preview has been received successfully.
166 * @see startPreviewJob()
167 */
168 void slotGotPreview(const KFileItem& item, const QPixmap& pixmap);
169
170 /**
171 * Is invoked after generating a preview has failed.
172 * @see startPreviewJob()
173 */
174 void slotPreviewFailed(const KFileItem& item);
175
176 /**
177 * Is invoked when the preview job has been finished. Starts a new preview
178 * job if there are any interesting items without previews left, or updates
179 * the changed items otherwise. *
180 * @see startPreviewJob()
181 */
182 void slotPreviewJobFinished();
183
184 /**
185 * Is invoked when one of the KOverlayIconPlugin emit the signal that an overlay has changed
186 */
187 void slotOverlaysChanged(const QUrl& url, const QStringList&);
188
189 /**
190 * Resolves the sort role of the next item in m_pendingSortRole, applies it
191 * to the model, and invokes itself if there are any pending items left. If
192 * that is not the case, \a startUpdating() is called.
193 */
194 void resolveNextSortRole();
195
196 /**
197 * Resolves the icon name and (if previews are disabled) all other roles
198 * for the next interesting item. If there are no pending items left, any
199 * changed items are updated.
200 */
201 void resolveNextPendingRoles();
202
203 /**
204 * Resolves items that have not been resolved yet after the change has been
205 * notified by slotItemsChanged(). Is invoked if the m_changedItemsTimer
206 * expires.
207 */
208 void resolveRecentlyChangedItems();
209
210 void applyChangedBalooRoles(const QString& file);
211 void applyChangedBalooRolesForItem(const KFileItem& file);
212
213 void slotDirectoryContentsCountReceived(const QString& path, int count, long size);
214
215 private:
216 /**
217 * Starts the updating of all roles. The visible items are handled first.
218 */
219 void startUpdating();
220
221 /**
222 * Loads the icons for the visible items. After 200 ms, the function
223 * stops determining mime types and only loads preliminary icons.
224 * This is a compromise that prevents that
225 * (a) the GUI is blocked for more than 200 ms, and
226 * (b) "unknown" icons could be shown in the view.
227 */
228 void updateVisibleIcons();
229
230 /**
231 * Creates previews for the items starting from the first item in
232 * m_pendingPreviewItems.
233 * @see slotGotPreview()
234 * @see slotPreviewFailed()
235 * @see slotPreviewJobFinished()
236 */
237 void startPreviewJob();
238
239 /**
240 * Ensures that icons, previews, and other roles are determined for any
241 * items that have been changed.
242 */
243 void updateChangedItems();
244
245 /**
246 * Resolves the sort role of the item and applies it to the model.
247 */
248 void applySortRole(int index);
249
250 void applySortProgressToModel();
251
252 enum ResolveHint {
253 ResolveFast,
254 ResolveAll
255 };
256 bool applyResolvedRoles(int index, ResolveHint hint);
257 QHash<QByteArray, QVariant> rolesData(const KFileItem& item);
258
259 /**
260 * @return The number of items of the path \a path.
261 */
262 int subItemsCount(const QString& path) const;
263
264 /**
265 * Must be invoked if a property has been changed that affects
266 * the look of the preview. Takes care to update all previews.
267 */
268 void updateAllPreviews();
269
270 void killPreviewJob();
271
272 QList<int> indexesToResolve() const;
273
274 private:
275 enum State {
276 Idle,
277 Paused,
278 ResolvingSortRole,
279 ResolvingAllRoles,
280 PreviewJobRunning
281 };
282
283 State m_state;
284
285 // Property changes during pausing must be remembered to be able
286 // to react when unpausing again:
287 bool m_previewChangedDuringPausing;
288 bool m_iconSizeChangedDuringPausing;
289 bool m_rolesChangedDuringPausing;
290
291 // Property for setPreviewsShown()/previewsShown().
292 bool m_previewShown;
293
294 // Property for setEnlargeSmallPreviews()/enlargeSmallPreviews()
295 bool m_enlargeSmallPreviews;
296
297 // True if the role "iconPixmap" should be cleared when resolving the next
298 // role with resolveRole(). Is necessary if the preview gets disabled
299 // during the roles-updater has been paused by setPaused().
300 bool m_clearPreviews;
301
302 // Remembers which items have been handled already, to prevent that
303 // previews and other expensive roles are determined again.
304 QSet<KFileItem> m_finishedItems;
305
306 KFileItemModel* m_model;
307 QSize m_iconSize;
308 int m_firstVisibleIndex;
309 int m_lastVisibleIndex;
310 int m_maximumVisibleItems;
311 QSet<QByteArray> m_roles;
312 QSet<QByteArray> m_resolvableRoles;
313 QStringList m_enabledPlugins;
314 qulonglong m_localFileSizePreviewLimit;
315
316 // Items for which the sort role still has to be determined.
317 QSet<KFileItem> m_pendingSortRoleItems;
318
319 // Indexes of items which still have to be handled by
320 // resolveNextPendingRoles().
321 QList<int> m_pendingIndexes;
322
323 // Items which have been left over from the last call of startPreviewJob().
324 // A new preview job will be started from them once the first one finishes.
325 KFileItemList m_pendingPreviewItems;
326
327 KIO::PreviewJob* m_previewJob;
328
329 // When downloading or copying large files, the slot slotItemsChanged()
330 // will be called periodically within a quite short delay. To prevent
331 // a high CPU-load by generating e.g. previews for each notification, the update
332 // will be postponed until no file change has been done within a longer period
333 // of time.
334 QTimer* m_recentlyChangedItemsTimer;
335 QSet<KFileItem> m_recentlyChangedItems;
336
337 // Items which have not been changed repeatedly recently.
338 QSet<KFileItem> m_changedItems;
339
340 KDirectoryContentsCounter* m_directoryContentsCounter;
341
342 QList<KOverlayIconPlugin*> m_overlayIconsPlugin;
343
344 #ifdef HAVE_BALOO
345 Baloo::FileMonitor* m_balooFileMonitor;
346 Baloo::IndexerConfig m_balooConfig;
347 #endif
348 };
349
350 #endif
351
352