]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistview.cpp
Merge remote-tracking branch 'origin/KDE/4.11'
[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 if (itemCount == 1) {
123 return KItemListView::createDragPixmap(indexes);
124 }
125
126 // If more than one item is dragged, align the items inside a
127 // rectangular grid. The maximum grid size is limited to 5 x 5 items.
128 int xCount;
129 int size;
130 if (itemCount > 16) {
131 xCount = 5;
132 size = KIconLoader::SizeSmall;
133 } else if (itemCount > 9) {
134 xCount = 4;
135 size = KIconLoader::SizeSmallMedium;
136 } else {
137 xCount = 3;
138 size = KIconLoader::SizeMedium;
139 }
140
141 if (itemCount < xCount) {
142 xCount = itemCount;
143 }
144
145 int yCount = itemCount / xCount;
146 if (itemCount % xCount != 0) {
147 ++yCount;
148 }
149 if (yCount > xCount) {
150 yCount = xCount;
151 }
152
153 // Draw the selected items into the grid cells.
154 QPixmap dragPixmap(xCount * size + xCount, yCount * size + yCount);
155 dragPixmap.fill(Qt::transparent);
156
157 QPainter painter(&dragPixmap);
158 int x = 0;
159 int y = 0;
160 QSetIterator<int> it(indexes);
161 while (it.hasNext()) {
162 const int index = it.next();
163
164 QPixmap pixmap = model()->data(index).value("iconPixmap").value<QPixmap>();
165 if (pixmap.isNull()) {
166 KIcon icon(model()->data(index).value("iconName").toString());
167 pixmap = icon.pixmap(size, size);
168 } else {
169 KPixmapModifier::scale(pixmap, QSize(size, size));
170 }
171
172 painter.drawPixmap(x, y, pixmap);
173
174 x += size + 1;
175 if (x >= dragPixmap.width()) {
176 x = 0;
177 y += size + 1;
178 }
179
180 if (y >= dragPixmap.height()) {
181 break;
182 }
183 }
184
185 return dragPixmap;
186 }
187
188 KItemListWidgetCreatorBase* KFileItemListView::defaultWidgetCreator() const
189 {
190 return new KItemListWidgetCreator<KFileItemListWidget>();
191 }
192
193 void KFileItemListView::initializeItemListWidget(KItemListWidget* item)
194 {
195 KStandardItemListView::initializeItemListWidget(item);
196
197 // Make sure that the item has an icon.
198 QHash<QByteArray, QVariant> data = item->data();
199 if (!data.contains("iconName") && data["iconPixmap"].value<QPixmap>().isNull()) {
200 Q_ASSERT(qobject_cast<KFileItemModel*>(model()));
201 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(model());
202
203 const KFileItem fileItem = fileItemModel->fileItem(item->index());
204 data.insert("iconName", fileItem.iconName());
205 item->setData(data, QSet<QByteArray>() << "iconName");
206 }
207 }
208
209 void KFileItemListView::onPreviewsShownChanged(bool shown)
210 {
211 Q_UNUSED(shown);
212 }
213
214 void KFileItemListView::onItemLayoutChanged(ItemLayout current, ItemLayout previous)
215 {
216 KStandardItemListView::onItemLayoutChanged(current, previous);
217 triggerVisibleIndexRangeUpdate();
218 }
219
220 void KFileItemListView::onModelChanged(KItemModelBase* current, KItemModelBase* previous)
221 {
222 Q_ASSERT(qobject_cast<KFileItemModel*>(current));
223 KStandardItemListView::onModelChanged(current, previous);
224
225 delete m_modelRolesUpdater;
226 m_modelRolesUpdater = 0;
227
228 if (current) {
229 m_modelRolesUpdater = new KFileItemModelRolesUpdater(static_cast<KFileItemModel*>(current), this);
230 m_modelRolesUpdater->setIconSize(availableIconSize());
231
232 applyRolesToModel();
233 }
234 }
235
236 void KFileItemListView::onScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
237 {
238 KStandardItemListView::onScrollOrientationChanged(current, previous);
239 triggerVisibleIndexRangeUpdate();
240 }
241
242 void KFileItemListView::onItemSizeChanged(const QSizeF& current, const QSizeF& previous)
243 {
244 Q_UNUSED(current);
245 Q_UNUSED(previous);
246 triggerVisibleIndexRangeUpdate();
247 }
248
249 void KFileItemListView::onScrollOffsetChanged(qreal current, qreal previous)
250 {
251 KStandardItemListView::onScrollOffsetChanged(current, previous);
252 triggerVisibleIndexRangeUpdate();
253 }
254
255 void KFileItemListView::onVisibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous)
256 {
257 KStandardItemListView::onVisibleRolesChanged(current, previous);
258 applyRolesToModel();
259 }
260
261 void KFileItemListView::onStyleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous)
262 {
263 KStandardItemListView::onStyleOptionChanged(current, previous);
264 triggerIconSizeUpdate();
265 }
266
267 void KFileItemListView::onSupportsItemExpandingChanged(bool supportsExpanding)
268 {
269 applyRolesToModel();
270 KStandardItemListView::onSupportsItemExpandingChanged(supportsExpanding);
271 triggerVisibleIndexRangeUpdate();
272 }
273
274 void KFileItemListView::onTransactionBegin()
275 {
276 if (m_modelRolesUpdater) {
277 m_modelRolesUpdater->setPaused(true);
278 }
279 }
280
281 void KFileItemListView::onTransactionEnd()
282 {
283 if (!m_modelRolesUpdater) {
284 return;
285 }
286
287 // Only unpause the model-roles-updater if no timer is active. If one
288 // timer is still active the model-roles-updater will be unpaused later as
289 // soon as the timer has been exceeded.
290 const bool timerActive = m_updateVisibleIndexRangeTimer->isActive() ||
291 m_updateIconSizeTimer->isActive();
292 if (!timerActive) {
293 m_modelRolesUpdater->setPaused(false);
294 }
295 }
296
297 void KFileItemListView::resizeEvent(QGraphicsSceneResizeEvent* event)
298 {
299 KStandardItemListView::resizeEvent(event);
300 triggerVisibleIndexRangeUpdate();
301 }
302
303 void KFileItemListView::slotItemsRemoved(const KItemRangeList& itemRanges)
304 {
305 KStandardItemListView::slotItemsRemoved(itemRanges);
306 updateTimersInterval();
307 }
308
309 void KFileItemListView::slotSortRoleChanged(const QByteArray& current, const QByteArray& previous)
310 {
311 const QByteArray sortRole = model()->sortRole();
312 if (!visibleRoles().contains(sortRole)) {
313 applyRolesToModel();
314 }
315
316 KStandardItemListView::slotSortRoleChanged(current, previous);
317 }
318
319 void KFileItemListView::triggerVisibleIndexRangeUpdate()
320 {
321 if (!model()) {
322 return;
323 }
324 m_modelRolesUpdater->setPaused(true);
325 m_updateVisibleIndexRangeTimer->start();
326 }
327
328 void KFileItemListView::updateVisibleIndexRange()
329 {
330 if (!m_modelRolesUpdater) {
331 return;
332 }
333
334 const int index = firstVisibleIndex();
335 const int count = lastVisibleIndex() - index + 1;
336 m_modelRolesUpdater->setMaximumVisibleItems(maximumVisibleItems());
337 m_modelRolesUpdater->setVisibleIndexRange(index, count);
338
339 if (m_updateIconSizeTimer->isActive()) {
340 // If the icon-size update is pending do an immediate update
341 // of the icon-size before unpausing m_modelRolesUpdater. This prevents
342 // an unnecessary expensive recreation of all previews afterwards.
343 m_updateIconSizeTimer->stop();
344 m_modelRolesUpdater->setIconSize(availableIconSize());
345 }
346
347 m_modelRolesUpdater->setPaused(isTransactionActive());
348 updateTimersInterval();
349 }
350
351 void KFileItemListView::triggerIconSizeUpdate()
352 {
353 if (!model()) {
354 return;
355 }
356 m_modelRolesUpdater->setPaused(true);
357 m_updateIconSizeTimer->start();
358 }
359
360 void KFileItemListView::updateIconSize()
361 {
362 if (!m_modelRolesUpdater) {
363 return;
364 }
365
366 m_modelRolesUpdater->setIconSize(availableIconSize());
367
368 if (m_updateVisibleIndexRangeTimer->isActive()) {
369 // If the visibility-index-range update is pending do an immediate update
370 // of the range before unpausing m_modelRolesUpdater. This prevents
371 // an unnecessary expensive recreation of all previews afterwards.
372 m_updateVisibleIndexRangeTimer->stop();
373 const int index = firstVisibleIndex();
374 const int count = lastVisibleIndex() - index + 1;
375 m_modelRolesUpdater->setVisibleIndexRange(index, count);
376 }
377
378 m_modelRolesUpdater->setPaused(isTransactionActive());
379 updateTimersInterval();
380 }
381
382 void KFileItemListView::updateTimersInterval()
383 {
384 if (!model()) {
385 return;
386 }
387
388 // The ShortInterval is used for cases like switching the directory: If the
389 // model is empty and filled later the creation of the previews should be done
390 // as soon as possible. The LongInterval is used when the model already contains
391 // items and assures that operations like zooming don't result in too many temporary
392 // recreations of the previews.
393
394 const int interval = (model()->count() <= 0) ? ShortInterval : LongInterval;
395 m_updateVisibleIndexRangeTimer->setInterval(interval);
396 m_updateIconSizeTimer->setInterval(interval);
397 }
398
399 void KFileItemListView::applyRolesToModel()
400 {
401 if (!model()) {
402 return;
403 }
404
405 Q_ASSERT(qobject_cast<KFileItemModel*>(model()));
406 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(model());
407
408 // KFileItemModel does not distinct between "visible" and "invisible" roles.
409 // Add all roles that are mandatory for having a working KFileItemListView:
410 QSet<QByteArray> roles = visibleRoles().toSet();
411 roles.insert("iconPixmap");
412 roles.insert("iconName");
413 roles.insert("text");
414 roles.insert("isDir");
415 roles.insert("isLink");
416 if (supportsItemExpanding()) {
417 roles.insert("isExpanded");
418 roles.insert("isExpandable");
419 roles.insert("expandedParentsCount");
420 }
421
422 // Assure that the role that is used for sorting will be determined
423 roles.insert(fileItemModel->sortRole());
424
425 fileItemModel->setRoles(roles);
426 m_modelRolesUpdater->setRoles(roles);
427 }
428
429 QSize KFileItemListView::availableIconSize() const
430 {
431 const KItemListStyleOption& option = styleOption();
432 const int iconSize = option.iconSize;
433 if (itemLayout() == IconsLayout) {
434 const int maxIconWidth = itemSize().width() - 2 * option.padding;
435 return QSize(maxIconWidth, iconSize);
436 }
437
438 return QSize(iconSize, iconSize);
439 }
440
441 #include "kfileitemlistview.moc"