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