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>
41 DolphinIconsView::DolphinIconsView(QWidget
* parent
, DolphinController
* controller
) :
42 KCategorizedView(parent
),
43 m_enableScrollTo(false),
44 m_controller(controller
),
45 m_selectionManager(0),
49 m_decorationPosition(QStyleOptionViewItem::Top
),
50 m_displayAlignment(Qt::AlignHCenter
),
54 Q_ASSERT(controller
!= 0);
55 setViewMode(QListView::IconMode
);
56 setResizeMode(QListView::Adjust
);
57 setSpacing(KDialog::spacingHint());
58 setMovement(QListView::Static
);
60 setEditTriggers(QAbstractItemView::NoEditTriggers
);
61 viewport()->setAcceptDrops(true);
63 setMouseTracking(true);
65 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
66 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
67 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
68 // RETURN-key in keyPressEvent().
69 if (KGlobalSettings::singleClick()) {
70 connect(this, SIGNAL(clicked(const QModelIndex
&)),
71 controller
, SLOT(triggerItem(const QModelIndex
&)));
72 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
73 m_selectionManager
= new SelectionManager(this);
74 connect(m_selectionManager
, SIGNAL(selectionChanged()),
75 this, SLOT(requestActivation()));
76 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
77 m_selectionManager
, SLOT(reset()));
80 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
81 controller
, SLOT(triggerItem(const QModelIndex
&)));
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 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
196 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
201 KCategorizedView::mousePressEvent(event
);
204 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
206 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
207 // fix this in KDE 4.1
208 KCategorizedView::startDrag(supportedActions
);
209 DragAndDropHelper::startDrag(this, supportedActions
);
212 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
214 if (event
->mimeData()->hasUrls()) {
215 event
->acceptProposedAction();
219 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
221 KCategorizedView::dragLeaveEvent(event
);
222 setDirtyRegion(m_dropRect
);
225 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
227 KCategorizedView::dragMoveEvent(event
);
229 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
230 const QModelIndex index
= indexAt(event
->pos());
231 setDirtyRegion(m_dropRect
);
233 m_dropRect
.setSize(QSize()); // set as invalid
234 if (index
.isValid()) {
235 const KFileItem item
= m_controller
->itemForIndex(index
);
236 if (!item
.isNull() && item
.isDir()) {
237 m_dropRect
= visualRect(index
);
239 m_dropRect
.setSize(QSize()); // set as invalid
242 if (event
->mimeData()->hasUrls()) {
243 // accept url drops, independently from the destination item
244 event
->acceptProposedAction();
247 setDirtyRegion(m_dropRect
);
250 void DolphinIconsView::dropEvent(QDropEvent
* event
)
252 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
253 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
254 if (!urls
.isEmpty()) {
255 const QModelIndex index
= indexAt(event
->pos());
256 const KFileItem item
= m_controller
->itemForIndex(index
);
257 m_controller
->indicateDroppedUrls(urls
,
260 event
->acceptProposedAction();
264 KCategorizedView::dropEvent(event
);
267 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
269 KCategorizedView::keyPressEvent(event
);
270 m_controller
->handleKeyPressEvent(event
);
271 m_enableScrollTo
= true; // see DolphinIconsView::scrollTo()
274 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
276 if (m_selectionManager
!= 0) {
277 m_selectionManager
->reset();
280 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
281 if (event
->modifiers() & Qt::ControlModifier
) {
285 KCategorizedView::wheelEvent(event
);
286 // if the icons are aligned left to right, the vertical wheel event should
287 // be applied to the horizontal scrollbar
288 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
289 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
290 (settings
->arrangement() == QListView::LeftToRight
);
291 if (scrollHorizontal
) {
292 QWheelEvent
horizEvent(event
->pos(),
297 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
301 void DolphinIconsView::showEvent(QShowEvent
* event
)
303 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
304 delegate
->setMaximumSize(m_itemSize
);
306 KCategorizedView::showEvent(event
);
309 void DolphinIconsView::leaveEvent(QEvent
* event
)
311 KCategorizedView::leaveEvent(event
);
312 // if the mouse is above an item and moved very fast outside the widget,
313 // no viewportEntered() signal might be emitted although the mouse has been moved
314 // above the viewport
315 m_controller
->emitViewportEntered();
318 void DolphinIconsView::slotShowPreviewChanged()
320 const DolphinView
* view
= m_controller
->dolphinView();
321 updateGridSize(view
->showPreview(), additionalInfoCount());
324 void DolphinIconsView::slotAdditionalInfoChanged()
326 const DolphinView
* view
= m_controller
->dolphinView();
327 const bool showPreview
= view
->showPreview();
328 updateGridSize(showPreview
, view
->additionalInfo().count());
331 void DolphinIconsView::zoomIn()
333 if (isZoomInPossible()) {
334 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
336 const int oldIconSize
= settings
->iconSize();
337 int newIconSize
= oldIconSize
;
339 const bool showPreview
= m_controller
->dolphinView()->showPreview();
341 const int previewSize
= increasedIconSize(settings
->previewSize());
342 settings
->setPreviewSize(previewSize
);
344 newIconSize
= increasedIconSize(oldIconSize
);
345 settings
->setIconSize(newIconSize
);
348 // increase also the grid size
349 const int diff
= newIconSize
- oldIconSize
;
350 settings
->setItemWidth(settings
->itemWidth() + diff
);
351 settings
->setItemHeight(settings
->itemHeight() + diff
);
353 updateGridSize(showPreview
, additionalInfoCount());
357 void DolphinIconsView::zoomOut()
359 if (isZoomOutPossible()) {
360 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
362 const int oldIconSize
= settings
->iconSize();
363 int newIconSize
= oldIconSize
;
365 const bool showPreview
= m_controller
->dolphinView()->showPreview();
367 const int previewSize
= decreasedIconSize(settings
->previewSize());
368 settings
->setPreviewSize(previewSize
);
370 newIconSize
= decreasedIconSize(settings
->iconSize());
371 settings
->setIconSize(newIconSize
);
374 // decrease also the grid size
375 const int diff
= oldIconSize
- newIconSize
;
376 settings
->setItemWidth(settings
->itemWidth() - diff
);
377 settings
->setItemHeight(settings
->itemHeight() - diff
);
379 updateGridSize(showPreview
, additionalInfoCount());
383 void DolphinIconsView::requestActivation()
385 m_controller
->requestActivation();
388 void DolphinIconsView::updateFont()
390 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
391 Q_ASSERT(settings
!= 0);
393 if (settings
->useSystemFont()) {
394 m_font
= KGlobalSettings::generalFont();
398 bool DolphinIconsView::isZoomInPossible() const
400 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
401 const bool showPreview
= m_controller
->dolphinView()->showPreview();
402 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
403 return size
< KIconLoader::SizeEnormous
;
406 bool DolphinIconsView::isZoomOutPossible() const
408 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
409 const bool showPreview
= m_controller
->dolphinView()->showPreview();
410 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
411 return size
> KIconLoader::SizeSmall
;
414 int DolphinIconsView::increasedIconSize(int size
) const
418 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
419 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
420 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
421 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
422 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
423 default: Q_ASSERT(false); break;
428 int DolphinIconsView::decreasedIconSize(int size
) const
432 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
433 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
434 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
435 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
436 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
437 default: Q_ASSERT(false); break;
442 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
444 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
445 Q_ASSERT(settings
!= 0);
447 int itemWidth
= settings
->itemWidth();
448 int itemHeight
= settings
->itemHeight();
449 int size
= settings
->iconSize();
452 const int previewSize
= settings
->previewSize();
453 const int diff
= previewSize
- size
;
460 Q_ASSERT(additionalInfoCount
>= 0);
461 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
463 if (settings
->arrangement() == QListView::TopToBottom
) {
464 // The decoration width indirectly defines the maximum
465 // width for the text wrapping. To use the maximum item width
466 // for text wrapping, it is used as decoration width.
467 m_decorationSize
= QSize(itemWidth
, size
);
468 setIconSize(QSize(itemWidth
, size
));
470 m_decorationSize
= QSize(size
, size
);
471 setIconSize(QSize(size
, size
));
474 m_itemSize
= QSize(itemWidth
, itemHeight
);
476 const int spacing
= settings
->gridSpacing();
477 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
479 m_controller
->setZoomInPossible(isZoomInPossible());
480 m_controller
->setZoomOutPossible(isZoomOutPossible());
482 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
484 delegate
->setMaximumSize(m_itemSize
);
487 if (m_selectionManager
!= 0) {
488 m_selectionManager
->reset();
492 int DolphinIconsView::additionalInfoCount() const
494 const DolphinView
* view
= m_controller
->dolphinView();
495 return view
->additionalInfo().count();
498 #include "dolphiniconsview.moc"