]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
Blend in a toggle button when hovering items. This allows selecting items without...
[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 "dolphinsettings.h"
25 #include "dolphin_iconsmodesettings.h"
26 #include "dolphin_generalsettings.h"
27 #include "draganddrophelper.h"
28 #include "selectionmanager.h"
29
30 #include <kcategorizedsortfilterproxymodel.h>
31 #include <kdialog.h>
32 #include <kdirmodel.h>
33
34 #include <QAbstractProxyModel>
35 #include <QApplication>
36 #include <QPainter>
37 #include <QPoint>
38 #include <QScrollBar>
39
40 DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controller) :
41 KCategorizedView(parent),
42 m_controller(controller),
43 m_categoryDrawer(0),
44 m_font(),
45 m_decorationSize(),
46 m_decorationPosition(QStyleOptionViewItem::Top),
47 m_displayAlignment(Qt::AlignHCenter),
48 m_itemSize(),
49 m_dragging(false),
50 m_dropRect()
51 {
52 Q_ASSERT(controller != 0);
53 setViewMode(QListView::IconMode);
54 setResizeMode(QListView::Adjust);
55 setSpacing(KDialog::spacingHint());
56 setMovement(QListView::Static);
57 setDragEnabled(true);
58 viewport()->setAcceptDrops(true);
59
60 setMouseTracking(true);
61 viewport()->setAttribute(Qt::WA_Hover);
62
63 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
64 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
65 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
66 // RETURN-key in keyPressEvent().
67 if (KGlobalSettings::singleClick()) {
68 connect(this, SIGNAL(clicked(const QModelIndex&)),
69 this, SLOT(triggerItem(const QModelIndex&)));
70 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
71 SelectionManager* selManager = new SelectionManager(this);
72 connect(selManager, SIGNAL(selectionChanged()),
73 this, SLOT(requestActivation()));
74 }
75 } else {
76 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
77 this, SLOT(triggerItem(const QModelIndex&)));
78 }
79 connect(this, SIGNAL(viewportEntered()),
80 controller, SLOT(emitViewportEntered()));
81 connect(controller, SIGNAL(zoomIn()),
82 this, SLOT(zoomIn()));
83 connect(controller, SIGNAL(zoomOut()),
84 this, SLOT(zoomOut()));
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 connect(this, SIGNAL(entered(const QModelIndex&)),
93 this, SLOT(slotEntered(const QModelIndex&)));
94
95 // apply the icons mode settings to the widget
96 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
97 Q_ASSERT(settings != 0);
98
99 m_font = QFont(settings->fontFamily(), settings->fontSize());
100 m_font.setItalic(settings->italicFont());
101 m_font.setBold(settings->boldFont());
102
103 setWordWrap(settings->numberOfTextlines() > 1);
104 updateGridSize(view->showPreview(), 0);
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 m_categoryDrawer = new DolphinCategoryDrawer();
117 setCategoryDrawer(m_categoryDrawer);
118
119 setFocus();
120 }
121
122 DolphinIconsView::~DolphinIconsView()
123 {
124 delete m_categoryDrawer;
125 m_categoryDrawer = 0;
126 }
127
128 QRect DolphinIconsView::visualRect(const QModelIndex& index) const
129 {
130 const bool leftToRightFlow = (flow() == QListView::LeftToRight);
131
132 QRect itemRect = KCategorizedView::visualRect(index);
133
134 const int maxWidth = m_itemSize.width();
135 const int maxHeight = m_itemSize.height();
136
137 if (itemRect.width() > maxWidth) {
138 // assure that the maximum item width is not exceeded
139 if (leftToRightFlow) {
140 const int left = itemRect.left() + (itemRect.width() - maxWidth) / 2;
141 itemRect.setLeft(left);
142 }
143 itemRect.setWidth(maxWidth);
144 }
145
146 if (itemRect.height() > maxHeight) {
147 // assure that the maximum item height is not exceeded
148 if (!leftToRightFlow) {
149 const int top = itemRect.top() + (itemRect.height() - maxHeight) / 2;
150 itemRect.setTop(top);
151 }
152 itemRect.setHeight(maxHeight);
153 }
154
155 KCategorizedSortFilterProxyModel* proxyModel = dynamic_cast<KCategorizedSortFilterProxyModel*>(model());
156 if (leftToRightFlow && !proxyModel->isCategorizedModel()) {
157 // TODO: QListView::visualRect() calculates a wrong position of the items under
158 // certain circumstances (e. g. if the text is too long). This issue is bypassed
159 // by the following code (I'll try create a patch for Qt but as Dolphin must also work with
160 // Qt 4.3.0 this workaround must get applied at least for KDE 4.0).
161 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
162 const int margin = settings->gridSpacing();
163 const int gridWidth = gridSize().width();
164 const int gridIndex = (itemRect.left() - margin + 1) / gridWidth;
165 const int centerInc = (maxWidth - itemRect.width()) / 2;
166 itemRect.moveLeft((gridIndex * gridWidth) + margin + centerInc);
167 }
168
169 return itemRect;
170 }
171
172 QStyleOptionViewItem DolphinIconsView::viewOptions() const
173 {
174 QStyleOptionViewItem viewOptions = KCategorizedView::viewOptions();
175 viewOptions.font = m_font;
176 viewOptions.decorationPosition = m_decorationPosition;
177 viewOptions.decorationSize = m_decorationSize;
178 viewOptions.displayAlignment = m_displayAlignment;
179 viewOptions.showDecorationSelected = true;
180 return viewOptions;
181 }
182
183 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
184 {
185 KCategorizedView::contextMenuEvent(event);
186 m_controller->triggerContextMenuRequest(event->pos());
187 }
188
189 void DolphinIconsView::mousePressEvent(QMouseEvent* event)
190 {
191 m_controller->requestActivation();
192 if (!indexAt(event->pos()).isValid()) {
193 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
194 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
195 clearSelection();
196 }
197 }
198
199 KCategorizedView::mousePressEvent(event);
200 }
201
202 void DolphinIconsView::startDrag(Qt::DropActions supportedActions)
203 {
204 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
205 // fix this in KDE 4.1
206 KCategorizedView::startDrag(supportedActions);
207 DragAndDropHelper::startDrag(this, supportedActions);
208 }
209
210 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
211 {
212 if (event->mimeData()->hasUrls()) {
213 event->acceptProposedAction();
214 }
215 m_dragging = true;
216 }
217
218 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent* event)
219 {
220 KCategorizedView::dragLeaveEvent(event);
221
222 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
223 m_dragging = false;
224 setDirtyRegion(m_dropRect);
225 }
226
227 void DolphinIconsView::dragMoveEvent(QDragMoveEvent* event)
228 {
229 KCategorizedView::dragMoveEvent(event);
230
231 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
232 const QModelIndex index = indexAt(event->pos());
233 setDirtyRegion(m_dropRect);
234
235 m_dropRect.setSize(QSize()); // set as invalid
236 if (index.isValid()) {
237 const KFileItem item = itemForIndex(index);
238 if (!item.isNull() && item.isDir()) {
239 m_dropRect = visualRect(index);
240 } else {
241 m_dropRect.setSize(QSize()); // set as invalid
242 }
243 }
244 if (event->mimeData()->hasUrls()) {
245 // accept url drops, independently from the destination item
246 event->acceptProposedAction();
247 }
248
249 setDirtyRegion(m_dropRect);
250 }
251
252 void DolphinIconsView::dropEvent(QDropEvent* event)
253 {
254 if (!selectionModel()->isSelected(indexAt(event->pos()))) {
255 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
256 if (!urls.isEmpty()) {
257 const QModelIndex index = indexAt(event->pos());
258 const KFileItem item = itemForIndex(index);
259 m_controller->indicateDroppedUrls(urls,
260 m_controller->url(),
261 item);
262 event->acceptProposedAction();
263 }
264 }
265
266 KCategorizedView::dropEvent(event);
267
268 m_dragging = false;
269 }
270
271 void DolphinIconsView::paintEvent(QPaintEvent* event)
272 {
273 KCategorizedView::paintEvent(event);
274
275 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
276 if (m_dragging) {
277 const QBrush& brush = viewOptions().palette.brush(QPalette::Normal, QPalette::Highlight);
278 DragAndDropHelper::drawHoverIndication(this, m_dropRect, brush);
279 }
280 }
281
282 void DolphinIconsView::keyPressEvent(QKeyEvent* event)
283 {
284 KCategorizedView::keyPressEvent(event);
285
286 const QItemSelectionModel* selModel = selectionModel();
287 const QModelIndex currentIndex = selModel->currentIndex();
288 const bool trigger = currentIndex.isValid()
289 && (event->key() == Qt::Key_Return)
290 && (selModel->selectedIndexes().count() <= 1);
291 if (trigger) {
292 triggerItem(currentIndex);
293 }
294 }
295
296 void DolphinIconsView::wheelEvent(QWheelEvent* event)
297 {
298 KCategorizedView::wheelEvent(event);
299
300 // if the icons are aligned left to right, the vertical wheel event should
301 // be applied to the horizontal scrollbar
302 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
303 const bool scrollHorizontal = (event->orientation() == Qt::Vertical) &&
304 (settings->arrangement() == QListView::LeftToRight);
305 if (scrollHorizontal) {
306 QWheelEvent horizEvent(event->pos(),
307 event->delta(),
308 event->buttons(),
309 event->modifiers(),
310 Qt::Horizontal);
311 QApplication::sendEvent(horizontalScrollBar(), &horizEvent);
312 }
313 }
314
315 void DolphinIconsView::triggerItem(const QModelIndex& index)
316 {
317 m_controller->triggerItem(itemForIndex(index));
318 }
319
320 void DolphinIconsView::slotEntered(const QModelIndex& index)
321 {
322 m_controller->emitItemEntered(itemForIndex(index));
323 }
324
325 void DolphinIconsView::slotShowPreviewChanged()
326 {
327 const DolphinView* view = m_controller->dolphinView();
328 updateGridSize(view->showPreview(), additionalInfoCount());
329 }
330
331 void DolphinIconsView::slotAdditionalInfoChanged()
332 {
333 const DolphinView* view = m_controller->dolphinView();
334 const bool showPreview = view->showPreview();
335 updateGridSize(showPreview, view->additionalInfo().count());
336 }
337
338 void DolphinIconsView::zoomIn()
339 {
340 if (isZoomInPossible()) {
341 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
342
343 const int oldIconSize = settings->iconSize();
344 int newIconSize = oldIconSize;
345
346 const bool showPreview = m_controller->dolphinView()->showPreview();
347 if (showPreview) {
348 const int previewSize = increasedIconSize(settings->previewSize());
349 settings->setPreviewSize(previewSize);
350 } else {
351 newIconSize = increasedIconSize(oldIconSize);
352 settings->setIconSize(newIconSize);
353 if (settings->previewSize() < newIconSize) {
354 // assure that the preview size is always >= the icon size
355 settings->setPreviewSize(newIconSize);
356 }
357 }
358
359 // increase also the grid size
360 const int diff = newIconSize - oldIconSize;
361 settings->setItemWidth(settings->itemWidth() + diff);
362 settings->setItemHeight(settings->itemHeight() + diff);
363
364 updateGridSize(showPreview, additionalInfoCount());
365 }
366 }
367
368 void DolphinIconsView::zoomOut()
369 {
370 if (isZoomOutPossible()) {
371 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
372
373 const int oldIconSize = settings->iconSize();
374 int newIconSize = oldIconSize;
375
376 const bool showPreview = m_controller->dolphinView()->showPreview();
377 if (showPreview) {
378 const int previewSize = decreasedIconSize(settings->previewSize());
379 settings->setPreviewSize(previewSize);
380 if (settings->iconSize() > previewSize) {
381 // assure that the icon size is always <= the preview size
382 newIconSize = previewSize;
383 settings->setIconSize(newIconSize);
384 }
385 } else {
386 newIconSize = decreasedIconSize(settings->iconSize());
387 settings->setIconSize(newIconSize);
388 }
389
390 // decrease also the grid size
391 const int diff = oldIconSize - newIconSize;
392 settings->setItemWidth(settings->itemWidth() - diff);
393 settings->setItemHeight(settings->itemHeight() - diff);
394
395 updateGridSize(showPreview, additionalInfoCount());
396 }
397 }
398
399 void DolphinIconsView::requestActivation()
400 {
401 m_controller->requestActivation();
402 }
403
404 bool DolphinIconsView::isZoomInPossible() const
405 {
406 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
407 const bool showPreview = m_controller->dolphinView()->showPreview();
408 const int size = showPreview ? settings->previewSize() : settings->iconSize();
409 return size < KIconLoader::SizeEnormous;
410 }
411
412 bool DolphinIconsView::isZoomOutPossible() const
413 {
414 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
415 const bool showPreview = m_controller->dolphinView()->showPreview();
416 const int size = showPreview ? settings->previewSize() : settings->iconSize();
417 return size > KIconLoader::SizeSmall;
418 }
419
420 int DolphinIconsView::increasedIconSize(int size) const
421 {
422 int incSize = 0;
423 switch (size) {
424 case KIconLoader::SizeSmall: incSize = KIconLoader::SizeSmallMedium; break;
425 case KIconLoader::SizeSmallMedium: incSize = KIconLoader::SizeMedium; break;
426 case KIconLoader::SizeMedium: incSize = KIconLoader::SizeLarge; break;
427 case KIconLoader::SizeLarge: incSize = KIconLoader::SizeHuge; break;
428 case KIconLoader::SizeHuge: incSize = KIconLoader::SizeEnormous; break;
429 default: Q_ASSERT(false); break;
430 }
431 return incSize;
432 }
433
434 int DolphinIconsView::decreasedIconSize(int size) const
435 {
436 int decSize = 0;
437 switch (size) {
438 case KIconLoader::SizeSmallMedium: decSize = KIconLoader::SizeSmall; break;
439 case KIconLoader::SizeMedium: decSize = KIconLoader::SizeSmallMedium; break;
440 case KIconLoader::SizeLarge: decSize = KIconLoader::SizeMedium; break;
441 case KIconLoader::SizeHuge: decSize = KIconLoader::SizeLarge; break;
442 case KIconLoader::SizeEnormous: decSize = KIconLoader::SizeHuge; break;
443 default: Q_ASSERT(false); break;
444 }
445 return decSize;
446 }
447
448 void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
449 {
450 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
451 Q_ASSERT(settings != 0);
452
453 int itemWidth = settings->itemWidth();
454 int itemHeight = settings->itemHeight();
455 int size = settings->iconSize();
456
457 if (showPreview) {
458 const int previewSize = settings->previewSize();
459 const int diff = previewSize - size;
460 Q_ASSERT(diff >= 0);
461 itemWidth += diff;
462 itemHeight += diff;
463
464 size = previewSize;
465 }
466 setIconSize(QSize(size, size));
467
468 Q_ASSERT(additionalInfoCount >= 0);
469 itemHeight += additionalInfoCount * m_font.pointSize() * 2;
470
471 if (settings->arrangement() == QListView::TopToBottom) {
472 // The decoration width indirectly defines the maximum
473 // width for the text wrapping. To use the maximum item width
474 // for text wrapping, it is used as decoration width.
475 m_decorationSize = QSize(itemWidth, size);
476 } else {
477 m_decorationSize = QSize(size, size);
478 }
479
480 m_itemSize = QSize(itemWidth, itemHeight);
481
482 const int spacing = settings->gridSpacing();
483 setGridSize(QSize(itemWidth + spacing * 2, itemHeight + spacing));
484
485 m_controller->setZoomInPossible(isZoomInPossible());
486 m_controller->setZoomOutPossible(isZoomOutPossible());
487 }
488
489 KFileItem DolphinIconsView::itemForIndex(const QModelIndex& index) const
490 {
491 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(model());
492 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
493 const QModelIndex dirIndex = proxyModel->mapToSource(index);
494 return dirModel->itemForIndex(dirIndex);
495 }
496
497 int DolphinIconsView::additionalInfoCount() const
498 {
499 const DolphinView* view = m_controller->dolphinView();
500 return view->additionalInfo().count();
501 }
502
503 #include "dolphiniconsview.moc"