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>
36 #include <KFileItemDelegate>
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_categoryDrawer(new DolphinCategoryDrawer(this)),
49 m_extensionsFactory(0),
52 m_decorationPosition(QStyleOptionViewItem::Top
),
53 m_displayAlignment(Qt::AlignHCenter
),
57 Q_ASSERT(dolphinViewController
!= 0);
58 Q_ASSERT(viewModeController
!= 0);
61 setLayoutDirection(Qt::LeftToRight
);
62 setViewMode(QListView::IconMode
);
63 setResizeMode(QListView::Adjust
);
64 setMovement(QListView::Static
);
66 setEditTriggers(QAbstractItemView::NoEditTriggers
);
67 viewport()->setAcceptDrops(true);
69 setMouseTracking(true);
71 connect(this, SIGNAL(clicked(const QModelIndex
&)),
72 dolphinViewController
, SLOT(requestTab(const QModelIndex
&)));
73 if (KGlobalSettings::singleClick()) {
74 connect(this, SIGNAL(clicked(const QModelIndex
&)),
75 dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
77 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
78 dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
81 connect(this, SIGNAL(entered(const QModelIndex
&)),
82 dolphinViewController
, SLOT(emitItemEntered(const QModelIndex
&)));
83 connect(this, SIGNAL(viewportEntered()),
84 dolphinViewController
, SLOT(emitViewportEntered()));
85 connect(viewModeController
, SIGNAL(zoomLevelChanged(int)),
86 this, SLOT(setZoomLevel(int)));
88 const DolphinView
* view
= dolphinViewController
->view();
89 connect(view
, SIGNAL(showPreviewChanged()),
90 this, SLOT(slotShowPreviewChanged()));
91 connect(view
, SIGNAL(additionalInfoChanged()),
92 this, SLOT(slotAdditionalInfoChanged()));
94 // apply the icons mode settings to the widget
95 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
96 Q_ASSERT(settings
!= 0);
98 if (settings
->useSystemFont()) {
99 m_font
= KGlobalSettings::generalFont();
101 m_font
= QFont(settings
->fontFamily(),
102 qRound(settings
->fontSize()),
103 settings
->fontWeight(),
104 settings
->italicFont());
105 m_font
.setPointSizeF(settings
->fontSize());
108 setWordWrap(settings
->numberOfTextlines() > 1);
110 if (settings
->arrangement() == QListView::TopToBottom
) {
111 setFlow(QListView::LeftToRight
);
112 m_decorationPosition
= QStyleOptionViewItem::Top
;
113 m_displayAlignment
= Qt::AlignHCenter
;
115 setFlow(QListView::TopToBottom
);
116 m_decorationPosition
= QStyleOptionViewItem::Left
;
117 m_displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
120 connect(m_categoryDrawer
, SIGNAL(actionRequested(int,QModelIndex
)), this, SLOT(categoryDrawerActionRequested(int,QModelIndex
)));
121 setCategoryDrawer(m_categoryDrawer
);
123 connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)),
124 this, SLOT(slotGlobalSettingsChanged(int)));
126 updateGridSize(view
->showPreview(), 0);
127 m_extensionsFactory
= new ViewExtensionsFactory(this, dolphinViewController
, viewModeController
);
130 DolphinIconsView::~DolphinIconsView()
134 void DolphinIconsView::dataChanged(const QModelIndex
& topLeft
, const QModelIndex
& bottomRight
)
136 KCategorizedView::dataChanged(topLeft
, bottomRight
);
138 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
139 if (!proxyModel
->isCategorizedModel()) {
140 // bypass a QListView issue that items are not layout correctly if the decoration size of
142 scheduleDelayedItemsLayout();
146 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
148 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
149 viewOptions
.font
= m_font
;
150 viewOptions
.fontMetrics
= QFontMetrics(m_font
);
151 viewOptions
.decorationPosition
= m_decorationPosition
;
152 viewOptions
.decorationSize
= m_decorationSize
;
153 viewOptions
.displayAlignment
= m_displayAlignment
;
154 viewOptions
.showDecorationSelected
= true;
158 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
160 KCategorizedView::contextMenuEvent(event
);
161 m_dolphinViewController
->triggerContextMenuRequest(event
->pos());
164 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
166 m_dolphinViewController
->requestActivation();
167 const QModelIndex index
= indexAt(event
->pos());
168 if (index
.isValid() && (event
->button() == Qt::LeftButton
)) {
169 // TODO: It should not be necessary to manually set the dragging state, but I could
170 // not reproduce this issue with a Qt-only example yet to find the root cause.
171 // Issue description: start Dolphin, split the view and drag an item from the
172 // inactive view to the active view by a very fast mouse movement. Result:
173 // the item gets selected instead of being dragged...
174 setState(QAbstractItemView::DraggingState
);
177 if (!index
.isValid() && (QApplication::mouseButtons() & Qt::MidButton
)) {
178 m_dolphinViewController
->replaceUrlByClipboard();
181 KCategorizedView::mousePressEvent(event
);
184 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
186 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_dolphinViewController
);
189 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
191 event
->acceptProposedAction();
194 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
196 KCategorizedView::dragLeaveEvent(event
);
197 setDirtyRegion(m_dropRect
);
200 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
202 KCategorizedView::dragMoveEvent(event
);
204 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
205 const QModelIndex index
= indexAt(event
->pos());
206 setDirtyRegion(m_dropRect
);
208 m_dropRect
.setSize(QSize()); // set as invalid
209 if (index
.isValid()) {
210 const KFileItem item
= m_dolphinViewController
->itemForIndex(index
);
211 if (!item
.isNull() && item
.isDir()) {
212 m_dropRect
= visualRect(index
);
214 m_dropRect
.setSize(QSize()); // set as invalid
217 event
->acceptProposedAction();
219 setDirtyRegion(m_dropRect
);
222 void DolphinIconsView::dropEvent(QDropEvent
* event
)
224 const QModelIndex index
= indexAt(event
->pos());
225 const KFileItem item
= m_dolphinViewController
->itemForIndex(index
);
226 m_dolphinViewController
->indicateDroppedUrls(item
, event
);
227 // don't call KCategorizedView::dropEvent(event), as it moves
228 // the items which is not wanted
231 QModelIndex
DolphinIconsView::moveCursor(CursorAction cursorAction
, Qt::KeyboardModifiers modifiers
)
233 const QModelIndex oldCurrent
= currentIndex();
235 QModelIndex newCurrent
= KCategorizedView::moveCursor(cursorAction
, modifiers
);
236 if (newCurrent
!= oldCurrent
) {
240 // The cursor has not been moved by the base implementation. Provide a
241 // wrap behavior, so that the cursor will go to the next item when reaching
243 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
244 if (settings
->arrangement() == QListView::LeftToRight
) {
245 switch (cursorAction
) {
247 if (newCurrent
.row() == 0) {
250 newCurrent
= KCategorizedView::moveCursor(MoveLeft
, modifiers
);
251 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
252 newCurrent
= KCategorizedView::moveCursor(MovePageDown
, modifiers
);
256 if (newCurrent
.row() == (model()->rowCount() - 1)) {
259 newCurrent
= KCategorizedView::moveCursor(MovePageUp
, modifiers
);
260 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
261 newCurrent
= KCategorizedView::moveCursor(MoveRight
, modifiers
);
268 QModelIndex current
= oldCurrent
;
269 switch (cursorAction
) {
271 if (newCurrent
.row() == 0) {
274 newCurrent
= KCategorizedView::moveCursor(MoveUp
, modifiers
);
276 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
277 current
= newCurrent
;
278 newCurrent
= KCategorizedView::moveCursor(MoveRight
, modifiers
);
279 } while (newCurrent
!= current
);
283 if (newCurrent
.row() == (model()->rowCount() - 1)) {
287 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
288 current
= newCurrent
;
289 newCurrent
= KCategorizedView::moveCursor(MoveLeft
, modifiers
);
290 } while (newCurrent
!= current
);
291 newCurrent
= KCategorizedView::moveCursor(MoveDown
, modifiers
);
299 // Revert all changes of the current item to make sure that item selection works correctly
300 selectionModel()->setCurrentIndex(oldCurrent
, QItemSelectionModel::NoUpdate
);
304 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
306 KCategorizedView::keyPressEvent(event
);
307 m_dolphinViewController
->handleKeyPressEvent(event
);
310 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
312 horizontalScrollBar()->setSingleStep(m_itemSize
.width() / 5);
313 verticalScrollBar()->setSingleStep(m_itemSize
.height() / 5);
315 KCategorizedView::wheelEvent(event
);
316 // if the icons are aligned left to right, the vertical wheel event should
317 // be applied to the horizontal scrollbar
318 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
319 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
320 (settings
->arrangement() == QListView::LeftToRight
);
321 if (scrollHorizontal
) {
322 QWheelEvent
horizEvent(event
->pos(),
327 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
331 void DolphinIconsView::showEvent(QShowEvent
* event
)
333 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
334 delegate
->setMaximumSize(m_itemSize
);
336 KCategorizedView::showEvent(event
);
339 void DolphinIconsView::leaveEvent(QEvent
* event
)
341 KCategorizedView::leaveEvent(event
);
342 // if the mouse is above an item and moved very fast outside the widget,
343 // no viewportEntered() signal might be emitted although the mouse has been moved
344 // above the viewport
345 m_dolphinViewController
->emitViewportEntered();
348 void DolphinIconsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
350 KCategorizedView::currentChanged(current
, previous
);
351 m_extensionsFactory
->handleCurrentIndexChange(current
, previous
);
354 void DolphinIconsView::resizeEvent(QResizeEvent
* event
)
356 KCategorizedView::resizeEvent(event
);
357 const DolphinView
* view
= m_dolphinViewController
->view();
358 updateGridSize(view
->showPreview(), view
->additionalInfo().count());
361 void DolphinIconsView::slotShowPreviewChanged()
363 const DolphinView
* view
= m_dolphinViewController
->view();
364 updateGridSize(view
->showPreview(), additionalInfoCount());
367 void DolphinIconsView::slotAdditionalInfoChanged()
369 const DolphinView
* view
= m_dolphinViewController
->view();
370 const bool showPreview
= view
->showPreview();
371 updateGridSize(showPreview
, view
->additionalInfo().count());
374 void DolphinIconsView::setZoomLevel(int level
)
376 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
378 const int oldIconSize
= settings
->iconSize();
379 int newIconSize
= oldIconSize
;
381 const bool showPreview
= m_dolphinViewController
->view()->showPreview();
383 const int previewSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
384 settings
->setPreviewSize(previewSize
);
386 newIconSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
387 settings
->setIconSize(newIconSize
);
390 // increase also the grid size
391 const int diff
= newIconSize
- oldIconSize
;
392 settings
->setItemWidth(settings
->itemWidth() + diff
);
393 settings
->setItemHeight(settings
->itemHeight() + diff
);
395 updateGridSize(showPreview
, additionalInfoCount());
398 void DolphinIconsView::requestActivation()
400 m_dolphinViewController
->requestActivation();
403 void DolphinIconsView::slotGlobalSettingsChanged(int category
)
407 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
408 Q_ASSERT(settings
!= 0);
409 if (settings
->useSystemFont()) {
410 m_font
= KGlobalSettings::generalFont();
413 disconnect(this, SIGNAL(clicked(QModelIndex
)), m_dolphinViewController
, SLOT(triggerItem(QModelIndex
)));
414 disconnect(this, SIGNAL(doubleClicked(QModelIndex
)), m_dolphinViewController
, SLOT(triggerItem(QModelIndex
)));
415 if (KGlobalSettings::singleClick()) {
416 connect(this, SIGNAL(clicked(QModelIndex
)), m_dolphinViewController
, SLOT(triggerItem(QModelIndex
)));
418 connect(this, SIGNAL(doubleClicked(QModelIndex
)), m_dolphinViewController
, SLOT(triggerItem(QModelIndex
)));
422 void DolphinIconsView::categoryDrawerActionRequested(int action
, const QModelIndex
&index
)
424 const QSortFilterProxyModel
*model
= dynamic_cast<const QSortFilterProxyModel
*>(index
.model());
425 const QModelIndex topLeft
= model
->index(index
.row(), modelColumn());
426 QModelIndex bottomRight
= topLeft
;
427 const QString category
= model
->data(index
, KCategorizedSortFilterProxyModel::CategoryDisplayRole
).toString();
428 QModelIndex current
= topLeft
;
430 current
= model
->index(current
.row() + 1, modelColumn());
431 const QString curCategory
= model
->data(model
->index(current
.row(), index
.column()), KCategorizedSortFilterProxyModel::CategoryDisplayRole
).toString();
432 if (!current
.isValid() || category
!= curCategory
) {
435 bottomRight
= current
;
438 case DolphinCategoryDrawer::SelectAll
:
439 selectionModel()->select(QItemSelection(topLeft
, bottomRight
), QItemSelectionModel::Select
);
441 case DolphinCategoryDrawer::UnselectAll
:
442 selectionModel()->select(QItemSelection(topLeft
, bottomRight
), QItemSelectionModel::Deselect
);
449 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
451 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
452 Q_ASSERT(settings
!= 0);
454 int itemWidth
= settings
->itemWidth();
455 int itemHeight
= settings
->itemHeight();
456 int size
= settings
->iconSize();
459 const int previewSize
= settings
->previewSize();
460 const int diff
= previewSize
- size
;
467 Q_ASSERT(additionalInfoCount
>= 0);
468 itemHeight
+= additionalInfoCount
* QFontMetrics(m_font
).height();
470 // Optimize the item size of the grid in a way to prevent large gaps on the
471 // right border (= row arrangement) or the bottom border (= column arrangement).
472 // There is no public API in QListView to find out the used width of the viewport
473 // for the layout. The following calculation of 'contentWidth'/'contentHeight'
474 // is based on QListViewPrivate::prepareItemsLayout() (Copyright (C) 2009 Nokia Corporation).
475 int frameAroundContents
= 0;
476 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents
)) {
477 frameAroundContents
= style()->pixelMetric(QStyle::PM_DefaultFrameWidth
) * 2;
479 const int spacing
= settings
->gridSpacing();
480 if (settings
->arrangement() == QListView::TopToBottom
) {
481 const int contentWidth
= viewport()->width() - 1
482 - frameAroundContents
483 - style()->pixelMetric(QStyle::PM_ScrollBarExtent
, 0, horizontalScrollBar());
484 const int gridWidth
= itemWidth
+ spacing
* 2;
485 const int horizItemCount
= contentWidth
/ gridWidth
;
486 if (horizItemCount
> 0) {
487 itemWidth
+= (contentWidth
- horizItemCount
* gridWidth
) / horizItemCount
;
490 // The decoration width indirectly defines the maximum
491 // width for the text wrapping. To use the maximum item width
492 // for text wrapping, it is used as decoration width.
493 m_decorationSize
= QSize(itemWidth
, size
);
494 setIconSize(QSize(itemWidth
, size
));
496 const int contentHeight
= viewport()->height() - 1
497 - frameAroundContents
498 - style()->pixelMetric(QStyle::PM_ScrollBarExtent
, 0, verticalScrollBar());
499 const int gridHeight
= itemHeight
+ spacing
;
500 const int vertItemCount
= contentHeight
/ gridHeight
;
501 if (vertItemCount
> 0) {
502 itemHeight
+= (contentHeight
- vertItemCount
* gridHeight
) / vertItemCount
;
505 m_decorationSize
= QSize(size
, size
);
506 setIconSize(QSize(size
, size
));
509 m_itemSize
= QSize(itemWidth
, itemHeight
);
510 setGridSizeOwn(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
512 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
514 delegate
->setMaximumSize(m_itemSize
);
518 int DolphinIconsView::additionalInfoCount() const
520 const DolphinView
* view
= m_dolphinViewController
->view();
521 return view
->additionalInfo().count();
524 #include "dolphiniconsview.moc"