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 <kdirmodel.h>
36 #include <kfileitemdelegate.h>
38 #include <QAbstractProxyModel>
39 #include <QApplication>
42 DolphinIconsView::DolphinIconsView(QWidget
* parent
,
43 DolphinController
* controller
,
44 DolphinSortFilterProxyModel
* proxyModel
) :
45 KCategorizedView(parent
),
46 m_controller(controller
),
48 m_extensionsFactory(0),
51 m_decorationPosition(QStyleOptionViewItem::Top
),
52 m_displayAlignment(Qt::AlignHCenter
),
56 Q_ASSERT(controller
!= 0);
58 setLayoutDirection(Qt::LeftToRight
);
59 setViewMode(QListView::IconMode
);
60 setResizeMode(QListView::Adjust
);
61 setMovement(QListView::Static
);
63 setEditTriggers(QAbstractItemView::NoEditTriggers
);
64 viewport()->setAcceptDrops(true);
66 setMouseTracking(true);
68 connect(this, SIGNAL(clicked(const QModelIndex
&)),
69 controller
, SLOT(requestTab(const QModelIndex
&)));
70 if (KGlobalSettings::singleClick()) {
71 connect(this, SIGNAL(clicked(const QModelIndex
&)),
72 controller
, SLOT(triggerItem(const QModelIndex
&)));
74 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
75 controller
, SLOT(triggerItem(const QModelIndex
&)));
78 connect(this, SIGNAL(entered(const QModelIndex
&)),
79 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
80 connect(this, SIGNAL(viewportEntered()),
81 controller
, SLOT(emitViewportEntered()));
82 connect(controller
, SIGNAL(nameFilterChanged(const QString
&)),
83 this, SLOT(setNameFilter(const QString
&)));
84 connect(controller
, SIGNAL(zoomLevelChanged(int)),
85 this, SLOT(setZoomLevel(int)));
87 const DolphinView
* view
= controller
->dolphinView();
88 connect(view
, SIGNAL(showPreviewChanged()),
89 this, SLOT(slotShowPreviewChanged()));
90 connect(view
, SIGNAL(additionalInfoChanged()),
91 this, SLOT(slotAdditionalInfoChanged()));
93 // apply the icons mode settings to the widget
94 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
95 Q_ASSERT(settings
!= 0);
97 if (settings
->useSystemFont()) {
98 m_font
= KGlobalSettings::generalFont();
100 m_font
= QFont(settings
->fontFamily(),
101 settings
->fontSize(),
102 settings
->fontWeight(),
103 settings
->italicFont());
106 setWordWrap(settings
->numberOfTextlines() > 1);
108 if (settings
->arrangement() == QListView::TopToBottom
) {
109 setFlow(QListView::LeftToRight
);
110 m_decorationPosition
= QStyleOptionViewItem::Top
;
111 m_displayAlignment
= Qt::AlignHCenter
;
113 setFlow(QListView::TopToBottom
);
114 m_decorationPosition
= QStyleOptionViewItem::Left
;
115 m_displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
118 m_categoryDrawer
= new DolphinCategoryDrawer();
119 setCategoryDrawer(m_categoryDrawer
);
123 connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)),
124 this, SLOT(slotGlobalSettingsChanged(int)));
126 m_extensionsFactory
= new ViewExtensionsFactory(this, controller
);
127 updateGridSize(view
->showPreview(), 0);
130 DolphinIconsView::~DolphinIconsView()
132 delete m_categoryDrawer
;
133 m_categoryDrawer
= 0;
136 void DolphinIconsView::dataChanged(const QModelIndex
& topLeft
, const QModelIndex
& bottomRight
)
138 KCategorizedView::dataChanged(topLeft
, bottomRight
);
140 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
141 if (!proxyModel
->isCategorizedModel()) {
142 // bypass a QListView issue that items are not layout correctly if the decoration size of
144 scheduleDelayedItemsLayout();
148 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
150 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
151 viewOptions
.font
= m_font
;
152 viewOptions
.decorationPosition
= m_decorationPosition
;
153 viewOptions
.decorationSize
= m_decorationSize
;
154 viewOptions
.displayAlignment
= m_displayAlignment
;
155 viewOptions
.showDecorationSelected
= true;
159 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
161 KCategorizedView::contextMenuEvent(event
);
162 m_controller
->triggerContextMenuRequest(event
->pos());
165 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
167 m_controller
->requestActivation();
168 const QModelIndex index
= indexAt(event
->pos());
169 if (index
.isValid() && (event
->button() == Qt::LeftButton
)) {
170 // TODO: It should not be necessary to manually set the dragging state, but I could
171 // not reproduce this issue with a Qt-only example yet to find the root cause.
172 // Issue description: start Dolphin, split the view and drag an item from the
173 // inactive view to the active view by a very fast mouse movement. Result:
174 // the item gets selected instead of being dragged...
175 setState(QAbstractItemView::DraggingState
);
178 if (!index
.isValid()) {
179 if (QApplication::mouseButtons() & Qt::MidButton
) {
180 m_controller
->replaceUrlByClipboard();
182 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
183 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
188 KCategorizedView::mousePressEvent(event
);
191 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
193 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_controller
);
196 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
198 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
199 event
->acceptProposedAction();
203 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
206 setDirtyRegion(m_dropRect
);
209 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
211 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
212 const QModelIndex index
= indexAt(event
->pos());
213 setDirtyRegion(m_dropRect
);
215 m_dropRect
.setSize(QSize()); // set as invalid
216 if (index
.isValid()) {
217 const KFileItem item
= m_controller
->itemForIndex(index
);
218 if (!item
.isNull() && item
.isDir()) {
219 m_dropRect
= visualRect(index
);
221 m_dropRect
.setSize(QSize()); // set as invalid
224 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
225 // accept url drops, independently from the destination item
226 event
->acceptProposedAction();
229 setDirtyRegion(m_dropRect
);
232 void DolphinIconsView::dropEvent(QDropEvent
* event
)
234 const QModelIndex index
= indexAt(event
->pos());
235 const KFileItem item
= m_controller
->itemForIndex(index
);
236 m_controller
->indicateDroppedUrls(item
, m_controller
->url(), event
);
239 QModelIndex
DolphinIconsView::moveCursor(CursorAction cursorAction
, Qt::KeyboardModifiers modifiers
)
241 const QModelIndex oldCurrent
= currentIndex();
243 QModelIndex newCurrent
= KCategorizedView::moveCursor(cursorAction
, modifiers
);
244 if (newCurrent
!= oldCurrent
) {
248 // The cursor has not been moved by the base implementation. Provide a
249 // wrap behavior, so that the cursor will go to the next item when reaching
251 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
252 if (settings
->arrangement() == QListView::LeftToRight
) {
253 switch (cursorAction
) {
255 if (newCurrent
.row() == 0) {
258 newCurrent
= KCategorizedView::moveCursor(MoveLeft
, modifiers
);
259 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
260 newCurrent
= KCategorizedView::moveCursor(MovePageDown
, modifiers
);
264 if (newCurrent
.row() == (model()->rowCount() - 1)) {
267 newCurrent
= KCategorizedView::moveCursor(MovePageUp
, modifiers
);
268 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
269 newCurrent
= KCategorizedView::moveCursor(MoveRight
, modifiers
);
276 QModelIndex current
= oldCurrent
;
277 switch (cursorAction
) {
279 if (newCurrent
.row() == 0) {
282 newCurrent
= KCategorizedView::moveCursor(MoveUp
, modifiers
);
284 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
285 current
= newCurrent
;
286 newCurrent
= KCategorizedView::moveCursor(MoveRight
, modifiers
);
287 } while (newCurrent
!= current
);
291 if (newCurrent
.row() == (model()->rowCount() - 1)) {
295 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
296 current
= newCurrent
;
297 newCurrent
= KCategorizedView::moveCursor(MoveLeft
, modifiers
);
298 } while (newCurrent
!= current
);
299 newCurrent
= KCategorizedView::moveCursor(MoveDown
, modifiers
);
307 // Revert all changes of the current item to make sure that item selection works correctly
308 selectionModel()->setCurrentIndex(oldCurrent
, QItemSelectionModel::NoUpdate
);
312 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
314 KCategorizedView::keyPressEvent(event
);
315 m_controller
->handleKeyPressEvent(event
);
318 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
320 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
321 if (event
->modifiers() & Qt::ControlModifier
) {
326 horizontalScrollBar()->setSingleStep(m_itemSize
.width() / 10);
327 verticalScrollBar()->setSingleStep(m_itemSize
.height() / 10);
329 KCategorizedView::wheelEvent(event
);
330 // if the icons are aligned left to right, the vertical wheel event should
331 // be applied to the horizontal scrollbar
332 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
333 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
334 (settings
->arrangement() == QListView::LeftToRight
);
335 if (scrollHorizontal
) {
336 QWheelEvent
horizEvent(event
->pos(),
341 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
345 void DolphinIconsView::showEvent(QShowEvent
* event
)
347 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
348 delegate
->setMaximumSize(m_itemSize
);
350 KCategorizedView::showEvent(event
);
353 void DolphinIconsView::leaveEvent(QEvent
* event
)
355 KCategorizedView::leaveEvent(event
);
356 // if the mouse is above an item and moved very fast outside the widget,
357 // no viewportEntered() signal might be emitted although the mouse has been moved
358 // above the viewport
359 m_controller
->emitViewportEntered();
362 void DolphinIconsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
364 KCategorizedView::currentChanged(current
, previous
);
365 m_extensionsFactory
->handleCurrentIndexChange(current
, previous
);
368 void DolphinIconsView::resizeEvent(QResizeEvent
* event
)
370 KCategorizedView::resizeEvent(event
);
371 const DolphinView
* view
= m_controller
->dolphinView();
372 updateGridSize(view
->showPreview(), view
->additionalInfo().count());
375 void DolphinIconsView::slotShowPreviewChanged()
377 const DolphinView
* view
= m_controller
->dolphinView();
378 updateGridSize(view
->showPreview(), additionalInfoCount());
381 void DolphinIconsView::slotAdditionalInfoChanged()
383 const DolphinView
* view
= m_controller
->dolphinView();
384 const bool showPreview
= view
->showPreview();
385 updateGridSize(showPreview
, view
->additionalInfo().count());
388 void DolphinIconsView::setNameFilter(const QString
& nameFilter
)
390 DolphinSortFilterProxyModel
* proxyModel
= static_cast<DolphinSortFilterProxyModel
*>(model());
391 proxyModel
->setFilterRegExp(nameFilter
);
394 void DolphinIconsView::setZoomLevel(int level
)
396 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
398 const int oldIconSize
= settings
->iconSize();
399 int newIconSize
= oldIconSize
;
401 const bool showPreview
= m_controller
->dolphinView()->showPreview();
403 const int previewSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
404 settings
->setPreviewSize(previewSize
);
406 newIconSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
407 settings
->setIconSize(newIconSize
);
410 // increase also the grid size
411 const int diff
= newIconSize
- oldIconSize
;
412 settings
->setItemWidth(settings
->itemWidth() + diff
);
413 settings
->setItemHeight(settings
->itemHeight() + diff
);
415 updateGridSize(showPreview
, additionalInfoCount());
418 void DolphinIconsView::requestActivation()
420 m_controller
->requestActivation();
423 void DolphinIconsView::slotGlobalSettingsChanged(int category
)
427 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
428 Q_ASSERT(settings
!= 0);
429 if (settings
->useSystemFont()) {
430 m_font
= KGlobalSettings::generalFont();
433 disconnect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
434 disconnect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
435 if (KGlobalSettings::singleClick()) {
436 connect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
438 connect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
442 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
444 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
445 Q_ASSERT(settings
!= 0);
447 int itemWidth
= settings
->itemWidth();
448 int itemHeight
= settings
->itemHeight();
449 int size
= settings
->iconSize();
452 const int previewSize
= settings
->previewSize();
453 const int diff
= previewSize
- size
;
460 Q_ASSERT(additionalInfoCount
>= 0);
461 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
463 // Optimize the item size of the grid in a way to prevent large gaps on the
464 // right border (= row arrangement) or the bottom border (= column arrangement).
465 // There is no public API in QListView to find out the used width of the viewport
466 // for the layout. The following calculation of 'contentWidth'/'contentHeight'
467 // is based on QListViewPrivate::prepareItemsLayout() (Copyright (C) 2009 Nokia Corporation).
468 int frameAroundContents
= 0;
469 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents
)) {
470 frameAroundContents
= style()->pixelMetric(QStyle::PM_DefaultFrameWidth
) * 2;
472 const int spacing
= settings
->gridSpacing();
473 if (settings
->arrangement() == QListView::TopToBottom
) {
474 const int contentWidth
= viewport()->width() - 1
475 - frameAroundContents
476 - style()->pixelMetric(QStyle::PM_ScrollBarExtent
, 0, horizontalScrollBar());
477 const int gridWidth
= itemWidth
+ spacing
* 2;
478 const int horizItemCount
= contentWidth
/ gridWidth
;
479 if (horizItemCount
> 0) {
480 itemWidth
+= (contentWidth
- horizItemCount
* gridWidth
) / horizItemCount
;
483 // The decoration width indirectly defines the maximum
484 // width for the text wrapping. To use the maximum item width
485 // for text wrapping, it is used as decoration width.
486 m_decorationSize
= QSize(itemWidth
, size
);
487 setIconSize(QSize(itemWidth
, size
));
489 const int contentHeight
= viewport()->height() - 1
490 - frameAroundContents
491 - style()->pixelMetric(QStyle::PM_ScrollBarExtent
, 0, verticalScrollBar());
492 const int gridHeight
= itemHeight
+ spacing
;
493 const int vertItemCount
= contentHeight
/ gridHeight
;
494 if (vertItemCount
> 0) {
495 itemHeight
+= (contentHeight
- vertItemCount
* gridHeight
) / vertItemCount
;
498 m_decorationSize
= QSize(size
, size
);
499 setIconSize(QSize(size
, size
));
502 m_itemSize
= QSize(itemWidth
, itemHeight
);
503 setGridSizeOwn(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
505 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
507 delegate
->setMaximumSize(m_itemSize
);
511 int DolphinIconsView::additionalInfoCount() const
513 const DolphinView
* view
= m_controller
->dolphinView();
514 return view
->additionalInfo().count();
517 #include "dolphiniconsview.moc"