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_controller(controller
),
44 m_selectionManager(0),
48 m_decorationPosition(QStyleOptionViewItem::Top
),
49 m_displayAlignment(Qt::AlignHCenter
),
53 Q_ASSERT(controller
!= 0);
54 setViewMode(QListView::IconMode
);
55 setResizeMode(QListView::Adjust
);
56 setSpacing(KDialog::spacingHint());
57 setMovement(QListView::Static
);
59 setEditTriggers(QAbstractItemView::NoEditTriggers
);
60 viewport()->setAcceptDrops(true);
62 setMouseTracking(true);
64 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
65 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
66 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
67 // RETURN-key in keyPressEvent().
68 if (KGlobalSettings::singleClick()) {
69 connect(this, SIGNAL(clicked(const QModelIndex
&)),
70 controller
, SLOT(triggerItem(const QModelIndex
&)));
71 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
72 m_selectionManager
= new SelectionManager(this);
73 connect(m_selectionManager
, SIGNAL(selectionChanged()),
74 this, SLOT(requestActivation()));
75 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
76 m_selectionManager
, SLOT(reset()));
79 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
80 controller
, SLOT(triggerItem(const QModelIndex
&)));
82 connect(this, SIGNAL(entered(const QModelIndex
&)),
83 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
84 connect(this, SIGNAL(viewportEntered()),
85 controller
, SLOT(emitViewportEntered()));
86 connect(controller
, SIGNAL(zoomIn()),
87 this, SLOT(zoomIn()));
88 connect(controller
, SIGNAL(zoomOut()),
89 this, SLOT(zoomOut()));
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(kdisplayFontChanged()),
129 this, SLOT(updateFont()));
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 ((flow() == QListView::LeftToRight
) && !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 if (!indexAt(event
->pos()).isValid()) {
171 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
172 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
177 KCategorizedView::mousePressEvent(event
);
180 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
182 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
183 // fix this in KDE 4.1
184 KCategorizedView::startDrag(supportedActions
);
185 DragAndDropHelper::startDrag(this, supportedActions
);
188 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
190 if (event
->mimeData()->hasUrls()) {
191 event
->acceptProposedAction();
195 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
197 KCategorizedView::dragLeaveEvent(event
);
198 setDirtyRegion(m_dropRect
);
201 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
203 KCategorizedView::dragMoveEvent(event
);
205 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
206 const QModelIndex index
= indexAt(event
->pos());
207 setDirtyRegion(m_dropRect
);
209 m_dropRect
.setSize(QSize()); // set as invalid
210 if (index
.isValid()) {
211 const KFileItem item
= m_controller
->itemForIndex(index
);
212 if (!item
.isNull() && item
.isDir()) {
213 m_dropRect
= visualRect(index
);
215 m_dropRect
.setSize(QSize()); // set as invalid
218 if (event
->mimeData()->hasUrls()) {
219 // accept url drops, independently from the destination item
220 event
->acceptProposedAction();
223 setDirtyRegion(m_dropRect
);
226 void DolphinIconsView::dropEvent(QDropEvent
* event
)
228 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
229 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
230 if (!urls
.isEmpty()) {
231 const QModelIndex index
= indexAt(event
->pos());
232 const KFileItem item
= m_controller
->itemForIndex(index
);
233 m_controller
->indicateDroppedUrls(urls
,
236 event
->acceptProposedAction();
240 KCategorizedView::dropEvent(event
);
243 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
245 KCategorizedView::keyPressEvent(event
);
246 m_controller
->handleKeyPressEvent(event
);
249 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
251 if (m_selectionManager
!= 0) {
252 m_selectionManager
->reset();
255 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
256 if (event
->modifiers() & Qt::ControlModifier
) {
260 KCategorizedView::wheelEvent(event
);
261 // if the icons are aligned left to right, the vertical wheel event should
262 // be applied to the horizontal scrollbar
263 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
264 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
265 (settings
->arrangement() == QListView::LeftToRight
);
266 if (scrollHorizontal
) {
267 QWheelEvent
horizEvent(event
->pos(),
272 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
276 void DolphinIconsView::showEvent(QShowEvent
* event
)
278 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
279 delegate
->setMaximumSize(m_itemSize
);
281 KCategorizedView::showEvent(event
);
284 void DolphinIconsView::leaveEvent(QEvent
* event
)
286 KCategorizedView::leaveEvent(event
);
287 // if the mouse is above an item and moved very fast outside the widget,
288 // no viewportEntered() signal might be emitted although the mouse has been moved
289 // above the viewport
290 m_controller
->emitViewportEntered();
293 void DolphinIconsView::slotShowPreviewChanged()
295 const DolphinView
* view
= m_controller
->dolphinView();
296 updateGridSize(view
->showPreview(), additionalInfoCount());
299 void DolphinIconsView::slotAdditionalInfoChanged()
301 const DolphinView
* view
= m_controller
->dolphinView();
302 const bool showPreview
= view
->showPreview();
303 updateGridSize(showPreview
, view
->additionalInfo().count());
306 void DolphinIconsView::zoomIn()
308 if (isZoomInPossible()) {
309 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
311 const int oldIconSize
= settings
->iconSize();
312 int newIconSize
= oldIconSize
;
314 const bool showPreview
= m_controller
->dolphinView()->showPreview();
316 const int previewSize
= increasedIconSize(settings
->previewSize());
317 settings
->setPreviewSize(previewSize
);
319 newIconSize
= increasedIconSize(oldIconSize
);
320 settings
->setIconSize(newIconSize
);
323 // increase also the grid size
324 const int diff
= newIconSize
- oldIconSize
;
325 settings
->setItemWidth(settings
->itemWidth() + diff
);
326 settings
->setItemHeight(settings
->itemHeight() + diff
);
328 updateGridSize(showPreview
, additionalInfoCount());
332 void DolphinIconsView::zoomOut()
334 if (isZoomOutPossible()) {
335 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
337 const int oldIconSize
= settings
->iconSize();
338 int newIconSize
= oldIconSize
;
340 const bool showPreview
= m_controller
->dolphinView()->showPreview();
342 const int previewSize
= decreasedIconSize(settings
->previewSize());
343 settings
->setPreviewSize(previewSize
);
345 newIconSize
= decreasedIconSize(settings
->iconSize());
346 settings
->setIconSize(newIconSize
);
349 // decrease also the grid size
350 const int diff
= oldIconSize
- newIconSize
;
351 settings
->setItemWidth(settings
->itemWidth() - diff
);
352 settings
->setItemHeight(settings
->itemHeight() - diff
);
354 updateGridSize(showPreview
, additionalInfoCount());
358 void DolphinIconsView::requestActivation()
360 m_controller
->requestActivation();
363 void DolphinIconsView::updateFont()
365 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
366 Q_ASSERT(settings
!= 0);
368 if (settings
->useSystemFont()) {
369 m_font
= KGlobalSettings::generalFont();
373 bool DolphinIconsView::isZoomInPossible() const
375 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
376 const bool showPreview
= m_controller
->dolphinView()->showPreview();
377 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
378 return size
< KIconLoader::SizeEnormous
;
381 bool DolphinIconsView::isZoomOutPossible() const
383 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
384 const bool showPreview
= m_controller
->dolphinView()->showPreview();
385 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
386 return size
> KIconLoader::SizeSmall
;
389 int DolphinIconsView::increasedIconSize(int size
) const
393 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
394 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
395 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
396 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
397 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
398 default: Q_ASSERT(false); break;
403 int DolphinIconsView::decreasedIconSize(int size
) const
407 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
408 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
409 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
410 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
411 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
412 default: Q_ASSERT(false); break;
417 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
419 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
420 Q_ASSERT(settings
!= 0);
422 int itemWidth
= settings
->itemWidth();
423 int itemHeight
= settings
->itemHeight();
424 int size
= settings
->iconSize();
427 const int previewSize
= settings
->previewSize();
428 const int diff
= previewSize
- size
;
435 Q_ASSERT(additionalInfoCount
>= 0);
436 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
438 if (settings
->arrangement() == QListView::TopToBottom
) {
439 // The decoration width indirectly defines the maximum
440 // width for the text wrapping. To use the maximum item width
441 // for text wrapping, it is used as decoration width.
442 m_decorationSize
= QSize(itemWidth
, size
);
443 setIconSize(QSize(itemWidth
, size
));
445 m_decorationSize
= QSize(size
, size
);
446 setIconSize(QSize(size
, size
));
449 m_itemSize
= QSize(itemWidth
, itemHeight
);
451 const int spacing
= settings
->gridSpacing();
452 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
454 m_controller
->setZoomInPossible(isZoomInPossible());
455 m_controller
->setZoomOutPossible(isZoomOutPossible());
457 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
459 delegate
->setMaximumSize(m_itemSize
);
462 if (m_selectionManager
!= 0) {
463 m_selectionManager
->reset();
467 int DolphinIconsView::additionalInfoCount() const
469 const DolphinView
* view
= m_controller
->dolphinView();
470 return view
->additionalInfo().count();
473 #include "dolphiniconsview.moc"