1 /***************************************************************************
2 * Copyright (C) 2006 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 "dolphinviewautoscroller.h"
26 #include "dolphin_iconsmodesettings.h"
27 #include "dolphin_generalsettings.h"
28 #include "draganddrophelper.h"
29 #include "selectionmanager.h"
30 #include "zoomlevelinfo.h"
32 #include <kcategorizedsortfilterproxymodel.h>
34 #include <kdirmodel.h>
35 #include <kfileitemdelegate.h>
37 #include <QAbstractProxyModel>
38 #include <QApplication>
41 DolphinIconsView::DolphinIconsView(QWidget
* parent
, DolphinController
* controller
) :
42 KCategorizedView(parent
),
43 m_controller(controller
),
44 m_selectionManager(0),
49 m_decorationPosition(QStyleOptionViewItem::Top
),
50 m_displayAlignment(Qt::AlignHCenter
),
54 Q_ASSERT(controller
!= 0);
55 setLayoutDirection(Qt::LeftToRight
);
56 setViewMode(QListView::IconMode
);
57 setResizeMode(QListView::Adjust
);
58 setMovement(QListView::Static
);
60 setEditTriggers(QAbstractItemView::NoEditTriggers
);
61 viewport()->setAcceptDrops(true);
63 setMouseTracking(true);
64 m_autoScroller
= new DolphinViewAutoScroller(this);
66 connect(this, SIGNAL(clicked(const QModelIndex
&)),
67 controller
, SLOT(requestTab(const QModelIndex
&)));
68 if (KGlobalSettings::singleClick()) {
69 connect(this, SIGNAL(clicked(const QModelIndex
&)),
70 controller
, SLOT(triggerItem(const QModelIndex
&)));
72 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
73 controller
, SLOT(triggerItem(const QModelIndex
&)));
76 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
77 m_selectionManager
= new SelectionManager(this);
78 connect(m_selectionManager
, SIGNAL(selectionChanged()),
79 this, SLOT(requestActivation()));
80 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
81 m_selectionManager
, SLOT(reset()));
84 connect(this, SIGNAL(entered(const QModelIndex
&)),
85 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
86 connect(this, SIGNAL(viewportEntered()),
87 controller
, SLOT(emitViewportEntered()));
88 connect(controller
, SIGNAL(zoomLevelChanged(int)),
89 this, SLOT(setZoomLevel(int)));
91 const DolphinView
* view
= controller
->dolphinView();
92 connect(view
, SIGNAL(showPreviewChanged()),
93 this, SLOT(slotShowPreviewChanged()));
94 connect(view
, SIGNAL(additionalInfoChanged()),
95 this, SLOT(slotAdditionalInfoChanged()));
97 // apply the icons mode settings to the widget
98 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
99 Q_ASSERT(settings
!= 0);
101 if (settings
->useSystemFont()) {
102 m_font
= KGlobalSettings::generalFont();
104 m_font
= QFont(settings
->fontFamily(),
105 settings
->fontSize(),
106 settings
->fontWeight(),
107 settings
->italicFont());
110 setWordWrap(settings
->numberOfTextlines() > 1);
111 updateGridSize(view
->showPreview(), 0);
113 if (settings
->arrangement() == QListView::TopToBottom
) {
114 setFlow(QListView::LeftToRight
);
115 m_decorationPosition
= QStyleOptionViewItem::Top
;
116 m_displayAlignment
= Qt::AlignHCenter
;
118 setFlow(QListView::TopToBottom
);
119 m_decorationPosition
= QStyleOptionViewItem::Left
;
120 m_displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
123 m_categoryDrawer
= new DolphinCategoryDrawer();
124 setCategoryDrawer(m_categoryDrawer
);
128 connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)),
129 this, SLOT(slotGlobalSettingsChanged(int)));
132 DolphinIconsView::~DolphinIconsView()
134 delete m_categoryDrawer
;
135 m_categoryDrawer
= 0;
138 void DolphinIconsView::dataChanged(const QModelIndex
& topLeft
, const QModelIndex
& bottomRight
)
140 KCategorizedView::dataChanged(topLeft
, bottomRight
);
142 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
143 if (!proxyModel
->isCategorizedModel()) {
144 // bypass a QListView issue that items are not layout correctly if the decoration size of
146 scheduleDelayedItemsLayout();
150 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
152 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
153 viewOptions
.font
= m_font
;
154 viewOptions
.decorationPosition
= m_decorationPosition
;
155 viewOptions
.decorationSize
= m_decorationSize
;
156 viewOptions
.displayAlignment
= m_displayAlignment
;
157 viewOptions
.showDecorationSelected
= true;
161 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
163 KCategorizedView::contextMenuEvent(event
);
164 m_controller
->triggerContextMenuRequest(event
->pos());
167 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
169 m_controller
->requestActivation();
170 const QModelIndex index
= indexAt(event
->pos());
171 if (index
.isValid() && (event
->button() == Qt::LeftButton
)) {
172 // TODO: It should not be necessary to manually set the dragging state, but I could
173 // not reproduce this issue with a Qt-only example yet to find the root cause.
174 // Issue description: start Dolphin, split the view and drag an item from the
175 // inactive view to the active view by a very fast mouse movement. Result:
176 // the item gets selected instead of being dragged...
177 setState(QAbstractItemView::DraggingState
);
180 if (!index
.isValid()) {
181 if (QApplication::mouseButtons() & Qt::MidButton
) {
182 m_controller
->replaceUrlByClipboard();
184 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
185 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
190 KCategorizedView::mousePressEvent(event
);
193 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
195 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
196 // fix this in KDE 4.1
197 KCategorizedView::startDrag(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
)
210 KCategorizedView::dragLeaveEvent(event
);
211 setDirtyRegion(m_dropRect
);
214 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
216 KCategorizedView::dragMoveEvent(event
);
218 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
219 const QModelIndex index
= indexAt(event
->pos());
220 setDirtyRegion(m_dropRect
);
222 m_dropRect
.setSize(QSize()); // set as invalid
223 if (index
.isValid()) {
224 const KFileItem item
= m_controller
->itemForIndex(index
);
225 if (!item
.isNull() && item
.isDir()) {
226 m_dropRect
= visualRect(index
);
228 m_dropRect
.setSize(QSize()); // set as invalid
231 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
232 // accept url drops, independently from the destination item
233 event
->acceptProposedAction();
236 setDirtyRegion(m_dropRect
);
239 void DolphinIconsView::dropEvent(QDropEvent
* event
)
241 const QModelIndex index
= indexAt(event
->pos());
242 const KFileItem item
= m_controller
->itemForIndex(index
);
243 m_controller
->indicateDroppedUrls(item
, m_controller
->url(), event
);
245 KCategorizedView::dropEvent(event
);
248 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
250 KCategorizedView::keyPressEvent(event
);
251 m_controller
->handleKeyPressEvent(event
);
254 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
256 if (m_selectionManager
!= 0) {
257 m_selectionManager
->reset();
260 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
261 if (event
->modifiers() & Qt::ControlModifier
) {
266 horizontalScrollBar()->setSingleStep(m_itemSize
.width() / 10);
267 verticalScrollBar()->setSingleStep(m_itemSize
.height() / 10);
269 KCategorizedView::wheelEvent(event
);
270 // if the icons are aligned left to right, the vertical wheel event should
271 // be applied to the horizontal scrollbar
272 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
273 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
274 (settings
->arrangement() == QListView::LeftToRight
);
275 if (scrollHorizontal
) {
276 QWheelEvent
horizEvent(event
->pos(),
281 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
285 void DolphinIconsView::showEvent(QShowEvent
* event
)
287 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
288 delegate
->setMaximumSize(m_itemSize
);
290 KCategorizedView::showEvent(event
);
293 void DolphinIconsView::leaveEvent(QEvent
* event
)
295 KCategorizedView::leaveEvent(event
);
296 // if the mouse is above an item and moved very fast outside the widget,
297 // no viewportEntered() signal might be emitted although the mouse has been moved
298 // above the viewport
299 m_controller
->emitViewportEntered();
302 void DolphinIconsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
304 KCategorizedView::currentChanged(current
, previous
);
305 m_autoScroller
->handleCurrentIndexChange(current
, previous
);
308 void DolphinIconsView::resizeEvent(QResizeEvent
* event
)
310 KCategorizedView::resizeEvent(event
);
311 const DolphinView
* view
= m_controller
->dolphinView();
312 updateGridSize(view
->showPreview(), view
->additionalInfo().count());
315 void DolphinIconsView::slotShowPreviewChanged()
317 const DolphinView
* view
= m_controller
->dolphinView();
318 updateGridSize(view
->showPreview(), additionalInfoCount());
321 void DolphinIconsView::slotAdditionalInfoChanged()
323 const DolphinView
* view
= m_controller
->dolphinView();
324 const bool showPreview
= view
->showPreview();
325 updateGridSize(showPreview
, view
->additionalInfo().count());
328 void DolphinIconsView::setZoomLevel(int level
)
330 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
332 const int oldIconSize
= settings
->iconSize();
333 int newIconSize
= oldIconSize
;
335 const bool showPreview
= m_controller
->dolphinView()->showPreview();
337 const int previewSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
338 settings
->setPreviewSize(previewSize
);
340 newIconSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
341 settings
->setIconSize(newIconSize
);
344 // increase also the grid size
345 const int diff
= newIconSize
- oldIconSize
;
346 settings
->setItemWidth(settings
->itemWidth() + diff
);
347 settings
->setItemHeight(settings
->itemHeight() + diff
);
349 updateGridSize(showPreview
, additionalInfoCount());
352 void DolphinIconsView::requestActivation()
354 m_controller
->requestActivation();
357 void DolphinIconsView::slotGlobalSettingsChanged(int category
)
361 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
362 Q_ASSERT(settings
!= 0);
363 if (settings
->useSystemFont()) {
364 m_font
= KGlobalSettings::generalFont();
367 disconnect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
368 disconnect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
369 if (KGlobalSettings::singleClick()) {
370 connect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
372 connect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
376 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
378 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
379 Q_ASSERT(settings
!= 0);
381 int itemWidth
= settings
->itemWidth();
382 int itemHeight
= settings
->itemHeight();
383 int size
= settings
->iconSize();
386 const int previewSize
= settings
->previewSize();
387 const int diff
= previewSize
- size
;
394 Q_ASSERT(additionalInfoCount
>= 0);
395 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
397 // Optimize the item size of the grid in a way to prevent large gaps on the
398 // right border (= row arrangement) or the bottom border (= column arrangement).
399 // There is no public API in QListView to find out the used width of the viewport
400 // for the layout. The following calculation of 'contentWidth'/'contentHeight'
401 // is based on QListViewPrivate::prepareItemsLayout() (Copyright (C) 2009 Nokia Corporation).
402 int frameAroundContents
= 0;
403 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents
)) {
404 frameAroundContents
= style()->pixelMetric(QStyle::PM_DefaultFrameWidth
) * 2;
406 const int spacing
= settings
->gridSpacing();
407 if (settings
->arrangement() == QListView::TopToBottom
) {
408 const int contentWidth
= viewport()->width() - 1
409 - frameAroundContents
410 - style()->pixelMetric(QStyle::PM_ScrollBarExtent
, 0, horizontalScrollBar());
411 const int gridWidth
= itemWidth
+ spacing
* 2;
412 const int horizItemCount
= contentWidth
/ gridWidth
;
413 if (horizItemCount
> 0) {
414 itemWidth
+= (contentWidth
- horizItemCount
* gridWidth
) / horizItemCount
;
417 // The decoration width indirectly defines the maximum
418 // width for the text wrapping. To use the maximum item width
419 // for text wrapping, it is used as decoration width.
420 m_decorationSize
= QSize(itemWidth
, size
);
421 setIconSize(QSize(itemWidth
, size
));
423 const int contentHeight
= viewport()->height() - 1
424 - frameAroundContents
425 - style()->pixelMetric(QStyle::PM_ScrollBarExtent
, 0, verticalScrollBar());
426 const int gridHeight
= itemHeight
+ spacing
;
427 const int vertItemCount
= contentHeight
/ gridHeight
;
428 if (vertItemCount
> 0) {
429 itemHeight
+= (contentHeight
- vertItemCount
* gridHeight
) / vertItemCount
;
432 m_decorationSize
= QSize(size
, size
);
433 setIconSize(QSize(size
, size
));
436 m_itemSize
= QSize(itemWidth
, itemHeight
);
437 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
439 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
441 delegate
->setMaximumSize(m_itemSize
);
444 if (m_selectionManager
!= 0) {
445 m_selectionManager
->reset();
449 int DolphinIconsView::additionalInfoCount() const
451 const DolphinView
* view
= m_controller
->dolphinView();
452 return view
->additionalInfo().count();
455 #include "dolphiniconsview.moc"