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