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