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