]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
SVN_SILENT: warning--, fixed indentations
[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 "settings/dolphinsettings.h"
25 #include "dolphinviewautoscroller.h"
26 #include "dolphin_iconsmodesettings.h"
27 #include "dolphin_generalsettings.h"
28 #include "draganddrophelper.h"
29 #include "selectionmanager.h"
30 #include "zoomlevelinfo.h"
31
32 #include <kcategorizedsortfilterproxymodel.h>
33 #include <kdialog.h>
34 #include <kdirmodel.h>
35 #include <kfileitemdelegate.h>
36
37 #include <QAbstractProxyModel>
38 #include <QApplication>
39 #include <QScrollBar>
40
41 DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controller) :
42 KCategorizedView(parent),
43 m_controller(controller),
44 m_selectionManager(0),
45 m_autoScroller(0),
46 m_categoryDrawer(0),
47 m_font(),
48 m_decorationSize(),
49 m_decorationPosition(QStyleOptionViewItem::Top),
50 m_displayAlignment(Qt::AlignHCenter),
51 m_itemSize(),
52 m_dropRect()
53 {
54 Q_ASSERT(controller != 0);
55 setLayoutDirection(Qt::LeftToRight);
56 setViewMode(QListView::IconMode);
57 setResizeMode(QListView::Adjust);
58 setMovement(QListView::Static);
59 setDragEnabled(true);
60 setEditTriggers(QAbstractItemView::NoEditTriggers);
61 viewport()->setAcceptDrops(true);
62
63 setMouseTracking(true);
64 m_autoScroller = new DolphinViewAutoScroller(this);
65
66 connect(this, SIGNAL(clicked(const QModelIndex&)),
67 controller, SLOT(requestTab(const QModelIndex&)));
68 if (KGlobalSettings::singleClick()) {
69 connect(this, SIGNAL(clicked(const QModelIndex&)),
70 controller, SLOT(triggerItem(const QModelIndex&)));
71 } else {
72 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
73 controller, SLOT(triggerItem(const QModelIndex&)));
74 }
75
76 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
77 m_selectionManager = new SelectionManager(this);
78 connect(m_selectionManager, SIGNAL(selectionChanged()),
79 this, SLOT(requestActivation()));
80 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
81 m_selectionManager, SLOT(reset()));
82 }
83
84 connect(this, SIGNAL(entered(const QModelIndex&)),
85 controller, SLOT(emitItemEntered(const QModelIndex&)));
86 connect(this, SIGNAL(viewportEntered()),
87 controller, SLOT(emitViewportEntered()));
88 connect(controller, SIGNAL(zoomLevelChanged(int)),
89 this, SLOT(setZoomLevel(int)));
90
91 const DolphinView* view = controller->dolphinView();
92 connect(view, SIGNAL(showPreviewChanged()),
93 this, SLOT(slotShowPreviewChanged()));
94 connect(view, SIGNAL(additionalInfoChanged()),
95 this, SLOT(slotAdditionalInfoChanged()));
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(settingsChanged(int)),
129 this, SLOT(slotGlobalSettingsChanged(int)));
130 }
131
132 DolphinIconsView::~DolphinIconsView()
133 {
134 delete m_categoryDrawer;
135 m_categoryDrawer = 0;
136 }
137
138 void DolphinIconsView::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
139 {
140 KCategorizedView::dataChanged(topLeft, bottomRight);
141
142 KCategorizedSortFilterProxyModel* proxyModel = dynamic_cast<KCategorizedSortFilterProxyModel*>(model());
143 if (!proxyModel->isCategorizedModel()) {
144 // bypass a QListView issue that items are not layout correctly if the decoration size of
145 // an index changes
146 scheduleDelayedItemsLayout();
147 }
148 }
149
150 QStyleOptionViewItem DolphinIconsView::viewOptions() const
151 {
152 QStyleOptionViewItem viewOptions = KCategorizedView::viewOptions();
153 viewOptions.font = m_font;
154 viewOptions.decorationPosition = m_decorationPosition;
155 viewOptions.decorationSize = m_decorationSize;
156 viewOptions.displayAlignment = m_displayAlignment;
157 viewOptions.showDecorationSelected = true;
158 return viewOptions;
159 }
160
161 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
162 {
163 KCategorizedView::contextMenuEvent(event);
164 m_controller->triggerContextMenuRequest(event->pos());
165 }
166
167 void DolphinIconsView::mousePressEvent(QMouseEvent* event)
168 {
169 m_controller->requestActivation();
170 const QModelIndex index = indexAt(event->pos());
171 if (index.isValid() && (event->button() == Qt::LeftButton)) {
172 // TODO: It should not be necessary to manually set the dragging state, but I could
173 // not reproduce this issue with a Qt-only example yet to find the root cause.
174 // Issue description: start Dolphin, split the view and drag an item from the
175 // inactive view to the active view by a very fast mouse movement. Result:
176 // the item gets selected instead of being dragged...
177 setState(QAbstractItemView::DraggingState);
178 }
179
180 if (!index.isValid()) {
181 if (QApplication::mouseButtons() & Qt::MidButton) {
182 m_controller->replaceUrlByClipboard();
183 }
184 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
185 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
186 clearSelection();
187 }
188 }
189
190 KCategorizedView::mousePressEvent(event);
191 }
192
193 void DolphinIconsView::startDrag(Qt::DropActions supportedActions)
194 {
195 DragAndDropHelper::instance().startDrag(this, supportedActions, m_controller);
196 }
197
198 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
199 {
200 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
201 event->acceptProposedAction();
202 }
203 }
204
205 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent* event)
206 {
207 Q_UNUSED(event);
208 setDirtyRegion(m_dropRect);
209 }
210
211 void DolphinIconsView::dragMoveEvent(QDragMoveEvent* event)
212 {
213 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
214 const QModelIndex index = indexAt(event->pos());
215 setDirtyRegion(m_dropRect);
216
217 m_dropRect.setSize(QSize()); // set as invalid
218 if (index.isValid()) {
219 const KFileItem item = m_controller->itemForIndex(index);
220 if (!item.isNull() && item.isDir()) {
221 m_dropRect = visualRect(index);
222 } else {
223 m_dropRect.setSize(QSize()); // set as invalid
224 }
225 }
226 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
227 // accept url drops, independently from the destination item
228 event->acceptProposedAction();
229 }
230
231 setDirtyRegion(m_dropRect);
232 }
233
234 void DolphinIconsView::dropEvent(QDropEvent* event)
235 {
236 const QModelIndex index = indexAt(event->pos());
237 const KFileItem item = m_controller->itemForIndex(index);
238 m_controller->indicateDroppedUrls(item, m_controller->url(), event);
239 }
240
241 QModelIndex DolphinIconsView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
242 {
243 const QModelIndex oldCurrent = currentIndex();
244
245 QModelIndex newCurrent = KCategorizedView::moveCursor(cursorAction, modifiers);
246 if (newCurrent != oldCurrent) {
247 return newCurrent;
248 }
249
250 // The cursor has not been moved by the base implementation. Provide a
251 // wrap behavior, so that the cursor will go to the next item when reaching
252 // the border.
253 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
254 if (settings->arrangement() == QListView::LeftToRight) {
255 switch (cursorAction) {
256 case MoveUp:
257 if (newCurrent.row() == 0) {
258 return newCurrent;
259 }
260 newCurrent = KCategorizedView::moveCursor(MoveLeft, modifiers);
261 selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
262 newCurrent = KCategorizedView::moveCursor(MovePageDown, modifiers);
263 break;
264
265 case MoveDown:
266 if (newCurrent.row() == (model()->rowCount() - 1)) {
267 return newCurrent;
268 }
269 newCurrent = KCategorizedView::moveCursor(MovePageUp, modifiers);
270 selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
271 newCurrent = KCategorizedView::moveCursor(MoveRight, modifiers);
272 break;
273
274 default:
275 break;
276 }
277 } else {
278 QModelIndex current = oldCurrent;
279 switch (cursorAction) {
280 case MoveLeft:
281 if (newCurrent.row() == 0) {
282 return newCurrent;
283 }
284 newCurrent = KCategorizedView::moveCursor(MoveUp, modifiers);
285 do {
286 selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
287 current = newCurrent;
288 newCurrent = KCategorizedView::moveCursor(MoveRight, modifiers);
289 } while (newCurrent != current);
290 break;
291
292 case MoveRight:
293 if (newCurrent.row() == (model()->rowCount() - 1)) {
294 return newCurrent;
295 }
296 do {
297 selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
298 current = newCurrent;
299 newCurrent = KCategorizedView::moveCursor(MoveLeft, modifiers);
300 } while (newCurrent != current);
301 newCurrent = KCategorizedView::moveCursor(MoveDown, modifiers);
302 break;
303
304 default:
305 break;
306 }
307 }
308
309 // Revert all changes of the current item to make sure that item selection works correctly
310 selectionModel()->setCurrentIndex(oldCurrent, QItemSelectionModel::NoUpdate);
311 return newCurrent;
312 }
313
314 void DolphinIconsView::keyPressEvent(QKeyEvent* event)
315 {
316 KCategorizedView::keyPressEvent(event);
317 m_controller->handleKeyPressEvent(event);
318 }
319
320 void DolphinIconsView::wheelEvent(QWheelEvent* event)
321 {
322 if (m_selectionManager != 0) {
323 m_selectionManager->reset();
324 }
325
326 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
327 if (event->modifiers() & Qt::ControlModifier) {
328 event->ignore();
329 return;
330 }
331
332 horizontalScrollBar()->setSingleStep(m_itemSize.width() / 10);
333 verticalScrollBar()->setSingleStep(m_itemSize.height() / 10);
334
335 KCategorizedView::wheelEvent(event);
336 // if the icons are aligned left to right, the vertical wheel event should
337 // be applied to the horizontal scrollbar
338 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
339 const bool scrollHorizontal = (event->orientation() == Qt::Vertical) &&
340 (settings->arrangement() == QListView::LeftToRight);
341 if (scrollHorizontal) {
342 QWheelEvent horizEvent(event->pos(),
343 event->delta(),
344 event->buttons(),
345 event->modifiers(),
346 Qt::Horizontal);
347 QApplication::sendEvent(horizontalScrollBar(), &horizEvent);
348 }
349 }
350
351 void DolphinIconsView::showEvent(QShowEvent* event)
352 {
353 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
354 delegate->setMaximumSize(m_itemSize);
355
356 KCategorizedView::showEvent(event);
357 }
358
359 void DolphinIconsView::leaveEvent(QEvent* event)
360 {
361 KCategorizedView::leaveEvent(event);
362 // if the mouse is above an item and moved very fast outside the widget,
363 // no viewportEntered() signal might be emitted although the mouse has been moved
364 // above the viewport
365 m_controller->emitViewportEntered();
366 }
367
368 void DolphinIconsView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
369 {
370 KCategorizedView::currentChanged(current, previous);
371 m_autoScroller->handleCurrentIndexChange(current, previous);
372 }
373
374 void DolphinIconsView::resizeEvent(QResizeEvent* event)
375 {
376 KCategorizedView::resizeEvent(event);
377 const DolphinView* view = m_controller->dolphinView();
378 updateGridSize(view->showPreview(), view->additionalInfo().count());
379 }
380
381 void DolphinIconsView::slotShowPreviewChanged()
382 {
383 const DolphinView* view = m_controller->dolphinView();
384 updateGridSize(view->showPreview(), additionalInfoCount());
385 }
386
387 void DolphinIconsView::slotAdditionalInfoChanged()
388 {
389 const DolphinView* view = m_controller->dolphinView();
390 const bool showPreview = view->showPreview();
391 updateGridSize(showPreview, view->additionalInfo().count());
392 }
393
394 void DolphinIconsView::setZoomLevel(int level)
395 {
396 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
397
398 const int oldIconSize = settings->iconSize();
399 int newIconSize = oldIconSize;
400
401 const bool showPreview = m_controller->dolphinView()->showPreview();
402 if (showPreview) {
403 const int previewSize = ZoomLevelInfo::iconSizeForZoomLevel(level);
404 settings->setPreviewSize(previewSize);
405 } else {
406 newIconSize = ZoomLevelInfo::iconSizeForZoomLevel(level);
407 settings->setIconSize(newIconSize);
408 }
409
410 // increase also the grid size
411 const int diff = newIconSize - oldIconSize;
412 settings->setItemWidth(settings->itemWidth() + diff);
413 settings->setItemHeight(settings->itemHeight() + diff);
414
415 updateGridSize(showPreview, additionalInfoCount());
416 }
417
418 void DolphinIconsView::requestActivation()
419 {
420 m_controller->requestActivation();
421 }
422
423 void DolphinIconsView::slotGlobalSettingsChanged(int category)
424 {
425 Q_UNUSED(category);
426
427 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
428 Q_ASSERT(settings != 0);
429 if (settings->useSystemFont()) {
430 m_font = KGlobalSettings::generalFont();
431 }
432
433 disconnect(this, SIGNAL(clicked(QModelIndex)), m_controller, SLOT(triggerItem(QModelIndex)));
434 disconnect(this, SIGNAL(doubleClicked(QModelIndex)), m_controller, SLOT(triggerItem(QModelIndex)));
435 if (KGlobalSettings::singleClick()) {
436 connect(this, SIGNAL(clicked(QModelIndex)), m_controller, SLOT(triggerItem(QModelIndex)));
437 } else {
438 connect(this, SIGNAL(doubleClicked(QModelIndex)), m_controller, SLOT(triggerItem(QModelIndex)));
439 }
440 }
441
442 void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
443 {
444 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
445 Q_ASSERT(settings != 0);
446
447 int itemWidth = settings->itemWidth();
448 int itemHeight = settings->itemHeight();
449 int size = settings->iconSize();
450
451 if (showPreview) {
452 const int previewSize = settings->previewSize();
453 const int diff = previewSize - size;
454 itemWidth += diff;
455 itemHeight += diff;
456
457 size = previewSize;
458 }
459
460 Q_ASSERT(additionalInfoCount >= 0);
461 itemHeight += additionalInfoCount * m_font.pointSize() * 2;
462
463 // Optimize the item size of the grid in a way to prevent large gaps on the
464 // right border (= row arrangement) or the bottom border (= column arrangement).
465 // There is no public API in QListView to find out the used width of the viewport
466 // for the layout. The following calculation of 'contentWidth'/'contentHeight'
467 // is based on QListViewPrivate::prepareItemsLayout() (Copyright (C) 2009 Nokia Corporation).
468 int frameAroundContents = 0;
469 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents)) {
470 frameAroundContents = style()->pixelMetric(QStyle::PM_DefaultFrameWidth) * 2;
471 }
472 const int spacing = settings->gridSpacing();
473 if (settings->arrangement() == QListView::TopToBottom) {
474 const int contentWidth = viewport()->width() - 1
475 - frameAroundContents
476 - style()->pixelMetric(QStyle::PM_ScrollBarExtent, 0, horizontalScrollBar());
477 const int gridWidth = itemWidth + spacing * 2;
478 const int horizItemCount = contentWidth / gridWidth;
479 if (horizItemCount > 0) {
480 itemWidth += (contentWidth - horizItemCount * gridWidth) / horizItemCount;
481 }
482
483 // The decoration width indirectly defines the maximum
484 // width for the text wrapping. To use the maximum item width
485 // for text wrapping, it is used as decoration width.
486 m_decorationSize = QSize(itemWidth, size);
487 setIconSize(QSize(itemWidth, size));
488 } else {
489 const int contentHeight = viewport()->height() - 1
490 - frameAroundContents
491 - style()->pixelMetric(QStyle::PM_ScrollBarExtent, 0, verticalScrollBar());
492 const int gridHeight = itemHeight + spacing;
493 const int vertItemCount = contentHeight / gridHeight;
494 if (vertItemCount > 0) {
495 itemHeight += (contentHeight - vertItemCount * gridHeight) / vertItemCount;
496 }
497
498 m_decorationSize = QSize(size, size);
499 setIconSize(QSize(size, size));
500 }
501
502 m_itemSize = QSize(itemWidth, itemHeight);
503 setGridSizeOwn(QSize(itemWidth + spacing * 2, itemHeight + spacing));
504
505 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
506 if (delegate != 0) {
507 delegate->setMaximumSize(m_itemSize);
508 }
509
510 if (m_selectionManager != 0) {
511 m_selectionManager->reset();
512 }
513 }
514
515 int DolphinIconsView::additionalInfoCount() const
516 {
517 const DolphinView* view = m_controller->dolphinView();
518 return view->additionalInfo().count();
519 }
520
521 #include "dolphiniconsview.moc"