1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #ifndef KFILEITEMMODELROLESUPDATER_H
21 #define KFILEITEMMODELROLESUPDATER_H
23 #include <config-baloo.h>
26 #include <kitemviews/kitemmodelbase.h>
28 #include "dolphin_export.h"
33 #include <QStringList>
35 class KDirectoryContentsCounter
;
39 class KOverlayIconPlugin
;
50 #include <Baloo/IndexerConfig>
54 * @brief Resolves expensive roles asynchronously and applies them to the KFileItemModel.
56 * KFileItemModel only resolves roles that are inexpensive like e.g. the file name or
57 * the permissions. Creating previews or determining the MIME-type can be quite expensive
58 * and KFileItemModelRolesUpdater takes care to update such roles asynchronously.
60 * To prevent a huge CPU and I/O load, these roles are not updated for all
61 * items, but only for the visible items, some items around the visible area,
62 * and the items on the first and last pages of the view. This is a compromise
63 * that aims to minimize the risk that the user sees items with unknown icons
64 * in the view when scrolling or pressing Home or End.
66 * Determining the roles is done in several phases:
68 * 1. If the sort role is "slow", it is determined for all items. If this
69 * cannot be finished synchronously in 200 ms, the remaining items are
70 * handled asynchronously by \a resolveNextSortRole().
72 * 2. The function startUpdating(), which is called if either the sort role
73 * has been successfully determined for all items, or items are inserted
74 * in the view, or the visible items might have changed because items
75 * were removed or moved, tries to determine the icons for all visible
76 * items synchronously for 200 ms. Then:
78 * (a) If previews are disabled, icons and all other roles are determined
79 * asynchronously for the interesting items. This is done by the
80 * function \a resolveNextPendingRoles().
82 * (b) If previews are enabled, a \a KIO::PreviewJob is started that loads
83 * the previews for the interesting items. At the same time, the icons
84 * for these items are determined asynchronously as fast as possible
85 * by \a resolveNextPendingRoles(). This minimizes the risk that the
86 * user sees "unknown" icons when scrolling before the previews have
89 * 3. Finally, the entire process is repeated for any items that might have
90 * changed in the mean time.
92 class DOLPHIN_EXPORT KFileItemModelRolesUpdater
: public QObject
97 explicit KFileItemModelRolesUpdater(KFileItemModel
* model
, QObject
* parent
= 0);
98 ~KFileItemModelRolesUpdater() override
;
100 void setIconSize(const QSize
& size
);
101 QSize
iconSize() const;
104 * Sets the range of items that are visible currently. The roles
105 * of visible items are resolved first.
107 void setVisibleIndexRange(int index
, int count
);
109 void setMaximumVisibleItems(int count
);
112 * If \a show is set to true, the "iconPixmap" role will be filled with a preview
113 * of the file. If \a show is false the MIME type icon will be used for the "iconPixmap"
116 void setPreviewsShown(bool show
);
117 bool previewsShown() const;
120 * If enabled a small preview gets upscaled to the icon size in case where
121 * the icon size is larger than the preview. Per default enlarging is
124 void setEnlargeSmallPreviews(bool enlarge
);
125 bool enlargeSmallPreviews() const;
128 * If \a paused is set to true the asynchronous resolving of roles will be paused.
129 * State changes during pauses like changing the icon size or the preview-shown
130 * will be remembered and handled after unpausing.
132 void setPaused(bool paused
);
133 bool isPaused() const;
136 * Sets the roles that should be resolved asynchronously.
138 void setRoles(const QSet
<QByteArray
>& roles
);
139 QSet
<QByteArray
> roles() const;
142 * Sets the list of enabled thumbnail plugins that are used for previews.
143 * Per default all plugins enabled in the KConfigGroup "PreviewSettings"
146 * For a list of available plugins, call KServiceTypeTrader::self()->query("ThumbCreator").
148 * @see enabledPlugins
150 void setEnabledPlugins(const QStringList
& list
);
153 * Returns the list of enabled thumbnail plugins.
154 * @see setEnabledPlugins
156 QStringList
enabledPlugins() const;
159 void slotItemsInserted(const KItemRangeList
& itemRanges
);
160 void slotItemsRemoved(const KItemRangeList
& itemRanges
);
161 void slotItemsMoved(const KItemRange
& itemRange
, QList
<int> movedToIndexes
);
162 void slotItemsChanged(const KItemRangeList
& itemRanges
,
163 const QSet
<QByteArray
>& roles
);
164 void slotSortRoleChanged(const QByteArray
& current
,
165 const QByteArray
& previous
);
168 * Is invoked after a preview has been received successfully.
169 * @see startPreviewJob()
171 void slotGotPreview(const KFileItem
& item
, const QPixmap
& pixmap
);
174 * Is invoked after generating a preview has failed.
175 * @see startPreviewJob()
177 void slotPreviewFailed(const KFileItem
& item
);
180 * Is invoked when the preview job has been finished. Starts a new preview
181 * job if there are any interesting items without previews left, or updates
182 * the changed items otherwise. *
183 * @see startPreviewJob()
185 void slotPreviewJobFinished();
188 * Is invoked when one of the KOverlayIconPlugin emit the signal that an overlay has changed
190 void slotOverlaysChanged(const QUrl
& url
, const QStringList
&);
193 * Resolves the sort role of the next item in m_pendingSortRole, applies it
194 * to the model, and invokes itself if there are any pending items left. If
195 * that is not the case, \a startUpdating() is called.
197 void resolveNextSortRole();
200 * Resolves the icon name and (if previews are disabled) all other roles
201 * for the next interesting item. If there are no pending items left, any
202 * changed items are updated.
204 void resolveNextPendingRoles();
207 * Resolves items that have not been resolved yet after the change has been
208 * notified by slotItemsChanged(). Is invoked if the m_changedItemsTimer
211 void resolveRecentlyChangedItems();
213 void applyChangedBalooRoles(const QString
& file
);
214 void applyChangedBalooRolesForItem(const KFileItem
& file
);
216 void slotDirectoryContentsCountReceived(const QString
& path
, int count
);
220 * Starts the updating of all roles. The visible items are handled first.
222 void startUpdating();
225 * Loads the icons for the visible items. After 200 ms, the function
226 * stops determining mime types and only loads preliminary icons.
227 * This is a compromise that prevents that
228 * (a) the GUI is blocked for more than 200 ms, and
229 * (b) "unknown" icons could be shown in the view.
231 void updateVisibleIcons();
234 * Creates previews for the items starting from the first item in
235 * m_pendingPreviewItems.
236 * @see slotGotPreview()
237 * @see slotPreviewFailed()
238 * @see slotPreviewJobFinished()
240 void startPreviewJob();
243 * Ensures that icons, previews, and other roles are determined for any
244 * items that have been changed.
246 void updateChangedItems();
249 * Resolves the sort role of the item and applies it to the model.
251 void applySortRole(int index
);
253 void applySortProgressToModel();
259 bool applyResolvedRoles(int index
, ResolveHint hint
);
260 QHash
<QByteArray
, QVariant
> rolesData(const KFileItem
& item
);
263 * @return The number of items of the path \a path.
265 int subItemsCount(const QString
& path
) const;
268 * Must be invoked if a property has been changed that affects
269 * the look of the preview. Takes care to update all previews.
271 void updateAllPreviews();
273 void killPreviewJob();
275 QList
<int> indexesToResolve() const;
288 // Property changes during pausing must be remembered to be able
289 // to react when unpausing again:
290 bool m_previewChangedDuringPausing
;
291 bool m_iconSizeChangedDuringPausing
;
292 bool m_rolesChangedDuringPausing
;
294 // Property for setPreviewsShown()/previewsShown().
297 // Property for setEnlargeSmallPreviews()/enlargeSmallPreviews()
298 bool m_enlargeSmallPreviews
;
300 // True if the role "iconPixmap" should be cleared when resolving the next
301 // role with resolveRole(). Is necessary if the preview gets disabled
302 // during the roles-updater has been paused by setPaused().
303 bool m_clearPreviews
;
305 // Remembers which items have been handled already, to prevent that
306 // previews and other expensive roles are determined again.
307 QSet
<KFileItem
> m_finishedItems
;
309 KFileItemModel
* m_model
;
311 int m_firstVisibleIndex
;
312 int m_lastVisibleIndex
;
313 int m_maximumVisibleItems
;
314 QSet
<QByteArray
> m_roles
;
315 QSet
<QByteArray
> m_resolvableRoles
;
316 QStringList m_enabledPlugins
;
318 // Items for which the sort role still has to be determined.
319 QSet
<KFileItem
> m_pendingSortRoleItems
;
321 // Indexes of items which still have to be handled by
322 // resolveNextPendingRoles().
323 QList
<int> m_pendingIndexes
;
325 // Items which have been left over from the last call of startPreviewJob().
326 // A new preview job will be started from them once the first one finishes.
327 KFileItemList m_pendingPreviewItems
;
329 KIO::PreviewJob
* m_previewJob
;
331 // When downloading or copying large files, the slot slotItemsChanged()
332 // will be called periodically within a quite short delay. To prevent
333 // a high CPU-load by generating e.g. previews for each notification, the update
334 // will be postponed until no file change has been done within a longer period
336 QTimer
* m_recentlyChangedItemsTimer
;
337 QSet
<KFileItem
> m_recentlyChangedItems
;
339 // Items which have not been changed repeatedly recently.
340 QSet
<KFileItem
> m_changedItems
;
342 KDirectoryContentsCounter
* m_directoryContentsCounter
;
344 QList
<KOverlayIconPlugin
*> m_overlayIconsPlugin
;
347 Baloo::FileMonitor
* m_balooFileMonitor
;
348 Baloo::IndexerConfig m_balooConfig
;