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