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