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