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