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>
34 #include <QAbstractProxyModel>
35 #include <QApplication>
40 DolphinIconsView::DolphinIconsView(QWidget
* parent
, DolphinController
* controller
) :
41 KCategorizedView(parent
),
42 m_controller(controller
),
46 m_decorationPosition(QStyleOptionViewItem::Top
),
47 m_displayAlignment(Qt::AlignHCenter
),
51 Q_ASSERT(controller
!= 0);
52 setViewMode(QListView::IconMode
);
53 setResizeMode(QListView::Adjust
);
54 setSpacing(KDialog::spacingHint());
55 setMovement(QListView::Static
);
57 viewport()->setAcceptDrops(true);
59 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
60 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
61 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
62 // RETURN-key in keyPressEvent().
63 if (KGlobalSettings::singleClick()) {
64 connect(this, SIGNAL(clicked(const QModelIndex
&)),
65 controller
, SLOT(triggerItem(const QModelIndex
&)));
66 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
67 SelectionManager
* selManager
= new SelectionManager(this);
68 connect(selManager
, SIGNAL(selectionChanged()),
69 this, SLOT(requestActivation()));
70 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
71 selManager
, SLOT(reset()));
74 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
75 controller
, SLOT(triggerItem(const QModelIndex
&)));
77 connect(this, SIGNAL(entered(const QModelIndex
&)),
78 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
79 connect(this, SIGNAL(viewportEntered()),
80 controller
, SLOT(emitViewportEntered()));
81 connect(controller
, SIGNAL(zoomIn()),
82 this, SLOT(zoomIn()));
83 connect(controller
, SIGNAL(zoomOut()),
84 this, SLOT(zoomOut()));
86 const DolphinView
* view
= controller
->dolphinView();
87 connect(view
, SIGNAL(showPreviewChanged()),
88 this, SLOT(slotShowPreviewChanged()));
89 connect(view
, SIGNAL(additionalInfoChanged()),
90 this, SLOT(slotAdditionalInfoChanged()));
92 // apply the icons mode settings to the widget
93 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
94 Q_ASSERT(settings
!= 0);
96 if (settings
->useSystemFont()) {
97 m_font
= KGlobalSettings::generalFont();
99 m_font
= QFont(settings
->fontFamily(),
100 settings
->fontSize(),
101 settings
->fontWeight(),
102 settings
->italicFont());
105 setWordWrap(settings
->numberOfTextlines() > 1);
106 updateGridSize(view
->showPreview(), 0);
108 if (settings
->arrangement() == QListView::TopToBottom
) {
109 setFlow(QListView::LeftToRight
);
110 m_decorationPosition
= QStyleOptionViewItem::Top
;
111 m_displayAlignment
= Qt::AlignHCenter
;
113 setFlow(QListView::TopToBottom
);
114 m_decorationPosition
= QStyleOptionViewItem::Left
;
115 m_displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
118 m_categoryDrawer
= new DolphinCategoryDrawer();
119 setCategoryDrawer(m_categoryDrawer
);
123 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
124 this, SLOT(updateFont()));
127 DolphinIconsView::~DolphinIconsView()
129 delete m_categoryDrawer
;
130 m_categoryDrawer
= 0;
133 QRect
DolphinIconsView::visualRect(const QModelIndex
& index
) const
135 const bool leftToRightFlow
= (flow() == QListView::LeftToRight
);
137 QRect itemRect
= KCategorizedView::visualRect(index
);
139 const int maxWidth
= m_itemSize
.width();
140 const int maxHeight
= m_itemSize
.height();
142 if (itemRect
.width() > maxWidth
) {
143 // assure that the maximum item width is not exceeded
144 if (leftToRightFlow
) {
145 const int left
= itemRect
.left() + (itemRect
.width() - maxWidth
) / 2;
146 itemRect
.setLeft(left
);
148 itemRect
.setWidth(maxWidth
);
151 if (itemRect
.height() > maxHeight
) {
152 // assure that the maximum item height is not exceeded
153 if (!leftToRightFlow
) {
154 const int top
= itemRect
.top() + (itemRect
.height() - maxHeight
) / 2;
155 itemRect
.setTop(top
);
157 itemRect
.setHeight(maxHeight
);
160 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
161 if (leftToRightFlow
&& !proxyModel
->isCategorizedModel()) {
162 // TODO: QListView::visualRect() calculates a wrong position of the items under
163 // certain circumstances (e. g. if the text is too long). This issue is bypassed
164 // by the following code (I'll try create a patch for Qt but as Dolphin must also work with
165 // Qt 4.3.0 this workaround must get applied at least for KDE 4.0).
166 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
167 const int margin
= settings
->gridSpacing();
168 const int gridWidth
= gridSize().width();
169 const int gridIndex
= (itemRect
.left() - margin
+ 1) / gridWidth
;
170 const int centerInc
= (maxWidth
- itemRect
.width()) / 2;
171 itemRect
.moveLeft((gridIndex
* gridWidth
) + margin
+ centerInc
);
177 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
179 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
180 viewOptions
.font
= m_font
;
181 viewOptions
.decorationPosition
= m_decorationPosition
;
182 viewOptions
.decorationSize
= m_decorationSize
;
183 viewOptions
.displayAlignment
= m_displayAlignment
;
184 viewOptions
.showDecorationSelected
= true;
188 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
190 KCategorizedView::contextMenuEvent(event
);
191 m_controller
->triggerContextMenuRequest(event
->pos());
194 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
196 m_controller
->requestActivation();
197 if (!indexAt(event
->pos()).isValid()) {
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
);
276 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
278 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
279 if (event
->modifiers() & Qt::ControlModifier
) {
283 KCategorizedView::wheelEvent(event
);
284 // if the icons are aligned left to right, the vertical wheel event should
285 // be applied to the horizontal scrollbar
286 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
287 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
288 (settings
->arrangement() == QListView::LeftToRight
);
289 if (scrollHorizontal
) {
290 QWheelEvent
horizEvent(event
->pos(),
295 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
299 void DolphinIconsView::slotShowPreviewChanged()
301 const DolphinView
* view
= m_controller
->dolphinView();
302 updateGridSize(view
->showPreview(), additionalInfoCount());
305 void DolphinIconsView::slotAdditionalInfoChanged()
307 const DolphinView
* view
= m_controller
->dolphinView();
308 const bool showPreview
= view
->showPreview();
309 updateGridSize(showPreview
, view
->additionalInfo().count());
312 void DolphinIconsView::zoomIn()
314 if (isZoomInPossible()) {
315 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
317 const int oldIconSize
= settings
->iconSize();
318 int newIconSize
= oldIconSize
;
320 const bool showPreview
= m_controller
->dolphinView()->showPreview();
322 const int previewSize
= increasedIconSize(settings
->previewSize());
323 settings
->setPreviewSize(previewSize
);
325 newIconSize
= increasedIconSize(oldIconSize
);
326 settings
->setIconSize(newIconSize
);
327 if (settings
->previewSize() < newIconSize
) {
328 // assure that the preview size is always >= the icon size
329 settings
->setPreviewSize(newIconSize
);
333 // increase also the grid size
334 const int diff
= newIconSize
- oldIconSize
;
335 settings
->setItemWidth(settings
->itemWidth() + diff
);
336 settings
->setItemHeight(settings
->itemHeight() + diff
);
338 updateGridSize(showPreview
, additionalInfoCount());
342 void DolphinIconsView::zoomOut()
344 if (isZoomOutPossible()) {
345 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
347 const int oldIconSize
= settings
->iconSize();
348 int newIconSize
= oldIconSize
;
350 const bool showPreview
= m_controller
->dolphinView()->showPreview();
352 const int previewSize
= decreasedIconSize(settings
->previewSize());
353 settings
->setPreviewSize(previewSize
);
354 if (settings
->iconSize() > previewSize
) {
355 // assure that the icon size is always <= the preview size
356 newIconSize
= previewSize
;
357 settings
->setIconSize(newIconSize
);
360 newIconSize
= decreasedIconSize(settings
->iconSize());
361 settings
->setIconSize(newIconSize
);
364 // decrease also the grid size
365 const int diff
= oldIconSize
- newIconSize
;
366 settings
->setItemWidth(settings
->itemWidth() - diff
);
367 settings
->setItemHeight(settings
->itemHeight() - diff
);
369 updateGridSize(showPreview
, additionalInfoCount());
373 void DolphinIconsView::requestActivation()
375 m_controller
->requestActivation();
378 void DolphinIconsView::updateFont()
380 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
381 Q_ASSERT(settings
!= 0);
383 if (settings
->useSystemFont()) {
384 m_font
= KGlobalSettings::generalFont();
388 bool DolphinIconsView::isZoomInPossible() const
390 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
391 const bool showPreview
= m_controller
->dolphinView()->showPreview();
392 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
393 return size
< KIconLoader::SizeEnormous
;
396 bool DolphinIconsView::isZoomOutPossible() const
398 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
399 const bool showPreview
= m_controller
->dolphinView()->showPreview();
400 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
401 return size
> KIconLoader::SizeSmall
;
404 int DolphinIconsView::increasedIconSize(int size
) const
408 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
409 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
410 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
411 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
412 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
413 default: Q_ASSERT(false); break;
418 int DolphinIconsView::decreasedIconSize(int size
) const
422 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
423 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
424 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
425 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
426 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
427 default: Q_ASSERT(false); break;
432 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
434 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
435 Q_ASSERT(settings
!= 0);
437 int itemWidth
= settings
->itemWidth();
438 int itemHeight
= settings
->itemHeight();
439 int size
= settings
->iconSize();
442 const int previewSize
= settings
->previewSize();
443 const int diff
= previewSize
- size
;
451 Q_ASSERT(additionalInfoCount
>= 0);
452 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
454 if (settings
->arrangement() == QListView::TopToBottom
) {
455 // The decoration width indirectly defines the maximum
456 // width for the text wrapping. To use the maximum item width
457 // for text wrapping, it is used as decoration width.
458 m_decorationSize
= QSize(itemWidth
, size
);
459 setIconSize(QSize(itemWidth
, size
));
461 m_decorationSize
= QSize(size
, size
);
462 setIconSize(QSize(size
, size
));
465 m_itemSize
= QSize(itemWidth
, itemHeight
);
467 const int spacing
= settings
->gridSpacing();
468 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
470 m_controller
->setZoomInPossible(isZoomInPossible());
471 m_controller
->setZoomOutPossible(isZoomOutPossible());
474 int DolphinIconsView::additionalInfoCount() const
476 const DolphinView
* view
= m_controller
->dolphinView();
477 return view
->additionalInfo().count();
480 #include "dolphiniconsview.moc"