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