]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
Adapt KCategorizedView and KCategoryDrawer changes from kdelibs
[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 DragAndDropHelper::instance().startDrag(this, supportedActions, m_controller);
196 }
197
198 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
199 {
200 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
201 event->acceptProposedAction();
202 }
203 }
204
205 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent* event)
206 {
207 setDirtyRegion(m_dropRect);
208 }
209
210 void DolphinIconsView::dragMoveEvent(QDragMoveEvent* event)
211 {
212 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
213 const QModelIndex index = indexAt(event->pos());
214 setDirtyRegion(m_dropRect);
215
216 m_dropRect.setSize(QSize()); // set as invalid
217 if (index.isValid()) {
218 const KFileItem item = m_controller->itemForIndex(index);
219 if (!item.isNull() && item.isDir()) {
220 m_dropRect = visualRect(index);
221 } else {
222 m_dropRect.setSize(QSize()); // set as invalid
223 }
224 }
225 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
226 // accept url drops, independently from the destination item
227 event->acceptProposedAction();
228 }
229
230 setDirtyRegion(m_dropRect);
231 }
232
233 void DolphinIconsView::dropEvent(QDropEvent* event)
234 {
235 const QModelIndex index = indexAt(event->pos());
236 const KFileItem item = m_controller->itemForIndex(index);
237 m_controller->indicateDroppedUrls(item, m_controller->url(), event);
238 }
239
240 QModelIndex DolphinIconsView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
241 {
242 const QModelIndex oldCurrent = currentIndex();
243
244 QModelIndex newCurrent = KCategorizedView::moveCursor(cursorAction, modifiers);
245 if (newCurrent != oldCurrent) {
246 return newCurrent;
247 }
248
249 // The cursor has not been moved by the base implementation. Provide a
250 // wrap behavior, so that the cursor will go to the next item when reaching
251 // the border.
252 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
253 if (settings->arrangement() == QListView::LeftToRight) {
254 switch (cursorAction) {
255 case MoveUp:
256 if (newCurrent.row() == 0) {
257 return newCurrent;
258 }
259 newCurrent = KCategorizedView::moveCursor(MoveLeft, modifiers);
260 selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
261 newCurrent = KCategorizedView::moveCursor(MovePageDown, modifiers);
262 break;
263
264 case MoveDown:
265 if (newCurrent.row() == (model()->rowCount() - 1)) {
266 return newCurrent;
267 }
268 newCurrent = KCategorizedView::moveCursor(MovePageUp, modifiers);
269 selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
270 newCurrent = KCategorizedView::moveCursor(MoveRight, modifiers);
271 break;
272
273 default:
274 break;
275 }
276 } else {
277 QModelIndex current = oldCurrent;
278 switch (cursorAction) {
279 case MoveLeft:
280 if (newCurrent.row() == 0) {
281 return newCurrent;
282 }
283 newCurrent = KCategorizedView::moveCursor(MoveUp, modifiers);
284 do {
285 selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
286 current = newCurrent;
287 newCurrent = KCategorizedView::moveCursor(MoveRight, modifiers);
288 } while (newCurrent != current);
289 break;
290
291 case MoveRight:
292 if (newCurrent.row() == (model()->rowCount() - 1)) {
293 return newCurrent;
294 }
295 do {
296 selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
297 current = newCurrent;
298 newCurrent = KCategorizedView::moveCursor(MoveLeft, modifiers);
299 } while (newCurrent != current);
300 newCurrent = KCategorizedView::moveCursor(MoveDown, modifiers);
301 break;
302
303 default:
304 break;
305 }
306 }
307
308 // Revert all changes of the current item to make sure that item selection works correctly
309 selectionModel()->setCurrentIndex(oldCurrent, QItemSelectionModel::NoUpdate);
310 return newCurrent;
311 }
312
313 void DolphinIconsView::keyPressEvent(QKeyEvent* event)
314 {
315 KCategorizedView::keyPressEvent(event);
316 m_controller->handleKeyPressEvent(event);
317 }
318
319 void DolphinIconsView::wheelEvent(QWheelEvent* event)
320 {
321 if (m_selectionManager != 0) {
322 m_selectionManager->reset();
323 }
324
325 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
326 if (event->modifiers() & Qt::ControlModifier) {
327 event->ignore();
328 return;
329 }
330
331 horizontalScrollBar()->setSingleStep(m_itemSize.width() / 10);
332 verticalScrollBar()->setSingleStep(m_itemSize.height() / 10);
333
334 KCategorizedView::wheelEvent(event);
335 // if the icons are aligned left to right, the vertical wheel event should
336 // be applied to the horizontal scrollbar
337 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
338 const bool scrollHorizontal = (event->orientation() == Qt::Vertical) &&
339 (settings->arrangement() == QListView::LeftToRight);
340 if (scrollHorizontal) {
341 QWheelEvent horizEvent(event->pos(),
342 event->delta(),
343 event->buttons(),
344 event->modifiers(),
345 Qt::Horizontal);
346 QApplication::sendEvent(horizontalScrollBar(), &horizEvent);
347 }
348 }
349
350 void DolphinIconsView::showEvent(QShowEvent* event)
351 {
352 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
353 delegate->setMaximumSize(m_itemSize);
354
355 KCategorizedView::showEvent(event);
356 }
357
358 void DolphinIconsView::leaveEvent(QEvent* event)
359 {
360 KCategorizedView::leaveEvent(event);
361 // if the mouse is above an item and moved very fast outside the widget,
362 // no viewportEntered() signal might be emitted although the mouse has been moved
363 // above the viewport
364 m_controller->emitViewportEntered();
365 }
366
367 void DolphinIconsView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
368 {
369 KCategorizedView::currentChanged(current, previous);
370 m_autoScroller->handleCurrentIndexChange(current, previous);
371 }
372
373 void DolphinIconsView::resizeEvent(QResizeEvent* event)
374 {
375 KCategorizedView::resizeEvent(event);
376 const DolphinView* view = m_controller->dolphinView();
377 updateGridSize(view->showPreview(), view->additionalInfo().count());
378 }
379
380 void DolphinIconsView::slotShowPreviewChanged()
381 {
382 const DolphinView* view = m_controller->dolphinView();
383 updateGridSize(view->showPreview(), additionalInfoCount());
384 }
385
386 void DolphinIconsView::slotAdditionalInfoChanged()
387 {
388 const DolphinView* view = m_controller->dolphinView();
389 const bool showPreview = view->showPreview();
390 updateGridSize(showPreview, view->additionalInfo().count());
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 if (m_selectionManager != 0) {
510 m_selectionManager->reset();
511 }
512 }
513
514 int DolphinIconsView::additionalInfoCount() const
515 {
516 const DolphinView* view = m_controller->dolphinView();
517 return view->additionalInfo().count();
518 }
519
520 #include "dolphiniconsview.moc"