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>
37 DolphinIconsView::DolphinIconsView(QWidget
* parent
, DolphinController
* controller
) :
38 KCategorizedView(parent
),
39 m_controller(controller
),
45 Q_ASSERT(controller
!= 0);
46 setViewMode(QListView::IconMode
);
47 setResizeMode(QListView::Adjust
);
48 setSpacing(KDialog::spacingHint());
49 setMovement(QListView::Static
);
51 viewport()->setAcceptDrops(true);
53 setMouseTracking(true);
54 viewport()->setAttribute(Qt::WA_Hover
);
56 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
57 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
58 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
59 // RETURN-key in keyPressEvent().
60 if (KGlobalSettings::singleClick()) {
61 connect(this, SIGNAL(clicked(const QModelIndex
&)),
62 this, SLOT(triggerItem(const QModelIndex
&)));
64 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
65 this, SLOT(triggerItem(const QModelIndex
&)));
67 connect(this, SIGNAL(viewportEntered()),
68 controller
, SLOT(emitViewportEntered()));
69 connect(controller
, SIGNAL(zoomIn()),
70 this, SLOT(zoomIn()));
71 connect(controller
, SIGNAL(zoomOut()),
72 this, SLOT(zoomOut()));
74 const DolphinView
* view
= controller
->dolphinView();
75 connect(view
, SIGNAL(showPreviewChanged()),
76 this, SLOT(slotShowPreviewChanged()));
77 connect(view
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
78 this, SLOT(slotAdditionalInfoChanged(const KFileItemDelegate::InformationList
&)));
80 connect(this, SIGNAL(entered(const QModelIndex
&)),
81 this, SLOT(slotEntered(const QModelIndex
&)));
83 // apply the icons mode settings to the widget
84 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
85 Q_ASSERT(settings
!= 0);
87 m_viewOptions
= KCategorizedView::viewOptions();
88 m_viewOptions
.showDecorationSelected
= true;
90 QFont
font(settings
->fontFamily(), settings
->fontSize());
91 font
.setItalic(settings
->italicFont());
92 font
.setBold(settings
->boldFont());
93 m_viewOptions
.font
= font
;
95 setWordWrap(settings
->numberOfTextlines() > 1);
96 updateGridSize(view
->showPreview(), 0);
98 if (settings
->arrangement() == QListView::TopToBottom
) {
99 setFlow(QListView::LeftToRight
);
100 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Top
;
102 setFlow(QListView::TopToBottom
);
103 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Left
;
104 m_viewOptions
.displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
107 m_categoryDrawer
= new DolphinCategoryDrawer();
108 setCategoryDrawer(m_categoryDrawer
);
113 DolphinIconsView::~DolphinIconsView()
115 delete m_categoryDrawer
;
116 m_categoryDrawer
= 0;
119 QRect
DolphinIconsView::visualRect(const QModelIndex
& index
) const
121 const bool leftToRightFlow
= (flow() == QListView::LeftToRight
);
123 QRect itemRect
= KCategorizedView::visualRect(index
);
124 const int maxWidth
= m_itemSize
.width();
125 const int maxHeight
= m_itemSize
.height();
127 if (itemRect
.width() > maxWidth
) {
128 // assure that the maximum item width is not exceeded
129 if (leftToRightFlow
) {
130 const int left
= itemRect
.left() + (itemRect
.width() - maxWidth
) / 2;
131 itemRect
.setLeft(left
);
133 itemRect
.setWidth(maxWidth
);
136 if (itemRect
.height() > maxHeight
) {
137 // assure that the maximum item height is not exceeded
138 if (!leftToRightFlow
) {
139 const int top
= itemRect
.top() + (itemRect
.height() - maxHeight
) / 2;
140 itemRect
.setTop(top
);
142 itemRect
.setHeight(maxHeight
);
145 if (leftToRightFlow
&& bypassVisualRectIssue()) {
146 // TODO: check inline comment inside bypassVisualRectIssue() for details
147 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
148 const int margin
= settings
->gridSpacing();
149 const int gridWidth
= gridSize().width();
150 const int gridIndex
= (itemRect
.left() - margin
+ 1) / gridWidth
;
151 itemRect
.moveLeft(gridIndex
* gridWidth
+ margin
);
157 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
159 return m_viewOptions
;
162 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
164 KCategorizedView::contextMenuEvent(event
);
165 m_controller
->triggerContextMenuRequest(event
->pos());
168 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
170 m_controller
->requestActivation();
171 if (!indexAt(event
->pos()).isValid()) {
172 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
173 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
178 KCategorizedView::mousePressEvent(event
);
181 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
183 if (bypassVisualRectIssue()) {
184 // TODO: check inline comment inside bypassVisualRectIssue() for details
185 DragAndDropHelper::startDrag(this, supportedActions
);
187 KCategorizedView::startDrag(supportedActions
);
191 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
193 if (event
->mimeData()->hasUrls()) {
194 event
->acceptProposedAction();
199 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
201 if (bypassVisualRectIssue()) {
202 // TODO: check inline comment inside bypassVisualRectIssue() for details
203 QAbstractItemView::dragLeaveEvent(event
);
205 KCategorizedView::dragLeaveEvent(event
);
208 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
210 setDirtyRegion(m_dropRect
);
213 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
215 if (bypassVisualRectIssue()) {
216 // TODO: check inline comment inside bypassVisualRectIssue() for details
217 QAbstractItemView::dragMoveEvent(event
);
219 KCategorizedView::dragMoveEvent(event
);
222 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
223 const QModelIndex index
= indexAt(event
->pos());
224 setDirtyRegion(m_dropRect
);
225 if (itemForIndex(index
).isDir()) {
226 m_dropRect
= visualRect(index
);
228 m_dropRect
.setSize(QSize()); // set as invalid
230 setDirtyRegion(m_dropRect
);
233 void DolphinIconsView::dropEvent(QDropEvent
* event
)
235 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
236 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
237 if (!urls
.isEmpty()) {
238 const QModelIndex index
= indexAt(event
->pos());
239 const KFileItem item
= itemForIndex(index
);
240 m_controller
->indicateDroppedUrls(urls
,
243 event
->acceptProposedAction();
247 if (bypassVisualRectIssue()) {
248 // TODO: check inline comment inside bypassVisualRectIssue() for details
249 QAbstractItemView::dropEvent(event
);
251 KCategorizedView::dropEvent(event
);
257 void DolphinIconsView::paintEvent(QPaintEvent
* event
)
259 KCategorizedView::paintEvent(event
);
261 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
263 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
264 DragAndDropHelper::drawHoverIndication(viewport(), m_dropRect
, brush
);
268 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
270 KCategorizedView::keyPressEvent(event
);
272 const QItemSelectionModel
* selModel
= selectionModel();
273 const QModelIndex currentIndex
= selModel
->currentIndex();
274 const bool trigger
= currentIndex
.isValid()
275 && (event
->key() == Qt::Key_Return
)
276 && (selModel
->selectedIndexes().count() <= 1);
278 triggerItem(currentIndex
);
282 void DolphinIconsView::triggerItem(const QModelIndex
& index
)
284 m_controller
->triggerItem(itemForIndex(index
));
287 void DolphinIconsView::slotEntered(const QModelIndex
& index
)
289 m_controller
->emitItemEntered(itemForIndex(index
));
292 void DolphinIconsView::slotShowPreviewChanged()
294 const DolphinView
* view
= m_controller
->dolphinView();
295 const int infoCount
= view
->additionalInfo().count();
296 updateGridSize(view
->showPreview(), infoCount
);
299 void DolphinIconsView::slotAdditionalInfoChanged(const KFileItemDelegate::InformationList
& info
)
301 const bool showPreview
= m_controller
->dolphinView()->showPreview();
302 updateGridSize(showPreview
, info
.count());
305 void DolphinIconsView::zoomIn()
307 if (isZoomInPossible()) {
308 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
310 const int oldIconSize
= settings
->iconSize();
311 int newIconSize
= oldIconSize
;
313 const bool showPreview
= m_controller
->dolphinView()->showPreview();
315 const int previewSize
= increasedIconSize(settings
->previewSize());
316 settings
->setPreviewSize(previewSize
);
318 newIconSize
= increasedIconSize(oldIconSize
);
319 settings
->setIconSize(newIconSize
);
320 if (settings
->previewSize() < newIconSize
) {
321 // assure that the preview size is always >= the icon size
322 settings
->setPreviewSize(newIconSize
);
326 // increase also the grid size
327 const int diff
= newIconSize
- oldIconSize
;
328 settings
->setItemWidth(settings
->itemWidth() + diff
);
329 settings
->setItemHeight(settings
->itemHeight() + diff
);
331 const int infoCount
= m_controller
->dolphinView()->additionalInfo().count();
332 updateGridSize(showPreview
, infoCount
);
336 void DolphinIconsView::zoomOut()
338 if (isZoomOutPossible()) {
339 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
341 const int oldIconSize
= settings
->iconSize();
342 int newIconSize
= oldIconSize
;
344 const bool showPreview
= m_controller
->dolphinView()->showPreview();
346 const int previewSize
= decreasedIconSize(settings
->previewSize());
347 settings
->setPreviewSize(previewSize
);
348 if (settings
->iconSize() > previewSize
) {
349 // assure that the icon size is always <= the preview size
350 newIconSize
= previewSize
;
351 settings
->setIconSize(newIconSize
);
354 newIconSize
= decreasedIconSize(settings
->iconSize());
355 settings
->setIconSize(newIconSize
);
358 // decrease also the grid size
359 const int diff
= oldIconSize
- newIconSize
;
360 settings
->setItemWidth(settings
->itemWidth() - diff
);
361 settings
->setItemHeight(settings
->itemHeight() - diff
);
363 const int infoCount
= m_controller
->dolphinView()->additionalInfo().count();
364 updateGridSize(showPreview
, infoCount
);
368 bool DolphinIconsView::isZoomInPossible() const
370 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
371 const bool showPreview
= m_controller
->dolphinView()->showPreview();
372 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
373 return size
< KIconLoader::SizeEnormous
;
376 bool DolphinIconsView::isZoomOutPossible() const
378 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
379 const bool showPreview
= m_controller
->dolphinView()->showPreview();
380 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
381 return size
> KIconLoader::SizeSmall
;
384 int DolphinIconsView::increasedIconSize(int size
) const
388 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
389 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
390 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
391 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
392 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
393 default: Q_ASSERT(false); break;
398 int DolphinIconsView::decreasedIconSize(int size
) const
402 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
403 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
404 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
405 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
406 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
407 default: Q_ASSERT(false); break;
412 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
414 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
415 Q_ASSERT(settings
!= 0);
417 int itemWidth
= settings
->itemWidth();
418 int itemHeight
= settings
->itemHeight();
419 int size
= settings
->iconSize();
422 const int previewSize
= settings
->previewSize();
423 const int diff
= previewSize
- size
;
431 Q_ASSERT(additionalInfoCount
>= 0);
432 itemHeight
+= additionalInfoCount
* m_viewOptions
.font
.pointSize() * 2;
434 if (settings
->arrangement() == QListView::TopToBottom
) {
435 // The decoration width indirectly defines the maximum
436 // width for the text wrapping. To use the maximum item width
437 // for text wrapping, it is used as decoration width.
438 m_viewOptions
.decorationSize
= QSize(itemWidth
, size
);
440 m_viewOptions
.decorationSize
= QSize(size
, size
);
443 const int spacing
= settings
->gridSpacing();
444 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
446 m_itemSize
= QSize(itemWidth
, itemHeight
);
448 m_controller
->setZoomInPossible(isZoomInPossible());
449 m_controller
->setZoomOutPossible(isZoomOutPossible());
452 KFileItem
DolphinIconsView::itemForIndex(const QModelIndex
& index
) const
454 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
455 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
456 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
457 return dirModel
->itemForIndex(dirIndex
);
460 bool DolphinIconsView::bypassVisualRectIssue() const
462 // TODO: QListView::visualRect() calculates a wrong position of the items under
463 // certain circumstances (e. g. if the text is too long). This issue is bypassed
464 // inside DolphinIconsView::visualRect(), but internally QListView does not use
465 // visualRect() but the (non-virtual) QListView::rectForIndex(). This leads
466 // to problems in combination with drag & drop operations: visual fragments get
467 // created. To bypass the drag & drop issue the calls for QListView::dragMoveEvent(),
468 // QListView::dropEvent() are replaced by the QAbstractItemView counterparts and
469 // QAbstractItemView::startDrag() has been reimplemented.
471 // I'll try create a patch for Qt but as Dolphin must also work with
472 // Qt 4.3.0 this workaround must get applied at least for KDE 4.0.
473 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
474 return !proxyModel
->isCategorizedModel();
477 #include "dolphiniconsview.moc"