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 setMouseTracking(true);
61 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
62 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
63 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
64 // RETURN-key in keyPressEvent().
65 if (KGlobalSettings::singleClick()) {
66 connect(this, SIGNAL(clicked(const QModelIndex
&)),
67 controller
, SLOT(triggerItem(const QModelIndex
&)));
68 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
69 SelectionManager
* selManager
= new SelectionManager(this);
70 connect(selManager
, SIGNAL(selectionChanged()),
71 this, SLOT(requestActivation()));
72 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
73 selManager
, SLOT(reset()));
76 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
77 controller
, SLOT(triggerItem(const QModelIndex
&)));
79 connect(this, SIGNAL(entered(const QModelIndex
&)),
80 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
81 connect(this, SIGNAL(viewportEntered()),
82 controller
, SLOT(emitViewportEntered()));
83 connect(controller
, SIGNAL(zoomIn()),
84 this, SLOT(zoomIn()));
85 connect(controller
, SIGNAL(zoomOut()),
86 this, SLOT(zoomOut()));
88 const DolphinView
* view
= controller
->dolphinView();
89 connect(view
, SIGNAL(showPreviewChanged()),
90 this, SLOT(slotShowPreviewChanged()));
91 connect(view
, SIGNAL(additionalInfoChanged()),
92 this, SLOT(slotAdditionalInfoChanged()));
94 // apply the icons mode settings to the widget
95 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
96 Q_ASSERT(settings
!= 0);
98 if (settings
->useSystemFont()) {
99 m_font
= KGlobalSettings::generalFont();
101 m_font
= QFont(settings
->fontFamily(),
102 settings
->fontSize(),
103 settings
->fontWeight(),
104 settings
->italicFont());
107 setWordWrap(settings
->numberOfTextlines() > 1);
108 updateGridSize(view
->showPreview(), 0);
110 if (settings
->arrangement() == QListView::TopToBottom
) {
111 setFlow(QListView::LeftToRight
);
112 m_decorationPosition
= QStyleOptionViewItem::Top
;
113 m_displayAlignment
= Qt::AlignHCenter
;
115 setFlow(QListView::TopToBottom
);
116 m_decorationPosition
= QStyleOptionViewItem::Left
;
117 m_displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
120 m_categoryDrawer
= new DolphinCategoryDrawer();
121 setCategoryDrawer(m_categoryDrawer
);
125 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
126 this, SLOT(updateFont()));
129 DolphinIconsView::~DolphinIconsView()
131 delete m_categoryDrawer
;
132 m_categoryDrawer
= 0;
135 QRect
DolphinIconsView::visualRect(const QModelIndex
& index
) const
137 const bool leftToRightFlow
= (flow() == QListView::LeftToRight
);
139 QRect itemRect
= KCategorizedView::visualRect(index
);
141 const int maxWidth
= m_itemSize
.width();
142 const int maxHeight
= m_itemSize
.height();
144 if (itemRect
.width() > maxWidth
) {
145 // assure that the maximum item width is not exceeded
146 if (leftToRightFlow
) {
147 const int left
= itemRect
.left() + (itemRect
.width() - maxWidth
) / 2;
148 itemRect
.setLeft(left
);
150 itemRect
.setWidth(maxWidth
);
153 if (itemRect
.height() > maxHeight
) {
154 // assure that the maximum item height is not exceeded
155 if (!leftToRightFlow
) {
156 const int top
= itemRect
.top() + (itemRect
.height() - maxHeight
) / 2;
157 itemRect
.setTop(top
);
159 itemRect
.setHeight(maxHeight
);
162 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
163 if (leftToRightFlow
&& !proxyModel
->isCategorizedModel()) {
164 // TODO: QListView::visualRect() calculates a wrong position of the items under
165 // certain circumstances (e. g. if the text is too long). This issue is bypassed
166 // by the following code (I'll try create a patch for Qt but as Dolphin must also work with
167 // Qt 4.3.0 this workaround must get applied at least for KDE 4.0).
168 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
169 const int margin
= settings
->gridSpacing();
170 const int gridWidth
= gridSize().width();
171 const int gridIndex
= (itemRect
.left() - margin
+ 1) / gridWidth
;
172 const int centerInc
= (maxWidth
- itemRect
.width()) / 2;
173 itemRect
.moveLeft((gridIndex
* gridWidth
) + margin
+ centerInc
);
179 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
181 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
182 viewOptions
.font
= m_font
;
183 viewOptions
.decorationPosition
= m_decorationPosition
;
184 viewOptions
.decorationSize
= m_decorationSize
;
185 viewOptions
.displayAlignment
= m_displayAlignment
;
186 viewOptions
.showDecorationSelected
= true;
190 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
192 KCategorizedView::contextMenuEvent(event
);
193 m_controller
->triggerContextMenuRequest(event
->pos());
196 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
198 m_controller
->requestActivation();
199 if (!indexAt(event
->pos()).isValid()) {
200 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
201 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
206 KCategorizedView::mousePressEvent(event
);
209 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
211 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
212 // fix this in KDE 4.1
213 KCategorizedView::startDrag(supportedActions
);
214 DragAndDropHelper::startDrag(this, supportedActions
);
217 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
219 if (event
->mimeData()->hasUrls()) {
220 event
->acceptProposedAction();
224 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
226 KCategorizedView::dragLeaveEvent(event
);
227 setDirtyRegion(m_dropRect
);
230 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
232 KCategorizedView::dragMoveEvent(event
);
234 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
235 const QModelIndex index
= indexAt(event
->pos());
236 setDirtyRegion(m_dropRect
);
238 m_dropRect
.setSize(QSize()); // set as invalid
239 if (index
.isValid()) {
240 const KFileItem item
= m_controller
->itemForIndex(index
);
241 if (!item
.isNull() && item
.isDir()) {
242 m_dropRect
= visualRect(index
);
244 m_dropRect
.setSize(QSize()); // set as invalid
247 if (event
->mimeData()->hasUrls()) {
248 // accept url drops, independently from the destination item
249 event
->acceptProposedAction();
252 setDirtyRegion(m_dropRect
);
255 void DolphinIconsView::dropEvent(QDropEvent
* event
)
257 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
258 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
259 if (!urls
.isEmpty()) {
260 const QModelIndex index
= indexAt(event
->pos());
261 const KFileItem item
= m_controller
->itemForIndex(index
);
262 m_controller
->indicateDroppedUrls(urls
,
265 event
->acceptProposedAction();
269 KCategorizedView::dropEvent(event
);
272 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
274 KCategorizedView::keyPressEvent(event
);
275 m_controller
->handleKeyPressEvent(event
);
278 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
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::slotShowPreviewChanged()
303 const DolphinView
* view
= m_controller
->dolphinView();
304 updateGridSize(view
->showPreview(), additionalInfoCount());
307 void DolphinIconsView::slotAdditionalInfoChanged()
309 const DolphinView
* view
= m_controller
->dolphinView();
310 const bool showPreview
= view
->showPreview();
311 updateGridSize(showPreview
, view
->additionalInfo().count());
314 void DolphinIconsView::zoomIn()
316 if (isZoomInPossible()) {
317 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
319 const int oldIconSize
= settings
->iconSize();
320 int newIconSize
= oldIconSize
;
322 const bool showPreview
= m_controller
->dolphinView()->showPreview();
324 const int previewSize
= increasedIconSize(settings
->previewSize());
325 settings
->setPreviewSize(previewSize
);
327 newIconSize
= increasedIconSize(oldIconSize
);
328 settings
->setIconSize(newIconSize
);
329 if (settings
->previewSize() < newIconSize
) {
330 // assure that the preview size is always >= the icon size
331 settings
->setPreviewSize(newIconSize
);
335 // increase also the grid size
336 const int diff
= newIconSize
- oldIconSize
;
337 settings
->setItemWidth(settings
->itemWidth() + diff
);
338 settings
->setItemHeight(settings
->itemHeight() + diff
);
340 updateGridSize(showPreview
, additionalInfoCount());
344 void DolphinIconsView::zoomOut()
346 if (isZoomOutPossible()) {
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
= decreasedIconSize(settings
->previewSize());
355 settings
->setPreviewSize(previewSize
);
356 if (settings
->iconSize() > previewSize
) {
357 // assure that the icon size is always <= the preview size
358 newIconSize
= previewSize
;
359 settings
->setIconSize(newIconSize
);
362 newIconSize
= decreasedIconSize(settings
->iconSize());
363 settings
->setIconSize(newIconSize
);
366 // decrease also the grid size
367 const int diff
= oldIconSize
- newIconSize
;
368 settings
->setItemWidth(settings
->itemWidth() - diff
);
369 settings
->setItemHeight(settings
->itemHeight() - diff
);
371 updateGridSize(showPreview
, additionalInfoCount());
375 void DolphinIconsView::requestActivation()
377 m_controller
->requestActivation();
380 void DolphinIconsView::updateFont()
382 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
383 Q_ASSERT(settings
!= 0);
385 if (settings
->useSystemFont()) {
386 m_font
= KGlobalSettings::generalFont();
390 bool DolphinIconsView::isZoomInPossible() const
392 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
393 const bool showPreview
= m_controller
->dolphinView()->showPreview();
394 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
395 return size
< KIconLoader::SizeEnormous
;
398 bool DolphinIconsView::isZoomOutPossible() 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::SizeSmall
;
406 int DolphinIconsView::increasedIconSize(int size
) const
410 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
411 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
412 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
413 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
414 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
415 default: Q_ASSERT(false); break;
420 int DolphinIconsView::decreasedIconSize(int size
) const
424 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
425 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
426 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
427 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
428 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
429 default: Q_ASSERT(false); break;
434 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
436 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
437 Q_ASSERT(settings
!= 0);
439 int itemWidth
= settings
->itemWidth();
440 int itemHeight
= settings
->itemHeight();
441 int size
= settings
->iconSize();
444 const int previewSize
= settings
->previewSize();
445 const int diff
= previewSize
- size
;
453 Q_ASSERT(additionalInfoCount
>= 0);
454 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
456 if (settings
->arrangement() == QListView::TopToBottom
) {
457 // The decoration width indirectly defines the maximum
458 // width for the text wrapping. To use the maximum item width
459 // for text wrapping, it is used as decoration width.
460 m_decorationSize
= QSize(itemWidth
, size
);
461 setIconSize(QSize(itemWidth
, size
));
463 m_decorationSize
= QSize(size
, size
);
464 setIconSize(QSize(size
, size
));
467 m_itemSize
= QSize(itemWidth
, itemHeight
);
469 const int spacing
= settings
->gridSpacing();
470 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
472 m_controller
->setZoomInPossible(isZoomInPossible());
473 m_controller
->setZoomOutPossible(isZoomOutPossible());
476 int DolphinIconsView::additionalInfoCount() const
478 const DolphinView
* view
= m_controller
->dolphinView();
479 return view
->additionalInfo().count();
482 #include "dolphiniconsview.moc"