1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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 ***************************************************************************/
20 #include "dolphiniconsview.h"
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"
33 #include <kcategorizedsortfilterproxymodel.h>
35 #include <kfileitemdelegate.h>
37 #include <QAbstractProxyModel>
38 #include <QApplication>
41 DolphinIconsView::DolphinIconsView(QWidget
* parent
,
42 DolphinController
* controller
,
43 DolphinSortFilterProxyModel
* proxyModel
) :
44 KCategorizedView(parent
),
45 m_controller(controller
),
47 m_extensionsFactory(0),
50 m_decorationPosition(QStyleOptionViewItem::Top
),
51 m_displayAlignment(Qt::AlignHCenter
),
55 Q_ASSERT(controller
!= 0);
57 setLayoutDirection(Qt::LeftToRight
);
58 setViewMode(QListView::IconMode
);
59 setResizeMode(QListView::Adjust
);
60 setMovement(QListView::Static
);
62 setEditTriggers(QAbstractItemView::NoEditTriggers
);
63 viewport()->setAcceptDrops(true);
65 setMouseTracking(true);
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
&)));
73 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
74 controller
, SLOT(triggerItem(const QModelIndex
&)));
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)));
84 const DolphinView
* view
= controller
->dolphinView();
85 connect(view
, SIGNAL(showPreviewChanged()),
86 this, SLOT(slotShowPreviewChanged()));
87 connect(view
, SIGNAL(additionalInfoChanged()),
88 this, SLOT(slotAdditionalInfoChanged()));
90 // apply the icons mode settings to the widget
91 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
92 Q_ASSERT(settings
!= 0);
94 if (settings
->useSystemFont()) {
95 m_font
= KGlobalSettings::generalFont();
97 m_font
= QFont(settings
->fontFamily(),
99 settings
->fontWeight(),
100 settings
->italicFont());
103 setWordWrap(settings
->numberOfTextlines() > 1);
105 if (settings
->arrangement() == QListView::TopToBottom
) {
106 setFlow(QListView::LeftToRight
);
107 m_decorationPosition
= QStyleOptionViewItem::Top
;
108 m_displayAlignment
= Qt::AlignHCenter
;
110 setFlow(QListView::TopToBottom
);
111 m_decorationPosition
= QStyleOptionViewItem::Left
;
112 m_displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
115 m_categoryDrawer
= new DolphinCategoryDrawer();
116 setCategoryDrawer(m_categoryDrawer
);
120 connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)),
121 this, SLOT(slotGlobalSettingsChanged(int)));
123 updateGridSize(view
->showPreview(), 0);
124 m_extensionsFactory
= new ViewExtensionsFactory(this, controller
);
127 DolphinIconsView::~DolphinIconsView()
129 delete m_categoryDrawer
;
130 m_categoryDrawer
= 0;
133 void DolphinIconsView::dataChanged(const QModelIndex
& topLeft
, const QModelIndex
& bottomRight
)
135 KCategorizedView::dataChanged(topLeft
, bottomRight
);
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
141 scheduleDelayedItemsLayout();
145 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
147 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
148 viewOptions
.font
= m_font
;
149 viewOptions
.fontMetrics
= QFontMetrics(m_font
);
150 viewOptions
.decorationPosition
= m_decorationPosition
;
151 viewOptions
.decorationSize
= m_decorationSize
;
152 viewOptions
.displayAlignment
= m_displayAlignment
;
153 viewOptions
.showDecorationSelected
= true;
157 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
159 KCategorizedView::contextMenuEvent(event
);
160 m_controller
->triggerContextMenuRequest(event
->pos());
163 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
165 m_controller
->requestActivation();
166 const QModelIndex index
= indexAt(event
->pos());
167 if (index
.isValid() && (event
->button() == Qt::LeftButton
)) {
168 // TODO: It should not be necessary to manually set the dragging state, but I could
169 // not reproduce this issue with a Qt-only example yet to find the root cause.
170 // Issue description: start Dolphin, split the view and drag an item from the
171 // inactive view to the active view by a very fast mouse movement. Result:
172 // the item gets selected instead of being dragged...
173 setState(QAbstractItemView::DraggingState
);
176 if (!index
.isValid() && (QApplication::mouseButtons() & Qt::MidButton
)) {
177 m_controller
->replaceUrlByClipboard();
180 KCategorizedView::mousePressEvent(event
);
183 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
185 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_controller
);
188 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
190 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
191 event
->acceptProposedAction();
195 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
197 KCategorizedView::dragLeaveEvent(event
);
198 setDirtyRegion(m_dropRect
);
201 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
203 KCategorizedView::dragMoveEvent(event
);
205 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
206 const QModelIndex index
= indexAt(event
->pos());
207 setDirtyRegion(m_dropRect
);
209 m_dropRect
.setSize(QSize()); // set as invalid
210 if (index
.isValid()) {
211 const KFileItem item
= m_controller
->itemForIndex(index
);
212 if (!item
.isNull() && item
.isDir()) {
213 m_dropRect
= visualRect(index
);
215 m_dropRect
.setSize(QSize()); // set as invalid
218 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
219 // accept url drops, independently from the destination item
220 event
->acceptProposedAction();
223 setDirtyRegion(m_dropRect
);
226 void DolphinIconsView::dropEvent(QDropEvent
* event
)
228 const QModelIndex index
= indexAt(event
->pos());
229 const KFileItem item
= m_controller
->itemForIndex(index
);
230 m_controller
->indicateDroppedUrls(item
, m_controller
->url(), event
);
231 // don't call KCategorizedView::dropEvent(event), as it moves
232 // the items which is not wanted
235 QModelIndex
DolphinIconsView::moveCursor(CursorAction cursorAction
, Qt::KeyboardModifiers modifiers
)
237 const QModelIndex oldCurrent
= currentIndex();
239 QModelIndex newCurrent
= KCategorizedView::moveCursor(cursorAction
, modifiers
);
240 if (newCurrent
!= oldCurrent
) {
244 // The cursor has not been moved by the base implementation. Provide a
245 // wrap behavior, so that the cursor will go to the next item when reaching
247 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
248 if (settings
->arrangement() == QListView::LeftToRight
) {
249 switch (cursorAction
) {
251 if (newCurrent
.row() == 0) {
254 newCurrent
= KCategorizedView::moveCursor(MoveLeft
, modifiers
);
255 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
256 newCurrent
= KCategorizedView::moveCursor(MovePageDown
, modifiers
);
260 if (newCurrent
.row() == (model()->rowCount() - 1)) {
263 newCurrent
= KCategorizedView::moveCursor(MovePageUp
, modifiers
);
264 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
265 newCurrent
= KCategorizedView::moveCursor(MoveRight
, modifiers
);
272 QModelIndex current
= oldCurrent
;
273 switch (cursorAction
) {
275 if (newCurrent
.row() == 0) {
278 newCurrent
= KCategorizedView::moveCursor(MoveUp
, modifiers
);
280 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
281 current
= newCurrent
;
282 newCurrent
= KCategorizedView::moveCursor(MoveRight
, modifiers
);
283 } while (newCurrent
!= current
);
287 if (newCurrent
.row() == (model()->rowCount() - 1)) {
291 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
292 current
= newCurrent
;
293 newCurrent
= KCategorizedView::moveCursor(MoveLeft
, modifiers
);
294 } while (newCurrent
!= current
);
295 newCurrent
= KCategorizedView::moveCursor(MoveDown
, modifiers
);
303 // Revert all changes of the current item to make sure that item selection works correctly
304 selectionModel()->setCurrentIndex(oldCurrent
, QItemSelectionModel::NoUpdate
);
308 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
310 KCategorizedView::keyPressEvent(event
);
311 m_controller
->handleKeyPressEvent(event
);
314 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
316 horizontalScrollBar()->setSingleStep(m_itemSize
.width() / 5);
317 verticalScrollBar()->setSingleStep(m_itemSize
.height() / 5);
319 KCategorizedView::wheelEvent(event
);
320 // if the icons are aligned left to right, the vertical wheel event should
321 // be applied to the horizontal scrollbar
322 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
323 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
324 (settings
->arrangement() == QListView::LeftToRight
);
325 if (scrollHorizontal
) {
326 QWheelEvent
horizEvent(event
->pos(),
331 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
335 void DolphinIconsView::showEvent(QShowEvent
* event
)
337 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
338 delegate
->setMaximumSize(m_itemSize
);
340 KCategorizedView::showEvent(event
);
343 void DolphinIconsView::leaveEvent(QEvent
* event
)
345 KCategorizedView::leaveEvent(event
);
346 // if the mouse is above an item and moved very fast outside the widget,
347 // no viewportEntered() signal might be emitted although the mouse has been moved
348 // above the viewport
349 m_controller
->emitViewportEntered();
352 void DolphinIconsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
354 KCategorizedView::currentChanged(current
, previous
);
355 m_extensionsFactory
->handleCurrentIndexChange(current
, previous
);
358 void DolphinIconsView::resizeEvent(QResizeEvent
* event
)
360 KCategorizedView::resizeEvent(event
);
361 const DolphinView
* view
= m_controller
->dolphinView();
362 updateGridSize(view
->showPreview(), view
->additionalInfo().count());
365 void DolphinIconsView::slotShowPreviewChanged()
367 const DolphinView
* view
= m_controller
->dolphinView();
368 updateGridSize(view
->showPreview(), additionalInfoCount());
371 void DolphinIconsView::slotAdditionalInfoChanged()
373 const DolphinView
* view
= m_controller
->dolphinView();
374 const bool showPreview
= view
->showPreview();
375 updateGridSize(showPreview
, view
->additionalInfo().count());
378 void DolphinIconsView::setZoomLevel(int level
)
380 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
382 const int oldIconSize
= settings
->iconSize();
383 int newIconSize
= oldIconSize
;
385 const bool showPreview
= m_controller
->dolphinView()->showPreview();
387 const int previewSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
388 settings
->setPreviewSize(previewSize
);
390 newIconSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
391 settings
->setIconSize(newIconSize
);
394 // increase also the grid size
395 const int diff
= newIconSize
- oldIconSize
;
396 settings
->setItemWidth(settings
->itemWidth() + diff
);
397 settings
->setItemHeight(settings
->itemHeight() + diff
);
399 updateGridSize(showPreview
, additionalInfoCount());
402 void DolphinIconsView::requestActivation()
404 m_controller
->requestActivation();
407 void DolphinIconsView::slotGlobalSettingsChanged(int category
)
411 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
412 Q_ASSERT(settings
!= 0);
413 if (settings
->useSystemFont()) {
414 m_font
= KGlobalSettings::generalFont();
417 disconnect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
418 disconnect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
419 if (KGlobalSettings::singleClick()) {
420 connect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
422 connect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
426 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
428 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
429 Q_ASSERT(settings
!= 0);
431 int itemWidth
= settings
->itemWidth();
432 int itemHeight
= settings
->itemHeight();
433 int size
= settings
->iconSize();
436 const int previewSize
= settings
->previewSize();
437 const int diff
= previewSize
- size
;
444 Q_ASSERT(additionalInfoCount
>= 0);
445 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
447 // Optimize the item size of the grid in a way to prevent large gaps on the
448 // right border (= row arrangement) or the bottom border (= column arrangement).
449 // There is no public API in QListView to find out the used width of the viewport
450 // for the layout. The following calculation of 'contentWidth'/'contentHeight'
451 // is based on QListViewPrivate::prepareItemsLayout() (Copyright (C) 2009 Nokia Corporation).
452 int frameAroundContents
= 0;
453 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents
)) {
454 frameAroundContents
= style()->pixelMetric(QStyle::PM_DefaultFrameWidth
) * 2;
456 const int spacing
= settings
->gridSpacing();
457 if (settings
->arrangement() == QListView::TopToBottom
) {
458 const int contentWidth
= viewport()->width() - 1
459 - frameAroundContents
460 - style()->pixelMetric(QStyle::PM_ScrollBarExtent
, 0, horizontalScrollBar());
461 const int gridWidth
= itemWidth
+ spacing
* 2;
462 const int horizItemCount
= contentWidth
/ gridWidth
;
463 if (horizItemCount
> 0) {
464 itemWidth
+= (contentWidth
- horizItemCount
* gridWidth
) / horizItemCount
;
467 // The decoration width indirectly defines the maximum
468 // width for the text wrapping. To use the maximum item width
469 // for text wrapping, it is used as decoration width.
470 m_decorationSize
= QSize(itemWidth
, size
);
471 setIconSize(QSize(itemWidth
, size
));
473 const int contentHeight
= viewport()->height() - 1
474 - frameAroundContents
475 - style()->pixelMetric(QStyle::PM_ScrollBarExtent
, 0, verticalScrollBar());
476 const int gridHeight
= itemHeight
+ spacing
;
477 const int vertItemCount
= contentHeight
/ gridHeight
;
478 if (vertItemCount
> 0) {
479 itemHeight
+= (contentHeight
- vertItemCount
* gridHeight
) / vertItemCount
;
482 m_decorationSize
= QSize(size
, size
);
483 setIconSize(QSize(size
, size
));
486 m_itemSize
= QSize(itemWidth
, itemHeight
);
487 setGridSizeOwn(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
489 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
491 delegate
->setMaximumSize(m_itemSize
);
495 int DolphinIconsView::additionalInfoCount() const
497 const DolphinView
* view
= m_controller
->dolphinView();
498 return view
->additionalInfo().count();
501 #include "dolphiniconsview.moc"