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