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),
53 m_decorationPosition(QStyleOptionViewItem::Top
),
54 m_displayAlignment(Qt::AlignHCenter
),
58 Q_ASSERT(controller
!= 0);
60 setLayoutDirection(Qt::LeftToRight
);
61 setViewMode(QListView::IconMode
);
62 setResizeMode(QListView::Adjust
);
63 setMovement(QListView::Static
);
65 setEditTriggers(QAbstractItemView::NoEditTriggers
);
66 viewport()->setAcceptDrops(true);
68 setMouseTracking(true);
69 m_autoScroller
= new DolphinViewAutoScroller(this);
71 connect(this, SIGNAL(clicked(const QModelIndex
&)),
72 controller
, SLOT(requestTab(const QModelIndex
&)));
73 if (KGlobalSettings::singleClick()) {
74 connect(this, SIGNAL(clicked(const QModelIndex
&)),
75 controller
, SLOT(triggerItem(const QModelIndex
&)));
77 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
78 controller
, SLOT(triggerItem(const QModelIndex
&)));
81 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
82 m_selectionManager
= new SelectionManager(this);
83 connect(m_selectionManager
, SIGNAL(selectionChanged()),
84 this, SLOT(requestActivation()));
85 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
86 m_selectionManager
, SLOT(reset()));
89 connect(this, SIGNAL(entered(const QModelIndex
&)),
90 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
91 connect(this, SIGNAL(viewportEntered()),
92 controller
, SLOT(emitViewportEntered()));
93 connect(controller
, SIGNAL(nameFilterChanged(const QString
&)),
94 this, SLOT(setNameFilter(const QString
&)));
95 connect(controller
, SIGNAL(zoomLevelChanged(int)),
96 this, SLOT(setZoomLevel(int)));
98 const DolphinView
* view
= controller
->dolphinView();
99 connect(view
, SIGNAL(showPreviewChanged()),
100 this, SLOT(slotShowPreviewChanged()));
101 connect(view
, SIGNAL(additionalInfoChanged()),
102 this, SLOT(slotAdditionalInfoChanged()));
104 // apply the icons mode settings to the widget
105 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
106 Q_ASSERT(settings
!= 0);
108 if (settings
->useSystemFont()) {
109 m_font
= KGlobalSettings::generalFont();
111 m_font
= QFont(settings
->fontFamily(),
112 settings
->fontSize(),
113 settings
->fontWeight(),
114 settings
->italicFont());
117 setWordWrap(settings
->numberOfTextlines() > 1);
118 updateGridSize(view
->showPreview(), 0);
120 if (settings
->arrangement() == QListView::TopToBottom
) {
121 setFlow(QListView::LeftToRight
);
122 m_decorationPosition
= QStyleOptionViewItem::Top
;
123 m_displayAlignment
= Qt::AlignHCenter
;
125 setFlow(QListView::TopToBottom
);
126 m_decorationPosition
= QStyleOptionViewItem::Left
;
127 m_displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
130 m_categoryDrawer
= new DolphinCategoryDrawer();
131 setCategoryDrawer(m_categoryDrawer
);
135 connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)),
136 this, SLOT(slotGlobalSettingsChanged(int)));
138 new ViewExtensionsFactory(this, controller
);
141 DolphinIconsView::~DolphinIconsView()
143 delete m_categoryDrawer
;
144 m_categoryDrawer
= 0;
147 void DolphinIconsView::dataChanged(const QModelIndex
& topLeft
, const QModelIndex
& bottomRight
)
149 KCategorizedView::dataChanged(topLeft
, bottomRight
);
151 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
152 if (!proxyModel
->isCategorizedModel()) {
153 // bypass a QListView issue that items are not layout correctly if the decoration size of
155 scheduleDelayedItemsLayout();
159 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
161 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
162 viewOptions
.font
= m_font
;
163 viewOptions
.decorationPosition
= m_decorationPosition
;
164 viewOptions
.decorationSize
= m_decorationSize
;
165 viewOptions
.displayAlignment
= m_displayAlignment
;
166 viewOptions
.showDecorationSelected
= true;
170 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
172 KCategorizedView::contextMenuEvent(event
);
173 m_controller
->triggerContextMenuRequest(event
->pos());
176 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
178 m_controller
->requestActivation();
179 const QModelIndex index
= indexAt(event
->pos());
180 if (index
.isValid() && (event
->button() == Qt::LeftButton
)) {
181 // TODO: It should not be necessary to manually set the dragging state, but I could
182 // not reproduce this issue with a Qt-only example yet to find the root cause.
183 // Issue description: start Dolphin, split the view and drag an item from the
184 // inactive view to the active view by a very fast mouse movement. Result:
185 // the item gets selected instead of being dragged...
186 setState(QAbstractItemView::DraggingState
);
189 if (!index
.isValid()) {
190 if (QApplication::mouseButtons() & Qt::MidButton
) {
191 m_controller
->replaceUrlByClipboard();
193 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
194 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
199 KCategorizedView::mousePressEvent(event
);
202 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
204 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_controller
);
207 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
209 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
210 event
->acceptProposedAction();
214 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
217 setDirtyRegion(m_dropRect
);
220 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
222 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
223 const QModelIndex index
= indexAt(event
->pos());
224 setDirtyRegion(m_dropRect
);
226 m_dropRect
.setSize(QSize()); // set as invalid
227 if (index
.isValid()) {
228 const KFileItem item
= m_controller
->itemForIndex(index
);
229 if (!item
.isNull() && item
.isDir()) {
230 m_dropRect
= visualRect(index
);
232 m_dropRect
.setSize(QSize()); // set as invalid
235 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
236 // accept url drops, independently from the destination item
237 event
->acceptProposedAction();
240 setDirtyRegion(m_dropRect
);
243 void DolphinIconsView::dropEvent(QDropEvent
* event
)
245 const QModelIndex index
= indexAt(event
->pos());
246 const KFileItem item
= m_controller
->itemForIndex(index
);
247 m_controller
->indicateDroppedUrls(item
, m_controller
->url(), event
);
250 QModelIndex
DolphinIconsView::moveCursor(CursorAction cursorAction
, Qt::KeyboardModifiers modifiers
)
252 const QModelIndex oldCurrent
= currentIndex();
254 QModelIndex newCurrent
= KCategorizedView::moveCursor(cursorAction
, modifiers
);
255 if (newCurrent
!= oldCurrent
) {
259 // The cursor has not been moved by the base implementation. Provide a
260 // wrap behavior, so that the cursor will go to the next item when reaching
262 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
263 if (settings
->arrangement() == QListView::LeftToRight
) {
264 switch (cursorAction
) {
266 if (newCurrent
.row() == 0) {
269 newCurrent
= KCategorizedView::moveCursor(MoveLeft
, modifiers
);
270 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
271 newCurrent
= KCategorizedView::moveCursor(MovePageDown
, modifiers
);
275 if (newCurrent
.row() == (model()->rowCount() - 1)) {
278 newCurrent
= KCategorizedView::moveCursor(MovePageUp
, modifiers
);
279 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
280 newCurrent
= KCategorizedView::moveCursor(MoveRight
, modifiers
);
287 QModelIndex current
= oldCurrent
;
288 switch (cursorAction
) {
290 if (newCurrent
.row() == 0) {
293 newCurrent
= KCategorizedView::moveCursor(MoveUp
, modifiers
);
295 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
296 current
= newCurrent
;
297 newCurrent
= KCategorizedView::moveCursor(MoveRight
, modifiers
);
298 } while (newCurrent
!= current
);
302 if (newCurrent
.row() == (model()->rowCount() - 1)) {
306 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
307 current
= newCurrent
;
308 newCurrent
= KCategorizedView::moveCursor(MoveLeft
, modifiers
);
309 } while (newCurrent
!= current
);
310 newCurrent
= KCategorizedView::moveCursor(MoveDown
, modifiers
);
318 // Revert all changes of the current item to make sure that item selection works correctly
319 selectionModel()->setCurrentIndex(oldCurrent
, QItemSelectionModel::NoUpdate
);
323 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
325 KCategorizedView::keyPressEvent(event
);
326 m_controller
->handleKeyPressEvent(event
);
329 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
331 if (m_selectionManager
!= 0) {
332 m_selectionManager
->reset();
335 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
336 if (event
->modifiers() & Qt::ControlModifier
) {
341 horizontalScrollBar()->setSingleStep(m_itemSize
.width() / 10);
342 verticalScrollBar()->setSingleStep(m_itemSize
.height() / 10);
344 KCategorizedView::wheelEvent(event
);
345 // if the icons are aligned left to right, the vertical wheel event should
346 // be applied to the horizontal scrollbar
347 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
348 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
349 (settings
->arrangement() == QListView::LeftToRight
);
350 if (scrollHorizontal
) {
351 QWheelEvent
horizEvent(event
->pos(),
356 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
360 void DolphinIconsView::showEvent(QShowEvent
* event
)
362 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
363 delegate
->setMaximumSize(m_itemSize
);
365 KCategorizedView::showEvent(event
);
368 void DolphinIconsView::leaveEvent(QEvent
* event
)
370 KCategorizedView::leaveEvent(event
);
371 // if the mouse is above an item and moved very fast outside the widget,
372 // no viewportEntered() signal might be emitted although the mouse has been moved
373 // above the viewport
374 m_controller
->emitViewportEntered();
377 void DolphinIconsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
379 KCategorizedView::currentChanged(current
, previous
);
380 m_autoScroller
->handleCurrentIndexChange(current
, previous
);
383 void DolphinIconsView::resizeEvent(QResizeEvent
* event
)
385 KCategorizedView::resizeEvent(event
);
386 const DolphinView
* view
= m_controller
->dolphinView();
387 updateGridSize(view
->showPreview(), view
->additionalInfo().count());
390 void DolphinIconsView::slotShowPreviewChanged()
392 const DolphinView
* view
= m_controller
->dolphinView();
393 updateGridSize(view
->showPreview(), additionalInfoCount());
396 void DolphinIconsView::slotAdditionalInfoChanged()
398 const DolphinView
* view
= m_controller
->dolphinView();
399 const bool showPreview
= view
->showPreview();
400 updateGridSize(showPreview
, view
->additionalInfo().count());
403 void DolphinIconsView::setNameFilter(const QString
& nameFilter
)
405 DolphinSortFilterProxyModel
* proxyModel
= static_cast<DolphinSortFilterProxyModel
*>(model());
406 proxyModel
->setFilterRegExp(nameFilter
);
409 void DolphinIconsView::setZoomLevel(int level
)
411 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
413 const int oldIconSize
= settings
->iconSize();
414 int newIconSize
= oldIconSize
;
416 const bool showPreview
= m_controller
->dolphinView()->showPreview();
418 const int previewSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
419 settings
->setPreviewSize(previewSize
);
421 newIconSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
422 settings
->setIconSize(newIconSize
);
425 // increase also the grid size
426 const int diff
= newIconSize
- oldIconSize
;
427 settings
->setItemWidth(settings
->itemWidth() + diff
);
428 settings
->setItemHeight(settings
->itemHeight() + diff
);
430 updateGridSize(showPreview
, additionalInfoCount());
433 void DolphinIconsView::requestActivation()
435 m_controller
->requestActivation();
438 void DolphinIconsView::slotGlobalSettingsChanged(int category
)
442 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
443 Q_ASSERT(settings
!= 0);
444 if (settings
->useSystemFont()) {
445 m_font
= KGlobalSettings::generalFont();
448 disconnect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
449 disconnect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
450 if (KGlobalSettings::singleClick()) {
451 connect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
453 connect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
457 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
459 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
460 Q_ASSERT(settings
!= 0);
462 int itemWidth
= settings
->itemWidth();
463 int itemHeight
= settings
->itemHeight();
464 int size
= settings
->iconSize();
467 const int previewSize
= settings
->previewSize();
468 const int diff
= previewSize
- size
;
475 Q_ASSERT(additionalInfoCount
>= 0);
476 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
478 // Optimize the item size of the grid in a way to prevent large gaps on the
479 // right border (= row arrangement) or the bottom border (= column arrangement).
480 // There is no public API in QListView to find out the used width of the viewport
481 // for the layout. The following calculation of 'contentWidth'/'contentHeight'
482 // is based on QListViewPrivate::prepareItemsLayout() (Copyright (C) 2009 Nokia Corporation).
483 int frameAroundContents
= 0;
484 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents
)) {
485 frameAroundContents
= style()->pixelMetric(QStyle::PM_DefaultFrameWidth
) * 2;
487 const int spacing
= settings
->gridSpacing();
488 if (settings
->arrangement() == QListView::TopToBottom
) {
489 const int contentWidth
= viewport()->width() - 1
490 - frameAroundContents
491 - style()->pixelMetric(QStyle::PM_ScrollBarExtent
, 0, horizontalScrollBar());
492 const int gridWidth
= itemWidth
+ spacing
* 2;
493 const int horizItemCount
= contentWidth
/ gridWidth
;
494 if (horizItemCount
> 0) {
495 itemWidth
+= (contentWidth
- horizItemCount
* gridWidth
) / horizItemCount
;
498 // The decoration width indirectly defines the maximum
499 // width for the text wrapping. To use the maximum item width
500 // for text wrapping, it is used as decoration width.
501 m_decorationSize
= QSize(itemWidth
, size
);
502 setIconSize(QSize(itemWidth
, size
));
504 const int contentHeight
= viewport()->height() - 1
505 - frameAroundContents
506 - style()->pixelMetric(QStyle::PM_ScrollBarExtent
, 0, verticalScrollBar());
507 const int gridHeight
= itemHeight
+ spacing
;
508 const int vertItemCount
= contentHeight
/ gridHeight
;
509 if (vertItemCount
> 0) {
510 itemHeight
+= (contentHeight
- vertItemCount
* gridHeight
) / vertItemCount
;
513 m_decorationSize
= QSize(size
, size
);
514 setIconSize(QSize(size
, size
));
517 m_itemSize
= QSize(itemWidth
, itemHeight
);
518 setGridSizeOwn(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
520 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
522 delegate
->setMaximumSize(m_itemSize
);
525 if (m_selectionManager
!= 0) {
526 m_selectionManager
->reset();
530 int DolphinIconsView::additionalInfoCount() const
532 const DolphinView
* view
= m_controller
->dolphinView();
533 return view
->additionalInfo().count();
536 #include "dolphiniconsview.moc"