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