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