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