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