]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistview.cpp
Save bookmarks when adding them via the context menu.
[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 "private/kpixmapmodifier.h"
26
27 #include <QIcon>
28 #include <KIconLoader>
29
30 #include <QPainter>
31 #include <QTimer>
32
33 // #define KFILEITEMLISTVIEW_DEBUG
34
35 namespace {
36 // If the visible index range changes, KFileItemModelRolesUpdater is not
37 // informed immediatetly, but with a short delay. This ensures that scrolling
38 // always feels smooth and is not interrupted by icon loading (which can be
39 // quite expensive if a disk access is required to determine the final icon).
40 const int ShortInterval = 50;
41
42 // If the icon size changes, a longer delay is used. This prevents that
43 // the expensive re-generation of all previews is triggered repeatedly when
44 // changing the zoom level.
45 const int LongInterval = 300;
46 }
47
48 KFileItemListView::KFileItemListView(QGraphicsWidget* parent) :
49 KStandardItemListView(parent),
50 m_modelRolesUpdater(0),
51 m_updateVisibleIndexRangeTimer(0),
52 m_updateIconSizeTimer(0)
53 {
54 setAcceptDrops(true);
55
56 setScrollOrientation(Qt::Vertical);
57
58 m_updateVisibleIndexRangeTimer = new QTimer(this);
59 m_updateVisibleIndexRangeTimer->setSingleShot(true);
60 m_updateVisibleIndexRangeTimer->setInterval(ShortInterval);
61 connect(m_updateVisibleIndexRangeTimer, &QTimer::timeout, this, &KFileItemListView::updateVisibleIndexRange);
62
63 m_updateIconSizeTimer = new QTimer(this);
64 m_updateIconSizeTimer->setSingleShot(true);
65 m_updateIconSizeTimer->setInterval(LongInterval);
66 connect(m_updateIconSizeTimer, &QTimer::timeout, this, &KFileItemListView::updateIconSize);
67
68 setVisibleRoles({"text"});
69 }
70
71 KFileItemListView::~KFileItemListView()
72 {
73 }
74
75 void KFileItemListView::setPreviewsShown(bool show)
76 {
77 if (!m_modelRolesUpdater) {
78 return;
79 }
80
81 if (m_modelRolesUpdater->previewsShown() != show) {
82 beginTransaction();
83 m_modelRolesUpdater->setPreviewsShown(show);
84 onPreviewsShownChanged(show);
85 endTransaction();
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::setEnabledPlugins(const QStringList& list)
107 {
108 if (m_modelRolesUpdater) {
109 m_modelRolesUpdater->setEnabledPlugins(list);
110 }
111 }
112
113 QStringList KFileItemListView::enabledPlugins() const
114 {
115 return m_modelRolesUpdater ? m_modelRolesUpdater->enabledPlugins() : QStringList();
116 }
117
118 QPixmap KFileItemListView::createDragPixmap(const KItemSet& indexes) const
119 {
120 if (!model()) {
121 return QPixmap();
122 }
123
124 const int itemCount = indexes.count();
125 Q_ASSERT(itemCount > 0);
126 if (itemCount == 1) {
127 return KItemListView::createDragPixmap(indexes);
128 }
129
130 // If more than one item is dragged, align the items inside a
131 // rectangular grid. The maximum grid size is limited to 5 x 5 items.
132 int xCount;
133 int size;
134 if (itemCount > 16) {
135 xCount = 5;
136 size = KIconLoader::SizeSmall;
137 } else if (itemCount > 9) {
138 xCount = 4;
139 size = KIconLoader::SizeSmallMedium;
140 } else {
141 xCount = 3;
142 size = KIconLoader::SizeMedium;
143 }
144
145 if (itemCount < xCount) {
146 xCount = itemCount;
147 }
148
149 int yCount = itemCount / xCount;
150 if (itemCount % xCount != 0) {
151 ++yCount;
152 }
153 if (yCount > xCount) {
154 yCount = xCount;
155 }
156
157 // Draw the selected items into the grid cells.
158 QPixmap dragPixmap(xCount * size + xCount, yCount * size + yCount);
159 dragPixmap.fill(Qt::transparent);
160
161 QPainter painter(&dragPixmap);
162 int x = 0;
163 int y = 0;
164
165 foreach (int index, indexes) {
166 QPixmap pixmap = model()->data(index).value("iconPixmap").value<QPixmap>();
167 if (pixmap.isNull()) {
168 QIcon icon = QIcon::fromTheme(model()->data(index).value("iconName").toString());
169 pixmap = icon.pixmap(size, size);
170 } else {
171 KPixmapModifier::scale(pixmap, QSize(size, size));
172 }
173
174 painter.drawPixmap(x, y, pixmap);
175
176 x += size + 1;
177 if (x >= dragPixmap.width()) {
178 x = 0;
179 y += size + 1;
180 }
181
182 if (y >= dragPixmap.height()) {
183 break;
184 }
185 }
186
187 return dragPixmap;
188 }
189
190 KItemListWidgetCreatorBase* KFileItemListView::defaultWidgetCreator() const
191 {
192 return new KItemListWidgetCreator<KFileItemListWidget>();
193 }
194
195 void KFileItemListView::initializeItemListWidget(KItemListWidget* item)
196 {
197 KStandardItemListView::initializeItemListWidget(item);
198
199 // Make sure that the item has an icon.
200 QHash<QByteArray, QVariant> data = item->data();
201 if (!data.contains("iconName") && data["iconPixmap"].value<QPixmap>().isNull()) {
202 Q_ASSERT(qobject_cast<KFileItemModel*>(model()));
203 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(model());
204
205 const KFileItem fileItem = fileItemModel->fileItem(item->index());
206 data.insert("iconName", fileItem.iconName());
207 item->setData(data, {"iconName"});
208 }
209 }
210
211 void KFileItemListView::onPreviewsShownChanged(bool shown)
212 {
213 Q_UNUSED(shown);
214 }
215
216 void KFileItemListView::onItemLayoutChanged(ItemLayout current, ItemLayout previous)
217 {
218 KStandardItemListView::onItemLayoutChanged(current, previous);
219 triggerVisibleIndexRangeUpdate();
220 }
221
222 void KFileItemListView::onModelChanged(KItemModelBase* current, KItemModelBase* previous)
223 {
224 Q_ASSERT(qobject_cast<KFileItemModel*>(current));
225 KStandardItemListView::onModelChanged(current, previous);
226
227 delete m_modelRolesUpdater;
228 m_modelRolesUpdater = 0;
229
230 if (current) {
231 m_modelRolesUpdater = new KFileItemModelRolesUpdater(static_cast<KFileItemModel*>(current), this);
232 m_modelRolesUpdater->setIconSize(availableIconSize());
233
234 applyRolesToModel();
235 }
236 }
237
238 void KFileItemListView::onScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
239 {
240 KStandardItemListView::onScrollOrientationChanged(current, previous);
241 triggerVisibleIndexRangeUpdate();
242 }
243
244 void KFileItemListView::onItemSizeChanged(const QSizeF& current, const QSizeF& previous)
245 {
246 Q_UNUSED(current);
247 Q_UNUSED(previous);
248 triggerVisibleIndexRangeUpdate();
249 }
250
251 void KFileItemListView::onScrollOffsetChanged(qreal current, qreal previous)
252 {
253 KStandardItemListView::onScrollOffsetChanged(current, previous);
254 triggerVisibleIndexRangeUpdate();
255 }
256
257 void KFileItemListView::onVisibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous)
258 {
259 KStandardItemListView::onVisibleRolesChanged(current, previous);
260 applyRolesToModel();
261 }
262
263 void KFileItemListView::onStyleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous)
264 {
265 KStandardItemListView::onStyleOptionChanged(current, previous);
266 triggerIconSizeUpdate();
267 }
268
269 void KFileItemListView::onSupportsItemExpandingChanged(bool supportsExpanding)
270 {
271 applyRolesToModel();
272 KStandardItemListView::onSupportsItemExpandingChanged(supportsExpanding);
273 triggerVisibleIndexRangeUpdate();
274 }
275
276 void KFileItemListView::onTransactionBegin()
277 {
278 if (m_modelRolesUpdater) {
279 m_modelRolesUpdater->setPaused(true);
280 }
281 }
282
283 void KFileItemListView::onTransactionEnd()
284 {
285 if (!m_modelRolesUpdater) {
286 return;
287 }
288
289 // Only unpause the model-roles-updater if no timer is active. If one
290 // timer is still active the model-roles-updater will be unpaused later as
291 // soon as the timer has been exceeded.
292 const bool timerActive = m_updateVisibleIndexRangeTimer->isActive() ||
293 m_updateIconSizeTimer->isActive();
294 if (!timerActive) {
295 m_modelRolesUpdater->setPaused(false);
296 }
297 }
298
299 void KFileItemListView::resizeEvent(QGraphicsSceneResizeEvent* event)
300 {
301 KStandardItemListView::resizeEvent(event);
302 triggerVisibleIndexRangeUpdate();
303 }
304
305 void KFileItemListView::slotItemsRemoved(const KItemRangeList& itemRanges)
306 {
307 KStandardItemListView::slotItemsRemoved(itemRanges);
308 }
309
310 void KFileItemListView::slotSortRoleChanged(const QByteArray& current, const QByteArray& previous)
311 {
312 const QByteArray sortRole = model()->sortRole();
313 if (!visibleRoles().contains(sortRole)) {
314 applyRolesToModel();
315 }
316
317 KStandardItemListView::slotSortRoleChanged(current, previous);
318 }
319
320 void KFileItemListView::triggerVisibleIndexRangeUpdate()
321 {
322 if (!model()) {
323 return;
324 }
325 m_modelRolesUpdater->setPaused(true);
326
327 // If the icon size has been changed recently, wait until
328 // m_updateIconSizeTimer expires.
329 if (!m_updateIconSizeTimer->isActive()) {
330 m_updateVisibleIndexRangeTimer->start();
331 }
332 }
333
334 void KFileItemListView::updateVisibleIndexRange()
335 {
336 if (!m_modelRolesUpdater) {
337 return;
338 }
339
340 const int index = firstVisibleIndex();
341 const int count = lastVisibleIndex() - index + 1;
342 m_modelRolesUpdater->setMaximumVisibleItems(maximumVisibleItems());
343 m_modelRolesUpdater->setVisibleIndexRange(index, count);
344 m_modelRolesUpdater->setPaused(isTransactionActive());
345 }
346
347 void KFileItemListView::triggerIconSizeUpdate()
348 {
349 if (!model()) {
350 return;
351 }
352 m_modelRolesUpdater->setPaused(true);
353 m_updateIconSizeTimer->start();
354
355 // The visible index range will be updated when m_updateIconSizeTimer expires.
356 // Stop m_updateVisibleIndexRangeTimer to prevent an expensive re-generation
357 // of all previews (note that the user might change the icon size again soon).
358 m_updateVisibleIndexRangeTimer->stop();
359 }
360
361 void KFileItemListView::updateIconSize()
362 {
363 if (!m_modelRolesUpdater) {
364 return;
365 }
366
367 m_modelRolesUpdater->setIconSize(availableIconSize());
368
369 // Update the visible index range (which has most likely changed after the
370 // icon size change) before unpausing m_modelRolesUpdater.
371 const int index = firstVisibleIndex();
372 const int count = lastVisibleIndex() - index + 1;
373 m_modelRolesUpdater->setVisibleIndexRange(index, count);
374
375 m_modelRolesUpdater->setPaused(isTransactionActive());
376 }
377
378 void KFileItemListView::applyRolesToModel()
379 {
380 if (!model()) {
381 return;
382 }
383
384 Q_ASSERT(qobject_cast<KFileItemModel*>(model()));
385 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(model());
386
387 // KFileItemModel does not distinct between "visible" and "invisible" roles.
388 // Add all roles that are mandatory for having a working KFileItemListView:
389 QSet<QByteArray> roles = visibleRoles().toSet();
390 roles.insert("iconPixmap");
391 roles.insert("iconName");
392 roles.insert("text");
393 roles.insert("isDir");
394 roles.insert("isLink");
395 if (supportsItemExpanding()) {
396 roles.insert("isExpanded");
397 roles.insert("isExpandable");
398 roles.insert("expandedParentsCount");
399 }
400
401 // Assure that the role that is used for sorting will be determined
402 roles.insert(fileItemModel->sortRole());
403
404 fileItemModel->setRoles(roles);
405 m_modelRolesUpdater->setRoles(roles);
406 }
407
408 QSize KFileItemListView::availableIconSize() const
409 {
410 const KItemListStyleOption& option = styleOption();
411 const int iconSize = option.iconSize;
412 if (itemLayout() == IconsLayout) {
413 const int maxIconWidth = itemSize().width() - 2 * option.padding;
414 return QSize(maxIconWidth, iconSize);
415 }
416
417 return QSize(iconSize, iconSize);
418 }
419