]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphiniconsview.cpp
Sourcecode hierarchy cleanup: Create folder "views" and move view related sources...
[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 setFocus();
125
126 connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)),
127 this, SLOT(slotGlobalSettingsChanged(int)));
128
129 updateGridSize(view->showPreview(), 0);
130 m_extensionsFactory = new ViewExtensionsFactory(this, dolphinViewController, viewModeController);
131 }
132
133 DolphinIconsView::~DolphinIconsView()
134 {
135 }
136
137 void DolphinIconsView::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
138 {
139 KCategorizedView::dataChanged(topLeft, bottomRight);
140
141 KCategorizedSortFilterProxyModel* proxyModel = dynamic_cast<KCategorizedSortFilterProxyModel*>(model());
142 if (!proxyModel->isCategorizedModel()) {
143 // bypass a QListView issue that items are not layout correctly if the decoration size of
144 // an index changes
145 scheduleDelayedItemsLayout();
146 }
147 }
148
149 QStyleOptionViewItem DolphinIconsView::viewOptions() const
150 {
151 QStyleOptionViewItem viewOptions = KCategorizedView::viewOptions();
152 viewOptions.font = m_font;
153 viewOptions.fontMetrics = QFontMetrics(m_font);
154 viewOptions.decorationPosition = m_decorationPosition;
155 viewOptions.decorationSize = m_decorationSize;
156 viewOptions.displayAlignment = m_displayAlignment;
157 viewOptions.showDecorationSelected = true;
158 return viewOptions;
159 }
160
161 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
162 {
163 KCategorizedView::contextMenuEvent(event);
164 m_dolphinViewController->triggerContextMenuRequest(event->pos());
165 }
166
167 void DolphinIconsView::mousePressEvent(QMouseEvent* event)
168 {
169 m_dolphinViewController->requestActivation();
170 const QModelIndex index = indexAt(event->pos());
171 if (index.isValid() && (event->button() == Qt::LeftButton)) {
172 // TODO: It should not be necessary to manually set the dragging state, but I could
173 // not reproduce this issue with a Qt-only example yet to find the root cause.
174 // Issue description: start Dolphin, split the view and drag an item from the
175 // inactive view to the active view by a very fast mouse movement. Result:
176 // the item gets selected instead of being dragged...
177 setState(QAbstractItemView::DraggingState);
178 }
179
180 if (!index.isValid() && (QApplication::mouseButtons() & Qt::MidButton)) {
181 m_dolphinViewController->replaceUrlByClipboard();
182 }
183
184 KCategorizedView::mousePressEvent(event);
185 }
186
187 void DolphinIconsView::startDrag(Qt::DropActions supportedActions)
188 {
189 DragAndDropHelper::instance().startDrag(this, supportedActions, m_dolphinViewController);
190 }
191
192 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
193 {
194 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
195 event->acceptProposedAction();
196 }
197 }
198
199 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent* event)
200 {
201 KCategorizedView::dragLeaveEvent(event);
202 setDirtyRegion(m_dropRect);
203 }
204
205 void DolphinIconsView::dragMoveEvent(QDragMoveEvent* event)
206 {
207 KCategorizedView::dragMoveEvent(event);
208
209 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
210 const QModelIndex index = indexAt(event->pos());
211 setDirtyRegion(m_dropRect);
212
213 m_dropRect.setSize(QSize()); // set as invalid
214 if (index.isValid()) {
215 const KFileItem item = m_dolphinViewController->itemForIndex(index);
216 if (!item.isNull() && item.isDir()) {
217 m_dropRect = visualRect(index);
218 } else {
219 m_dropRect.setSize(QSize()); // set as invalid
220 }
221 }
222 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
223 // accept url drops, independently from the destination item
224 event->acceptProposedAction();
225 }
226
227 setDirtyRegion(m_dropRect);
228 }
229
230 void DolphinIconsView::dropEvent(QDropEvent* event)
231 {
232 const QModelIndex index = indexAt(event->pos());
233 const KFileItem item = m_dolphinViewController->itemForIndex(index);
234 m_dolphinViewController->indicateDroppedUrls(item, m_viewModeController->url(), event);
235 // don't call KCategorizedView::dropEvent(event), as it moves
236 // the items which is not wanted
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_dolphinViewController->handleKeyPressEvent(event);
316 }
317
318 void DolphinIconsView::wheelEvent(QWheelEvent* event)
319 {
320 horizontalScrollBar()->setSingleStep(m_itemSize.width() / 5);
321 verticalScrollBar()->setSingleStep(m_itemSize.height() / 5);
322
323 KCategorizedView::wheelEvent(event);
324 // if the icons are aligned left to right, the vertical wheel event should
325 // be applied to the horizontal scrollbar
326 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
327 const bool scrollHorizontal = (event->orientation() == Qt::Vertical) &&
328 (settings->arrangement() == QListView::LeftToRight);
329 if (scrollHorizontal) {
330 QWheelEvent horizEvent(event->pos(),
331 event->delta(),
332 event->buttons(),
333 event->modifiers(),
334 Qt::Horizontal);
335 QApplication::sendEvent(horizontalScrollBar(), &horizEvent);
336 }
337 }
338
339 void DolphinIconsView::showEvent(QShowEvent* event)
340 {
341 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
342 delegate->setMaximumSize(m_itemSize);
343
344 KCategorizedView::showEvent(event);
345 }
346
347 void DolphinIconsView::leaveEvent(QEvent* event)
348 {
349 KCategorizedView::leaveEvent(event);
350 // if the mouse is above an item and moved very fast outside the widget,
351 // no viewportEntered() signal might be emitted although the mouse has been moved
352 // above the viewport
353 m_dolphinViewController->emitViewportEntered();
354 }
355
356 void DolphinIconsView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
357 {
358 KCategorizedView::currentChanged(current, previous);
359 m_extensionsFactory->handleCurrentIndexChange(current, previous);
360 }
361
362 void DolphinIconsView::resizeEvent(QResizeEvent* event)
363 {
364 KCategorizedView::resizeEvent(event);
365 const DolphinView* view = m_dolphinViewController->view();
366 updateGridSize(view->showPreview(), view->additionalInfo().count());
367 }
368
369 void DolphinIconsView::slotShowPreviewChanged()
370 {
371 const DolphinView* view = m_dolphinViewController->view();
372 updateGridSize(view->showPreview(), additionalInfoCount());
373 }
374
375 void DolphinIconsView::slotAdditionalInfoChanged()
376 {
377 const DolphinView* view = m_dolphinViewController->view();
378 const bool showPreview = view->showPreview();
379 updateGridSize(showPreview, view->additionalInfo().count());
380 }
381
382 void DolphinIconsView::setZoomLevel(int level)
383 {
384 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
385
386 const int oldIconSize = settings->iconSize();
387 int newIconSize = oldIconSize;
388
389 const bool showPreview = m_dolphinViewController->view()->showPreview();
390 if (showPreview) {
391 const int previewSize = ZoomLevelInfo::iconSizeForZoomLevel(level);
392 settings->setPreviewSize(previewSize);
393 } else {
394 newIconSize = ZoomLevelInfo::iconSizeForZoomLevel(level);
395 settings->setIconSize(newIconSize);
396 }
397
398 // increase also the grid size
399 const int diff = newIconSize - oldIconSize;
400 settings->setItemWidth(settings->itemWidth() + diff);
401 settings->setItemHeight(settings->itemHeight() + diff);
402
403 updateGridSize(showPreview, additionalInfoCount());
404 }
405
406 void DolphinIconsView::requestActivation()
407 {
408 m_dolphinViewController->requestActivation();
409 }
410
411 void DolphinIconsView::slotGlobalSettingsChanged(int category)
412 {
413 Q_UNUSED(category);
414
415 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
416 Q_ASSERT(settings != 0);
417 if (settings->useSystemFont()) {
418 m_font = KGlobalSettings::generalFont();
419 }
420
421 disconnect(this, SIGNAL(clicked(QModelIndex)), m_dolphinViewController, SLOT(triggerItem(QModelIndex)));
422 disconnect(this, SIGNAL(doubleClicked(QModelIndex)), m_dolphinViewController, SLOT(triggerItem(QModelIndex)));
423 if (KGlobalSettings::singleClick()) {
424 connect(this, SIGNAL(clicked(QModelIndex)), m_dolphinViewController, SLOT(triggerItem(QModelIndex)));
425 } else {
426 connect(this, SIGNAL(doubleClicked(QModelIndex)), m_dolphinViewController, SLOT(triggerItem(QModelIndex)));
427 }
428 }
429
430 void DolphinIconsView::categoryDrawerActionRequested(int action, const QModelIndex &index)
431 {
432 const QSortFilterProxyModel *model = dynamic_cast<const QSortFilterProxyModel*>(index.model());
433 const QModelIndex topLeft = model->index(index.row(), modelColumn());
434 QModelIndex bottomRight = topLeft;
435 const QString category = model->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
436 QModelIndex current = topLeft;
437 while (true) {
438 current = model->index(current.row() + 1, modelColumn());
439 const QString curCategory = model->data(model->index(current.row(), index.column()), KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
440 if (!current.isValid() || category != curCategory) {
441 break;
442 }
443 bottomRight = current;
444 }
445 switch (action) {
446 case DolphinCategoryDrawer::SelectAll:
447 selectionModel()->select(QItemSelection(topLeft, bottomRight), QItemSelectionModel::Select);
448 break;
449 case DolphinCategoryDrawer::UnselectAll:
450 selectionModel()->select(QItemSelection(topLeft, bottomRight), QItemSelectionModel::Deselect);
451 break;
452 default:
453 break;
454 }
455 }
456
457 void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
458 {
459 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
460 Q_ASSERT(settings != 0);
461
462 int itemWidth = settings->itemWidth();
463 int itemHeight = settings->itemHeight();
464 int size = settings->iconSize();
465
466 if (showPreview) {
467 const int previewSize = settings->previewSize();
468 const int diff = previewSize - size;
469 itemWidth += diff;
470 itemHeight += diff;
471
472 size = previewSize;
473 }
474
475 Q_ASSERT(additionalInfoCount >= 0);
476 itemHeight += additionalInfoCount * QFontMetrics(m_font).height();
477
478 // Optimize the item size of the grid in a way to prevent large gaps on the
479 // right border (= row arrangement) or the bottom border (= column arrangement).
480 // There is no public API in QListView to find out the used width of the viewport
481 // for the layout. The following calculation of 'contentWidth'/'contentHeight'
482 // is based on QListViewPrivate::prepareItemsLayout() (Copyright (C) 2009 Nokia Corporation).
483 int frameAroundContents = 0;
484 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents)) {
485 frameAroundContents = style()->pixelMetric(QStyle::PM_DefaultFrameWidth) * 2;
486 }
487 const int spacing = settings->gridSpacing();
488 if (settings->arrangement() == QListView::TopToBottom) {
489 const int contentWidth = viewport()->width() - 1
490 - frameAroundContents
491 - style()->pixelMetric(QStyle::PM_ScrollBarExtent, 0, horizontalScrollBar());
492 const int gridWidth = itemWidth + spacing * 2;
493 const int horizItemCount = contentWidth / gridWidth;
494 if (horizItemCount > 0) {
495 itemWidth += (contentWidth - horizItemCount * gridWidth) / horizItemCount;
496 }
497
498 // The decoration width indirectly defines the maximum
499 // width for the text wrapping. To use the maximum item width
500 // for text wrapping, it is used as decoration width.
501 m_decorationSize = QSize(itemWidth, size);
502 setIconSize(QSize(itemWidth, size));
503 } else {
504 const int contentHeight = viewport()->height() - 1
505 - frameAroundContents
506 - style()->pixelMetric(QStyle::PM_ScrollBarExtent, 0, verticalScrollBar());
507 const int gridHeight = itemHeight + spacing;
508 const int vertItemCount = contentHeight / gridHeight;
509 if (vertItemCount > 0) {
510 itemHeight += (contentHeight - vertItemCount * gridHeight) / vertItemCount;
511 }
512
513 m_decorationSize = QSize(size, size);
514 setIconSize(QSize(size, size));
515 }
516
517 m_itemSize = QSize(itemWidth, itemHeight);
518 setGridSizeOwn(QSize(itemWidth + spacing * 2, itemHeight + spacing));
519
520 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
521 if (delegate != 0) {
522 delegate->setMaximumSize(m_itemSize);
523 }
524 }
525
526 int DolphinIconsView::additionalInfoCount() const
527 {
528 const DolphinView* view = m_dolphinViewController->view();
529 return view->additionalInfo().count();
530 }
531
532 #include "dolphiniconsview.moc"