]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
After renaming an item the view should be scrolled in a way to still have a fully...
[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_enableScrollTo(false),
44 m_controller(controller),
45 m_selectionManager(0),
46 m_autoScroller(0),
47 m_categoryDrawer(0),
48 m_font(),
49 m_decorationSize(),
50 m_decorationPosition(QStyleOptionViewItem::Top),
51 m_displayAlignment(Qt::AlignHCenter),
52 m_itemSize(),
53 m_dropRect()
54 {
55 Q_ASSERT(controller != 0);
56 setLayoutDirection(Qt::LeftToRight);
57 setViewMode(QListView::IconMode);
58 setResizeMode(QListView::Adjust);
59 setMovement(QListView::Static);
60 setDragEnabled(true);
61 setEditTriggers(QAbstractItemView::NoEditTriggers);
62 viewport()->setAcceptDrops(true);
63
64 setMouseTracking(true);
65 m_autoScroller = new DolphinViewAutoScroller(this);
66
67 connect(this, SIGNAL(clicked(const QModelIndex&)),
68 controller, SLOT(requestTab(const QModelIndex&)));
69 if (KGlobalSettings::singleClick()) {
70 connect(this, SIGNAL(clicked(const QModelIndex&)),
71 controller, SLOT(triggerItem(const QModelIndex&)));
72 } else {
73 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
74 controller, SLOT(triggerItem(const QModelIndex&)));
75 }
76
77 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
78 m_selectionManager = new SelectionManager(this);
79 connect(m_selectionManager, SIGNAL(selectionChanged()),
80 this, SLOT(requestActivation()));
81 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
82 m_selectionManager, SLOT(reset()));
83 }
84
85 connect(this, SIGNAL(entered(const QModelIndex&)),
86 controller, SLOT(emitItemEntered(const QModelIndex&)));
87 connect(this, SIGNAL(viewportEntered()),
88 controller, SLOT(emitViewportEntered()));
89 connect(controller, SIGNAL(zoomLevelChanged(int)),
90 this, SLOT(setZoomLevel(int)));
91 connect(controller, SIGNAL(scrollToCurrentItem()),
92 this, SLOT(scrollToCurrentItem()));
93
94 const DolphinView* view = controller->dolphinView();
95 connect(view, SIGNAL(showPreviewChanged()),
96 this, SLOT(slotShowPreviewChanged()));
97 connect(view, SIGNAL(additionalInfoChanged()),
98 this, SLOT(slotAdditionalInfoChanged()));
99
100 // apply the icons mode settings to the widget
101 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
102 Q_ASSERT(settings != 0);
103
104 if (settings->useSystemFont()) {
105 m_font = KGlobalSettings::generalFont();
106 } else {
107 m_font = QFont(settings->fontFamily(),
108 settings->fontSize(),
109 settings->fontWeight(),
110 settings->italicFont());
111 }
112
113 setWordWrap(settings->numberOfTextlines() > 1);
114 updateGridSize(view->showPreview(), 0);
115
116 if (settings->arrangement() == QListView::TopToBottom) {
117 setFlow(QListView::LeftToRight);
118 m_decorationPosition = QStyleOptionViewItem::Top;
119 m_displayAlignment = Qt::AlignHCenter;
120 } else {
121 setFlow(QListView::TopToBottom);
122 m_decorationPosition = QStyleOptionViewItem::Left;
123 m_displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
124 }
125
126 m_categoryDrawer = new DolphinCategoryDrawer();
127 setCategoryDrawer(m_categoryDrawer);
128
129 setFocus();
130
131 connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)),
132 this, SLOT(slotGlobalSettingsChanged(int)));
133 }
134
135 DolphinIconsView::~DolphinIconsView()
136 {
137 delete m_categoryDrawer;
138 m_categoryDrawer = 0;
139 }
140
141 void DolphinIconsView::scrollTo(const QModelIndex& index, ScrollHint hint)
142 {
143 // Enable the QListView implementation of scrollTo() only if it has been
144 // triggered by a key press. Otherwise QAbstractItemView wants to scroll to the current
145 // index each time the layout has been changed. This becomes an issue when
146 // previews are loaded and the scrollbar is used: the scrollbar will always
147 // be reset to 0 on each new preview.
148 if (m_enableScrollTo || (state() != QAbstractItemView::NoState)) {
149 KCategorizedView::scrollTo(index, hint);
150 m_enableScrollTo = false;
151 }
152 }
153
154 void DolphinIconsView::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
155 {
156 KCategorizedView::dataChanged(topLeft, bottomRight);
157
158 KCategorizedSortFilterProxyModel* proxyModel = dynamic_cast<KCategorizedSortFilterProxyModel*>(model());
159 if (!proxyModel->isCategorizedModel()) {
160 // bypass a QListView issue that items are not layout correctly if the decoration size of
161 // an index changes
162 scheduleDelayedItemsLayout();
163 }
164 }
165
166 QStyleOptionViewItem DolphinIconsView::viewOptions() const
167 {
168 QStyleOptionViewItem viewOptions = KCategorizedView::viewOptions();
169 viewOptions.font = m_font;
170 viewOptions.decorationPosition = m_decorationPosition;
171 viewOptions.decorationSize = m_decorationSize;
172 viewOptions.displayAlignment = m_displayAlignment;
173 viewOptions.showDecorationSelected = true;
174 return viewOptions;
175 }
176
177 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
178 {
179 KCategorizedView::contextMenuEvent(event);
180 m_controller->triggerContextMenuRequest(event->pos());
181 }
182
183 void DolphinIconsView::mousePressEvent(QMouseEvent* event)
184 {
185 m_controller->requestActivation();
186 const QModelIndex index = indexAt(event->pos());
187 if (index.isValid() && (event->button() == Qt::LeftButton)) {
188 // TODO: It should not be necessary to manually set the dragging state, but I could
189 // not reproduce this issue with a Qt-only example yet to find the root cause.
190 // Issue description: start Dolphin, split the view and drag an item from the
191 // inactive view to the active view by a very fast mouse movement. Result:
192 // the item gets selected instead of being dragged...
193 setState(QAbstractItemView::DraggingState);
194 }
195
196 if (!index.isValid()) {
197 if (QApplication::mouseButtons() & Qt::MidButton) {
198 m_controller->replaceUrlByClipboard();
199 }
200 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
201 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
202 clearSelection();
203 }
204 }
205
206 KCategorizedView::mousePressEvent(event);
207 }
208
209 void DolphinIconsView::startDrag(Qt::DropActions supportedActions)
210 {
211 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
212 // fix this in KDE 4.1
213 KCategorizedView::startDrag(supportedActions);
214 DragAndDropHelper::instance().startDrag(this, supportedActions, m_controller);
215 }
216
217 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
218 {
219 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
220 event->acceptProposedAction();
221 }
222 }
223
224 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent* event)
225 {
226 KCategorizedView::dragLeaveEvent(event);
227 setDirtyRegion(m_dropRect);
228 }
229
230 void DolphinIconsView::dragMoveEvent(QDragMoveEvent* event)
231 {
232 KCategorizedView::dragMoveEvent(event);
233
234 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
235 const QModelIndex index = indexAt(event->pos());
236 setDirtyRegion(m_dropRect);
237
238 m_dropRect.setSize(QSize()); // set as invalid
239 if (index.isValid()) {
240 const KFileItem item = m_controller->itemForIndex(index);
241 if (!item.isNull() && item.isDir()) {
242 m_dropRect = visualRect(index);
243 } else {
244 m_dropRect.setSize(QSize()); // set as invalid
245 }
246 }
247 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
248 // accept url drops, independently from the destination item
249 event->acceptProposedAction();
250 }
251
252 setDirtyRegion(m_dropRect);
253 }
254
255 void DolphinIconsView::dropEvent(QDropEvent* event)
256 {
257 const QModelIndex index = indexAt(event->pos());
258 const KFileItem item = m_controller->itemForIndex(index);
259 m_controller->indicateDroppedUrls(item, m_controller->url(), event);
260
261 KCategorizedView::dropEvent(event);
262 }
263
264 void DolphinIconsView::keyPressEvent(QKeyEvent* event)
265 {
266 m_enableScrollTo = true; // see DolphinIconsView::scrollTo()
267 KCategorizedView::keyPressEvent(event);
268 m_controller->handleKeyPressEvent(event);
269 }
270
271 void DolphinIconsView::wheelEvent(QWheelEvent* event)
272 {
273 if (m_selectionManager != 0) {
274 m_selectionManager->reset();
275 }
276
277 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
278 if (event->modifiers() & Qt::ControlModifier) {
279 event->ignore();
280 return;
281 }
282
283 horizontalScrollBar()->setSingleStep(m_itemSize.width() / 10);
284 verticalScrollBar()->setSingleStep(m_itemSize.height() / 10);
285
286 KCategorizedView::wheelEvent(event);
287 // if the icons are aligned left to right, the vertical wheel event should
288 // be applied to the horizontal scrollbar
289 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
290 const bool scrollHorizontal = (event->orientation() == Qt::Vertical) &&
291 (settings->arrangement() == QListView::LeftToRight);
292 if (scrollHorizontal) {
293 QWheelEvent horizEvent(event->pos(),
294 event->delta(),
295 event->buttons(),
296 event->modifiers(),
297 Qt::Horizontal);
298 QApplication::sendEvent(horizontalScrollBar(), &horizEvent);
299 }
300 }
301
302 void DolphinIconsView::showEvent(QShowEvent* event)
303 {
304 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
305 delegate->setMaximumSize(m_itemSize);
306
307 KCategorizedView::showEvent(event);
308 }
309
310 void DolphinIconsView::leaveEvent(QEvent* event)
311 {
312 KCategorizedView::leaveEvent(event);
313 // if the mouse is above an item and moved very fast outside the widget,
314 // no viewportEntered() signal might be emitted although the mouse has been moved
315 // above the viewport
316 m_controller->emitViewportEntered();
317 }
318
319 void DolphinIconsView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
320 {
321 KCategorizedView::currentChanged(current, previous);
322 if (current.isValid() && !m_autoScroller->isActive()) {
323 scrollTo(current);
324 }
325 }
326
327 void DolphinIconsView::resizeEvent(QResizeEvent* event)
328 {
329 KCategorizedView::resizeEvent(event);
330 const DolphinView* view = m_controller->dolphinView();
331 updateGridSize(view->showPreview(), view->additionalInfo().count());
332 }
333
334 void DolphinIconsView::slotShowPreviewChanged()
335 {
336 const DolphinView* view = m_controller->dolphinView();
337 updateGridSize(view->showPreview(), additionalInfoCount());
338 }
339
340 void DolphinIconsView::slotAdditionalInfoChanged()
341 {
342 const DolphinView* view = m_controller->dolphinView();
343 const bool showPreview = view->showPreview();
344 updateGridSize(showPreview, view->additionalInfo().count());
345 }
346
347 void DolphinIconsView::setZoomLevel(int level)
348 {
349 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
350
351 const int oldIconSize = settings->iconSize();
352 int newIconSize = oldIconSize;
353
354 const bool showPreview = m_controller->dolphinView()->showPreview();
355 if (showPreview) {
356 const int previewSize = ZoomLevelInfo::iconSizeForZoomLevel(level);
357 settings->setPreviewSize(previewSize);
358 } else {
359 newIconSize = ZoomLevelInfo::iconSizeForZoomLevel(level);
360 settings->setIconSize(newIconSize);
361 }
362
363 // increase also the grid size
364 const int diff = newIconSize - oldIconSize;
365 settings->setItemWidth(settings->itemWidth() + diff);
366 settings->setItemHeight(settings->itemHeight() + diff);
367
368 updateGridSize(showPreview, additionalInfoCount());
369 }
370
371 void DolphinIconsView::requestActivation()
372 {
373 m_controller->requestActivation();
374 }
375
376 void DolphinIconsView::slotGlobalSettingsChanged(int category)
377 {
378 Q_UNUSED(category);
379
380 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
381 Q_ASSERT(settings != 0);
382 if (settings->useSystemFont()) {
383 m_font = KGlobalSettings::generalFont();
384 }
385
386 disconnect(this, SIGNAL(clicked(QModelIndex)), m_controller, SLOT(triggerItem(QModelIndex)));
387 disconnect(this, SIGNAL(doubleClicked(QModelIndex)), m_controller, SLOT(triggerItem(QModelIndex)));
388 if (KGlobalSettings::singleClick()) {
389 connect(this, SIGNAL(clicked(QModelIndex)), m_controller, SLOT(triggerItem(QModelIndex)));
390 } else {
391 connect(this, SIGNAL(doubleClicked(QModelIndex)), m_controller, SLOT(triggerItem(QModelIndex)));
392 }
393 }
394
395 void DolphinIconsView::scrollToCurrentItem()
396 {
397 m_enableScrollTo = true;
398 scrollTo(currentIndex());
399 m_enableScrollTo = false;
400 }
401
402 void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
403 {
404 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
405 Q_ASSERT(settings != 0);
406
407 int itemWidth = settings->itemWidth();
408 int itemHeight = settings->itemHeight();
409 int size = settings->iconSize();
410
411 if (showPreview) {
412 const int previewSize = settings->previewSize();
413 const int diff = previewSize - size;
414 itemWidth += diff;
415 itemHeight += diff;
416
417 size = previewSize;
418 }
419
420 Q_ASSERT(additionalInfoCount >= 0);
421 itemHeight += additionalInfoCount * m_font.pointSize() * 2;
422
423 // Optimize the item size of the grid in a way to prevent large gaps on the
424 // right border (= row arrangement) or the bottom border (= column arrangement).
425 // There is no public API in QListView to find out the used width of the viewport
426 // for the layout. The following calculation of 'contentWidth'/'contentHeight'
427 // is based on QListViewPrivate::prepareItemsLayout() (Copyright (C) 2009 Nokia Corporation).
428 int frameAroundContents = 0;
429 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents)) {
430 frameAroundContents = style()->pixelMetric(QStyle::PM_DefaultFrameWidth) * 2;
431 }
432 const int spacing = settings->gridSpacing();
433 if (settings->arrangement() == QListView::TopToBottom) {
434 const int contentWidth = viewport()->width() - 1
435 - frameAroundContents
436 - style()->pixelMetric(QStyle::PM_ScrollBarExtent, 0, horizontalScrollBar());
437 const int gridWidth = itemWidth + spacing * 2;
438 const int horizItemCount = contentWidth / gridWidth;
439 if (horizItemCount > 0) {
440 itemWidth += (contentWidth - horizItemCount * gridWidth) / horizItemCount;
441 }
442
443 // The decoration width indirectly defines the maximum
444 // width for the text wrapping. To use the maximum item width
445 // for text wrapping, it is used as decoration width.
446 m_decorationSize = QSize(itemWidth, size);
447 setIconSize(QSize(itemWidth, size));
448 } else {
449 const int contentHeight = viewport()->height() - 1
450 - frameAroundContents
451 - style()->pixelMetric(QStyle::PM_ScrollBarExtent, 0, verticalScrollBar());
452 const int gridHeight = itemHeight + spacing;
453 const int vertItemCount = contentHeight / gridHeight;
454 if (vertItemCount > 0) {
455 itemHeight += (contentHeight - vertItemCount * gridHeight) / vertItemCount;
456 }
457
458 m_decorationSize = QSize(size, size);
459 setIconSize(QSize(size, size));
460 }
461
462 m_itemSize = QSize(itemWidth, itemHeight);
463 setGridSize(QSize(itemWidth + spacing * 2, itemHeight + spacing));
464
465 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
466 if (delegate != 0) {
467 delegate->setMaximumSize(m_itemSize);
468 }
469
470 if (m_selectionManager != 0) {
471 m_selectionManager->reset();
472 }
473 }
474
475 int DolphinIconsView::additionalInfoCount() const
476 {
477 const DolphinView* view = m_controller->dolphinView();
478 return view->additionalInfo().count();
479 }
480
481 #include "dolphiniconsview.moc"