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_enableScrollTo(false),
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(zoomLevelChanged(int)),
90 this, SLOT(setZoomLevel(int)));
91 connect(controller
, SIGNAL(scrollToCurrentItem()),
92 this, SLOT(scrollToCurrentItem()));
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::scrollTo(const QModelIndex
& index
, ScrollHint hint
)
143 // Enable the QListView implementation of scrollTo() only if it has been
144 // triggered by a key press. Otherwise QAbstractItemView wants to scroll to the current
145 // index each time the layout has been changed. This becomes an issue when
146 // previews are loaded and the scrollbar is used: the scrollbar will always
147 // be reset to 0 on each new preview.
148 if (m_enableScrollTo
|| (state() != QAbstractItemView::NoState
)) {
149 KCategorizedView::scrollTo(index
, hint
);
150 m_enableScrollTo
= false;
154 void DolphinIconsView::dataChanged(const QModelIndex
& topLeft
, const QModelIndex
& bottomRight
)
156 KCategorizedView::dataChanged(topLeft
, bottomRight
);
158 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
159 if (!proxyModel
->isCategorizedModel()) {
160 // bypass a QListView issue that items are not layout correctly if the decoration size of
162 scheduleDelayedItemsLayout();
166 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
168 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
169 viewOptions
.font
= m_font
;
170 viewOptions
.decorationPosition
= m_decorationPosition
;
171 viewOptions
.decorationSize
= m_decorationSize
;
172 viewOptions
.displayAlignment
= m_displayAlignment
;
173 viewOptions
.showDecorationSelected
= true;
177 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
179 KCategorizedView::contextMenuEvent(event
);
180 m_controller
->triggerContextMenuRequest(event
->pos());
183 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
185 m_controller
->requestActivation();
186 const QModelIndex index
= indexAt(event
->pos());
187 if (index
.isValid() && (event
->button() == Qt::LeftButton
)) {
188 // TODO: It should not be necessary to manually set the dragging state, but I could
189 // not reproduce this issue with a Qt-only example yet to find the root cause.
190 // Issue description: start Dolphin, split the view and drag an item from the
191 // inactive view to the active view by a very fast mouse movement. Result:
192 // the item gets selected instead of being dragged...
193 setState(QAbstractItemView::DraggingState
);
196 if (!index
.isValid()) {
197 if (QApplication::mouseButtons() & Qt::MidButton
) {
198 m_controller
->replaceUrlByClipboard();
200 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
201 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
206 KCategorizedView::mousePressEvent(event
);
209 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
211 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
212 // fix this in KDE 4.1
213 KCategorizedView::startDrag(supportedActions
);
214 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_controller
);
217 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
219 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
220 event
->acceptProposedAction();
224 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
226 KCategorizedView::dragLeaveEvent(event
);
227 setDirtyRegion(m_dropRect
);
230 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
232 KCategorizedView::dragMoveEvent(event
);
234 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
235 const QModelIndex index
= indexAt(event
->pos());
236 setDirtyRegion(m_dropRect
);
238 m_dropRect
.setSize(QSize()); // set as invalid
239 if (index
.isValid()) {
240 const KFileItem item
= m_controller
->itemForIndex(index
);
241 if (!item
.isNull() && item
.isDir()) {
242 m_dropRect
= visualRect(index
);
244 m_dropRect
.setSize(QSize()); // set as invalid
247 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
248 // accept url drops, independently from the destination item
249 event
->acceptProposedAction();
252 setDirtyRegion(m_dropRect
);
255 void DolphinIconsView::dropEvent(QDropEvent
* event
)
257 const QModelIndex index
= indexAt(event
->pos());
258 const KFileItem item
= m_controller
->itemForIndex(index
);
259 m_controller
->indicateDroppedUrls(item
, m_controller
->url(), event
);
261 KCategorizedView::dropEvent(event
);
264 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
266 m_enableScrollTo
= true; // see DolphinIconsView::scrollTo()
267 KCategorizedView::keyPressEvent(event
);
268 m_controller
->handleKeyPressEvent(event
);
271 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
273 if (m_selectionManager
!= 0) {
274 m_selectionManager
->reset();
277 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
278 if (event
->modifiers() & Qt::ControlModifier
) {
283 horizontalScrollBar()->setSingleStep(m_itemSize
.width() / 10);
284 verticalScrollBar()->setSingleStep(m_itemSize
.height() / 10);
286 KCategorizedView::wheelEvent(event
);
287 // if the icons are aligned left to right, the vertical wheel event should
288 // be applied to the horizontal scrollbar
289 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
290 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
291 (settings
->arrangement() == QListView::LeftToRight
);
292 if (scrollHorizontal
) {
293 QWheelEvent
horizEvent(event
->pos(),
298 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
302 void DolphinIconsView::showEvent(QShowEvent
* event
)
304 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
305 delegate
->setMaximumSize(m_itemSize
);
307 KCategorizedView::showEvent(event
);
310 void DolphinIconsView::leaveEvent(QEvent
* event
)
312 KCategorizedView::leaveEvent(event
);
313 // if the mouse is above an item and moved very fast outside the widget,
314 // no viewportEntered() signal might be emitted although the mouse has been moved
315 // above the viewport
316 m_controller
->emitViewportEntered();
319 void DolphinIconsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
321 KCategorizedView::currentChanged(current
, previous
);
322 if (current
.isValid() && !m_autoScroller
->isActive()) {
327 void DolphinIconsView::resizeEvent(QResizeEvent
* event
)
329 KCategorizedView::resizeEvent(event
);
330 const DolphinView
* view
= m_controller
->dolphinView();
331 updateGridSize(view
->showPreview(), view
->additionalInfo().count());
334 void DolphinIconsView::slotShowPreviewChanged()
336 const DolphinView
* view
= m_controller
->dolphinView();
337 updateGridSize(view
->showPreview(), additionalInfoCount());
340 void DolphinIconsView::slotAdditionalInfoChanged()
342 const DolphinView
* view
= m_controller
->dolphinView();
343 const bool showPreview
= view
->showPreview();
344 updateGridSize(showPreview
, view
->additionalInfo().count());
347 void DolphinIconsView::setZoomLevel(int level
)
349 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
351 const int oldIconSize
= settings
->iconSize();
352 int newIconSize
= oldIconSize
;
354 const bool showPreview
= m_controller
->dolphinView()->showPreview();
356 const int previewSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
357 settings
->setPreviewSize(previewSize
);
359 newIconSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
360 settings
->setIconSize(newIconSize
);
363 // increase also the grid size
364 const int diff
= newIconSize
- oldIconSize
;
365 settings
->setItemWidth(settings
->itemWidth() + diff
);
366 settings
->setItemHeight(settings
->itemHeight() + diff
);
368 updateGridSize(showPreview
, additionalInfoCount());
371 void DolphinIconsView::requestActivation()
373 m_controller
->requestActivation();
376 void DolphinIconsView::slotGlobalSettingsChanged(int category
)
380 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
381 Q_ASSERT(settings
!= 0);
382 if (settings
->useSystemFont()) {
383 m_font
= KGlobalSettings::generalFont();
386 disconnect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
387 disconnect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
388 if (KGlobalSettings::singleClick()) {
389 connect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
391 connect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
395 void DolphinIconsView::scrollToCurrentItem()
397 m_enableScrollTo
= true;
398 scrollTo(currentIndex());
399 m_enableScrollTo
= false;
402 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
404 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
405 Q_ASSERT(settings
!= 0);
407 int itemWidth
= settings
->itemWidth();
408 int itemHeight
= settings
->itemHeight();
409 int size
= settings
->iconSize();
412 const int previewSize
= settings
->previewSize();
413 const int diff
= previewSize
- size
;
420 Q_ASSERT(additionalInfoCount
>= 0);
421 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
423 // Optimize the item size of the grid in a way to prevent large gaps on the
424 // right border (= row arrangement) or the bottom border (= column arrangement).
425 // There is no public API in QListView to find out the used width of the viewport
426 // for the layout. The following calculation of 'contentWidth'/'contentHeight'
427 // is based on QListViewPrivate::prepareItemsLayout() (Copyright (C) 2009 Nokia Corporation).
428 int frameAroundContents
= 0;
429 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents
)) {
430 frameAroundContents
= style()->pixelMetric(QStyle::PM_DefaultFrameWidth
) * 2;
432 const int spacing
= settings
->gridSpacing();
433 if (settings
->arrangement() == QListView::TopToBottom
) {
434 const int contentWidth
= viewport()->width() - 1
435 - frameAroundContents
436 - style()->pixelMetric(QStyle::PM_ScrollBarExtent
, 0, horizontalScrollBar());
437 const int gridWidth
= itemWidth
+ spacing
* 2;
438 const int horizItemCount
= contentWidth
/ gridWidth
;
439 if (horizItemCount
> 0) {
440 itemWidth
+= (contentWidth
- horizItemCount
* gridWidth
) / horizItemCount
;
443 // The decoration width indirectly defines the maximum
444 // width for the text wrapping. To use the maximum item width
445 // for text wrapping, it is used as decoration width.
446 m_decorationSize
= QSize(itemWidth
, size
);
447 setIconSize(QSize(itemWidth
, size
));
449 const int contentHeight
= viewport()->height() - 1
450 - frameAroundContents
451 - style()->pixelMetric(QStyle::PM_ScrollBarExtent
, 0, verticalScrollBar());
452 const int gridHeight
= itemHeight
+ spacing
;
453 const int vertItemCount
= contentHeight
/ gridHeight
;
454 if (vertItemCount
> 0) {
455 itemHeight
+= (contentHeight
- vertItemCount
* gridHeight
) / vertItemCount
;
458 m_decorationSize
= QSize(size
, size
);
459 setIconSize(QSize(size
, size
));
462 m_itemSize
= QSize(itemWidth
, itemHeight
);
463 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
465 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
467 delegate
->setMaximumSize(m_itemSize
);
470 if (m_selectionManager
!= 0) {
471 m_selectionManager
->reset();
475 int DolphinIconsView::additionalInfoCount() const
477 const DolphinView
* view
= m_controller
->dolphinView();
478 return view
->additionalInfo().count();
481 #include "dolphiniconsview.moc"