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 "dolphinviewcontroller.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 "viewmodecontroller.h"
32 #include "zoomlevelinfo.h"
34 #include <kcategorizedsortfilterproxymodel.h>
36 #include <kfileitemdelegate.h>
38 #include <QAbstractProxyModel>
39 #include <QApplication>
42 DolphinIconsView::DolphinIconsView(QWidget
* parent
,
43 DolphinViewController
* dolphinViewController
,
44 const ViewModeController
* viewModeController
,
45 DolphinSortFilterProxyModel
* proxyModel
) :
46 KCategorizedView(parent
),
47 m_dolphinViewController(dolphinViewController
),
48 m_viewModeController(viewModeController
),
49 m_categoryDrawer(new DolphinCategoryDrawer(this)),
50 m_extensionsFactory(0),
53 m_decorationPosition(QStyleOptionViewItem::Top
),
54 m_displayAlignment(Qt::AlignHCenter
),
58 Q_ASSERT(dolphinViewController
!= 0);
59 Q_ASSERT(viewModeController
!= 0);
62 setLayoutDirection(Qt::LeftToRight
);
63 setViewMode(QListView::IconMode
);
64 setResizeMode(QListView::Adjust
);
65 setMovement(QListView::Static
);
67 setEditTriggers(QAbstractItemView::NoEditTriggers
);
68 viewport()->setAcceptDrops(true);
70 setMouseTracking(true);
72 connect(this, SIGNAL(clicked(const QModelIndex
&)),
73 dolphinViewController
, SLOT(requestTab(const QModelIndex
&)));
74 if (KGlobalSettings::singleClick()) {
75 connect(this, SIGNAL(clicked(const QModelIndex
&)),
76 dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
78 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
79 dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
82 connect(this, SIGNAL(entered(const QModelIndex
&)),
83 dolphinViewController
, SLOT(emitItemEntered(const QModelIndex
&)));
84 connect(this, SIGNAL(viewportEntered()),
85 dolphinViewController
, SLOT(emitViewportEntered()));
86 connect(viewModeController
, SIGNAL(zoomLevelChanged(int)),
87 this, SLOT(setZoomLevel(int)));
89 const DolphinView
* view
= dolphinViewController
->view();
90 connect(view
, SIGNAL(showPreviewChanged()),
91 this, SLOT(slotShowPreviewChanged()));
92 connect(view
, SIGNAL(additionalInfoChanged()),
93 this, SLOT(slotAdditionalInfoChanged()));
95 // apply the icons mode settings to the widget
96 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
97 Q_ASSERT(settings
!= 0);
99 if (settings
->useSystemFont()) {
100 m_font
= KGlobalSettings::generalFont();
102 m_font
= QFont(settings
->fontFamily(),
103 qRound(settings
->fontSize()),
104 settings
->fontWeight(),
105 settings
->italicFont());
106 m_font
.setPointSizeF(settings
->fontSize());
109 setWordWrap(settings
->numberOfTextlines() > 1);
111 if (settings
->arrangement() == QListView::TopToBottom
) {
112 setFlow(QListView::LeftToRight
);
113 m_decorationPosition
= QStyleOptionViewItem::Top
;
114 m_displayAlignment
= Qt::AlignHCenter
;
116 setFlow(QListView::TopToBottom
);
117 m_decorationPosition
= QStyleOptionViewItem::Left
;
118 m_displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
121 connect(m_categoryDrawer
, SIGNAL(actionRequested(int,QModelIndex
)), this, SLOT(categoryDrawerActionRequested(int,QModelIndex
)));
122 setCategoryDrawer(m_categoryDrawer
);
124 connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)),
125 this, SLOT(slotGlobalSettingsChanged(int)));
127 updateGridSize(view
->showPreview(), 0);
128 m_extensionsFactory
= new ViewExtensionsFactory(this, dolphinViewController
, viewModeController
);
130 // setFocus() must be called after m_extensionsFactory is initialised (see bug 240374).
134 DolphinIconsView::~DolphinIconsView()
138 void DolphinIconsView::dataChanged(const QModelIndex
& topLeft
, const QModelIndex
& bottomRight
)
140 KCategorizedView::dataChanged(topLeft
, bottomRight
);
142 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
143 if (!proxyModel
->isCategorizedModel()) {
144 // bypass a QListView issue that items are not layout correctly if the decoration size of
146 scheduleDelayedItemsLayout();
150 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
152 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
153 viewOptions
.font
= m_font
;
154 viewOptions
.fontMetrics
= QFontMetrics(m_font
);
155 viewOptions
.decorationPosition
= m_decorationPosition
;
156 viewOptions
.decorationSize
= m_decorationSize
;
157 viewOptions
.displayAlignment
= m_displayAlignment
;
158 viewOptions
.showDecorationSelected
= true;
162 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
164 KCategorizedView::contextMenuEvent(event
);
165 m_dolphinViewController
->triggerContextMenuRequest(event
->pos());
168 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
170 m_dolphinViewController
->requestActivation();
171 const QModelIndex index
= indexAt(event
->pos());
172 if (index
.isValid() && (event
->button() == Qt::LeftButton
)) {
173 // TODO: It should not be necessary to manually set the dragging state, but I could
174 // not reproduce this issue with a Qt-only example yet to find the root cause.
175 // Issue description: start Dolphin, split the view and drag an item from the
176 // inactive view to the active view by a very fast mouse movement. Result:
177 // the item gets selected instead of being dragged...
178 setState(QAbstractItemView::DraggingState
);
181 if (!index
.isValid() && (QApplication::mouseButtons() & Qt::MidButton
)) {
182 m_dolphinViewController
->replaceUrlByClipboard();
185 KCategorizedView::mousePressEvent(event
);
188 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
190 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_dolphinViewController
);
193 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
195 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
196 event
->acceptProposedAction();
200 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
202 KCategorizedView::dragLeaveEvent(event
);
203 setDirtyRegion(m_dropRect
);
206 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
208 KCategorizedView::dragMoveEvent(event
);
210 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
211 const QModelIndex index
= indexAt(event
->pos());
212 setDirtyRegion(m_dropRect
);
214 m_dropRect
.setSize(QSize()); // set as invalid
215 if (index
.isValid()) {
216 const KFileItem item
= m_dolphinViewController
->itemForIndex(index
);
217 if (!item
.isNull() && item
.isDir()) {
218 m_dropRect
= visualRect(index
);
220 m_dropRect
.setSize(QSize()); // set as invalid
223 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
224 // accept url drops, independently from the destination item
225 event
->acceptProposedAction();
228 setDirtyRegion(m_dropRect
);
231 void DolphinIconsView::dropEvent(QDropEvent
* event
)
233 const QModelIndex index
= indexAt(event
->pos());
234 const KFileItem item
= m_dolphinViewController
->itemForIndex(index
);
235 m_dolphinViewController
->indicateDroppedUrls(item
, m_viewModeController
->url(), event
);
236 // don't call KCategorizedView::dropEvent(event), as it moves
237 // the items which is not wanted
240 QModelIndex
DolphinIconsView::moveCursor(CursorAction cursorAction
, Qt::KeyboardModifiers modifiers
)
242 const QModelIndex oldCurrent
= currentIndex();
244 QModelIndex newCurrent
= KCategorizedView::moveCursor(cursorAction
, modifiers
);
245 if (newCurrent
!= oldCurrent
) {
249 // The cursor has not been moved by the base implementation. Provide a
250 // wrap behavior, so that the cursor will go to the next item when reaching
252 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
253 if (settings
->arrangement() == QListView::LeftToRight
) {
254 switch (cursorAction
) {
256 if (newCurrent
.row() == 0) {
259 newCurrent
= KCategorizedView::moveCursor(MoveLeft
, modifiers
);
260 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
261 newCurrent
= KCategorizedView::moveCursor(MovePageDown
, modifiers
);
265 if (newCurrent
.row() == (model()->rowCount() - 1)) {
268 newCurrent
= KCategorizedView::moveCursor(MovePageUp
, modifiers
);
269 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
270 newCurrent
= KCategorizedView::moveCursor(MoveRight
, modifiers
);
277 QModelIndex current
= oldCurrent
;
278 switch (cursorAction
) {
280 if (newCurrent
.row() == 0) {
283 newCurrent
= KCategorizedView::moveCursor(MoveUp
, modifiers
);
285 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
286 current
= newCurrent
;
287 newCurrent
= KCategorizedView::moveCursor(MoveRight
, modifiers
);
288 } while (newCurrent
!= current
);
292 if (newCurrent
.row() == (model()->rowCount() - 1)) {
296 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
297 current
= newCurrent
;
298 newCurrent
= KCategorizedView::moveCursor(MoveLeft
, modifiers
);
299 } while (newCurrent
!= current
);
300 newCurrent
= KCategorizedView::moveCursor(MoveDown
, modifiers
);
308 // Revert all changes of the current item to make sure that item selection works correctly
309 selectionModel()->setCurrentIndex(oldCurrent
, QItemSelectionModel::NoUpdate
);
313 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
315 KCategorizedView::keyPressEvent(event
);
316 m_dolphinViewController
->handleKeyPressEvent(event
);
319 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
321 horizontalScrollBar()->setSingleStep(m_itemSize
.width() / 5);
322 verticalScrollBar()->setSingleStep(m_itemSize
.height() / 5);
324 KCategorizedView::wheelEvent(event
);
325 // if the icons are aligned left to right, the vertical wheel event should
326 // be applied to the horizontal scrollbar
327 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
328 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
329 (settings
->arrangement() == QListView::LeftToRight
);
330 if (scrollHorizontal
) {
331 QWheelEvent
horizEvent(event
->pos(),
336 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
340 void DolphinIconsView::showEvent(QShowEvent
* event
)
342 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
343 delegate
->setMaximumSize(m_itemSize
);
345 KCategorizedView::showEvent(event
);
348 void DolphinIconsView::leaveEvent(QEvent
* event
)
350 KCategorizedView::leaveEvent(event
);
351 // if the mouse is above an item and moved very fast outside the widget,
352 // no viewportEntered() signal might be emitted although the mouse has been moved
353 // above the viewport
354 m_dolphinViewController
->emitViewportEntered();
357 void DolphinIconsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
359 KCategorizedView::currentChanged(current
, previous
);
360 m_extensionsFactory
->handleCurrentIndexChange(current
, previous
);
363 void DolphinIconsView::resizeEvent(QResizeEvent
* event
)
365 KCategorizedView::resizeEvent(event
);
366 const DolphinView
* view
= m_dolphinViewController
->view();
367 updateGridSize(view
->showPreview(), view
->additionalInfo().count());
370 void DolphinIconsView::slotShowPreviewChanged()
372 const DolphinView
* view
= m_dolphinViewController
->view();
373 updateGridSize(view
->showPreview(), additionalInfoCount());
376 void DolphinIconsView::slotAdditionalInfoChanged()
378 const DolphinView
* view
= m_dolphinViewController
->view();
379 const bool showPreview
= view
->showPreview();
380 updateGridSize(showPreview
, view
->additionalInfo().count());
383 void DolphinIconsView::setZoomLevel(int level
)
385 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
387 const int oldIconSize
= settings
->iconSize();
388 int newIconSize
= oldIconSize
;
390 const bool showPreview
= m_dolphinViewController
->view()->showPreview();
392 const int previewSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
393 settings
->setPreviewSize(previewSize
);
395 newIconSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
396 settings
->setIconSize(newIconSize
);
399 // increase also the grid size
400 const int diff
= newIconSize
- oldIconSize
;
401 settings
->setItemWidth(settings
->itemWidth() + diff
);
402 settings
->setItemHeight(settings
->itemHeight() + diff
);
404 updateGridSize(showPreview
, additionalInfoCount());
407 void DolphinIconsView::requestActivation()
409 m_dolphinViewController
->requestActivation();
412 void DolphinIconsView::slotGlobalSettingsChanged(int category
)
416 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
417 Q_ASSERT(settings
!= 0);
418 if (settings
->useSystemFont()) {
419 m_font
= KGlobalSettings::generalFont();
422 disconnect(this, SIGNAL(clicked(QModelIndex
)), m_dolphinViewController
, SLOT(triggerItem(QModelIndex
)));
423 disconnect(this, SIGNAL(doubleClicked(QModelIndex
)), m_dolphinViewController
, SLOT(triggerItem(QModelIndex
)));
424 if (KGlobalSettings::singleClick()) {
425 connect(this, SIGNAL(clicked(QModelIndex
)), m_dolphinViewController
, SLOT(triggerItem(QModelIndex
)));
427 connect(this, SIGNAL(doubleClicked(QModelIndex
)), m_dolphinViewController
, SLOT(triggerItem(QModelIndex
)));
431 void DolphinIconsView::categoryDrawerActionRequested(int action
, const QModelIndex
&index
)
433 const QSortFilterProxyModel
*model
= dynamic_cast<const QSortFilterProxyModel
*>(index
.model());
434 const QModelIndex topLeft
= model
->index(index
.row(), modelColumn());
435 QModelIndex bottomRight
= topLeft
;
436 const QString category
= model
->data(index
, KCategorizedSortFilterProxyModel::CategoryDisplayRole
).toString();
437 QModelIndex current
= topLeft
;
439 current
= model
->index(current
.row() + 1, modelColumn());
440 const QString curCategory
= model
->data(model
->index(current
.row(), index
.column()), KCategorizedSortFilterProxyModel::CategoryDisplayRole
).toString();
441 if (!current
.isValid() || category
!= curCategory
) {
444 bottomRight
= current
;
447 case DolphinCategoryDrawer::SelectAll
:
448 selectionModel()->select(QItemSelection(topLeft
, bottomRight
), QItemSelectionModel::Select
);
450 case DolphinCategoryDrawer::UnselectAll
:
451 selectionModel()->select(QItemSelection(topLeft
, bottomRight
), QItemSelectionModel::Deselect
);
458 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
460 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
461 Q_ASSERT(settings
!= 0);
463 int itemWidth
= settings
->itemWidth();
464 int itemHeight
= settings
->itemHeight();
465 int size
= settings
->iconSize();
468 const int previewSize
= settings
->previewSize();
469 const int diff
= previewSize
- size
;
476 Q_ASSERT(additionalInfoCount
>= 0);
477 itemHeight
+= additionalInfoCount
* QFontMetrics(m_font
).height();
479 // Optimize the item size of the grid in a way to prevent large gaps on the
480 // right border (= row arrangement) or the bottom border (= column arrangement).
481 // There is no public API in QListView to find out the used width of the viewport
482 // for the layout. The following calculation of 'contentWidth'/'contentHeight'
483 // is based on QListViewPrivate::prepareItemsLayout() (Copyright (C) 2009 Nokia Corporation).
484 int frameAroundContents
= 0;
485 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents
)) {
486 frameAroundContents
= style()->pixelMetric(QStyle::PM_DefaultFrameWidth
) * 2;
488 const int spacing
= settings
->gridSpacing();
489 if (settings
->arrangement() == QListView::TopToBottom
) {
490 const int contentWidth
= viewport()->width() - 1
491 - frameAroundContents
492 - style()->pixelMetric(QStyle::PM_ScrollBarExtent
, 0, horizontalScrollBar());
493 const int gridWidth
= itemWidth
+ spacing
* 2;
494 const int horizItemCount
= contentWidth
/ gridWidth
;
495 if (horizItemCount
> 0) {
496 itemWidth
+= (contentWidth
- horizItemCount
* gridWidth
) / horizItemCount
;
499 // The decoration width indirectly defines the maximum
500 // width for the text wrapping. To use the maximum item width
501 // for text wrapping, it is used as decoration width.
502 m_decorationSize
= QSize(itemWidth
, size
);
503 setIconSize(QSize(itemWidth
, size
));
505 const int contentHeight
= viewport()->height() - 1
506 - frameAroundContents
507 - style()->pixelMetric(QStyle::PM_ScrollBarExtent
, 0, verticalScrollBar());
508 const int gridHeight
= itemHeight
+ spacing
;
509 const int vertItemCount
= contentHeight
/ gridHeight
;
510 if (vertItemCount
> 0) {
511 itemHeight
+= (contentHeight
- vertItemCount
* gridHeight
) / vertItemCount
;
514 m_decorationSize
= QSize(size
, size
);
515 setIconSize(QSize(size
, size
));
518 m_itemSize
= QSize(itemWidth
, itemHeight
);
519 setGridSizeOwn(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
521 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
523 delegate
->setMaximumSize(m_itemSize
);
527 int DolphinIconsView::additionalInfoCount() const
529 const DolphinView
* view
= m_dolphinViewController
->view();
530 return view
->additionalInfo().count();
533 #include "dolphiniconsview.moc"