]> cloud.milkyroute.net Git - dolphin.git/blob - src/iconmanager.h
Improve the performance of the code part which checks which items are visible. Althou...
[dolphin.git] / src / iconmanager.h
1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #ifndef ICONMANAGER_H
21 #define ICONMANAGER_H
22
23 #include <kfileitem.h>
24 #include <kurl.h>
25
26 #include <QList>
27 #include <QObject>
28 #include <QPixmap>
29
30 class DolphinModel;
31 class DolphinSortFilterProxyModel;
32 class KJob;
33 class KMimeTypeResolver;
34 class QAbstractItemView;
35
36 /**
37 * @brief Manages the icon state of a directory model.
38 *
39 * Per default a preview is generated for each item.
40 * Additionally the clipboard is checked for cut items.
41 * The icon state for cut items gets dimmed automatically.
42 *
43 * The following strategy is used when creating previews:
44 * - The previews for currently visible items are created before
45 * the previews for invisible items.
46 * - If the user changes the visible area by using the scrollbars,
47 * all pending previews get paused. As soon as the user stays
48 * on the same position for a short delay, the previews are
49 * resumed. Also in this case the previews for the visible items
50 * are generated first.
51 */
52 class IconManager : public QObject
53 {
54 Q_OBJECT
55
56 public:
57 IconManager(QAbstractItemView* parent, DolphinSortFilterProxyModel* model);
58 virtual ~IconManager();
59 void setShowPreview(bool show);
60 bool showPreview() const;
61
62 /**
63 * Updates the previews for all already available items. It is only necessary
64 * to invoke this method when the icon size of the abstract item view has
65 * been changed.
66 */
67 void updatePreviews();
68
69 /**
70 * Cancels all pending previews. Should be invoked when the URL of the item
71 * view has been changed.
72 */
73 void cancelPreviews();
74
75 private slots:
76 /**
77 * Generates previews for the items \a items asynchronously.
78 */
79 void generatePreviews(const KFileItemList& items);
80
81 /**
82 * Adds the preview \a pixmap for the item \a item to the preview
83 * queue and starts a timer which will dispatch the preview queue
84 * later.
85 */
86 void addToPreviewQueue(const KFileItem& item, const QPixmap& pixmap);
87
88 /**
89 * Is invoked when the preview job has been finished and
90 * removes the job from the m_previewJobs list.
91 */
92 void slotPreviewJobFinished(KJob* job);
93
94 /** Synchronizes the item icon with the clipboard of cut items. */
95 void updateCutItems();
96
97 /**
98 * Dispatches the preview queue block by block within
99 * time slices.
100 */
101 void dispatchPreviewQueue();
102
103 /**
104 * Pauses all preview jobs and invokes IconManager::resumePreviews()
105 * after a short delay. Is invoked as soon as the user has moved
106 * a scrollbar.
107 */
108 void pausePreviews();
109
110 /**
111 * Resumes the previews that have been paused after moving the
112 * scrollbar. The previews for the current visible area are
113 * generated first.
114 */
115 void resumePreviews();
116
117 private:
118 /**
119 * Replaces the icon of the item with the \a url by the preview pixmap
120 * \a pixmap.
121 */
122 void replaceIcon(const KUrl& url, const QPixmap& pixmap);
123
124 /**
125 * Returns true, if the item \a item has been cut into
126 * the clipboard.
127 */
128 bool isCutItem(const KFileItem& item) const;
129
130 /** Applies an item effect to all cut items. */
131 void applyCutItemEffect();
132
133 /**
134 * Applies a frame around the icon. False is returned if
135 * no frame has been added because the icon is too small.
136 */
137 bool applyImageFrame(QPixmap& icon);
138
139 /**
140 * Resizes the icon to \a maxSize if the icon size does not
141 * fit into the maximum size. The aspect ratio of the icon
142 * is kept.
143 */
144 void limitToSize(QPixmap& icon, const QSize& maxSize);
145
146 /**
147 * Starts a new preview job for the items \a to m_previewJobs
148 * and triggers the preview timer.
149 */
150 void startPreviewJob(const KFileItemList& items);
151
152 /** Kills all ongoing preview jobs. */
153 void killPreviewJobs();
154
155 /**
156 * Returns true, if the item list \a items contains an item with the
157 * URL \a url. This is a helper method for IconManager::generatePreviews().
158 */
159 bool itemListContains(const KFileItemList& items, const KUrl& url) const;
160
161 private:
162 /** Remembers the pixmap for an item specified by an URL. */
163 struct ItemInfo
164 {
165 KUrl url;
166 QPixmap pixmap;
167 };
168
169 bool m_showPreview;
170
171 /**
172 * True, if m_pendingItems and m_dispatchedItems should be
173 * cleared when the preview jobs have been finished.
174 */
175 bool m_clearItemQueues;
176
177 QAbstractItemView* m_view;
178 QTimer* m_previewTimer;
179 QTimer* m_scrollAreaTimer;
180 QList<KJob*> m_previewJobs;
181 DolphinModel* m_dolphinModel;
182 DolphinSortFilterProxyModel* m_proxyModel;
183
184 KMimeTypeResolver* m_mimeTypeResolver;
185
186 QList<ItemInfo> m_cutItemsCache;
187 QList<ItemInfo> m_previews;
188
189 /**
190 * Contains the URLs of all items where a preview must be generated, but
191 * where the preview job has not dispatched the items yet.
192 */
193 QList<KUrl> m_pendingItems;
194
195 /**
196 * Contains the URLs of all items, where a preview has already been
197 * generated by the preview jobs.
198 */
199 QList<KUrl> m_dispatchedItems;
200 };
201
202 inline bool IconManager::showPreview() const
203 {
204 return m_showPreview;
205 }
206
207 #endif