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 <libdolphin_export.h>
26 #include <kitemviews/kitemmodelbase.h>
31 #include <QStringList>
39 * @brief Resolves expensive roles asynchronously and applies them to the KFileItemModel.
41 * KFileItemModel only resolves roles that are inexpensive like e.g. the file name or
42 * the permissions. Creating previews or determining the MIME-type can be quite expensive
43 * and KFileItemModelRolesUpdater takes care to update such roles asynchronously.
45 class LIBDOLPHINPRIVATE_EXPORT KFileItemModelRolesUpdater
: public QObject
50 explicit KFileItemModelRolesUpdater(KFileItemModel
* model
, QObject
* parent
= 0);
51 virtual ~KFileItemModelRolesUpdater();
53 void setIconSize(const QSize
& size
);
54 QSize
iconSize() const;
57 * Sets the range of items that are visible currently. The roles
58 * of visible items are resolved first.
60 void setVisibleIndexRange(int index
, int count
);
63 * If \a show is set to true, the "iconPixmap" role will be filled with a preview
64 * of the file. If \a show is false the MIME type icon will be used for the "iconPixmap"
67 void setPreviewShown(bool show
);
68 bool isPreviewShown() const;
71 * If \a paused is set to true the asynchronous resolving of roles will be paused.
72 * State changes during pauses like changing the icon size or the preview-shown
73 * will be remembered and handled after unpausing.
75 void setPaused(bool paused
);
76 bool isPaused() const;
79 * Sets the roles that should be resolved asynchronously.
81 void setRoles(const QSet
<QByteArray
>& roles
);
82 QSet
<QByteArray
> roles() const;
85 * Sets the list of enabled thumbnail plugins.
86 * Per default all plugins enabled in the KConfigGroup "PreviewSettings"
89 * Note that this method doesn't cause already generated previews
92 * For a list of available plugins, call KServiceTypeTrader::self()->query("ThumbCreator").
96 void setEnabledPlugins(const QStringList
& list
);
99 * Returns the list of enabled thumbnail plugins.
100 * @see setEnabledPlugins
102 QStringList
enabledPlugins() const;
105 void slotItemsInserted(const KItemRangeList
& itemRanges
);
106 void slotItemsRemoved(const KItemRangeList
& itemRanges
);
107 void slotItemsChanged(const KItemRangeList
& itemRanges
,
108 const QSet
<QByteArray
>& roles
);
110 void slotGotPreview(const KFileItem
& item
, const QPixmap
& pixmap
);
111 void slotPreviewFailed(const KFileItem
& item
);
114 * Is invoked when the preview job has been finished and
115 * removes the job from the m_previewJobs list.
117 void slotPreviewJobFinished(KJob
* job
);
119 void resolvePendingRoles();
120 void resolveNextPendingRoles();
123 void startPreviewJob(const KFileItemList
& items
);
125 bool hasPendingRoles() const;
126 void resetPendingRoles();
127 void triggerPendingRolesResolving(int count
);
128 void sortAndResolveAllRoles();
129 void sortAndResolvePendingRoles();
135 bool applyResolvedRoles(const KFileItem
& item
, ResolveHint hint
);
136 QHash
<QByteArray
, QVariant
> rolesData(const KFileItem
& item
) const;
138 KFileItemList
sortedItems(const QSet
<KFileItem
>& items
) const;
140 static int subDirectoriesCount(const QString
& path
);
143 // Property for setPaused()/isPaused().
146 // Property changes during pausing must be remembered to be able
147 // to react when unpausing again:
148 bool m_previewChangedDuringPausing
;
149 bool m_iconSizeChangedDuringPausing
;
150 bool m_rolesChangedDuringPausing
;
152 // Property for setPreviewShown()/previewShown().
155 // True if the role "iconPixmap" should be cleared when resolving the next
156 // role with resolveRole(). Is necessary if the preview gets disabled
157 // during the roles-updater has been paused by setPaused().
158 bool m_clearPreviews
;
160 KFileItemModel
* m_model
;
162 int m_firstVisibleIndex
;
163 int m_lastVisibleIndex
;
164 QSet
<QByteArray
> m_roles
;
165 QStringList m_enabledPlugins
;
167 QSet
<KFileItem
> m_pendingVisibleItems
;
168 QSet
<KFileItem
> m_pendingInvisibleItems
;
169 QList
<KJob
*> m_previewJobs
;
171 QTimer
* m_resolvePendingRolesTimer
;