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