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