]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistview.cpp
Fix crash when closing Dolphin during generating previews
[dolphin.git] / src / kitemviews / kfileitemlistview.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
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 #include "kfileitemlistview.h"
21
22 #include "kfileitemmodelrolesupdater.h"
23 #include "kfileitemlistwidget.h"
24 #include "kfileitemmodel.h"
25 #include <KLocale>
26 #include <KStringHandler>
27 #include "private/kpixmapmodifier.h"
28
29 #include <KDebug>
30 #include <KIcon>
31 #include <KTextEdit>
32
33 #include <QPainter>
34 #include <QTextLine>
35 #include <QTimer>
36
37 // #define KFILEITEMLISTVIEW_DEBUG
38
39 namespace {
40 const int ShortInterval = 50;
41 const int LongInterval = 300;
42 }
43
44 KFileItemListView::KFileItemListView(QGraphicsWidget* parent) :
45 KStandardItemListView(parent),
46 m_modelRolesUpdater(0),
47 m_updateVisibleIndexRangeTimer(0),
48 m_updateIconSizeTimer(0)
49 {
50 setAcceptDrops(true);
51
52 setScrollOrientation(Qt::Vertical);
53
54 m_updateVisibleIndexRangeTimer = new QTimer(this);
55 m_updateVisibleIndexRangeTimer->setSingleShot(true);
56 m_updateVisibleIndexRangeTimer->setInterval(ShortInterval);
57 connect(m_updateVisibleIndexRangeTimer, SIGNAL(timeout()), this, SLOT(updateVisibleIndexRange()));
58
59 m_updateIconSizeTimer = new QTimer(this);
60 m_updateIconSizeTimer->setSingleShot(true);
61 m_updateIconSizeTimer->setInterval(ShortInterval);
62 connect(m_updateIconSizeTimer, SIGNAL(timeout()), this, SLOT(updateIconSize()));
63
64 setVisibleRoles(QList<QByteArray>() << "text");
65 }
66
67 KFileItemListView::~KFileItemListView()
68 {
69 }
70
71 void KFileItemListView::setPreviewsShown(bool show)
72 {
73 if (!m_modelRolesUpdater) {
74 return;
75 }
76
77 if (m_modelRolesUpdater->previewsShown() != show) {
78 beginTransaction();
79 m_modelRolesUpdater->setPreviewsShown(show);
80 onPreviewsShownChanged(show);
81 endTransaction();
82 }
83 }
84
85 bool KFileItemListView::previewsShown() const
86 {
87 return m_modelRolesUpdater ? m_modelRolesUpdater->previewsShown() : false;
88 }
89
90 void KFileItemListView::setEnlargeSmallPreviews(bool enlarge)
91 {
92 if (m_modelRolesUpdater) {
93 m_modelRolesUpdater->setEnlargeSmallPreviews(enlarge);
94 }
95 }
96
97 bool KFileItemListView::enlargeSmallPreviews() const
98 {
99 return m_modelRolesUpdater ? m_modelRolesUpdater->enlargeSmallPreviews() : false;
100 }
101
102 void KFileItemListView::setEnabledPlugins(const QStringList& list)
103 {
104 if (m_modelRolesUpdater) {
105 m_modelRolesUpdater->setEnabledPlugins(list);
106 }
107 }
108
109 QStringList KFileItemListView::enabledPlugins() const
110 {
111 return m_modelRolesUpdater ? m_modelRolesUpdater->enabledPlugins() : QStringList();
112 }
113
114 QPixmap KFileItemListView::createDragPixmap(const QSet<int>& indexes) const
115 {
116 if (!model()) {
117 return QPixmap();
118 }
119
120 const int itemCount = indexes.count();
121 Q_ASSERT(itemCount > 0);
122
123 // If more than one item is dragged, align the items inside a
124 // rectangular grid. The maximum grid size is limited to 5 x 5 items.
125 int xCount;
126 int size;
127 if (itemCount > 16) {
128 xCount = 5;
129 size = KIconLoader::SizeSmall;
130 } else if (itemCount > 9) {
131 xCount = 4;
132 size = KIconLoader::SizeSmallMedium;
133 } else {
134 xCount = 3;
135 size = KIconLoader::SizeMedium;
136 }
137
138 if (itemCount < xCount) {
139 xCount = itemCount;
140 }
141
142 int yCount = itemCount / xCount;
143 if (itemCount % xCount != 0) {
144 ++yCount;
145 }
146 if (yCount > xCount) {
147 yCount = xCount;
148 }
149
150 // Draw the selected items into the grid cells.
151 QPixmap dragPixmap(xCount * size + xCount, yCount * size + yCount);
152 dragPixmap.fill(Qt::transparent);
153
154 QPainter painter(&dragPixmap);
155 int x = 0;
156 int y = 0;
157 QSetIterator<int> it(indexes);
158 while (it.hasNext()) {
159 const int index = it.next();
160
161 QPixmap pixmap = model()->data(index).value("iconPixmap").value<QPixmap>();
162 if (pixmap.isNull()) {
163 KIcon icon(model()->data(index).value("iconName").toString());
164 pixmap = icon.pixmap(size, size);
165 } else {
166 KPixmapModifier::scale(pixmap, QSize(size, size));
167 }
168
169 painter.drawPixmap(x, y, pixmap);
170
171 x += size + 1;
172 if (x >= dragPixmap.width()) {
173 x = 0;
174 y += size + 1;
175 }
176
177 if (y >= dragPixmap.height()) {
178 break;
179 }
180 }
181
182 return dragPixmap;
183 }
184
185 KItemListWidgetCreatorBase* KFileItemListView::defaultWidgetCreator() const
186 {
187 return new KItemListWidgetCreator<KFileItemListWidget>();
188 }
189
190 void KFileItemListView::onPreviewsShownChanged(bool shown)
191 {
192 Q_UNUSED(shown);
193 }
194
195 void KFileItemListView::onItemLayoutChanged(ItemLayout current, ItemLayout previous)
196 {
197 if (previous == DetailsLayout || current == DetailsLayout) {
198 // The details-layout requires some invisible roles that
199 // must be added to the model if the new layout is "details".
200 // If the old layout was "details" the roles will get removed.
201 applyRolesToModel();
202 }
203 KStandardItemListView::onItemLayoutChanged(current, previous);
204 triggerVisibleIndexRangeUpdate();
205 }
206
207 void KFileItemListView::onModelChanged(KItemModelBase* current, KItemModelBase* previous)
208 {
209 Q_ASSERT(qobject_cast<KFileItemModel*>(current));
210 KStandardItemListView::onModelChanged(current, previous);
211
212 delete m_modelRolesUpdater;
213 m_modelRolesUpdater = 0;
214
215 if (current) {
216 m_modelRolesUpdater = new KFileItemModelRolesUpdater(static_cast<KFileItemModel*>(current), this);
217 m_modelRolesUpdater->setIconSize(availableIconSize());
218
219 applyRolesToModel();
220 }
221 }
222
223 void KFileItemListView::onScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
224 {
225 KStandardItemListView::onScrollOrientationChanged(current, previous);
226 triggerVisibleIndexRangeUpdate();
227 }
228
229 void KFileItemListView::onItemSizeChanged(const QSizeF& current, const QSizeF& previous)
230 {
231 Q_UNUSED(current);
232 Q_UNUSED(previous);
233 triggerVisibleIndexRangeUpdate();
234 }
235
236 void KFileItemListView::onScrollOffsetChanged(qreal current, qreal previous)
237 {
238 KStandardItemListView::onScrollOffsetChanged(current, previous);
239 triggerVisibleIndexRangeUpdate();
240 }
241
242 void KFileItemListView::onVisibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous)
243 {
244 KStandardItemListView::onVisibleRolesChanged(current, previous);
245 applyRolesToModel();
246 }
247
248 void KFileItemListView::onStyleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous)
249 {
250 KStandardItemListView::onStyleOptionChanged(current, previous);
251 triggerIconSizeUpdate();
252 }
253
254 void KFileItemListView::onSupportsItemExpandingChanged(bool supportsExpanding)
255 {
256 applyRolesToModel();
257 KStandardItemListView::onSupportsItemExpandingChanged(supportsExpanding);
258 triggerVisibleIndexRangeUpdate();
259 }
260
261 void KFileItemListView::onTransactionBegin()
262 {
263 if (m_modelRolesUpdater) {
264 m_modelRolesUpdater->setPaused(true);
265 }
266 }
267
268 void KFileItemListView::onTransactionEnd()
269 {
270 if (!m_modelRolesUpdater) {
271 return;
272 }
273
274 // Only unpause the model-roles-updater if no timer is active. If one
275 // timer is still active the model-roles-updater will be unpaused later as
276 // soon as the timer has been exceeded.
277 const bool timerActive = m_updateVisibleIndexRangeTimer->isActive() ||
278 m_updateIconSizeTimer->isActive();
279 if (!timerActive) {
280 m_modelRolesUpdater->setPaused(false);
281 }
282 }
283
284 void KFileItemListView::resizeEvent(QGraphicsSceneResizeEvent* event)
285 {
286 KStandardItemListView::resizeEvent(event);
287 triggerVisibleIndexRangeUpdate();
288 }
289
290 void KFileItemListView::slotItemsRemoved(const KItemRangeList& itemRanges)
291 {
292 KStandardItemListView::slotItemsRemoved(itemRanges);
293 updateTimersInterval();
294 }
295
296 void KFileItemListView::slotSortRoleChanged(const QByteArray& current, const QByteArray& previous)
297 {
298 const QByteArray sortRole = model()->sortRole();
299 if (!visibleRoles().contains(sortRole)) {
300 applyRolesToModel();
301 }
302
303 KStandardItemListView::slotSortRoleChanged(current, previous);
304 }
305
306 void KFileItemListView::triggerVisibleIndexRangeUpdate()
307 {
308 if (!model()) {
309 return;
310 }
311 m_modelRolesUpdater->setPaused(true);
312 m_updateVisibleIndexRangeTimer->start();
313 }
314
315 void KFileItemListView::updateVisibleIndexRange()
316 {
317 if (!m_modelRolesUpdater) {
318 return;
319 }
320
321 const int index = firstVisibleIndex();
322 const int count = lastVisibleIndex() - index + 1;
323 m_modelRolesUpdater->setVisibleIndexRange(index, count);
324
325 if (m_updateIconSizeTimer->isActive()) {
326 // If the icon-size update is pending do an immediate update
327 // of the icon-size before unpausing m_modelRolesUpdater. This prevents
328 // an unnecessary expensive recreation of all previews afterwards.
329 m_updateIconSizeTimer->stop();
330 m_modelRolesUpdater->setIconSize(availableIconSize());
331 }
332
333 m_modelRolesUpdater->setPaused(isTransactionActive());
334 updateTimersInterval();
335 }
336
337 void KFileItemListView::triggerIconSizeUpdate()
338 {
339 if (!model()) {
340 return;
341 }
342 m_modelRolesUpdater->setPaused(true);
343 m_updateIconSizeTimer->start();
344 }
345
346 void KFileItemListView::updateIconSize()
347 {
348 if (!m_modelRolesUpdater) {
349 return;
350 }
351
352 m_modelRolesUpdater->setIconSize(availableIconSize());
353
354 if (m_updateVisibleIndexRangeTimer->isActive()) {
355 // If the visibility-index-range update is pending do an immediate update
356 // of the range before unpausing m_modelRolesUpdater. This prevents
357 // an unnecessary expensive recreation of all previews afterwards.
358 m_updateVisibleIndexRangeTimer->stop();
359 const int index = firstVisibleIndex();
360 const int count = lastVisibleIndex() - index + 1;
361 m_modelRolesUpdater->setVisibleIndexRange(index, count);
362 }
363
364 m_modelRolesUpdater->setPaused(isTransactionActive());
365 updateTimersInterval();
366 }
367
368 void KFileItemListView::updateTimersInterval()
369 {
370 if (!model()) {
371 return;
372 }
373
374 // The ShortInterval is used for cases like switching the directory: If the
375 // model is empty and filled later the creation of the previews should be done
376 // as soon as possible. The LongInterval is used when the model already contains
377 // items and assures that operations like zooming don't result in too many temporary
378 // recreations of the previews.
379
380 const int interval = (model()->count() <= 0) ? ShortInterval : LongInterval;
381 m_updateVisibleIndexRangeTimer->setInterval(interval);
382 m_updateIconSizeTimer->setInterval(interval);
383 }
384
385 void KFileItemListView::applyRolesToModel()
386 {
387 if (!model()) {
388 return;
389 }
390
391 Q_ASSERT(qobject_cast<KFileItemModel*>(model()));
392 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(model());
393
394 // KFileItemModel does not distinct between "visible" and "invisible" roles.
395 // Add all roles that are mandatory for having a working KFileItemListView:
396 QSet<QByteArray> roles = visibleRoles().toSet();
397 roles.insert("iconPixmap");
398 roles.insert("iconName");
399 roles.insert("text");
400 roles.insert("isDir");
401 if (supportsItemExpanding()) {
402 roles.insert("isExpanded");
403 roles.insert("isExpandable");
404 roles.insert("expandedParentsCount");
405 }
406
407 // Assure that the role that is used for sorting will be determined
408 roles.insert(fileItemModel->sortRole());
409
410 fileItemModel->setRoles(roles);
411 m_modelRolesUpdater->setRoles(roles);
412 }
413
414 QSize KFileItemListView::availableIconSize() const
415 {
416 const KItemListStyleOption& option = styleOption();
417 const int iconSize = option.iconSize;
418 if (itemLayout() == IconsLayout) {
419 const int maxIconWidth = itemSize().width() - 2 * option.padding;
420 return QSize(maxIconWidth, iconSize);
421 }
422
423 return QSize(iconSize, iconSize);
424 }
425
426 #include "kfileitemlistview.moc"