]> cloud.milkyroute.net Git - dolphin.git/blob - src/iconmanager.h
fix crash when opening a lot of columns and going back very fast by clicking on each...
[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 * Returns true, if the item \a item has been cut into
120 * the clipboard.
121 */
122 bool isCutItem(const KFileItem& item) const;
123
124 /** Applies an item effect to all cut items. */
125 void applyCutItemEffect();
126
127 /**
128 * Applies a frame around the icon. False is returned if
129 * no frame has been added because the icon is too small.
130 */
131 bool applyImageFrame(QPixmap& icon);
132
133 /**
134 * Resizes the icon to \a maxSize if the icon size does not
135 * fit into the maximum size. The aspect ratio of the icon
136 * is kept.
137 */
138 void limitToSize(QPixmap& icon, const QSize& maxSize);
139
140 /**
141 * Starts a new preview job for the items \a to m_previewJobs
142 * and triggers the preview timer.
143 */
144 void startPreviewJob(const KFileItemList& items);
145
146 /** Kills all ongoing preview jobs. */
147 void killPreviewJobs();
148
149 /**
150 * Orders the items \a items in a way that the visible items
151 * are moved to the front of the list. When passing this
152 * list to a preview job, the visible items will get generated
153 * first.
154 */
155 void orderItems(KFileItemList& items);
156
157 private:
158 /** Remembers the pixmap for an item specified by an URL. */
159 struct ItemInfo
160 {
161 KUrl url;
162 QPixmap pixmap;
163 };
164
165 bool m_showPreview;
166
167 /**
168 * True, if m_pendingItems and m_dispatchedItems should be
169 * cleared when the preview jobs have been finished.
170 */
171 bool m_clearItemQueues;
172
173 /**
174 * True if a selection has been done which should cut items.
175 */
176 bool m_hasCutSelection;
177
178 int m_pendingVisiblePreviews;
179
180 QAbstractItemView* m_view;
181 QTimer* m_previewTimer;
182 QTimer* m_scrollAreaTimer;
183 QList<KJob*> m_previewJobs;
184 DolphinModel* m_dolphinModel;
185 DolphinSortFilterProxyModel* m_proxyModel;
186
187 KMimeTypeResolver* m_mimeTypeResolver;
188
189 QList<ItemInfo> m_cutItemsCache;
190 QList<ItemInfo> m_previews;
191
192 /**
193 * Contains all items where a preview must be generated, but
194 * where the preview job has not dispatched the items yet.
195 */
196 KFileItemList m_pendingItems;
197
198 /**
199 * Contains all items, where a preview has already been
200 * generated by the preview jobs.
201 */
202 KFileItemList m_dispatchedItems;
203 };
204
205 inline bool IconManager::showPreview() const
206 {
207 return m_showPreview;
208 }
209
210 #endif