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