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 "dolphinsettings.h"
25 #include "dolphin_iconsmodesettings.h"
26 #include "dolphin_generalsettings.h"
27 #include "draganddrophelper.h"
28 #include "selectionmanager.h"
30 #include <kcategorizedsortfilterproxymodel.h>
32 #include <kdirmodel.h>
33 #include <kfileitemdelegate.h>
35 #include <QAbstractProxyModel>
36 #include <QApplication>
39 DolphinIconsView::DolphinIconsView(QWidget
* parent
, DolphinController
* controller
) :
40 KCategorizedView(parent
),
41 m_enableScrollTo(false),
42 m_controller(controller
),
43 m_selectionManager(0),
47 m_decorationPosition(QStyleOptionViewItem::Top
),
48 m_displayAlignment(Qt::AlignHCenter
),
52 Q_ASSERT(controller
!= 0);
53 setViewMode(QListView::IconMode
);
54 setResizeMode(QListView::Adjust
);
55 setSpacing(KDialog::spacingHint());
56 setMovement(QListView::Static
);
58 setEditTriggers(QAbstractItemView::NoEditTriggers
);
59 viewport()->setAcceptDrops(true);
61 setMouseTracking(true);
63 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
64 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
65 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
66 // RETURN-key in keyPressEvent().
67 if (KGlobalSettings::singleClick()) {
68 connect(this, SIGNAL(clicked(const QModelIndex
&)),
69 controller
, SLOT(triggerItem(const QModelIndex
&)));
71 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
72 controller
, SLOT(triggerItem(const QModelIndex
&)));
75 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
76 m_selectionManager
= new SelectionManager(this);
77 connect(m_selectionManager
, SIGNAL(selectionChanged()),
78 this, SLOT(requestActivation()));
79 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
80 m_selectionManager
, SLOT(reset()));
83 connect(this, SIGNAL(entered(const QModelIndex
&)),
84 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
85 connect(this, SIGNAL(viewportEntered()),
86 controller
, SLOT(emitViewportEntered()));
87 connect(controller
, SIGNAL(zoomIn()),
88 this, SLOT(zoomIn()));
89 connect(controller
, SIGNAL(zoomOut()),
90 this, SLOT(zoomOut()));
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 ((flow() == QListView::LeftToRight
) && !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::startDrag(this, supportedActions
);
215 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
217 if (event
->mimeData()->hasUrls()) {
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 (event
->mimeData()->hasUrls()) {
246 // accept url drops, independently from the destination item
247 event
->acceptProposedAction();
250 setDirtyRegion(m_dropRect
);
253 void DolphinIconsView::dropEvent(QDropEvent
* event
)
255 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
256 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
257 if (!urls
.isEmpty()) {
258 const QModelIndex index
= indexAt(event
->pos());
259 const KFileItem item
= m_controller
->itemForIndex(index
);
260 m_controller
->indicateDroppedUrls(urls
,
263 event
->acceptProposedAction();
267 KCategorizedView::dropEvent(event
);
270 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
272 KCategorizedView::keyPressEvent(event
);
273 m_controller
->handleKeyPressEvent(event
);
274 m_enableScrollTo
= true; // see DolphinIconsView::scrollTo()
277 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
279 if (m_selectionManager
!= 0) {
280 m_selectionManager
->reset();
283 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
284 if (event
->modifiers() & Qt::ControlModifier
) {
288 KCategorizedView::wheelEvent(event
);
289 // if the icons are aligned left to right, the vertical wheel event should
290 // be applied to the horizontal scrollbar
291 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
292 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
293 (settings
->arrangement() == QListView::LeftToRight
);
294 if (scrollHorizontal
) {
295 QWheelEvent
horizEvent(event
->pos(),
300 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
304 void DolphinIconsView::showEvent(QShowEvent
* event
)
306 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
307 delegate
->setMaximumSize(m_itemSize
);
309 KCategorizedView::showEvent(event
);
312 void DolphinIconsView::leaveEvent(QEvent
* event
)
314 KCategorizedView::leaveEvent(event
);
315 // if the mouse is above an item and moved very fast outside the widget,
316 // no viewportEntered() signal might be emitted although the mouse has been moved
317 // above the viewport
318 m_controller
->emitViewportEntered();
321 void DolphinIconsView::slotShowPreviewChanged()
323 const DolphinView
* view
= m_controller
->dolphinView();
324 updateGridSize(view
->showPreview(), additionalInfoCount());
327 void DolphinIconsView::slotAdditionalInfoChanged()
329 const DolphinView
* view
= m_controller
->dolphinView();
330 const bool showPreview
= view
->showPreview();
331 updateGridSize(showPreview
, view
->additionalInfo().count());
334 void DolphinIconsView::zoomIn()
336 if (isZoomInPossible()) {
337 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
339 const int oldIconSize
= settings
->iconSize();
340 int newIconSize
= oldIconSize
;
342 const bool showPreview
= m_controller
->dolphinView()->showPreview();
344 const int previewSize
= increasedIconSize(settings
->previewSize());
345 settings
->setPreviewSize(previewSize
);
347 newIconSize
= increasedIconSize(oldIconSize
);
348 settings
->setIconSize(newIconSize
);
351 // increase also the grid size
352 const int diff
= newIconSize
- oldIconSize
;
353 settings
->setItemWidth(settings
->itemWidth() + diff
);
354 settings
->setItemHeight(settings
->itemHeight() + diff
);
356 updateGridSize(showPreview
, additionalInfoCount());
360 void DolphinIconsView::zoomOut()
362 if (isZoomOutPossible()) {
363 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
365 const int oldIconSize
= settings
->iconSize();
366 int newIconSize
= oldIconSize
;
368 const bool showPreview
= m_controller
->dolphinView()->showPreview();
370 const int previewSize
= decreasedIconSize(settings
->previewSize());
371 settings
->setPreviewSize(previewSize
);
373 newIconSize
= decreasedIconSize(settings
->iconSize());
374 settings
->setIconSize(newIconSize
);
377 // decrease also the grid size
378 const int diff
= oldIconSize
- newIconSize
;
379 settings
->setItemWidth(settings
->itemWidth() - diff
);
380 settings
->setItemHeight(settings
->itemHeight() - diff
);
382 updateGridSize(showPreview
, additionalInfoCount());
386 void DolphinIconsView::requestActivation()
388 m_controller
->requestActivation();
391 void DolphinIconsView::updateFont()
393 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
394 Q_ASSERT(settings
!= 0);
396 if (settings
->useSystemFont()) {
397 m_font
= KGlobalSettings::generalFont();
401 bool DolphinIconsView::isZoomInPossible() const
403 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
404 const bool showPreview
= m_controller
->dolphinView()->showPreview();
405 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
406 return size
< KIconLoader::SizeEnormous
;
409 bool DolphinIconsView::isZoomOutPossible() const
411 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
412 const bool showPreview
= m_controller
->dolphinView()->showPreview();
413 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
414 return size
> KIconLoader::SizeSmall
;
417 int DolphinIconsView::increasedIconSize(int size
) const
421 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
422 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
423 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
424 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
425 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
426 default: Q_ASSERT(false); break;
431 int DolphinIconsView::decreasedIconSize(int size
) const
435 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
436 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
437 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
438 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
439 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
440 default: Q_ASSERT(false); break;
445 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
447 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
448 Q_ASSERT(settings
!= 0);
450 int itemWidth
= settings
->itemWidth();
451 int itemHeight
= settings
->itemHeight();
452 int size
= settings
->iconSize();
455 const int previewSize
= settings
->previewSize();
456 const int diff
= previewSize
- size
;
463 Q_ASSERT(additionalInfoCount
>= 0);
464 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
466 if (settings
->arrangement() == QListView::TopToBottom
) {
467 // The decoration width indirectly defines the maximum
468 // width for the text wrapping. To use the maximum item width
469 // for text wrapping, it is used as decoration width.
470 m_decorationSize
= QSize(itemWidth
, size
);
471 setIconSize(QSize(itemWidth
, size
));
473 m_decorationSize
= QSize(size
, size
);
474 setIconSize(QSize(size
, size
));
477 m_itemSize
= QSize(itemWidth
, itemHeight
);
479 const int spacing
= settings
->gridSpacing();
480 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
482 m_controller
->setZoomInPossible(isZoomInPossible());
483 m_controller
->setZoomOutPossible(isZoomOutPossible());
485 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
487 delegate
->setMaximumSize(m_itemSize
);
490 if (m_selectionManager
!= 0) {
491 m_selectionManager
->reset();
495 int DolphinIconsView::additionalInfoCount() const
497 const DolphinView
* view
= m_controller
->dolphinView();
498 return view
->additionalInfo().count();
501 #include "dolphiniconsview.moc"