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