]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
Let the DolphinController be aware on which QAbstractItemView instance he is working...
[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 if ((event->modifiers() & Qt::ControlModifier) == Qt::ControlModifier) {
301 event->ignore();
302 return;
303 }
304 KCategorizedView::wheelEvent(event);
305 // if the icons are aligned left to right, the vertical wheel event should
306 // be applied to the horizontal scrollbar
307 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
308 const bool scrollHorizontal = (event->orientation() == Qt::Vertical) &&
309 (settings->arrangement() == QListView::LeftToRight);
310 if (scrollHorizontal) {
311 QWheelEvent horizEvent(event->pos(),
312 event->delta(),
313 event->buttons(),
314 event->modifiers(),
315 Qt::Horizontal);
316 QApplication::sendEvent(horizontalScrollBar(), &horizEvent);
317 }
318 }
319
320 void DolphinIconsView::slotShowPreviewChanged()
321 {
322 const DolphinView* view = m_controller->dolphinView();
323 updateGridSize(view->showPreview(), additionalInfoCount());
324 }
325
326 void DolphinIconsView::slotAdditionalInfoChanged()
327 {
328 const DolphinView* view = m_controller->dolphinView();
329 const bool showPreview = view->showPreview();
330 updateGridSize(showPreview, view->additionalInfo().count());
331 }
332
333 void DolphinIconsView::zoomIn()
334 {
335 if (isZoomInPossible()) {
336 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
337
338 const int oldIconSize = settings->iconSize();
339 int newIconSize = oldIconSize;
340
341 const bool showPreview = m_controller->dolphinView()->showPreview();
342 if (showPreview) {
343 const int previewSize = increasedIconSize(settings->previewSize());
344 settings->setPreviewSize(previewSize);
345 } else {
346 newIconSize = increasedIconSize(oldIconSize);
347 settings->setIconSize(newIconSize);
348 if (settings->previewSize() < newIconSize) {
349 // assure that the preview size is always >= the icon size
350 settings->setPreviewSize(newIconSize);
351 }
352 }
353
354 // increase also the grid size
355 const int diff = newIconSize - oldIconSize;
356 settings->setItemWidth(settings->itemWidth() + diff);
357 settings->setItemHeight(settings->itemHeight() + diff);
358
359 updateGridSize(showPreview, additionalInfoCount());
360 }
361 }
362
363 void DolphinIconsView::zoomOut()
364 {
365 if (isZoomOutPossible()) {
366 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
367
368 const int oldIconSize = settings->iconSize();
369 int newIconSize = oldIconSize;
370
371 const bool showPreview = m_controller->dolphinView()->showPreview();
372 if (showPreview) {
373 const int previewSize = decreasedIconSize(settings->previewSize());
374 settings->setPreviewSize(previewSize);
375 if (settings->iconSize() > previewSize) {
376 // assure that the icon size is always <= the preview size
377 newIconSize = previewSize;
378 settings->setIconSize(newIconSize);
379 }
380 } else {
381 newIconSize = decreasedIconSize(settings->iconSize());
382 settings->setIconSize(newIconSize);
383 }
384
385 // decrease also the grid size
386 const int diff = oldIconSize - newIconSize;
387 settings->setItemWidth(settings->itemWidth() - diff);
388 settings->setItemHeight(settings->itemHeight() - diff);
389
390 updateGridSize(showPreview, additionalInfoCount());
391 }
392 }
393
394 void DolphinIconsView::requestActivation()
395 {
396 m_controller->requestActivation();
397 }
398
399 void DolphinIconsView::updateFont()
400 {
401 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
402 Q_ASSERT(settings != 0);
403
404 if (settings->useSystemFont()) {
405 m_font = KGlobalSettings::generalFont();
406 }
407 }
408
409 bool DolphinIconsView::isZoomInPossible() const
410 {
411 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
412 const bool showPreview = m_controller->dolphinView()->showPreview();
413 const int size = showPreview ? settings->previewSize() : settings->iconSize();
414 return size < KIconLoader::SizeEnormous;
415 }
416
417 bool DolphinIconsView::isZoomOutPossible() const
418 {
419 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
420 const bool showPreview = m_controller->dolphinView()->showPreview();
421 const int size = showPreview ? settings->previewSize() : settings->iconSize();
422 return size > KIconLoader::SizeSmall;
423 }
424
425 int DolphinIconsView::increasedIconSize(int size) const
426 {
427 int incSize = 0;
428 switch (size) {
429 case KIconLoader::SizeSmall: incSize = KIconLoader::SizeSmallMedium; break;
430 case KIconLoader::SizeSmallMedium: incSize = KIconLoader::SizeMedium; break;
431 case KIconLoader::SizeMedium: incSize = KIconLoader::SizeLarge; break;
432 case KIconLoader::SizeLarge: incSize = KIconLoader::SizeHuge; break;
433 case KIconLoader::SizeHuge: incSize = KIconLoader::SizeEnormous; break;
434 default: Q_ASSERT(false); break;
435 }
436 return incSize;
437 }
438
439 int DolphinIconsView::decreasedIconSize(int size) const
440 {
441 int decSize = 0;
442 switch (size) {
443 case KIconLoader::SizeSmallMedium: decSize = KIconLoader::SizeSmall; break;
444 case KIconLoader::SizeMedium: decSize = KIconLoader::SizeSmallMedium; break;
445 case KIconLoader::SizeLarge: decSize = KIconLoader::SizeMedium; break;
446 case KIconLoader::SizeHuge: decSize = KIconLoader::SizeLarge; break;
447 case KIconLoader::SizeEnormous: decSize = KIconLoader::SizeHuge; break;
448 default: Q_ASSERT(false); break;
449 }
450 return decSize;
451 }
452
453 void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
454 {
455 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
456 Q_ASSERT(settings != 0);
457
458 int itemWidth = settings->itemWidth();
459 int itemHeight = settings->itemHeight();
460 int size = settings->iconSize();
461
462 if (showPreview) {
463 const int previewSize = settings->previewSize();
464 const int diff = previewSize - size;
465 Q_ASSERT(diff >= 0);
466 itemWidth += diff;
467 itemHeight += diff;
468
469 size = previewSize;
470 }
471 setIconSize(QSize(size, size));
472
473 Q_ASSERT(additionalInfoCount >= 0);
474 itemHeight += additionalInfoCount * m_font.pointSize() * 2;
475
476 if (settings->arrangement() == QListView::TopToBottom) {
477 // The decoration width indirectly defines the maximum
478 // width for the text wrapping. To use the maximum item width
479 // for text wrapping, it is used as decoration width.
480 m_decorationSize = QSize(itemWidth, size);
481 } else {
482 m_decorationSize = QSize(size, size);
483 }
484
485 m_itemSize = QSize(itemWidth, itemHeight);
486
487 const int spacing = settings->gridSpacing();
488 setGridSize(QSize(itemWidth + spacing * 2, itemHeight + spacing));
489
490 m_controller->setZoomInPossible(isZoomInPossible());
491 m_controller->setZoomOutPossible(isZoomOutPossible());
492 }
493
494 int DolphinIconsView::additionalInfoCount() const
495 {
496 const DolphinView* view = m_controller->dolphinView();
497 return view->additionalInfo().count();
498 }
499
500 #include "dolphiniconsview.moc"