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)));
92 const DolphinView
* view
= controller
->dolphinView();
93 connect(view
, SIGNAL(showPreviewChanged()),
94 this, SLOT(slotShowPreviewChanged()));
95 connect(view
, SIGNAL(additionalInfoChanged()),
96 this, SLOT(slotAdditionalInfoChanged()));
98 // apply the icons mode settings to the widget
99 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
100 Q_ASSERT(settings
!= 0);
102 if (settings
->useSystemFont()) {
103 m_font
= KGlobalSettings::generalFont();
105 m_font
= QFont(settings
->fontFamily(),
106 settings
->fontSize(),
107 settings
->fontWeight(),
108 settings
->italicFont());
111 setWordWrap(settings
->numberOfTextlines() > 1);
112 updateGridSize(view
->showPreview(), 0);
114 if (settings
->arrangement() == QListView::TopToBottom
) {
115 setFlow(QListView::LeftToRight
);
116 m_decorationPosition
= QStyleOptionViewItem::Top
;
117 m_displayAlignment
= Qt::AlignHCenter
;
119 setFlow(QListView::TopToBottom
);
120 m_decorationPosition
= QStyleOptionViewItem::Left
;
121 m_displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
124 m_categoryDrawer
= new DolphinCategoryDrawer();
125 setCategoryDrawer(m_categoryDrawer
);
129 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
130 this, SLOT(updateFont()));
133 DolphinIconsView::~DolphinIconsView()
135 delete m_categoryDrawer
;
136 m_categoryDrawer
= 0;
139 void DolphinIconsView::scrollTo(const QModelIndex
& index
, ScrollHint hint
)
141 // Enable the QListView implementation of scrollTo() only if it has been
142 // triggered by a key press. Otherwise QAbstractItemView wants to scroll to the current
143 // index each time the layout has been changed. This becomes an issue when
144 // previews are loaded and the scrollbar is used: the scrollbar will always
145 // be reset to 0 on each new preview.
146 if (m_enableScrollTo
|| (state() != QAbstractItemView::NoState
)) {
147 KCategorizedView::scrollTo(index
, hint
);
148 m_enableScrollTo
= false;
152 void DolphinIconsView::dataChanged(const QModelIndex
& topLeft
, const QModelIndex
& bottomRight
)
154 KCategorizedView::dataChanged(topLeft
, bottomRight
);
156 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
157 if (!proxyModel
->isCategorizedModel()) {
158 // bypass a QListView issue that items are not layout correctly if the decoration size of
160 scheduleDelayedItemsLayout();
164 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
166 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
167 viewOptions
.font
= m_font
;
168 viewOptions
.decorationPosition
= m_decorationPosition
;
169 viewOptions
.decorationSize
= m_decorationSize
;
170 viewOptions
.displayAlignment
= m_displayAlignment
;
171 viewOptions
.showDecorationSelected
= true;
175 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
177 KCategorizedView::contextMenuEvent(event
);
178 m_controller
->triggerContextMenuRequest(event
->pos());
181 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
183 m_controller
->requestActivation();
184 const QModelIndex index
= indexAt(event
->pos());
185 if (index
.isValid() && (event
->button() == Qt::LeftButton
)) {
186 // TODO: It should not be necessary to manually set the dragging state, but I could
187 // not reproduce this issue with a Qt-only example yet to find the root cause.
188 // Issue description: start Dolphin, split the view and drag an item from the
189 // inactive view to the active view by a very fast mouse movement. Result:
190 // the item gets selected instead of being dragged...
191 setState(QAbstractItemView::DraggingState
);
194 if (!index
.isValid()) {
195 if (QApplication::mouseButtons() & Qt::MidButton
) {
196 m_controller
->replaceUrlByClipboard();
198 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
199 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
204 KCategorizedView::mousePressEvent(event
);
207 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
209 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
210 // fix this in KDE 4.1
211 KCategorizedView::startDrag(supportedActions
);
212 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_controller
);
215 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
217 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
218 event
->acceptProposedAction();
222 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
224 KCategorizedView::dragLeaveEvent(event
);
225 setDirtyRegion(m_dropRect
);
228 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
230 KCategorizedView::dragMoveEvent(event
);
232 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
233 const QModelIndex index
= indexAt(event
->pos());
234 setDirtyRegion(m_dropRect
);
236 m_dropRect
.setSize(QSize()); // set as invalid
237 if (index
.isValid()) {
238 const KFileItem item
= m_controller
->itemForIndex(index
);
239 if (!item
.isNull() && item
.isDir()) {
240 m_dropRect
= visualRect(index
);
242 m_dropRect
.setSize(QSize()); // set as invalid
245 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
246 // accept url drops, independently from the destination item
247 event
->acceptProposedAction();
250 setDirtyRegion(m_dropRect
);
253 void DolphinIconsView::dropEvent(QDropEvent
* event
)
255 const QModelIndex index
= indexAt(event
->pos());
256 const KFileItem item
= m_controller
->itemForIndex(index
);
257 m_controller
->indicateDroppedUrls(item
, m_controller
->url(), event
);
259 KCategorizedView::dropEvent(event
);
262 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
264 m_enableScrollTo
= true; // see DolphinIconsView::scrollTo()
265 KCategorizedView::keyPressEvent(event
);
266 m_controller
->handleKeyPressEvent(event
);
269 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
271 if (m_selectionManager
!= 0) {
272 m_selectionManager
->reset();
275 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
276 if (event
->modifiers() & Qt::ControlModifier
) {
281 horizontalScrollBar()->setSingleStep(m_itemSize
.width() / 10);
282 verticalScrollBar()->setSingleStep(m_itemSize
.height() / 10);
284 KCategorizedView::wheelEvent(event
);
285 // if the icons are aligned left to right, the vertical wheel event should
286 // be applied to the horizontal scrollbar
287 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
288 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
289 (settings
->arrangement() == QListView::LeftToRight
);
290 if (scrollHorizontal
) {
291 QWheelEvent
horizEvent(event
->pos(),
296 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
300 void DolphinIconsView::showEvent(QShowEvent
* event
)
302 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
303 delegate
->setMaximumSize(m_itemSize
);
305 KCategorizedView::showEvent(event
);
308 void DolphinIconsView::leaveEvent(QEvent
* event
)
310 KCategorizedView::leaveEvent(event
);
311 // if the mouse is above an item and moved very fast outside the widget,
312 // no viewportEntered() signal might be emitted although the mouse has been moved
313 // above the viewport
314 m_controller
->emitViewportEntered();
317 void DolphinIconsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
319 KCategorizedView::currentChanged(current
, previous
);
320 if (current
.isValid() && !m_autoScroller
->isActive()) {
325 void DolphinIconsView::resizeEvent(QResizeEvent
* event
)
327 KCategorizedView::resizeEvent(event
);
328 const DolphinView
* view
= m_controller
->dolphinView();
329 updateGridSize(view
->showPreview(), view
->additionalInfo().count());
332 void DolphinIconsView::slotShowPreviewChanged()
334 const DolphinView
* view
= m_controller
->dolphinView();
335 updateGridSize(view
->showPreview(), additionalInfoCount());
338 void DolphinIconsView::slotAdditionalInfoChanged()
340 const DolphinView
* view
= m_controller
->dolphinView();
341 const bool showPreview
= view
->showPreview();
342 updateGridSize(showPreview
, view
->additionalInfo().count());
345 void DolphinIconsView::setZoomLevel(int level
)
347 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
349 const int oldIconSize
= settings
->iconSize();
350 int newIconSize
= oldIconSize
;
352 const bool showPreview
= m_controller
->dolphinView()->showPreview();
354 const int previewSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
355 settings
->setPreviewSize(previewSize
);
357 newIconSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
358 settings
->setIconSize(newIconSize
);
361 // increase also the grid size
362 const int diff
= newIconSize
- oldIconSize
;
363 settings
->setItemWidth(settings
->itemWidth() + diff
);
364 settings
->setItemHeight(settings
->itemHeight() + diff
);
366 updateGridSize(showPreview
, additionalInfoCount());
369 void DolphinIconsView::requestActivation()
371 m_controller
->requestActivation();
374 void DolphinIconsView::updateFont()
376 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
377 Q_ASSERT(settings
!= 0);
379 if (settings
->useSystemFont()) {
380 m_font
= KGlobalSettings::generalFont();
384 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
386 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
387 Q_ASSERT(settings
!= 0);
389 int itemWidth
= settings
->itemWidth();
390 int itemHeight
= settings
->itemHeight();
391 int size
= settings
->iconSize();
394 const int previewSize
= settings
->previewSize();
395 const int diff
= previewSize
- size
;
402 Q_ASSERT(additionalInfoCount
>= 0);
403 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
405 // optimize the item size of the grid in a way to prevent large gaps on the
406 // right border (= row arrangement) or the bottom border (= column arrangement)
407 const int spacing
= settings
->gridSpacing();
408 if (settings
->arrangement() == QListView::TopToBottom
) {
409 const int contentWidth
= viewport()->width() - 1 -
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 style()->pixelMetric(QStyle::PM_ScrollBarExtent
, 0, verticalScrollBar());
425 const int gridHeight
= itemHeight
+ spacing
;
426 const int vertItemCount
= contentHeight
/ gridHeight
;
427 if (vertItemCount
> 0) {
428 itemHeight
+= (contentHeight
- vertItemCount
* gridHeight
) / vertItemCount
;
431 m_decorationSize
= QSize(size
, size
);
432 setIconSize(QSize(size
, size
));
435 m_itemSize
= QSize(itemWidth
, itemHeight
);
436 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
438 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
440 delegate
->setMaximumSize(m_itemSize
);
443 if (m_selectionManager
!= 0) {
444 m_selectionManager
->reset();
448 int DolphinIconsView::additionalInfoCount() const
450 const DolphinView
* view
= m_controller
->dolphinView();
451 return view
->additionalInfo().count();
454 #include "dolphiniconsview.moc"