]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
- restore functionality that selections are kept when changing the view mode
[dolphin.git] / src / dolphiniconsview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz@gmx.at> *
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 "dolphiniconsview.h"
21
22 #include "dolphincategorydrawer.h"
23 #include "dolphincontroller.h"
24 #include "settings/dolphinsettings.h"
25 #include "dolphinsortfilterproxymodel.h"
26 #include "dolphin_iconsmodesettings.h"
27 #include "dolphin_generalsettings.h"
28 #include "draganddrophelper.h"
29 #include "selectionmanager.h"
30 #include "viewextensionsfactory.h"
31 #include "zoomlevelinfo.h"
32
33 #include <kcategorizedsortfilterproxymodel.h>
34 #include <kdialog.h>
35 #include <kdirmodel.h>
36 #include <kfileitemdelegate.h>
37
38 #include <QAbstractProxyModel>
39 #include <QApplication>
40 #include <QScrollBar>
41
42 DolphinIconsView::DolphinIconsView(QWidget* parent,
43 DolphinController* controller,
44 DolphinSortFilterProxyModel* proxyModel) :
45 KCategorizedView(parent),
46 m_controller(controller),
47 m_categoryDrawer(0),
48 m_extensionsFactory(0),
49 m_font(),
50 m_decorationSize(),
51 m_decorationPosition(QStyleOptionViewItem::Top),
52 m_displayAlignment(Qt::AlignHCenter),
53 m_itemSize(),
54 m_dropRect()
55 {
56 Q_ASSERT(controller != 0);
57 setModel(proxyModel);
58 setLayoutDirection(Qt::LeftToRight);
59 setViewMode(QListView::IconMode);
60 setResizeMode(QListView::Adjust);
61 setMovement(QListView::Static);
62 setDragEnabled(true);
63 setEditTriggers(QAbstractItemView::NoEditTriggers);
64 viewport()->setAcceptDrops(true);
65
66 setMouseTracking(true);
67
68 connect(this, SIGNAL(clicked(const QModelIndex&)),
69 controller, SLOT(requestTab(const QModelIndex&)));
70 if (KGlobalSettings::singleClick()) {
71 connect(this, SIGNAL(clicked(const QModelIndex&)),
72 controller, SLOT(triggerItem(const QModelIndex&)));
73 } else {
74 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
75 controller, SLOT(triggerItem(const QModelIndex&)));
76 }
77
78 connect(this, SIGNAL(entered(const QModelIndex&)),
79 controller, SLOT(emitItemEntered(const QModelIndex&)));
80 connect(this, SIGNAL(viewportEntered()),
81 controller, SLOT(emitViewportEntered()));
82 connect(controller, SIGNAL(nameFilterChanged(const QString&)),
83 this, SLOT(setNameFilter(const QString&)));
84 connect(controller, SIGNAL(zoomLevelChanged(int)),
85 this, SLOT(setZoomLevel(int)));
86
87 const DolphinView* view = controller->dolphinView();
88 connect(view, SIGNAL(showPreviewChanged()),
89 this, SLOT(slotShowPreviewChanged()));
90 connect(view, SIGNAL(additionalInfoChanged()),
91 this, SLOT(slotAdditionalInfoChanged()));
92
93 // apply the icons mode settings to the widget
94 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
95 Q_ASSERT(settings != 0);
96
97 if (settings->useSystemFont()) {
98 m_font = KGlobalSettings::generalFont();
99 } else {
100 m_font = QFont(settings->fontFamily(),
101 settings->fontSize(),
102 settings->fontWeight(),
103 settings->italicFont());
104 }
105
106 setWordWrap(settings->numberOfTextlines() > 1);
107
108 if (settings->arrangement() == QListView::TopToBottom) {
109 setFlow(QListView::LeftToRight);
110 m_decorationPosition = QStyleOptionViewItem::Top;
111 m_displayAlignment = Qt::AlignHCenter;
112 } else {
113 setFlow(QListView::TopToBottom);
114 m_decorationPosition = QStyleOptionViewItem::Left;
115 m_displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
116 }
117
118 m_categoryDrawer = new DolphinCategoryDrawer();
119 setCategoryDrawer(m_categoryDrawer);
120
121 setFocus();
122
123 connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)),
124 this, SLOT(slotGlobalSettingsChanged(int)));
125
126 m_extensionsFactory = new ViewExtensionsFactory(this, controller);
127 updateGridSize(view->showPreview(), 0);
128 }
129
130 DolphinIconsView::~DolphinIconsView()
131 {
132 delete m_categoryDrawer;
133 m_categoryDrawer = 0;
134 }
135
136 void DolphinIconsView::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
137 {
138 KCategorizedView::dataChanged(topLeft, bottomRight);
139
140 KCategorizedSortFilterProxyModel* proxyModel = dynamic_cast<KCategorizedSortFilterProxyModel*>(model());
141 if (!proxyModel->isCategorizedModel()) {
142 // bypass a QListView issue that items are not layout correctly if the decoration size of
143 // an index changes
144 scheduleDelayedItemsLayout();
145 }
146 }
147
148 QStyleOptionViewItem DolphinIconsView::viewOptions() const
149 {
150 QStyleOptionViewItem viewOptions = KCategorizedView::viewOptions();
151 viewOptions.font = m_font;
152 viewOptions.decorationPosition = m_decorationPosition;
153 viewOptions.decorationSize = m_decorationSize;
154 viewOptions.displayAlignment = m_displayAlignment;
155 viewOptions.showDecorationSelected = true;
156 return viewOptions;
157 }
158
159 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
160 {
161 KCategorizedView::contextMenuEvent(event);
162 m_controller->triggerContextMenuRequest(event->pos());
163 }
164
165 void DolphinIconsView::mousePressEvent(QMouseEvent* event)
166 {
167 m_controller->requestActivation();
168 const QModelIndex index = indexAt(event->pos());
169 if (index.isValid() && (event->button() == Qt::LeftButton)) {
170 // TODO: It should not be necessary to manually set the dragging state, but I could
171 // not reproduce this issue with a Qt-only example yet to find the root cause.
172 // Issue description: start Dolphin, split the view and drag an item from the
173 // inactive view to the active view by a very fast mouse movement. Result:
174 // the item gets selected instead of being dragged...
175 setState(QAbstractItemView::DraggingState);
176 }
177
178 if (!index.isValid()) {
179 if (QApplication::mouseButtons() & Qt::MidButton) {
180 m_controller->replaceUrlByClipboard();
181 }
182 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
183 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
184 clearSelection();
185 }
186 }
187
188 KCategorizedView::mousePressEvent(event);
189 }
190
191 void DolphinIconsView::startDrag(Qt::DropActions supportedActions)
192 {
193 DragAndDropHelper::instance().startDrag(this, supportedActions, m_controller);
194 }
195
196 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
197 {
198 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
199 event->acceptProposedAction();
200 }
201 }
202
203 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent* event)
204 {
205 Q_UNUSED(event);
206 setDirtyRegion(m_dropRect);
207 }
208
209 void DolphinIconsView::dragMoveEvent(QDragMoveEvent* event)
210 {
211 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
212 const QModelIndex index = indexAt(event->pos());
213 setDirtyRegion(m_dropRect);
214
215 m_dropRect.setSize(QSize()); // set as invalid
216 if (index.isValid()) {
217 const KFileItem item = m_controller->itemForIndex(index);
218 if (!item.isNull() && item.isDir()) {
219 m_dropRect = visualRect(index);
220 } else {
221 m_dropRect.setSize(QSize()); // set as invalid
222 }
223 }
224 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
225 // accept url drops, independently from the destination item
226 event->acceptProposedAction();
227 }
228
229 setDirtyRegion(m_dropRect);
230 }
231
232 void DolphinIconsView::dropEvent(QDropEvent* event)
233 {
234 const QModelIndex index = indexAt(event->pos());
235 const KFileItem item = m_controller->itemForIndex(index);
236 m_controller->indicateDroppedUrls(item, m_controller->url(), event);
237 }
238
239 QModelIndex DolphinIconsView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
240 {
241 const QModelIndex oldCurrent = currentIndex();
242
243 QModelIndex newCurrent = KCategorizedView::moveCursor(cursorAction, modifiers);
244 if (newCurrent != oldCurrent) {
245 return newCurrent;
246 }
247
248 // The cursor has not been moved by the base implementation. Provide a
249 // wrap behavior, so that the cursor will go to the next item when reaching
250 // the border.
251 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
252 if (settings->arrangement() == QListView::LeftToRight) {
253 switch (cursorAction) {
254 case MoveUp:
255 if (newCurrent.row() == 0) {
256 return newCurrent;
257 }
258 newCurrent = KCategorizedView::moveCursor(MoveLeft, modifiers);
259 selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
260 newCurrent = KCategorizedView::moveCursor(MovePageDown, modifiers);
261 break;
262
263 case MoveDown:
264 if (newCurrent.row() == (model()->rowCount() - 1)) {
265 return newCurrent;
266 }
267 newCurrent = KCategorizedView::moveCursor(MovePageUp, modifiers);
268 selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
269 newCurrent = KCategorizedView::moveCursor(MoveRight, modifiers);
270 break;
271
272 default:
273 break;
274 }
275 } else {
276 QModelIndex current = oldCurrent;
277 switch (cursorAction) {
278 case MoveLeft:
279 if (newCurrent.row() == 0) {
280 return newCurrent;
281 }
282 newCurrent = KCategorizedView::moveCursor(MoveUp, modifiers);
283 do {
284 selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
285 current = newCurrent;
286 newCurrent = KCategorizedView::moveCursor(MoveRight, modifiers);
287 } while (newCurrent != current);
288 break;
289
290 case MoveRight:
291 if (newCurrent.row() == (model()->rowCount() - 1)) {
292 return newCurrent;
293 }
294 do {
295 selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
296 current = newCurrent;
297 newCurrent = KCategorizedView::moveCursor(MoveLeft, modifiers);
298 } while (newCurrent != current);
299 newCurrent = KCategorizedView::moveCursor(MoveDown, modifiers);
300 break;
301
302 default:
303 break;
304 }
305 }
306
307 // Revert all changes of the current item to make sure that item selection works correctly
308 selectionModel()->setCurrentIndex(oldCurrent, QItemSelectionModel::NoUpdate);
309 return newCurrent;
310 }
311
312 void DolphinIconsView::keyPressEvent(QKeyEvent* event)
313 {
314 KCategorizedView::keyPressEvent(event);
315 m_controller->handleKeyPressEvent(event);
316 }
317
318 void DolphinIconsView::wheelEvent(QWheelEvent* event)
319 {
320 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
321 if (event->modifiers() & Qt::ControlModifier) {
322 event->ignore();
323 return;
324 }
325
326 horizontalScrollBar()->setSingleStep(m_itemSize.width() / 10);
327 verticalScrollBar()->setSingleStep(m_itemSize.height() / 10);
328
329 KCategorizedView::wheelEvent(event);
330 // if the icons are aligned left to right, the vertical wheel event should
331 // be applied to the horizontal scrollbar
332 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
333 const bool scrollHorizontal = (event->orientation() == Qt::Vertical) &&
334 (settings->arrangement() == QListView::LeftToRight);
335 if (scrollHorizontal) {
336 QWheelEvent horizEvent(event->pos(),
337 event->delta(),
338 event->buttons(),
339 event->modifiers(),
340 Qt::Horizontal);
341 QApplication::sendEvent(horizontalScrollBar(), &horizEvent);
342 }
343 }
344
345 void DolphinIconsView::showEvent(QShowEvent* event)
346 {
347 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
348 delegate->setMaximumSize(m_itemSize);
349
350 KCategorizedView::showEvent(event);
351 }
352
353 void DolphinIconsView::leaveEvent(QEvent* event)
354 {
355 KCategorizedView::leaveEvent(event);
356 // if the mouse is above an item and moved very fast outside the widget,
357 // no viewportEntered() signal might be emitted although the mouse has been moved
358 // above the viewport
359 m_controller->emitViewportEntered();
360 }
361
362 void DolphinIconsView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
363 {
364 KCategorizedView::currentChanged(current, previous);
365 m_extensionsFactory->handleCurrentIndexChange(current, previous);
366 }
367
368 void DolphinIconsView::resizeEvent(QResizeEvent* event)
369 {
370 KCategorizedView::resizeEvent(event);
371 const DolphinView* view = m_controller->dolphinView();
372 updateGridSize(view->showPreview(), view->additionalInfo().count());
373 }
374
375 void DolphinIconsView::slotShowPreviewChanged()
376 {
377 const DolphinView* view = m_controller->dolphinView();
378 updateGridSize(view->showPreview(), additionalInfoCount());
379 }
380
381 void DolphinIconsView::slotAdditionalInfoChanged()
382 {
383 const DolphinView* view = m_controller->dolphinView();
384 const bool showPreview = view->showPreview();
385 updateGridSize(showPreview, view->additionalInfo().count());
386 }
387
388 void DolphinIconsView::setNameFilter(const QString& nameFilter)
389 {
390 DolphinSortFilterProxyModel* proxyModel = static_cast<DolphinSortFilterProxyModel*>(model());
391 proxyModel->setFilterRegExp(nameFilter);
392 }
393
394 void DolphinIconsView::setZoomLevel(int level)
395 {
396 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
397
398 const int oldIconSize = settings->iconSize();
399 int newIconSize = oldIconSize;
400
401 const bool showPreview = m_controller->dolphinView()->showPreview();
402 if (showPreview) {
403 const int previewSize = ZoomLevelInfo::iconSizeForZoomLevel(level);
404 settings->setPreviewSize(previewSize);
405 } else {
406 newIconSize = ZoomLevelInfo::iconSizeForZoomLevel(level);
407 settings->setIconSize(newIconSize);
408 }
409
410 // increase also the grid size
411 const int diff = newIconSize - oldIconSize;
412 settings->setItemWidth(settings->itemWidth() + diff);
413 settings->setItemHeight(settings->itemHeight() + diff);
414
415 updateGridSize(showPreview, additionalInfoCount());
416 }
417
418 void DolphinIconsView::requestActivation()
419 {
420 m_controller->requestActivation();
421 }
422
423 void DolphinIconsView::slotGlobalSettingsChanged(int category)
424 {
425 Q_UNUSED(category);
426
427 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
428 Q_ASSERT(settings != 0);
429 if (settings->useSystemFont()) {
430 m_font = KGlobalSettings::generalFont();
431 }
432
433 disconnect(this, SIGNAL(clicked(QModelIndex)), m_controller, SLOT(triggerItem(QModelIndex)));
434 disconnect(this, SIGNAL(doubleClicked(QModelIndex)), m_controller, SLOT(triggerItem(QModelIndex)));
435 if (KGlobalSettings::singleClick()) {
436 connect(this, SIGNAL(clicked(QModelIndex)), m_controller, SLOT(triggerItem(QModelIndex)));
437 } else {
438 connect(this, SIGNAL(doubleClicked(QModelIndex)), m_controller, SLOT(triggerItem(QModelIndex)));
439 }
440 }
441
442 void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
443 {
444 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
445 Q_ASSERT(settings != 0);
446
447 int itemWidth = settings->itemWidth();
448 int itemHeight = settings->itemHeight();
449 int size = settings->iconSize();
450
451 if (showPreview) {
452 const int previewSize = settings->previewSize();
453 const int diff = previewSize - size;
454 itemWidth += diff;
455 itemHeight += diff;
456
457 size = previewSize;
458 }
459
460 Q_ASSERT(additionalInfoCount >= 0);
461 itemHeight += additionalInfoCount * m_font.pointSize() * 2;
462
463 // Optimize the item size of the grid in a way to prevent large gaps on the
464 // right border (= row arrangement) or the bottom border (= column arrangement).
465 // There is no public API in QListView to find out the used width of the viewport
466 // for the layout. The following calculation of 'contentWidth'/'contentHeight'
467 // is based on QListViewPrivate::prepareItemsLayout() (Copyright (C) 2009 Nokia Corporation).
468 int frameAroundContents = 0;
469 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents)) {
470 frameAroundContents = style()->pixelMetric(QStyle::PM_DefaultFrameWidth) * 2;
471 }
472 const int spacing = settings->gridSpacing();
473 if (settings->arrangement() == QListView::TopToBottom) {
474 const int contentWidth = viewport()->width() - 1
475 - frameAroundContents
476 - style()->pixelMetric(QStyle::PM_ScrollBarExtent, 0, horizontalScrollBar());
477 const int gridWidth = itemWidth + spacing * 2;
478 const int horizItemCount = contentWidth / gridWidth;
479 if (horizItemCount > 0) {
480 itemWidth += (contentWidth - horizItemCount * gridWidth) / horizItemCount;
481 }
482
483 // The decoration width indirectly defines the maximum
484 // width for the text wrapping. To use the maximum item width
485 // for text wrapping, it is used as decoration width.
486 m_decorationSize = QSize(itemWidth, size);
487 setIconSize(QSize(itemWidth, size));
488 } else {
489 const int contentHeight = viewport()->height() - 1
490 - frameAroundContents
491 - style()->pixelMetric(QStyle::PM_ScrollBarExtent, 0, verticalScrollBar());
492 const int gridHeight = itemHeight + spacing;
493 const int vertItemCount = contentHeight / gridHeight;
494 if (vertItemCount > 0) {
495 itemHeight += (contentHeight - vertItemCount * gridHeight) / vertItemCount;
496 }
497
498 m_decorationSize = QSize(size, size);
499 setIconSize(QSize(size, size));
500 }
501
502 m_itemSize = QSize(itemWidth, itemHeight);
503 setGridSizeOwn(QSize(itemWidth + spacing * 2, itemHeight + spacing));
504
505 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
506 if (delegate != 0) {
507 delegate->setMaximumSize(m_itemSize);
508 }
509 }
510
511 int DolphinIconsView::additionalInfoCount() const
512 {
513 const DolphinView* view = m_controller->dolphinView();
514 return view->additionalInfo().count();
515 }
516
517 #include "dolphiniconsview.moc"