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