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 "dolphinviewautoscroller.h"
27 #include "dolphin_iconsmodesettings.h"
28 #include "dolphin_generalsettings.h"
29 #include "draganddrophelper.h"
30 #include "selectionmanager.h"
31 #include "viewextensionsfactory.h"
32 #include "zoomlevelinfo.h"
34 #include <kcategorizedsortfilterproxymodel.h>
36 #include <kdirmodel.h>
37 #include <kfileitemdelegate.h>
39 #include <QAbstractProxyModel>
40 #include <QApplication>
43 DolphinIconsView::DolphinIconsView(QWidget
* parent
,
44 DolphinController
* controller
,
45 DolphinSortFilterProxyModel
* proxyModel
) :
46 KCategorizedView(parent
),
47 m_controller(controller
),
48 m_selectionManager(0),
51 m_extensionsFactory(0),
54 m_decorationPosition(QStyleOptionViewItem::Top
),
55 m_displayAlignment(Qt::AlignHCenter
),
59 Q_ASSERT(controller
!= 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);
70 m_autoScroller
= new DolphinViewAutoScroller(this);
72 connect(this, SIGNAL(clicked(const QModelIndex
&)),
73 controller
, SLOT(requestTab(const QModelIndex
&)));
74 if (KGlobalSettings::singleClick()) {
75 connect(this, SIGNAL(clicked(const QModelIndex
&)),
76 controller
, SLOT(triggerItem(const QModelIndex
&)));
78 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
79 controller
, SLOT(triggerItem(const QModelIndex
&)));
82 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
83 m_selectionManager
= new SelectionManager(this);
84 connect(m_selectionManager
, SIGNAL(selectionChanged()),
85 this, SLOT(requestActivation()));
86 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
87 m_selectionManager
, SLOT(reset()));
90 connect(this, SIGNAL(entered(const QModelIndex
&)),
91 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
92 connect(this, SIGNAL(viewportEntered()),
93 controller
, SLOT(emitViewportEntered()));
94 connect(controller
, SIGNAL(nameFilterChanged(const QString
&)),
95 this, SLOT(setNameFilter(const QString
&)));
96 connect(controller
, SIGNAL(zoomLevelChanged(int)),
97 this, SLOT(setZoomLevel(int)));
99 const DolphinView
* view
= controller
->dolphinView();
100 connect(view
, SIGNAL(showPreviewChanged()),
101 this, SLOT(slotShowPreviewChanged()));
102 connect(view
, SIGNAL(additionalInfoChanged()),
103 this, SLOT(slotAdditionalInfoChanged()));
105 // apply the icons mode settings to the widget
106 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
107 Q_ASSERT(settings
!= 0);
109 if (settings
->useSystemFont()) {
110 m_font
= KGlobalSettings::generalFont();
112 m_font
= QFont(settings
->fontFamily(),
113 settings
->fontSize(),
114 settings
->fontWeight(),
115 settings
->italicFont());
118 setWordWrap(settings
->numberOfTextlines() > 1);
119 updateGridSize(view
->showPreview(), 0);
121 if (settings
->arrangement() == QListView::TopToBottom
) {
122 setFlow(QListView::LeftToRight
);
123 m_decorationPosition
= QStyleOptionViewItem::Top
;
124 m_displayAlignment
= Qt::AlignHCenter
;
126 setFlow(QListView::TopToBottom
);
127 m_decorationPosition
= QStyleOptionViewItem::Left
;
128 m_displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
131 m_categoryDrawer
= new DolphinCategoryDrawer();
132 setCategoryDrawer(m_categoryDrawer
);
136 connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)),
137 this, SLOT(slotGlobalSettingsChanged(int)));
139 m_extensionsFactory
= new ViewExtensionsFactory(this, controller
);
142 DolphinIconsView::~DolphinIconsView()
144 delete m_categoryDrawer
;
145 m_categoryDrawer
= 0;
148 void DolphinIconsView::dataChanged(const QModelIndex
& topLeft
, const QModelIndex
& bottomRight
)
150 KCategorizedView::dataChanged(topLeft
, bottomRight
);
152 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
153 if (!proxyModel
->isCategorizedModel()) {
154 // bypass a QListView issue that items are not layout correctly if the decoration size of
156 scheduleDelayedItemsLayout();
160 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
162 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
163 viewOptions
.font
= m_font
;
164 viewOptions
.decorationPosition
= m_decorationPosition
;
165 viewOptions
.decorationSize
= m_decorationSize
;
166 viewOptions
.displayAlignment
= m_displayAlignment
;
167 viewOptions
.showDecorationSelected
= true;
171 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
173 KCategorizedView::contextMenuEvent(event
);
174 m_controller
->triggerContextMenuRequest(event
->pos());
177 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
179 m_controller
->requestActivation();
180 const QModelIndex index
= indexAt(event
->pos());
181 if (index
.isValid() && (event
->button() == Qt::LeftButton
)) {
182 // TODO: It should not be necessary to manually set the dragging state, but I could
183 // not reproduce this issue with a Qt-only example yet to find the root cause.
184 // Issue description: start Dolphin, split the view and drag an item from the
185 // inactive view to the active view by a very fast mouse movement. Result:
186 // the item gets selected instead of being dragged...
187 setState(QAbstractItemView::DraggingState
);
190 if (!index
.isValid()) {
191 if (QApplication::mouseButtons() & Qt::MidButton
) {
192 m_controller
->replaceUrlByClipboard();
194 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
195 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
200 KCategorizedView::mousePressEvent(event
);
203 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
205 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_controller
);
208 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
210 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
211 event
->acceptProposedAction();
215 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
218 setDirtyRegion(m_dropRect
);
221 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
223 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
224 const QModelIndex index
= indexAt(event
->pos());
225 setDirtyRegion(m_dropRect
);
227 m_dropRect
.setSize(QSize()); // set as invalid
228 if (index
.isValid()) {
229 const KFileItem item
= m_controller
->itemForIndex(index
);
230 if (!item
.isNull() && item
.isDir()) {
231 m_dropRect
= visualRect(index
);
233 m_dropRect
.setSize(QSize()); // set as invalid
236 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
237 // accept url drops, independently from the destination item
238 event
->acceptProposedAction();
241 setDirtyRegion(m_dropRect
);
244 void DolphinIconsView::dropEvent(QDropEvent
* event
)
246 const QModelIndex index
= indexAt(event
->pos());
247 const KFileItem item
= m_controller
->itemForIndex(index
);
248 m_controller
->indicateDroppedUrls(item
, m_controller
->url(), event
);
251 QModelIndex
DolphinIconsView::moveCursor(CursorAction cursorAction
, Qt::KeyboardModifiers modifiers
)
253 const QModelIndex oldCurrent
= currentIndex();
255 QModelIndex newCurrent
= KCategorizedView::moveCursor(cursorAction
, modifiers
);
256 if (newCurrent
!= oldCurrent
) {
260 // The cursor has not been moved by the base implementation. Provide a
261 // wrap behavior, so that the cursor will go to the next item when reaching
263 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
264 if (settings
->arrangement() == QListView::LeftToRight
) {
265 switch (cursorAction
) {
267 if (newCurrent
.row() == 0) {
270 newCurrent
= KCategorizedView::moveCursor(MoveLeft
, modifiers
);
271 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
272 newCurrent
= KCategorizedView::moveCursor(MovePageDown
, modifiers
);
276 if (newCurrent
.row() == (model()->rowCount() - 1)) {
279 newCurrent
= KCategorizedView::moveCursor(MovePageUp
, modifiers
);
280 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
281 newCurrent
= KCategorizedView::moveCursor(MoveRight
, modifiers
);
288 QModelIndex current
= oldCurrent
;
289 switch (cursorAction
) {
291 if (newCurrent
.row() == 0) {
294 newCurrent
= KCategorizedView::moveCursor(MoveUp
, modifiers
);
296 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
297 current
= newCurrent
;
298 newCurrent
= KCategorizedView::moveCursor(MoveRight
, modifiers
);
299 } while (newCurrent
!= current
);
303 if (newCurrent
.row() == (model()->rowCount() - 1)) {
307 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
308 current
= newCurrent
;
309 newCurrent
= KCategorizedView::moveCursor(MoveLeft
, modifiers
);
310 } while (newCurrent
!= current
);
311 newCurrent
= KCategorizedView::moveCursor(MoveDown
, modifiers
);
319 // Revert all changes of the current item to make sure that item selection works correctly
320 selectionModel()->setCurrentIndex(oldCurrent
, QItemSelectionModel::NoUpdate
);
324 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
326 KCategorizedView::keyPressEvent(event
);
327 m_controller
->handleKeyPressEvent(event
);
330 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
332 if (m_selectionManager
!= 0) {
333 m_selectionManager
->reset();
336 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
337 if (event
->modifiers() & Qt::ControlModifier
) {
342 horizontalScrollBar()->setSingleStep(m_itemSize
.width() / 10);
343 verticalScrollBar()->setSingleStep(m_itemSize
.height() / 10);
345 KCategorizedView::wheelEvent(event
);
346 // if the icons are aligned left to right, the vertical wheel event should
347 // be applied to the horizontal scrollbar
348 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
349 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
350 (settings
->arrangement() == QListView::LeftToRight
);
351 if (scrollHorizontal
) {
352 QWheelEvent
horizEvent(event
->pos(),
357 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
361 void DolphinIconsView::showEvent(QShowEvent
* event
)
363 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
364 delegate
->setMaximumSize(m_itemSize
);
366 KCategorizedView::showEvent(event
);
369 void DolphinIconsView::leaveEvent(QEvent
* event
)
371 KCategorizedView::leaveEvent(event
);
372 // if the mouse is above an item and moved very fast outside the widget,
373 // no viewportEntered() signal might be emitted although the mouse has been moved
374 // above the viewport
375 m_controller
->emitViewportEntered();
378 void DolphinIconsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
380 KCategorizedView::currentChanged(current
, previous
);
381 m_autoScroller
->handleCurrentIndexChange(current
, previous
);
384 void DolphinIconsView::resizeEvent(QResizeEvent
* event
)
386 KCategorizedView::resizeEvent(event
);
387 const DolphinView
* view
= m_controller
->dolphinView();
388 updateGridSize(view
->showPreview(), view
->additionalInfo().count());
391 void DolphinIconsView::slotShowPreviewChanged()
393 const DolphinView
* view
= m_controller
->dolphinView();
394 updateGridSize(view
->showPreview(), additionalInfoCount());
397 void DolphinIconsView::slotAdditionalInfoChanged()
399 const DolphinView
* view
= m_controller
->dolphinView();
400 const bool showPreview
= view
->showPreview();
401 updateGridSize(showPreview
, view
->additionalInfo().count());
404 void DolphinIconsView::setNameFilter(const QString
& nameFilter
)
406 DolphinSortFilterProxyModel
* proxyModel
= static_cast<DolphinSortFilterProxyModel
*>(model());
407 proxyModel
->setFilterRegExp(nameFilter
);
410 void DolphinIconsView::setZoomLevel(int level
)
412 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
414 const int oldIconSize
= settings
->iconSize();
415 int newIconSize
= oldIconSize
;
417 const bool showPreview
= m_controller
->dolphinView()->showPreview();
419 const int previewSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
420 settings
->setPreviewSize(previewSize
);
422 newIconSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
423 settings
->setIconSize(newIconSize
);
426 // increase also the grid size
427 const int diff
= newIconSize
- oldIconSize
;
428 settings
->setItemWidth(settings
->itemWidth() + diff
);
429 settings
->setItemHeight(settings
->itemHeight() + diff
);
431 updateGridSize(showPreview
, additionalInfoCount());
434 void DolphinIconsView::requestActivation()
436 m_controller
->requestActivation();
439 void DolphinIconsView::slotGlobalSettingsChanged(int category
)
443 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
444 Q_ASSERT(settings
!= 0);
445 if (settings
->useSystemFont()) {
446 m_font
= KGlobalSettings::generalFont();
449 disconnect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
450 disconnect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
451 if (KGlobalSettings::singleClick()) {
452 connect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
454 connect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
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
* m_font
.pointSize() * 2;
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
);
526 if (m_selectionManager
!= 0) {
527 m_selectionManager
->reset();
531 int DolphinIconsView::additionalInfoCount() const
533 const DolphinView
* view
= m_controller
->dolphinView();
534 return view
->additionalInfo().count();
537 #include "dolphiniconsview.moc"