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