]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
Fixed issue that the vertical scrollbar jumps back to the initial state during previe...
[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 #include <kfileitemdelegate.h>
34
35 #include <QAbstractProxyModel>
36 #include <QApplication>
37 #include <QPainter>
38 #include <QPoint>
39 #include <QScrollBar>
40
41 DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controller) :
42 KCategorizedView(parent),
43 m_controller(controller),
44 m_selectionManager(0),
45 m_categoryDrawer(0),
46 m_font(),
47 m_decorationSize(),
48 m_decorationPosition(QStyleOptionViewItem::Top),
49 m_displayAlignment(Qt::AlignHCenter),
50 m_itemSize(),
51 m_dropRect()
52 {
53 Q_ASSERT(controller != 0);
54 setViewMode(QListView::IconMode);
55 setResizeMode(QListView::Adjust);
56 setSpacing(KDialog::spacingHint());
57 setMovement(QListView::Static);
58 setDragEnabled(true);
59 setEditTriggers(QAbstractItemView::NoEditTriggers);
60 viewport()->setAcceptDrops(true);
61
62 setMouseTracking(true);
63
64 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
65 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
66 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
67 // RETURN-key in keyPressEvent().
68 if (KGlobalSettings::singleClick()) {
69 connect(this, SIGNAL(clicked(const QModelIndex&)),
70 controller, SLOT(triggerItem(const QModelIndex&)));
71 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
72 m_selectionManager = new SelectionManager(this);
73 connect(m_selectionManager, SIGNAL(selectionChanged()),
74 this, SLOT(requestActivation()));
75 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
76 m_selectionManager, SLOT(reset()));
77 }
78 } else {
79 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
80 controller, SLOT(triggerItem(const QModelIndex&)));
81 }
82 connect(this, SIGNAL(entered(const QModelIndex&)),
83 controller, SLOT(emitItemEntered(const QModelIndex&)));
84 connect(this, SIGNAL(viewportEntered()),
85 controller, SLOT(emitViewportEntered()));
86 connect(controller, SIGNAL(zoomIn()),
87 this, SLOT(zoomIn()));
88 connect(controller, SIGNAL(zoomOut()),
89 this, SLOT(zoomOut()));
90
91 const DolphinView* view = controller->dolphinView();
92 connect(view, SIGNAL(showPreviewChanged()),
93 this, SLOT(slotShowPreviewChanged()));
94 connect(view, SIGNAL(additionalInfoChanged()),
95 this, SLOT(slotAdditionalInfoChanged()));
96
97 // apply the icons mode settings to the widget
98 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
99 Q_ASSERT(settings != 0);
100
101 if (settings->useSystemFont()) {
102 m_font = KGlobalSettings::generalFont();
103 } else {
104 m_font = QFont(settings->fontFamily(),
105 settings->fontSize(),
106 settings->fontWeight(),
107 settings->italicFont());
108 }
109
110 setWordWrap(settings->numberOfTextlines() > 1);
111 updateGridSize(view->showPreview(), 0);
112
113 if (settings->arrangement() == QListView::TopToBottom) {
114 setFlow(QListView::LeftToRight);
115 m_decorationPosition = QStyleOptionViewItem::Top;
116 m_displayAlignment = Qt::AlignHCenter;
117 } else {
118 setFlow(QListView::TopToBottom);
119 m_decorationPosition = QStyleOptionViewItem::Left;
120 m_displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
121 }
122
123 m_categoryDrawer = new DolphinCategoryDrawer();
124 setCategoryDrawer(m_categoryDrawer);
125
126 setFocus();
127
128 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
129 this, SLOT(updateFont()));
130 }
131
132 DolphinIconsView::~DolphinIconsView()
133 {
134 delete m_categoryDrawer;
135 m_categoryDrawer = 0;
136 }
137
138 void DolphinIconsView::scrollTo(const QModelIndex& index, ScrollHint hint)
139 {
140 Q_UNUSED(index);
141 Q_UNUSED(hint);
142 // Disable the QListView implementation of scrollTo(), as QAbstractItemView
143 // wants to scroll to the current index each time the layout has been changed.
144 // This becomes an issue when previews are loaded and the scrollbar is used: the
145 // scrollbar will always be reset to 0 on each new preview.
146 }
147
148 void DolphinIconsView::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
149 {
150 KCategorizedView::dataChanged(topLeft, bottomRight);
151
152 KCategorizedSortFilterProxyModel* proxyModel = dynamic_cast<KCategorizedSortFilterProxyModel*>(model());
153 if ((flow() == QListView::LeftToRight) && !proxyModel->isCategorizedModel()) {
154 // bypass a QListView issue that items are not layout correctly if the decoration size of
155 // an index changes
156 scheduleDelayedItemsLayout();
157 }
158 }
159
160 QStyleOptionViewItem DolphinIconsView::viewOptions() const
161 {
162 QStyleOptionViewItem viewOptions = KCategorizedView::viewOptions();
163 viewOptions.font = m_font;
164 viewOptions.decorationPosition = m_decorationPosition;
165 viewOptions.decorationSize = m_decorationSize;
166 viewOptions.displayAlignment = m_displayAlignment;
167 viewOptions.showDecorationSelected = true;
168 return viewOptions;
169 }
170
171 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
172 {
173 KCategorizedView::contextMenuEvent(event);
174 m_controller->triggerContextMenuRequest(event->pos());
175 }
176
177 void DolphinIconsView::mousePressEvent(QMouseEvent* event)
178 {
179 m_controller->requestActivation();
180 const QModelIndex index = indexAt(event->pos());
181 if (index.isValid() && (event->button() == Qt::LeftButton)) {
182 // TODO: It should not be necessary to manually set the dragging state, but I could
183 // not reproduce this issue with a Qt-only example yet to find the root cause.
184 // Issue description: start Dolphin, split the view and drag an item from the
185 // inactive view to the active view by a very fast mouse movement. Result:
186 // the item gets selected instead of being dragged...
187 setState(QAbstractItemView::DraggingState);
188 }
189
190 if (!index.isValid()) {
191 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
192 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
193 clearSelection();
194 }
195 }
196
197 KCategorizedView::mousePressEvent(event);
198 }
199
200 void DolphinIconsView::startDrag(Qt::DropActions supportedActions)
201 {
202 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
203 // fix this in KDE 4.1
204 KCategorizedView::startDrag(supportedActions);
205 DragAndDropHelper::startDrag(this, supportedActions);
206 }
207
208 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
209 {
210 if (event->mimeData()->hasUrls()) {
211 event->acceptProposedAction();
212 }
213 }
214
215 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent* event)
216 {
217 KCategorizedView::dragLeaveEvent(event);
218 setDirtyRegion(m_dropRect);
219 }
220
221 void DolphinIconsView::dragMoveEvent(QDragMoveEvent* event)
222 {
223 KCategorizedView::dragMoveEvent(event);
224
225 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
226 const QModelIndex index = indexAt(event->pos());
227 setDirtyRegion(m_dropRect);
228
229 m_dropRect.setSize(QSize()); // set as invalid
230 if (index.isValid()) {
231 const KFileItem item = m_controller->itemForIndex(index);
232 if (!item.isNull() && item.isDir()) {
233 m_dropRect = visualRect(index);
234 } else {
235 m_dropRect.setSize(QSize()); // set as invalid
236 }
237 }
238 if (event->mimeData()->hasUrls()) {
239 // accept url drops, independently from the destination item
240 event->acceptProposedAction();
241 }
242
243 setDirtyRegion(m_dropRect);
244 }
245
246 void DolphinIconsView::dropEvent(QDropEvent* event)
247 {
248 if (!selectionModel()->isSelected(indexAt(event->pos()))) {
249 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
250 if (!urls.isEmpty()) {
251 const QModelIndex index = indexAt(event->pos());
252 const KFileItem item = m_controller->itemForIndex(index);
253 m_controller->indicateDroppedUrls(urls,
254 m_controller->url(),
255 item);
256 event->acceptProposedAction();
257 }
258 }
259
260 KCategorizedView::dropEvent(event);
261 }
262
263 void DolphinIconsView::keyPressEvent(QKeyEvent* event)
264 {
265 KCategorizedView::keyPressEvent(event);
266 m_controller->handleKeyPressEvent(event);
267 }
268
269 void DolphinIconsView::wheelEvent(QWheelEvent* event)
270 {
271 if (m_selectionManager != 0) {
272 m_selectionManager->reset();
273 }
274
275 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
276 if (event->modifiers() & Qt::ControlModifier) {
277 event->ignore();
278 return;
279 }
280 KCategorizedView::wheelEvent(event);
281 // if the icons are aligned left to right, the vertical wheel event should
282 // be applied to the horizontal scrollbar
283 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
284 const bool scrollHorizontal = (event->orientation() == Qt::Vertical) &&
285 (settings->arrangement() == QListView::LeftToRight);
286 if (scrollHorizontal) {
287 QWheelEvent horizEvent(event->pos(),
288 event->delta(),
289 event->buttons(),
290 event->modifiers(),
291 Qt::Horizontal);
292 QApplication::sendEvent(horizontalScrollBar(), &horizEvent);
293 }
294 }
295
296 void DolphinIconsView::showEvent(QShowEvent* event)
297 {
298 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
299 delegate->setMaximumSize(m_itemSize);
300
301 KCategorizedView::showEvent(event);
302 }
303
304 void DolphinIconsView::leaveEvent(QEvent* event)
305 {
306 KCategorizedView::leaveEvent(event);
307 // if the mouse is above an item and moved very fast outside the widget,
308 // no viewportEntered() signal might be emitted although the mouse has been moved
309 // above the viewport
310 m_controller->emitViewportEntered();
311 }
312
313 void DolphinIconsView::slotShowPreviewChanged()
314 {
315 const DolphinView* view = m_controller->dolphinView();
316 updateGridSize(view->showPreview(), additionalInfoCount());
317 }
318
319 void DolphinIconsView::slotAdditionalInfoChanged()
320 {
321 const DolphinView* view = m_controller->dolphinView();
322 const bool showPreview = view->showPreview();
323 updateGridSize(showPreview, view->additionalInfo().count());
324 }
325
326 void DolphinIconsView::zoomIn()
327 {
328 if (isZoomInPossible()) {
329 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
330
331 const int oldIconSize = settings->iconSize();
332 int newIconSize = oldIconSize;
333
334 const bool showPreview = m_controller->dolphinView()->showPreview();
335 if (showPreview) {
336 const int previewSize = increasedIconSize(settings->previewSize());
337 settings->setPreviewSize(previewSize);
338 } else {
339 newIconSize = increasedIconSize(oldIconSize);
340 settings->setIconSize(newIconSize);
341 }
342
343 // increase also the grid size
344 const int diff = newIconSize - oldIconSize;
345 settings->setItemWidth(settings->itemWidth() + diff);
346 settings->setItemHeight(settings->itemHeight() + diff);
347
348 updateGridSize(showPreview, additionalInfoCount());
349 }
350 }
351
352 void DolphinIconsView::zoomOut()
353 {
354 if (isZoomOutPossible()) {
355 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
356
357 const int oldIconSize = settings->iconSize();
358 int newIconSize = oldIconSize;
359
360 const bool showPreview = m_controller->dolphinView()->showPreview();
361 if (showPreview) {
362 const int previewSize = decreasedIconSize(settings->previewSize());
363 settings->setPreviewSize(previewSize);
364 } else {
365 newIconSize = decreasedIconSize(settings->iconSize());
366 settings->setIconSize(newIconSize);
367 }
368
369 // decrease also the grid size
370 const int diff = oldIconSize - newIconSize;
371 settings->setItemWidth(settings->itemWidth() - diff);
372 settings->setItemHeight(settings->itemHeight() - diff);
373
374 updateGridSize(showPreview, additionalInfoCount());
375 }
376 }
377
378 void DolphinIconsView::requestActivation()
379 {
380 m_controller->requestActivation();
381 }
382
383 void DolphinIconsView::updateFont()
384 {
385 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
386 Q_ASSERT(settings != 0);
387
388 if (settings->useSystemFont()) {
389 m_font = KGlobalSettings::generalFont();
390 }
391 }
392
393 bool DolphinIconsView::isZoomInPossible() const
394 {
395 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
396 const bool showPreview = m_controller->dolphinView()->showPreview();
397 const int size = showPreview ? settings->previewSize() : settings->iconSize();
398 return size < KIconLoader::SizeEnormous;
399 }
400
401 bool DolphinIconsView::isZoomOutPossible() const
402 {
403 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
404 const bool showPreview = m_controller->dolphinView()->showPreview();
405 const int size = showPreview ? settings->previewSize() : settings->iconSize();
406 return size > KIconLoader::SizeSmall;
407 }
408
409 int DolphinIconsView::increasedIconSize(int size) const
410 {
411 int incSize = 0;
412 switch (size) {
413 case KIconLoader::SizeSmall: incSize = KIconLoader::SizeSmallMedium; break;
414 case KIconLoader::SizeSmallMedium: incSize = KIconLoader::SizeMedium; break;
415 case KIconLoader::SizeMedium: incSize = KIconLoader::SizeLarge; break;
416 case KIconLoader::SizeLarge: incSize = KIconLoader::SizeHuge; break;
417 case KIconLoader::SizeHuge: incSize = KIconLoader::SizeEnormous; break;
418 default: Q_ASSERT(false); break;
419 }
420 return incSize;
421 }
422
423 int DolphinIconsView::decreasedIconSize(int size) const
424 {
425 int decSize = 0;
426 switch (size) {
427 case KIconLoader::SizeSmallMedium: decSize = KIconLoader::SizeSmall; break;
428 case KIconLoader::SizeMedium: decSize = KIconLoader::SizeSmallMedium; break;
429 case KIconLoader::SizeLarge: decSize = KIconLoader::SizeMedium; break;
430 case KIconLoader::SizeHuge: decSize = KIconLoader::SizeLarge; break;
431 case KIconLoader::SizeEnormous: decSize = KIconLoader::SizeHuge; break;
432 default: Q_ASSERT(false); break;
433 }
434 return decSize;
435 }
436
437 void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
438 {
439 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
440 Q_ASSERT(settings != 0);
441
442 int itemWidth = settings->itemWidth();
443 int itemHeight = settings->itemHeight();
444 int size = settings->iconSize();
445
446 if (showPreview) {
447 const int previewSize = settings->previewSize();
448 const int diff = previewSize - size;
449 itemWidth += diff;
450 itemHeight += diff;
451
452 size = previewSize;
453 }
454
455 Q_ASSERT(additionalInfoCount >= 0);
456 itemHeight += additionalInfoCount * m_font.pointSize() * 2;
457
458 if (settings->arrangement() == QListView::TopToBottom) {
459 // The decoration width indirectly defines the maximum
460 // width for the text wrapping. To use the maximum item width
461 // for text wrapping, it is used as decoration width.
462 m_decorationSize = QSize(itemWidth, size);
463 setIconSize(QSize(itemWidth, size));
464 } else {
465 m_decorationSize = QSize(size, size);
466 setIconSize(QSize(size, size));
467 }
468
469 m_itemSize = QSize(itemWidth, itemHeight);
470
471 const int spacing = settings->gridSpacing();
472 setGridSize(QSize(itemWidth + spacing * 2, itemHeight + spacing));
473
474 m_controller->setZoomInPossible(isZoomInPossible());
475 m_controller->setZoomOutPossible(isZoomOutPossible());
476
477 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
478 if (delegate != 0) {
479 delegate->setMaximumSize(m_itemSize);
480 }
481
482 if (m_selectionManager != 0) {
483 m_selectionManager->reset();
484 }
485 }
486
487 int DolphinIconsView::additionalInfoCount() const
488 {
489 const DolphinView* view = m_controller->dolphinView();
490 return view->additionalInfo().count();
491 }
492
493 #include "dolphiniconsview.moc"