]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
No longer use Obsolete Member for QWidget:
[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_dragging(false),
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 viewport()->setAcceptDrops(true);
59
60 setMouseTracking(true);
61 viewport()->setAttribute(Qt::WA_Hover);
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 this, SLOT(triggerItem(const QModelIndex&)));
70 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
71 SelectionManager* selManager = new SelectionManager(this);
72 connect(selManager, SIGNAL(selectionChanged()),
73 this, SLOT(requestActivation()));
74 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
75 selManager, SLOT(reset()));
76 }
77 } else {
78 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
79 this, SLOT(triggerItem(const QModelIndex&)));
80 }
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 connect(this, SIGNAL(entered(const QModelIndex&)),
95 this, SLOT(slotEntered(const QModelIndex&)));
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 QRect DolphinIconsView::visualRect(const QModelIndex& index) const
139 {
140 const bool leftToRightFlow = (flow() == QListView::LeftToRight);
141
142 QRect itemRect = KCategorizedView::visualRect(index);
143
144 const int maxWidth = m_itemSize.width();
145 const int maxHeight = m_itemSize.height();
146
147 if (itemRect.width() > maxWidth) {
148 // assure that the maximum item width is not exceeded
149 if (leftToRightFlow) {
150 const int left = itemRect.left() + (itemRect.width() - maxWidth) / 2;
151 itemRect.setLeft(left);
152 }
153 itemRect.setWidth(maxWidth);
154 }
155
156 if (itemRect.height() > maxHeight) {
157 // assure that the maximum item height is not exceeded
158 if (!leftToRightFlow) {
159 const int top = itemRect.top() + (itemRect.height() - maxHeight) / 2;
160 itemRect.setTop(top);
161 }
162 itemRect.setHeight(maxHeight);
163 }
164
165 KCategorizedSortFilterProxyModel* proxyModel = dynamic_cast<KCategorizedSortFilterProxyModel*>(model());
166 if (leftToRightFlow && !proxyModel->isCategorizedModel()) {
167 // TODO: QListView::visualRect() calculates a wrong position of the items under
168 // certain circumstances (e. g. if the text is too long). This issue is bypassed
169 // by the following code (I'll try create a patch for Qt but as Dolphin must also work with
170 // Qt 4.3.0 this workaround must get applied at least for KDE 4.0).
171 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
172 const int margin = settings->gridSpacing();
173 const int gridWidth = gridSize().width();
174 const int gridIndex = (itemRect.left() - margin + 1) / gridWidth;
175 const int centerInc = (maxWidth - itemRect.width()) / 2;
176 itemRect.moveLeft((gridIndex * gridWidth) + margin + centerInc);
177 }
178
179 return itemRect;
180 }
181
182 QStyleOptionViewItem DolphinIconsView::viewOptions() const
183 {
184 QStyleOptionViewItem viewOptions = KCategorizedView::viewOptions();
185 viewOptions.font = m_font;
186 viewOptions.decorationPosition = m_decorationPosition;
187 viewOptions.decorationSize = m_decorationSize;
188 viewOptions.displayAlignment = m_displayAlignment;
189 viewOptions.showDecorationSelected = true;
190 return viewOptions;
191 }
192
193 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
194 {
195 KCategorizedView::contextMenuEvent(event);
196 m_controller->triggerContextMenuRequest(event->pos());
197 }
198
199 void DolphinIconsView::mousePressEvent(QMouseEvent* event)
200 {
201 m_controller->requestActivation();
202 if (!indexAt(event->pos()).isValid()) {
203 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
204 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
205 clearSelection();
206 }
207 }
208
209 KCategorizedView::mousePressEvent(event);
210 }
211
212 void DolphinIconsView::startDrag(Qt::DropActions supportedActions)
213 {
214 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
215 // fix this in KDE 4.1
216 KCategorizedView::startDrag(supportedActions);
217 DragAndDropHelper::startDrag(this, supportedActions);
218 }
219
220 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
221 {
222 if (event->mimeData()->hasUrls()) {
223 event->acceptProposedAction();
224 }
225 m_dragging = true;
226 }
227
228 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent* event)
229 {
230 KCategorizedView::dragLeaveEvent(event);
231
232 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
233 m_dragging = false;
234 setDirtyRegion(m_dropRect);
235 }
236
237 void DolphinIconsView::dragMoveEvent(QDragMoveEvent* event)
238 {
239 KCategorizedView::dragMoveEvent(event);
240
241 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
242 const QModelIndex index = indexAt(event->pos());
243 setDirtyRegion(m_dropRect);
244
245 m_dropRect.setSize(QSize()); // set as invalid
246 if (index.isValid()) {
247 const KFileItem item = itemForIndex(index);
248 if (!item.isNull() && item.isDir()) {
249 m_dropRect = visualRect(index);
250 } else {
251 m_dropRect.setSize(QSize()); // set as invalid
252 }
253 }
254 if (event->mimeData()->hasUrls()) {
255 // accept url drops, independently from the destination item
256 event->acceptProposedAction();
257 }
258
259 setDirtyRegion(m_dropRect);
260 }
261
262 void DolphinIconsView::dropEvent(QDropEvent* event)
263 {
264 if (!selectionModel()->isSelected(indexAt(event->pos()))) {
265 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
266 if (!urls.isEmpty()) {
267 const QModelIndex index = indexAt(event->pos());
268 const KFileItem item = itemForIndex(index);
269 m_controller->indicateDroppedUrls(urls,
270 m_controller->url(),
271 item);
272 event->acceptProposedAction();
273 }
274 }
275
276 KCategorizedView::dropEvent(event);
277
278 m_dragging = false;
279 }
280
281 void DolphinIconsView::paintEvent(QPaintEvent* event)
282 {
283 KCategorizedView::paintEvent(event);
284
285 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
286 if (m_dragging) {
287 const QBrush& brush = viewOptions().palette.brush(QPalette::Normal, QPalette::Highlight);
288 DragAndDropHelper::drawHoverIndication(this, m_dropRect, brush);
289 }
290 }
291
292 void DolphinIconsView::keyPressEvent(QKeyEvent* event)
293 {
294 KCategorizedView::keyPressEvent(event);
295
296 const QItemSelectionModel* selModel = selectionModel();
297 const QModelIndex currentIndex = selModel->currentIndex();
298 const bool trigger = currentIndex.isValid()
299 && (event->key() == Qt::Key_Return)
300 && (selModel->selectedIndexes().count() <= 1);
301 if (trigger) {
302 triggerItem(currentIndex);
303 }
304 }
305
306 void DolphinIconsView::wheelEvent(QWheelEvent* event)
307 {
308 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
309 if ((event->modifiers() & Qt::ControlModifier) == Qt::ControlModifier) {
310 event->ignore();
311 return;
312 }
313 KCategorizedView::wheelEvent(event);
314 // if the icons are aligned left to right, the vertical wheel event should
315 // be applied to the horizontal scrollbar
316 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
317 const bool scrollHorizontal = (event->orientation() == Qt::Vertical) &&
318 (settings->arrangement() == QListView::LeftToRight);
319 if (scrollHorizontal) {
320 QWheelEvent horizEvent(event->pos(),
321 event->delta(),
322 event->buttons(),
323 event->modifiers(),
324 Qt::Horizontal);
325 QApplication::sendEvent(horizontalScrollBar(), &horizEvent);
326 }
327 }
328
329 void DolphinIconsView::triggerItem(const QModelIndex& index)
330 {
331 m_controller->triggerItem(itemForIndex(index));
332 }
333
334 void DolphinIconsView::slotEntered(const QModelIndex& index)
335 {
336 m_controller->emitItemEntered(itemForIndex(index));
337 }
338
339 void DolphinIconsView::slotShowPreviewChanged()
340 {
341 const DolphinView* view = m_controller->dolphinView();
342 updateGridSize(view->showPreview(), additionalInfoCount());
343 }
344
345 void DolphinIconsView::slotAdditionalInfoChanged()
346 {
347 const DolphinView* view = m_controller->dolphinView();
348 const bool showPreview = view->showPreview();
349 updateGridSize(showPreview, view->additionalInfo().count());
350 }
351
352 void DolphinIconsView::zoomIn()
353 {
354 if (isZoomInPossible()) {
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 = increasedIconSize(settings->previewSize());
363 settings->setPreviewSize(previewSize);
364 } else {
365 newIconSize = increasedIconSize(oldIconSize);
366 settings->setIconSize(newIconSize);
367 if (settings->previewSize() < newIconSize) {
368 // assure that the preview size is always >= the icon size
369 settings->setPreviewSize(newIconSize);
370 }
371 }
372
373 // increase also the grid size
374 const int diff = newIconSize - oldIconSize;
375 settings->setItemWidth(settings->itemWidth() + diff);
376 settings->setItemHeight(settings->itemHeight() + diff);
377
378 updateGridSize(showPreview, additionalInfoCount());
379 }
380 }
381
382 void DolphinIconsView::zoomOut()
383 {
384 if (isZoomOutPossible()) {
385 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
386
387 const int oldIconSize = settings->iconSize();
388 int newIconSize = oldIconSize;
389
390 const bool showPreview = m_controller->dolphinView()->showPreview();
391 if (showPreview) {
392 const int previewSize = decreasedIconSize(settings->previewSize());
393 settings->setPreviewSize(previewSize);
394 if (settings->iconSize() > previewSize) {
395 // assure that the icon size is always <= the preview size
396 newIconSize = previewSize;
397 settings->setIconSize(newIconSize);
398 }
399 } else {
400 newIconSize = decreasedIconSize(settings->iconSize());
401 settings->setIconSize(newIconSize);
402 }
403
404 // decrease also the grid size
405 const int diff = oldIconSize - newIconSize;
406 settings->setItemWidth(settings->itemWidth() - diff);
407 settings->setItemHeight(settings->itemHeight() - diff);
408
409 updateGridSize(showPreview, additionalInfoCount());
410 }
411 }
412
413 void DolphinIconsView::requestActivation()
414 {
415 m_controller->requestActivation();
416 }
417
418 void DolphinIconsView::updateFont()
419 {
420 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
421 Q_ASSERT(settings != 0);
422
423 if (settings->useSystemFont()) {
424 m_font = KGlobalSettings::generalFont();
425 }
426 }
427
428 bool DolphinIconsView::isZoomInPossible() const
429 {
430 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
431 const bool showPreview = m_controller->dolphinView()->showPreview();
432 const int size = showPreview ? settings->previewSize() : settings->iconSize();
433 return size < KIconLoader::SizeEnormous;
434 }
435
436 bool DolphinIconsView::isZoomOutPossible() const
437 {
438 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
439 const bool showPreview = m_controller->dolphinView()->showPreview();
440 const int size = showPreview ? settings->previewSize() : settings->iconSize();
441 return size > KIconLoader::SizeSmall;
442 }
443
444 int DolphinIconsView::increasedIconSize(int size) const
445 {
446 int incSize = 0;
447 switch (size) {
448 case KIconLoader::SizeSmall: incSize = KIconLoader::SizeSmallMedium; break;
449 case KIconLoader::SizeSmallMedium: incSize = KIconLoader::SizeMedium; break;
450 case KIconLoader::SizeMedium: incSize = KIconLoader::SizeLarge; break;
451 case KIconLoader::SizeLarge: incSize = KIconLoader::SizeHuge; break;
452 case KIconLoader::SizeHuge: incSize = KIconLoader::SizeEnormous; break;
453 default: Q_ASSERT(false); break;
454 }
455 return incSize;
456 }
457
458 int DolphinIconsView::decreasedIconSize(int size) const
459 {
460 int decSize = 0;
461 switch (size) {
462 case KIconLoader::SizeSmallMedium: decSize = KIconLoader::SizeSmall; break;
463 case KIconLoader::SizeMedium: decSize = KIconLoader::SizeSmallMedium; break;
464 case KIconLoader::SizeLarge: decSize = KIconLoader::SizeMedium; break;
465 case KIconLoader::SizeHuge: decSize = KIconLoader::SizeLarge; break;
466 case KIconLoader::SizeEnormous: decSize = KIconLoader::SizeHuge; break;
467 default: Q_ASSERT(false); break;
468 }
469 return decSize;
470 }
471
472 void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
473 {
474 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
475 Q_ASSERT(settings != 0);
476
477 int itemWidth = settings->itemWidth();
478 int itemHeight = settings->itemHeight();
479 int size = settings->iconSize();
480
481 if (showPreview) {
482 const int previewSize = settings->previewSize();
483 const int diff = previewSize - size;
484 Q_ASSERT(diff >= 0);
485 itemWidth += diff;
486 itemHeight += diff;
487
488 size = previewSize;
489 }
490 setIconSize(QSize(size, size));
491
492 Q_ASSERT(additionalInfoCount >= 0);
493 itemHeight += additionalInfoCount * m_font.pointSize() * 2;
494
495 if (settings->arrangement() == QListView::TopToBottom) {
496 // The decoration width indirectly defines the maximum
497 // width for the text wrapping. To use the maximum item width
498 // for text wrapping, it is used as decoration width.
499 m_decorationSize = QSize(itemWidth, size);
500 } else {
501 m_decorationSize = QSize(size, size);
502 }
503
504 m_itemSize = QSize(itemWidth, itemHeight);
505
506 const int spacing = settings->gridSpacing();
507 setGridSize(QSize(itemWidth + spacing * 2, itemHeight + spacing));
508
509 m_controller->setZoomInPossible(isZoomInPossible());
510 m_controller->setZoomOutPossible(isZoomOutPossible());
511 }
512
513 KFileItem DolphinIconsView::itemForIndex(const QModelIndex& index) const
514 {
515 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(model());
516 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
517 const QModelIndex dirIndex = proxyModel->mapToSource(index);
518 return dirModel->itemForIndex(dirIndex);
519 }
520
521 int DolphinIconsView::additionalInfoCount() const
522 {
523 const DolphinView* view = m_controller->dolphinView();
524 return view->additionalInfo().count();
525 }
526
527 #include "dolphiniconsview.moc"