]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
First step of refactoring to improve the zooming capabilities of views:
[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 #include <kfileitemdelegate.h>
34
35 #include <QAbstractProxyModel>
36 #include <QApplication>
37 #include <QScrollBar>
38
39 DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controller) :
40 KCategorizedView(parent),
41 m_enableScrollTo(false),
42 m_controller(controller),
43 m_selectionManager(0),
44 m_categoryDrawer(0),
45 m_font(),
46 m_decorationSize(),
47 m_decorationPosition(QStyleOptionViewItem::Top),
48 m_displayAlignment(Qt::AlignHCenter),
49 m_itemSize(),
50 m_dropRect()
51 {
52 Q_ASSERT(controller != 0);
53 setViewMode(QListView::IconMode);
54 setResizeMode(QListView::Adjust);
55 setSpacing(KDialog::spacingHint());
56 setMovement(QListView::Static);
57 setDragEnabled(true);
58 setEditTriggers(QAbstractItemView::NoEditTriggers);
59 viewport()->setAcceptDrops(true);
60
61 setMouseTracking(true);
62
63 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
64 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
65 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
66 // RETURN-key in keyPressEvent().
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::startDrag(this, supportedActions);
211 }
212
213 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
214 {
215 if (event->mimeData()->hasUrls()) {
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 (event->mimeData()->hasUrls()) {
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 if (!selectionModel()->isSelected(indexAt(event->pos()))) {
254 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
255 if (!urls.isEmpty()) {
256 const QModelIndex index = indexAt(event->pos());
257 const KFileItem item = m_controller->itemForIndex(index);
258 m_controller->indicateDroppedUrls(urls,
259 m_controller->url(),
260 item);
261 event->acceptProposedAction();
262 }
263 }
264
265 KCategorizedView::dropEvent(event);
266 }
267
268 void DolphinIconsView::keyPressEvent(QKeyEvent* event)
269 {
270 KCategorizedView::keyPressEvent(event);
271 m_controller->handleKeyPressEvent(event);
272 m_enableScrollTo = true; // see DolphinIconsView::scrollTo()
273 }
274
275 void DolphinIconsView::wheelEvent(QWheelEvent* event)
276 {
277 if (m_selectionManager != 0) {
278 m_selectionManager->reset();
279 }
280
281 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
282 if (event->modifiers() & Qt::ControlModifier) {
283 event->ignore();
284 return;
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::slotShowPreviewChanged()
320 {
321 const DolphinView* view = m_controller->dolphinView();
322 updateGridSize(view->showPreview(), additionalInfoCount());
323 }
324
325 void DolphinIconsView::slotAdditionalInfoChanged()
326 {
327 const DolphinView* view = m_controller->dolphinView();
328 const bool showPreview = view->showPreview();
329 updateGridSize(showPreview, view->additionalInfo().count());
330 }
331
332 void DolphinIconsView::setZoomLevel(int level)
333 {
334 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
335
336 const int oldIconSize = settings->iconSize();
337 int newIconSize = oldIconSize;
338
339 const bool showPreview = m_controller->dolphinView()->showPreview();
340 if (showPreview) {
341 const int previewSize = DolphinController::iconSizeForZoomLevel(level);
342 settings->setPreviewSize(previewSize);
343 } else {
344 newIconSize = DolphinController::iconSizeForZoomLevel(level);
345 settings->setIconSize(newIconSize);
346 }
347
348 // increase also the grid size
349 const int diff = newIconSize - oldIconSize;
350 settings->setItemWidth(settings->itemWidth() + diff);
351 settings->setItemHeight(settings->itemHeight() + diff);
352
353 updateGridSize(showPreview, additionalInfoCount());
354 }
355
356 void DolphinIconsView::requestActivation()
357 {
358 m_controller->requestActivation();
359 }
360
361 void DolphinIconsView::updateFont()
362 {
363 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
364 Q_ASSERT(settings != 0);
365
366 if (settings->useSystemFont()) {
367 m_font = KGlobalSettings::generalFont();
368 }
369 }
370
371 void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
372 {
373 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
374 Q_ASSERT(settings != 0);
375
376 int itemWidth = settings->itemWidth();
377 int itemHeight = settings->itemHeight();
378 int size = settings->iconSize();
379
380 if (showPreview) {
381 const int previewSize = settings->previewSize();
382 const int diff = previewSize - size;
383 itemWidth += diff;
384 itemHeight += diff;
385
386 size = previewSize;
387 }
388
389 Q_ASSERT(additionalInfoCount >= 0);
390 itemHeight += additionalInfoCount * m_font.pointSize() * 2;
391
392 if (settings->arrangement() == QListView::TopToBottom) {
393 // The decoration width indirectly defines the maximum
394 // width for the text wrapping. To use the maximum item width
395 // for text wrapping, it is used as decoration width.
396 m_decorationSize = QSize(itemWidth, size);
397 setIconSize(QSize(itemWidth, size));
398 } else {
399 m_decorationSize = QSize(size, size);
400 setIconSize(QSize(size, size));
401 }
402
403 m_itemSize = QSize(itemWidth, itemHeight);
404
405 const int spacing = settings->gridSpacing();
406 setGridSize(QSize(itemWidth + spacing * 2, itemHeight + spacing));
407
408 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
409 if (delegate != 0) {
410 delegate->setMaximumSize(m_itemSize);
411 }
412
413 if (m_selectionManager != 0) {
414 m_selectionManager->reset();
415 }
416 }
417
418 int DolphinIconsView::additionalInfoCount() const
419 {
420 const DolphinView* view = m_controller->dolphinView();
421 return view->additionalInfo().count();
422 }
423
424 #include "dolphiniconsview.moc"