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
),
52 Q_ASSERT(controller
!= 0);
53 setViewMode(QListView::IconMode
);
54 setResizeMode(QListView::Adjust
);
55 setSpacing(KDialog::spacingHint());
56 setMovement(QListView::Static
);
58 viewport()->setAcceptDrops(true);
60 setMouseTracking(true);
61 viewport()->setAttribute(Qt::WA_Hover
);
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 this, SLOT(triggerItem(const QModelIndex
&)));
70 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
71 SelectionManager
* selManager
= new SelectionManager(this);
72 connect(selManager
, SIGNAL(selectionChanged()),
73 this, SLOT(requestActivation()));
74 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
75 selManager
, SLOT(reset()));
78 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
79 this, SLOT(triggerItem(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 connect(this, SIGNAL(entered(const QModelIndex
&)),
95 this, SLOT(slotEntered(const QModelIndex
&)));
97 // apply the icons mode settings to the widget
98 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
99 Q_ASSERT(settings
!= 0);
101 m_font
= QFont(settings
->fontFamily(), settings
->fontSize());
102 m_font
.setItalic(settings
->italicFont());
103 m_font
.setBold(settings
->boldFont());
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
);
124 DolphinIconsView::~DolphinIconsView()
126 delete m_categoryDrawer
;
127 m_categoryDrawer
= 0;
130 QRect
DolphinIconsView::visualRect(const QModelIndex
& index
) const
132 const bool leftToRightFlow
= (flow() == QListView::LeftToRight
);
134 QRect itemRect
= KCategorizedView::visualRect(index
);
136 const int maxWidth
= m_itemSize
.width();
137 const int maxHeight
= m_itemSize
.height();
139 if (itemRect
.width() > maxWidth
) {
140 // assure that the maximum item width is not exceeded
141 if (leftToRightFlow
) {
142 const int left
= itemRect
.left() + (itemRect
.width() - maxWidth
) / 2;
143 itemRect
.setLeft(left
);
145 itemRect
.setWidth(maxWidth
);
148 if (itemRect
.height() > maxHeight
) {
149 // assure that the maximum item height is not exceeded
150 if (!leftToRightFlow
) {
151 const int top
= itemRect
.top() + (itemRect
.height() - maxHeight
) / 2;
152 itemRect
.setTop(top
);
154 itemRect
.setHeight(maxHeight
);
157 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
158 if (leftToRightFlow
&& !proxyModel
->isCategorizedModel()) {
159 // TODO: QListView::visualRect() calculates a wrong position of the items under
160 // certain circumstances (e. g. if the text is too long). This issue is bypassed
161 // by the following code (I'll try create a patch for Qt but as Dolphin must also work with
162 // Qt 4.3.0 this workaround must get applied at least for KDE 4.0).
163 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
164 const int margin
= settings
->gridSpacing();
165 const int gridWidth
= gridSize().width();
166 const int gridIndex
= (itemRect
.left() - margin
+ 1) / gridWidth
;
167 const int centerInc
= (maxWidth
- itemRect
.width()) / 2;
168 itemRect
.moveLeft((gridIndex
* gridWidth
) + margin
+ centerInc
);
174 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
176 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
177 viewOptions
.font
= m_font
;
178 viewOptions
.decorationPosition
= m_decorationPosition
;
179 viewOptions
.decorationSize
= m_decorationSize
;
180 viewOptions
.displayAlignment
= m_displayAlignment
;
181 viewOptions
.showDecorationSelected
= true;
185 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
187 KCategorizedView::contextMenuEvent(event
);
188 m_controller
->triggerContextMenuRequest(event
->pos());
191 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
193 m_controller
->requestActivation();
194 if (!indexAt(event
->pos()).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();
220 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
222 KCategorizedView::dragLeaveEvent(event
);
224 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
226 setDirtyRegion(m_dropRect
);
229 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
231 KCategorizedView::dragMoveEvent(event
);
233 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
234 const QModelIndex index
= indexAt(event
->pos());
235 setDirtyRegion(m_dropRect
);
237 m_dropRect
.setSize(QSize()); // set as invalid
238 if (index
.isValid()) {
239 const KFileItem item
= itemForIndex(index
);
240 if (!item
.isNull() && item
.isDir()) {
241 m_dropRect
= visualRect(index
);
243 m_dropRect
.setSize(QSize()); // set as invalid
246 if (event
->mimeData()->hasUrls()) {
247 // accept url drops, independently from the destination item
248 event
->acceptProposedAction();
251 setDirtyRegion(m_dropRect
);
254 void DolphinIconsView::dropEvent(QDropEvent
* event
)
256 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
257 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
258 if (!urls
.isEmpty()) {
259 const QModelIndex index
= indexAt(event
->pos());
260 const KFileItem item
= itemForIndex(index
);
261 m_controller
->indicateDroppedUrls(urls
,
264 event
->acceptProposedAction();
268 KCategorizedView::dropEvent(event
);
273 void DolphinIconsView::paintEvent(QPaintEvent
* event
)
275 KCategorizedView::paintEvent(event
);
277 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
279 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
280 DragAndDropHelper::drawHoverIndication(this, m_dropRect
, brush
);
284 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
286 KCategorizedView::keyPressEvent(event
);
288 const QItemSelectionModel
* selModel
= selectionModel();
289 const QModelIndex currentIndex
= selModel
->currentIndex();
290 const bool trigger
= currentIndex
.isValid()
291 && (event
->key() == Qt::Key_Return
)
292 && (selModel
->selectedIndexes().count() <= 1);
294 triggerItem(currentIndex
);
298 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
300 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
301 if ((event
->modifiers() & Qt::ControlModifier
) == Qt::ControlModifier
) {
305 KCategorizedView::wheelEvent(event
);
306 // if the icons are aligned left to right, the vertical wheel event should
307 // be applied to the horizontal scrollbar
308 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
309 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
310 (settings
->arrangement() == QListView::LeftToRight
);
311 if (scrollHorizontal
) {
312 QWheelEvent
horizEvent(event
->pos(),
317 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
321 void DolphinIconsView::triggerItem(const QModelIndex
& index
)
323 m_controller
->triggerItem(itemForIndex(index
));
326 void DolphinIconsView::slotEntered(const QModelIndex
& index
)
328 m_controller
->emitItemEntered(itemForIndex(index
));
331 void DolphinIconsView::slotShowPreviewChanged()
333 const DolphinView
* view
= m_controller
->dolphinView();
334 updateGridSize(view
->showPreview(), additionalInfoCount());
337 void DolphinIconsView::slotAdditionalInfoChanged()
339 const DolphinView
* view
= m_controller
->dolphinView();
340 const bool showPreview
= view
->showPreview();
341 updateGridSize(showPreview
, view
->additionalInfo().count());
344 void DolphinIconsView::zoomIn()
346 if (isZoomInPossible()) {
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
= increasedIconSize(settings
->previewSize());
355 settings
->setPreviewSize(previewSize
);
357 newIconSize
= increasedIconSize(oldIconSize
);
358 settings
->setIconSize(newIconSize
);
359 if (settings
->previewSize() < newIconSize
) {
360 // assure that the preview size is always >= the icon size
361 settings
->setPreviewSize(newIconSize
);
365 // increase also the grid size
366 const int diff
= newIconSize
- oldIconSize
;
367 settings
->setItemWidth(settings
->itemWidth() + diff
);
368 settings
->setItemHeight(settings
->itemHeight() + diff
);
370 updateGridSize(showPreview
, additionalInfoCount());
374 void DolphinIconsView::zoomOut()
376 if (isZoomOutPossible()) {
377 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
379 const int oldIconSize
= settings
->iconSize();
380 int newIconSize
= oldIconSize
;
382 const bool showPreview
= m_controller
->dolphinView()->showPreview();
384 const int previewSize
= decreasedIconSize(settings
->previewSize());
385 settings
->setPreviewSize(previewSize
);
386 if (settings
->iconSize() > previewSize
) {
387 // assure that the icon size is always <= the preview size
388 newIconSize
= previewSize
;
389 settings
->setIconSize(newIconSize
);
392 newIconSize
= decreasedIconSize(settings
->iconSize());
393 settings
->setIconSize(newIconSize
);
396 // decrease also the grid size
397 const int diff
= oldIconSize
- newIconSize
;
398 settings
->setItemWidth(settings
->itemWidth() - diff
);
399 settings
->setItemHeight(settings
->itemHeight() - diff
);
401 updateGridSize(showPreview
, additionalInfoCount());
405 void DolphinIconsView::requestActivation()
407 m_controller
->requestActivation();
410 bool DolphinIconsView::isZoomInPossible() const
412 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
413 const bool showPreview
= m_controller
->dolphinView()->showPreview();
414 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
415 return size
< KIconLoader::SizeEnormous
;
418 bool DolphinIconsView::isZoomOutPossible() const
420 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
421 const bool showPreview
= m_controller
->dolphinView()->showPreview();
422 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
423 return size
> KIconLoader::SizeSmall
;
426 int DolphinIconsView::increasedIconSize(int size
) const
430 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
431 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
432 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
433 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
434 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
435 default: Q_ASSERT(false); break;
440 int DolphinIconsView::decreasedIconSize(int size
) const
444 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
445 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
446 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
447 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
448 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
449 default: Q_ASSERT(false); break;
454 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
456 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
457 Q_ASSERT(settings
!= 0);
459 int itemWidth
= settings
->itemWidth();
460 int itemHeight
= settings
->itemHeight();
461 int size
= settings
->iconSize();
464 const int previewSize
= settings
->previewSize();
465 const int diff
= previewSize
- size
;
472 setIconSize(QSize(size
, size
));
474 Q_ASSERT(additionalInfoCount
>= 0);
475 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
477 if (settings
->arrangement() == QListView::TopToBottom
) {
478 // The decoration width indirectly defines the maximum
479 // width for the text wrapping. To use the maximum item width
480 // for text wrapping, it is used as decoration width.
481 m_decorationSize
= QSize(itemWidth
, size
);
483 m_decorationSize
= QSize(size
, size
);
486 m_itemSize
= QSize(itemWidth
, itemHeight
);
488 const int spacing
= settings
->gridSpacing();
489 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
491 m_controller
->setZoomInPossible(isZoomInPossible());
492 m_controller
->setZoomOutPossible(isZoomOutPossible());
495 KFileItem
DolphinIconsView::itemForIndex(const QModelIndex
& index
) const
497 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
498 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
499 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
500 return dirModel
->itemForIndex(dirIndex
);
503 int DolphinIconsView::additionalInfoCount() const
505 const DolphinView
* view
= m_controller
->dolphinView();
506 return view
->additionalInfo().count();
509 #include "dolphiniconsview.moc"