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 "draganddrophelper.h"
28 #include <kcategorizedsortfilterproxymodel.h>
30 #include <kdirmodel.h>
32 #include <QAbstractProxyModel>
33 #include <QApplication>
38 DolphinIconsView::DolphinIconsView(QWidget
* parent
, DolphinController
* controller
) :
39 KCategorizedView(parent
),
40 m_controller(controller
),
44 m_decorationPosition(QStyleOptionViewItem::Top
),
45 m_displayAlignment(Qt::AlignHCenter
),
50 Q_ASSERT(controller
!= 0);
51 setViewMode(QListView::IconMode
);
52 setResizeMode(QListView::Adjust
);
53 setSpacing(KDialog::spacingHint());
54 setMovement(QListView::Static
);
56 viewport()->setAcceptDrops(true);
58 setMouseTracking(true);
59 viewport()->setAttribute(Qt::WA_Hover
);
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 this, SLOT(triggerItem(const QModelIndex
&)));
69 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
70 this, SLOT(triggerItem(const QModelIndex
&)));
72 connect(this, SIGNAL(viewportEntered()),
73 controller
, SLOT(emitViewportEntered()));
74 connect(controller
, SIGNAL(zoomIn()),
75 this, SLOT(zoomIn()));
76 connect(controller
, SIGNAL(zoomOut()),
77 this, SLOT(zoomOut()));
79 const DolphinView
* view
= controller
->dolphinView();
80 connect(view
, SIGNAL(showPreviewChanged()),
81 this, SLOT(slotShowPreviewChanged()));
82 connect(view
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
83 this, SLOT(slotAdditionalInfoChanged(const KFileItemDelegate::InformationList
&)));
85 connect(this, SIGNAL(entered(const QModelIndex
&)),
86 this, SLOT(slotEntered(const QModelIndex
&)));
88 // apply the icons mode settings to the widget
89 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
90 Q_ASSERT(settings
!= 0);
92 m_font
= QFont(settings
->fontFamily(), settings
->fontSize());
93 m_font
.setItalic(settings
->italicFont());
94 m_font
.setBold(settings
->boldFont());
96 setWordWrap(settings
->numberOfTextlines() > 1);
97 updateGridSize(view
->showPreview(), 0);
99 if (settings
->arrangement() == QListView::TopToBottom
) {
100 setFlow(QListView::LeftToRight
);
101 m_decorationPosition
= QStyleOptionViewItem::Top
;
102 m_displayAlignment
= Qt::AlignHCenter
;
104 setFlow(QListView::TopToBottom
);
105 m_decorationPosition
= QStyleOptionViewItem::Left
;
106 m_displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
109 m_categoryDrawer
= new DolphinCategoryDrawer();
110 setCategoryDrawer(m_categoryDrawer
);
115 DolphinIconsView::~DolphinIconsView()
117 delete m_categoryDrawer
;
118 m_categoryDrawer
= 0;
121 QRect
DolphinIconsView::visualRect(const QModelIndex
& index
) const
123 const bool leftToRightFlow
= (flow() == QListView::LeftToRight
);
125 QRect itemRect
= KCategorizedView::visualRect(index
);
127 const int maxWidth
= m_itemSize
.width();
128 const int maxHeight
= m_itemSize
.height();
130 if (itemRect
.width() > maxWidth
) {
131 // assure that the maximum item width is not exceeded
132 if (leftToRightFlow
) {
133 const int left
= itemRect
.left() + (itemRect
.width() - maxWidth
) / 2;
134 itemRect
.setLeft(left
);
136 itemRect
.setWidth(maxWidth
);
139 if (itemRect
.height() > maxHeight
) {
140 // assure that the maximum item height is not exceeded
141 if (!leftToRightFlow
) {
142 const int top
= itemRect
.top() + (itemRect
.height() - maxHeight
) / 2;
143 itemRect
.setTop(top
);
145 itemRect
.setHeight(maxHeight
);
148 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
149 if (leftToRightFlow
&& !proxyModel
->isCategorizedModel()) {
150 // TODO: QListView::visualRect() calculates a wrong position of the items under
151 // certain circumstances (e. g. if the text is too long). This issue is bypassed
152 // by the following code (I'll try create a patch for Qt but as Dolphin must also work with
153 // Qt 4.3.0 this workaround must get applied at least for KDE 4.0).
154 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
155 const int margin
= settings
->gridSpacing();
156 const int gridWidth
= gridSize().width();
157 const int gridIndex
= (itemRect
.left() - margin
+ 1) / gridWidth
;
158 const int centerInc
= (maxWidth
- itemRect
.width()) / 2;
159 itemRect
.moveLeft((gridIndex
* gridWidth
) + margin
+ centerInc
);
165 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
167 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
168 viewOptions
.font
= m_font
;
169 viewOptions
.decorationPosition
= m_decorationPosition
;
170 viewOptions
.decorationSize
= m_decorationSize
;
171 viewOptions
.displayAlignment
= m_displayAlignment
;
172 viewOptions
.showDecorationSelected
= true;
176 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
178 KCategorizedView::contextMenuEvent(event
);
179 m_controller
->triggerContextMenuRequest(event
->pos());
182 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
184 m_controller
->requestActivation();
185 if (!indexAt(event
->pos()).isValid()) {
186 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
187 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
192 KCategorizedView::mousePressEvent(event
);
195 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
197 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
198 // fix this in KDE 4.1
199 KCategorizedView::startDrag(supportedActions
);
200 DragAndDropHelper::startDrag(this, supportedActions
);
203 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
205 if (event
->mimeData()->hasUrls()) {
206 event
->acceptProposedAction();
211 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
213 KCategorizedView::dragLeaveEvent(event
);
215 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
217 setDirtyRegion(m_dropRect
);
220 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
222 KCategorizedView::dragMoveEvent(event
);
224 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
225 const QModelIndex index
= indexAt(event
->pos());
226 setDirtyRegion(m_dropRect
);
228 m_dropRect
.setSize(QSize()); // set as invalid
229 if (index
.isValid()) {
230 const KFileItem item
= itemForIndex(index
);
231 if (!item
.isNull() && item
.isDir()) {
232 m_dropRect
= visualRect(index
);
234 m_dropRect
.setSize(QSize()); // set as invalid
237 if (event
->mimeData()->hasUrls()) {
238 // accept url drops, independently from the destination item
239 event
->acceptProposedAction();
242 setDirtyRegion(m_dropRect
);
245 void DolphinIconsView::dropEvent(QDropEvent
* event
)
247 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
248 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
249 if (!urls
.isEmpty()) {
250 const QModelIndex index
= indexAt(event
->pos());
251 const KFileItem item
= itemForIndex(index
);
252 m_controller
->indicateDroppedUrls(urls
,
255 event
->acceptProposedAction();
259 KCategorizedView::dropEvent(event
);
264 void DolphinIconsView::paintEvent(QPaintEvent
* event
)
266 KCategorizedView::paintEvent(event
);
268 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
270 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
271 DragAndDropHelper::drawHoverIndication(this, m_dropRect
, brush
);
275 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
277 KCategorizedView::keyPressEvent(event
);
279 const QItemSelectionModel
* selModel
= selectionModel();
280 const QModelIndex currentIndex
= selModel
->currentIndex();
281 const bool trigger
= currentIndex
.isValid()
282 && (event
->key() == Qt::Key_Return
)
283 && (selModel
->selectedIndexes().count() <= 1);
285 triggerItem(currentIndex
);
289 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
291 KCategorizedView::wheelEvent(event
);
293 // if the icons are aligned left to right, the vertical wheel event should
294 // be applied to the horizontal scrollbar
295 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
296 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
297 (settings
->arrangement() == QListView::LeftToRight
);
298 if (scrollHorizontal
) {
299 QWheelEvent
horizEvent(event
->pos(),
304 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
308 void DolphinIconsView::triggerItem(const QModelIndex
& index
)
310 m_controller
->triggerItem(itemForIndex(index
));
313 void DolphinIconsView::slotEntered(const QModelIndex
& index
)
315 m_controller
->emitItemEntered(itemForIndex(index
));
318 void DolphinIconsView::slotShowPreviewChanged()
320 const DolphinView
* view
= m_controller
->dolphinView();
321 updateGridSize(view
->showPreview(), additionalInfoCount());
324 void DolphinIconsView::slotAdditionalInfoChanged(const KFileItemDelegate::InformationList
& info
)
326 const bool showPreview
= m_controller
->dolphinView()->showPreview();
327 updateGridSize(showPreview
, info
.count());
330 void DolphinIconsView::zoomIn()
332 if (isZoomInPossible()) {
333 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
335 const int oldIconSize
= settings
->iconSize();
336 int newIconSize
= oldIconSize
;
338 const bool showPreview
= m_controller
->dolphinView()->showPreview();
340 const int previewSize
= increasedIconSize(settings
->previewSize());
341 settings
->setPreviewSize(previewSize
);
343 newIconSize
= increasedIconSize(oldIconSize
);
344 settings
->setIconSize(newIconSize
);
345 if (settings
->previewSize() < newIconSize
) {
346 // assure that the preview size is always >= the icon size
347 settings
->setPreviewSize(newIconSize
);
351 // increase also the grid size
352 const int diff
= newIconSize
- oldIconSize
;
353 settings
->setItemWidth(settings
->itemWidth() + diff
);
354 settings
->setItemHeight(settings
->itemHeight() + diff
);
356 updateGridSize(showPreview
, additionalInfoCount());
360 void DolphinIconsView::zoomOut()
362 if (isZoomOutPossible()) {
363 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
365 const int oldIconSize
= settings
->iconSize();
366 int newIconSize
= oldIconSize
;
368 const bool showPreview
= m_controller
->dolphinView()->showPreview();
370 const int previewSize
= decreasedIconSize(settings
->previewSize());
371 settings
->setPreviewSize(previewSize
);
372 if (settings
->iconSize() > previewSize
) {
373 // assure that the icon size is always <= the preview size
374 newIconSize
= previewSize
;
375 settings
->setIconSize(newIconSize
);
378 newIconSize
= decreasedIconSize(settings
->iconSize());
379 settings
->setIconSize(newIconSize
);
382 // decrease also the grid size
383 const int diff
= oldIconSize
- newIconSize
;
384 settings
->setItemWidth(settings
->itemWidth() - diff
);
385 settings
->setItemHeight(settings
->itemHeight() - diff
);
387 updateGridSize(showPreview
, additionalInfoCount());
391 bool DolphinIconsView::isZoomInPossible() const
393 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
394 const bool showPreview
= m_controller
->dolphinView()->showPreview();
395 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
396 return size
< KIconLoader::SizeEnormous
;
399 bool DolphinIconsView::isZoomOutPossible() const
401 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
402 const bool showPreview
= m_controller
->dolphinView()->showPreview();
403 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
404 return size
> KIconLoader::SizeSmall
;
407 int DolphinIconsView::increasedIconSize(int size
) const
411 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
412 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
413 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
414 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
415 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
416 default: Q_ASSERT(false); break;
421 int DolphinIconsView::decreasedIconSize(int size
) const
425 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
426 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
427 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
428 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
429 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
430 default: Q_ASSERT(false); break;
435 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
437 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
438 Q_ASSERT(settings
!= 0);
440 int itemWidth
= settings
->itemWidth();
441 int itemHeight
= settings
->itemHeight();
442 int size
= settings
->iconSize();
445 const int previewSize
= settings
->previewSize();
446 const int diff
= previewSize
- size
;
454 Q_ASSERT(additionalInfoCount
>= 0);
455 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
457 if (settings
->arrangement() == QListView::TopToBottom
) {
458 // The decoration width indirectly defines the maximum
459 // width for the text wrapping. To use the maximum item width
460 // for text wrapping, it is used as decoration width.
461 m_decorationSize
= QSize(itemWidth
, size
);
463 m_decorationSize
= QSize(size
, size
);
466 m_itemSize
= QSize(itemWidth
, itemHeight
);
468 const int spacing
= settings
->gridSpacing();
469 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
471 m_controller
->setZoomInPossible(isZoomInPossible());
472 m_controller
->setZoomOutPossible(isZoomOutPossible());
475 KFileItem
DolphinIconsView::itemForIndex(const QModelIndex
& index
) const
477 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
478 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
479 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
480 return dirModel
->itemForIndex(dirIndex
);
483 int DolphinIconsView::additionalInfoCount() const
485 const DolphinView
* view
= m_controller
->dolphinView();
486 return view
->additionalInfo().count();
489 #include "dolphiniconsview.moc"