]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistview.cpp
Merge remote-tracking branch 'origin/KDE/4.10'
[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::onPreviewsShownChanged(bool shown)
194 {
195 Q_UNUSED(shown);
196 }
197
198 void KFileItemListView::onItemLayoutChanged(ItemLayout current, ItemLayout previous)
199 {
200 if (previous == DetailsLayout || current == DetailsLayout) {
201 // The details-layout requires some invisible roles that
202 // must be added to the model if the new layout is "details".
203 // If the old layout was "details" the roles will get removed.
204 applyRolesToModel();
205 }
206 KStandardItemListView::onItemLayoutChanged(current, previous);
207 triggerVisibleIndexRangeUpdate();
208 }
209
210 void KFileItemListView::onModelChanged(KItemModelBase* current, KItemModelBase* previous)
211 {
212 Q_ASSERT(qobject_cast<KFileItemModel*>(current));
213 KStandardItemListView::onModelChanged(current, previous);
214
215 delete m_modelRolesUpdater;
216 m_modelRolesUpdater = 0;
217
218 if (current) {
219 m_modelRolesUpdater = new KFileItemModelRolesUpdater(static_cast<KFileItemModel*>(current), this);
220 m_modelRolesUpdater->setIconSize(availableIconSize());
221
222 applyRolesToModel();
223 }
224 }
225
226 void KFileItemListView::onScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
227 {
228 KStandardItemListView::onScrollOrientationChanged(current, previous);
229 triggerVisibleIndexRangeUpdate();
230 }
231
232 void KFileItemListView::onItemSizeChanged(const QSizeF& current, const QSizeF& previous)
233 {
234 Q_UNUSED(current);
235 Q_UNUSED(previous);
236 triggerVisibleIndexRangeUpdate();
237 }
238
239 void KFileItemListView::onScrollOffsetChanged(qreal current, qreal previous)
240 {
241 KStandardItemListView::onScrollOffsetChanged(current, previous);
242 triggerVisibleIndexRangeUpdate();
243 }
244
245 void KFileItemListView::onVisibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous)
246 {
247 KStandardItemListView::onVisibleRolesChanged(current, previous);
248 applyRolesToModel();
249 }
250
251 void KFileItemListView::onStyleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous)
252 {
253 KStandardItemListView::onStyleOptionChanged(current, previous);
254 triggerIconSizeUpdate();
255 }
256
257 void KFileItemListView::onSupportsItemExpandingChanged(bool supportsExpanding)
258 {
259 applyRolesToModel();
260 KStandardItemListView::onSupportsItemExpandingChanged(supportsExpanding);
261 triggerVisibleIndexRangeUpdate();
262 }
263
264 void KFileItemListView::onTransactionBegin()
265 {
266 if (m_modelRolesUpdater) {
267 m_modelRolesUpdater->setPaused(true);
268 }
269 }
270
271 void KFileItemListView::onTransactionEnd()
272 {
273 if (!m_modelRolesUpdater) {
274 return;
275 }
276
277 // Only unpause the model-roles-updater if no timer is active. If one
278 // timer is still active the model-roles-updater will be unpaused later as
279 // soon as the timer has been exceeded.
280 const bool timerActive = m_updateVisibleIndexRangeTimer->isActive() ||
281 m_updateIconSizeTimer->isActive();
282 if (!timerActive) {
283 m_modelRolesUpdater->setPaused(false);
284 }
285 }
286
287 void KFileItemListView::resizeEvent(QGraphicsSceneResizeEvent* event)
288 {
289 KStandardItemListView::resizeEvent(event);
290 triggerVisibleIndexRangeUpdate();
291 }
292
293 void KFileItemListView::slotItemsRemoved(const KItemRangeList& itemRanges)
294 {
295 KStandardItemListView::slotItemsRemoved(itemRanges);
296 updateTimersInterval();
297 }
298
299 void KFileItemListView::slotSortRoleChanged(const QByteArray& current, const QByteArray& previous)
300 {
301 const QByteArray sortRole = model()->sortRole();
302 if (!visibleRoles().contains(sortRole)) {
303 applyRolesToModel();
304 }
305
306 KStandardItemListView::slotSortRoleChanged(current, previous);
307 }
308
309 void KFileItemListView::triggerVisibleIndexRangeUpdate()
310 {
311 if (!model()) {
312 return;
313 }
314 m_modelRolesUpdater->setPaused(true);
315 m_updateVisibleIndexRangeTimer->start();
316 }
317
318 void KFileItemListView::updateVisibleIndexRange()
319 {
320 if (!m_modelRolesUpdater) {
321 return;
322 }
323
324 const int index = firstVisibleIndex();
325 const int count = lastVisibleIndex() - index + 1;
326 m_modelRolesUpdater->setMaximumVisibleItems(maximumVisibleItems());
327 m_modelRolesUpdater->setVisibleIndexRange(index, count);
328
329 if (m_updateIconSizeTimer->isActive()) {
330 // If the icon-size update is pending do an immediate update
331 // of the icon-size before unpausing m_modelRolesUpdater. This prevents
332 // an unnecessary expensive recreation of all previews afterwards.
333 m_updateIconSizeTimer->stop();
334 m_modelRolesUpdater->setIconSize(availableIconSize());
335 }
336
337 m_modelRolesUpdater->setPaused(isTransactionActive());
338 updateTimersInterval();
339 }
340
341 void KFileItemListView::triggerIconSizeUpdate()
342 {
343 if (!model()) {
344 return;
345 }
346 m_modelRolesUpdater->setPaused(true);
347 m_updateIconSizeTimer->start();
348 }
349
350 void KFileItemListView::updateIconSize()
351 {
352 if (!m_modelRolesUpdater) {
353 return;
354 }
355
356 m_modelRolesUpdater->setIconSize(availableIconSize());
357
358 if (m_updateVisibleIndexRangeTimer->isActive()) {
359 // If the visibility-index-range update is pending do an immediate update
360 // of the range before unpausing m_modelRolesUpdater. This prevents
361 // an unnecessary expensive recreation of all previews afterwards.
362 m_updateVisibleIndexRangeTimer->stop();
363 const int index = firstVisibleIndex();
364 const int count = lastVisibleIndex() - index + 1;
365 m_modelRolesUpdater->setVisibleIndexRange(index, count);
366 }
367
368 m_modelRolesUpdater->setPaused(isTransactionActive());
369 updateTimersInterval();
370 }
371
372 void KFileItemListView::updateTimersInterval()
373 {
374 if (!model()) {
375 return;
376 }
377
378 // The ShortInterval is used for cases like switching the directory: If the
379 // model is empty and filled later the creation of the previews should be done
380 // as soon as possible. The LongInterval is used when the model already contains
381 // items and assures that operations like zooming don't result in too many temporary
382 // recreations of the previews.
383
384 const int interval = (model()->count() <= 0) ? ShortInterval : LongInterval;
385 m_updateVisibleIndexRangeTimer->setInterval(interval);
386 m_updateIconSizeTimer->setInterval(interval);
387 }
388
389 void KFileItemListView::applyRolesToModel()
390 {
391 if (!model()) {
392 return;
393 }
394
395 Q_ASSERT(qobject_cast<KFileItemModel*>(model()));
396 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(model());
397
398 // KFileItemModel does not distinct between "visible" and "invisible" roles.
399 // Add all roles that are mandatory for having a working KFileItemListView:
400 QSet<QByteArray> roles = visibleRoles().toSet();
401 roles.insert("iconPixmap");
402 roles.insert("iconName");
403 roles.insert("text");
404 roles.insert("isDir");
405 roles.insert("isLink");
406 if (supportsItemExpanding()) {
407 roles.insert("isExpanded");
408 roles.insert("isExpandable");
409 roles.insert("expandedParentsCount");
410 }
411
412 // Assure that the role that is used for sorting will be determined
413 roles.insert(fileItemModel->sortRole());
414
415 fileItemModel->setRoles(roles);
416 m_modelRolesUpdater->setRoles(roles);
417 }
418
419 QSize KFileItemListView::availableIconSize() const
420 {
421 const KItemListStyleOption& option = styleOption();
422 const int iconSize = option.iconSize;
423 if (itemLayout() == IconsLayout) {
424 const int maxIconWidth = itemSize().width() - 2 * option.padding;
425 return QSize(maxIconWidth, iconSize);
426 }
427
428 return QSize(iconSize, iconSize);
429 }
430
431 #include "kfileitemlistview.moc"