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