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