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