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 "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
, DolphinController
* controller
) :
43 KCategorizedView(parent
),
44 m_controller(controller
),
45 m_selectionManager(0),
50 m_decorationPosition(QStyleOptionViewItem::Top
),
51 m_displayAlignment(Qt::AlignHCenter
),
55 Q_ASSERT(controller
!= 0);
56 setLayoutDirection(Qt::LeftToRight
);
57 setViewMode(QListView::IconMode
);
58 setResizeMode(QListView::Adjust
);
59 setMovement(QListView::Static
);
61 setEditTriggers(QAbstractItemView::NoEditTriggers
);
62 viewport()->setAcceptDrops(true);
64 setMouseTracking(true);
65 m_autoScroller
= new DolphinViewAutoScroller(this);
67 connect(this, SIGNAL(clicked(const QModelIndex
&)),
68 controller
, SLOT(requestTab(const QModelIndex
&)));
69 if (KGlobalSettings::singleClick()) {
70 connect(this, SIGNAL(clicked(const QModelIndex
&)),
71 controller
, SLOT(triggerItem(const QModelIndex
&)));
73 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
74 controller
, SLOT(triggerItem(const QModelIndex
&)));
77 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
78 m_selectionManager
= new SelectionManager(this);
79 connect(m_selectionManager
, SIGNAL(selectionChanged()),
80 this, SLOT(requestActivation()));
81 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
82 m_selectionManager
, SLOT(reset()));
85 connect(this, SIGNAL(entered(const QModelIndex
&)),
86 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
87 connect(this, SIGNAL(viewportEntered()),
88 controller
, SLOT(emitViewportEntered()));
89 connect(controller
, SIGNAL(nameFilterChanged(const QString
&)),
90 this, SLOT(setNameFilter(const QString
&)));
91 connect(controller
, SIGNAL(zoomLevelChanged(int)),
92 this, SLOT(setZoomLevel(int)));
94 const DolphinView
* view
= controller
->dolphinView();
95 connect(view
, SIGNAL(showPreviewChanged()),
96 this, SLOT(slotShowPreviewChanged()));
97 connect(view
, SIGNAL(additionalInfoChanged()),
98 this, SLOT(slotAdditionalInfoChanged()));
100 // apply the icons mode settings to the widget
101 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
102 Q_ASSERT(settings
!= 0);
104 if (settings
->useSystemFont()) {
105 m_font
= KGlobalSettings::generalFont();
107 m_font
= QFont(settings
->fontFamily(),
108 settings
->fontSize(),
109 settings
->fontWeight(),
110 settings
->italicFont());
113 setWordWrap(settings
->numberOfTextlines() > 1);
114 updateGridSize(view
->showPreview(), 0);
116 if (settings
->arrangement() == QListView::TopToBottom
) {
117 setFlow(QListView::LeftToRight
);
118 m_decorationPosition
= QStyleOptionViewItem::Top
;
119 m_displayAlignment
= Qt::AlignHCenter
;
121 setFlow(QListView::TopToBottom
);
122 m_decorationPosition
= QStyleOptionViewItem::Left
;
123 m_displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
126 m_categoryDrawer
= new DolphinCategoryDrawer();
127 setCategoryDrawer(m_categoryDrawer
);
131 connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)),
132 this, SLOT(slotGlobalSettingsChanged(int)));
135 DolphinIconsView::~DolphinIconsView()
137 delete m_categoryDrawer
;
138 m_categoryDrawer
= 0;
141 void DolphinIconsView::dataChanged(const QModelIndex
& topLeft
, const QModelIndex
& bottomRight
)
143 KCategorizedView::dataChanged(topLeft
, bottomRight
);
145 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
146 if (!proxyModel
->isCategorizedModel()) {
147 // bypass a QListView issue that items are not layout correctly if the decoration size of
149 scheduleDelayedItemsLayout();
153 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
155 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
156 viewOptions
.font
= m_font
;
157 viewOptions
.decorationPosition
= m_decorationPosition
;
158 viewOptions
.decorationSize
= m_decorationSize
;
159 viewOptions
.displayAlignment
= m_displayAlignment
;
160 viewOptions
.showDecorationSelected
= true;
164 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
166 KCategorizedView::contextMenuEvent(event
);
167 m_controller
->triggerContextMenuRequest(event
->pos());
170 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
172 m_controller
->requestActivation();
173 const QModelIndex index
= indexAt(event
->pos());
174 if (index
.isValid() && (event
->button() == Qt::LeftButton
)) {
175 // TODO: It should not be necessary to manually set the dragging state, but I could
176 // not reproduce this issue with a Qt-only example yet to find the root cause.
177 // Issue description: start Dolphin, split the view and drag an item from the
178 // inactive view to the active view by a very fast mouse movement. Result:
179 // the item gets selected instead of being dragged...
180 setState(QAbstractItemView::DraggingState
);
183 if (!index
.isValid()) {
184 if (QApplication::mouseButtons() & Qt::MidButton
) {
185 m_controller
->replaceUrlByClipboard();
187 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
188 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
193 KCategorizedView::mousePressEvent(event
);
196 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
198 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_controller
);
201 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
203 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
204 event
->acceptProposedAction();
208 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
211 setDirtyRegion(m_dropRect
);
214 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
216 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
217 const QModelIndex index
= indexAt(event
->pos());
218 setDirtyRegion(m_dropRect
);
220 m_dropRect
.setSize(QSize()); // set as invalid
221 if (index
.isValid()) {
222 const KFileItem item
= m_controller
->itemForIndex(index
);
223 if (!item
.isNull() && item
.isDir()) {
224 m_dropRect
= visualRect(index
);
226 m_dropRect
.setSize(QSize()); // set as invalid
229 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
230 // accept url drops, independently from the destination item
231 event
->acceptProposedAction();
234 setDirtyRegion(m_dropRect
);
237 void DolphinIconsView::dropEvent(QDropEvent
* event
)
239 const QModelIndex index
= indexAt(event
->pos());
240 const KFileItem item
= m_controller
->itemForIndex(index
);
241 m_controller
->indicateDroppedUrls(item
, m_controller
->url(), event
);
244 QModelIndex
DolphinIconsView::moveCursor(CursorAction cursorAction
, Qt::KeyboardModifiers modifiers
)
246 const QModelIndex oldCurrent
= currentIndex();
248 QModelIndex newCurrent
= KCategorizedView::moveCursor(cursorAction
, modifiers
);
249 if (newCurrent
!= oldCurrent
) {
253 // The cursor has not been moved by the base implementation. Provide a
254 // wrap behavior, so that the cursor will go to the next item when reaching
256 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
257 if (settings
->arrangement() == QListView::LeftToRight
) {
258 switch (cursorAction
) {
260 if (newCurrent
.row() == 0) {
263 newCurrent
= KCategorizedView::moveCursor(MoveLeft
, modifiers
);
264 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
265 newCurrent
= KCategorizedView::moveCursor(MovePageDown
, modifiers
);
269 if (newCurrent
.row() == (model()->rowCount() - 1)) {
272 newCurrent
= KCategorizedView::moveCursor(MovePageUp
, modifiers
);
273 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
274 newCurrent
= KCategorizedView::moveCursor(MoveRight
, modifiers
);
281 QModelIndex current
= oldCurrent
;
282 switch (cursorAction
) {
284 if (newCurrent
.row() == 0) {
287 newCurrent
= KCategorizedView::moveCursor(MoveUp
, modifiers
);
289 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
290 current
= newCurrent
;
291 newCurrent
= KCategorizedView::moveCursor(MoveRight
, modifiers
);
292 } while (newCurrent
!= current
);
296 if (newCurrent
.row() == (model()->rowCount() - 1)) {
300 selectionModel()->setCurrentIndex(newCurrent
, QItemSelectionModel::NoUpdate
);
301 current
= newCurrent
;
302 newCurrent
= KCategorizedView::moveCursor(MoveLeft
, modifiers
);
303 } while (newCurrent
!= current
);
304 newCurrent
= KCategorizedView::moveCursor(MoveDown
, modifiers
);
312 // Revert all changes of the current item to make sure that item selection works correctly
313 selectionModel()->setCurrentIndex(oldCurrent
, QItemSelectionModel::NoUpdate
);
317 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
319 KCategorizedView::keyPressEvent(event
);
320 m_controller
->handleKeyPressEvent(event
);
323 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
325 if (m_selectionManager
!= 0) {
326 m_selectionManager
->reset();
329 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
330 if (event
->modifiers() & Qt::ControlModifier
) {
335 horizontalScrollBar()->setSingleStep(m_itemSize
.width() / 10);
336 verticalScrollBar()->setSingleStep(m_itemSize
.height() / 10);
338 KCategorizedView::wheelEvent(event
);
339 // if the icons are aligned left to right, the vertical wheel event should
340 // be applied to the horizontal scrollbar
341 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
342 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
343 (settings
->arrangement() == QListView::LeftToRight
);
344 if (scrollHorizontal
) {
345 QWheelEvent
horizEvent(event
->pos(),
350 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
354 void DolphinIconsView::showEvent(QShowEvent
* event
)
356 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
357 delegate
->setMaximumSize(m_itemSize
);
359 KCategorizedView::showEvent(event
);
362 void DolphinIconsView::leaveEvent(QEvent
* event
)
364 KCategorizedView::leaveEvent(event
);
365 // if the mouse is above an item and moved very fast outside the widget,
366 // no viewportEntered() signal might be emitted although the mouse has been moved
367 // above the viewport
368 m_controller
->emitViewportEntered();
371 void DolphinIconsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
373 KCategorizedView::currentChanged(current
, previous
);
374 m_autoScroller
->handleCurrentIndexChange(current
, previous
);
377 void DolphinIconsView::resizeEvent(QResizeEvent
* event
)
379 KCategorizedView::resizeEvent(event
);
380 const DolphinView
* view
= m_controller
->dolphinView();
381 updateGridSize(view
->showPreview(), view
->additionalInfo().count());
384 void DolphinIconsView::slotShowPreviewChanged()
386 const DolphinView
* view
= m_controller
->dolphinView();
387 updateGridSize(view
->showPreview(), additionalInfoCount());
390 void DolphinIconsView::slotAdditionalInfoChanged()
392 const DolphinView
* view
= m_controller
->dolphinView();
393 const bool showPreview
= view
->showPreview();
394 updateGridSize(showPreview
, view
->additionalInfo().count());
397 void DolphinIconsView::setNameFilter(const QString
& nameFilter
)
399 DolphinSortFilterProxyModel
* proxyModel
= static_cast<DolphinSortFilterProxyModel
*>(model());
400 proxyModel
->setFilterRegExp(nameFilter
);
403 void DolphinIconsView::setZoomLevel(int level
)
405 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
407 const int oldIconSize
= settings
->iconSize();
408 int newIconSize
= oldIconSize
;
410 const bool showPreview
= m_controller
->dolphinView()->showPreview();
412 const int previewSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
413 settings
->setPreviewSize(previewSize
);
415 newIconSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
416 settings
->setIconSize(newIconSize
);
419 // increase also the grid size
420 const int diff
= newIconSize
- oldIconSize
;
421 settings
->setItemWidth(settings
->itemWidth() + diff
);
422 settings
->setItemHeight(settings
->itemHeight() + diff
);
424 updateGridSize(showPreview
, additionalInfoCount());
427 void DolphinIconsView::requestActivation()
429 m_controller
->requestActivation();
432 void DolphinIconsView::slotGlobalSettingsChanged(int category
)
436 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
437 Q_ASSERT(settings
!= 0);
438 if (settings
->useSystemFont()) {
439 m_font
= KGlobalSettings::generalFont();
442 disconnect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
443 disconnect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
444 if (KGlobalSettings::singleClick()) {
445 connect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
447 connect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
451 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
453 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
454 Q_ASSERT(settings
!= 0);
456 int itemWidth
= settings
->itemWidth();
457 int itemHeight
= settings
->itemHeight();
458 int size
= settings
->iconSize();
461 const int previewSize
= settings
->previewSize();
462 const int diff
= previewSize
- size
;
469 Q_ASSERT(additionalInfoCount
>= 0);
470 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
472 // Optimize the item size of the grid in a way to prevent large gaps on the
473 // right border (= row arrangement) or the bottom border (= column arrangement).
474 // There is no public API in QListView to find out the used width of the viewport
475 // for the layout. The following calculation of 'contentWidth'/'contentHeight'
476 // is based on QListViewPrivate::prepareItemsLayout() (Copyright (C) 2009 Nokia Corporation).
477 int frameAroundContents
= 0;
478 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents
)) {
479 frameAroundContents
= style()->pixelMetric(QStyle::PM_DefaultFrameWidth
) * 2;
481 const int spacing
= settings
->gridSpacing();
482 if (settings
->arrangement() == QListView::TopToBottom
) {
483 const int contentWidth
= viewport()->width() - 1
484 - frameAroundContents
485 - style()->pixelMetric(QStyle::PM_ScrollBarExtent
, 0, horizontalScrollBar());
486 const int gridWidth
= itemWidth
+ spacing
* 2;
487 const int horizItemCount
= contentWidth
/ gridWidth
;
488 if (horizItemCount
> 0) {
489 itemWidth
+= (contentWidth
- horizItemCount
* gridWidth
) / horizItemCount
;
492 // The decoration width indirectly defines the maximum
493 // width for the text wrapping. To use the maximum item width
494 // for text wrapping, it is used as decoration width.
495 m_decorationSize
= QSize(itemWidth
, size
);
496 setIconSize(QSize(itemWidth
, size
));
498 const int contentHeight
= viewport()->height() - 1
499 - frameAroundContents
500 - style()->pixelMetric(QStyle::PM_ScrollBarExtent
, 0, verticalScrollBar());
501 const int gridHeight
= itemHeight
+ spacing
;
502 const int vertItemCount
= contentHeight
/ gridHeight
;
503 if (vertItemCount
> 0) {
504 itemHeight
+= (contentHeight
- vertItemCount
* gridHeight
) / vertItemCount
;
507 m_decorationSize
= QSize(size
, size
);
508 setIconSize(QSize(size
, size
));
511 m_itemSize
= QSize(itemWidth
, itemHeight
);
512 setGridSizeOwn(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
514 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
516 delegate
->setMaximumSize(m_itemSize
);
519 if (m_selectionManager
!= 0) {
520 m_selectionManager
->reset();
524 int DolphinIconsView::additionalInfoCount() const
526 const DolphinView
* view
= m_controller
->dolphinView();
527 return view
->additionalInfo().count();
530 #include "dolphiniconsview.moc"