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