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()));
76 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
77 this, SLOT(triggerItem(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 connect(this, SIGNAL(entered(const QModelIndex
&)),
93 this, SLOT(slotEntered(const QModelIndex
&)));
95 // apply the icons mode settings to the widget
96 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
97 Q_ASSERT(settings
!= 0);
99 m_font
= QFont(settings
->fontFamily(), settings
->fontSize());
100 m_font
.setItalic(settings
->italicFont());
101 m_font
.setBold(settings
->boldFont());
103 setWordWrap(settings
->numberOfTextlines() > 1);
104 updateGridSize(view
->showPreview(), 0);
106 if (settings
->arrangement() == QListView::TopToBottom
) {
107 setFlow(QListView::LeftToRight
);
108 m_decorationPosition
= QStyleOptionViewItem::Top
;
109 m_displayAlignment
= Qt::AlignHCenter
;
111 setFlow(QListView::TopToBottom
);
112 m_decorationPosition
= QStyleOptionViewItem::Left
;
113 m_displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
116 m_categoryDrawer
= new DolphinCategoryDrawer();
117 setCategoryDrawer(m_categoryDrawer
);
122 DolphinIconsView::~DolphinIconsView()
124 delete m_categoryDrawer
;
125 m_categoryDrawer
= 0;
128 QRect
DolphinIconsView::visualRect(const QModelIndex
& index
) const
130 const bool leftToRightFlow
= (flow() == QListView::LeftToRight
);
132 QRect itemRect
= KCategorizedView::visualRect(index
);
134 const int maxWidth
= m_itemSize
.width();
135 const int maxHeight
= m_itemSize
.height();
137 if (itemRect
.width() > maxWidth
) {
138 // assure that the maximum item width is not exceeded
139 if (leftToRightFlow
) {
140 const int left
= itemRect
.left() + (itemRect
.width() - maxWidth
) / 2;
141 itemRect
.setLeft(left
);
143 itemRect
.setWidth(maxWidth
);
146 if (itemRect
.height() > maxHeight
) {
147 // assure that the maximum item height is not exceeded
148 if (!leftToRightFlow
) {
149 const int top
= itemRect
.top() + (itemRect
.height() - maxHeight
) / 2;
150 itemRect
.setTop(top
);
152 itemRect
.setHeight(maxHeight
);
155 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
156 if (leftToRightFlow
&& !proxyModel
->isCategorizedModel()) {
157 // TODO: QListView::visualRect() calculates a wrong position of the items under
158 // certain circumstances (e. g. if the text is too long). This issue is bypassed
159 // by the following code (I'll try create a patch for Qt but as Dolphin must also work with
160 // Qt 4.3.0 this workaround must get applied at least for KDE 4.0).
161 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
162 const int margin
= settings
->gridSpacing();
163 const int gridWidth
= gridSize().width();
164 const int gridIndex
= (itemRect
.left() - margin
+ 1) / gridWidth
;
165 const int centerInc
= (maxWidth
- itemRect
.width()) / 2;
166 itemRect
.moveLeft((gridIndex
* gridWidth
) + margin
+ centerInc
);
172 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
174 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
175 viewOptions
.font
= m_font
;
176 viewOptions
.decorationPosition
= m_decorationPosition
;
177 viewOptions
.decorationSize
= m_decorationSize
;
178 viewOptions
.displayAlignment
= m_displayAlignment
;
179 viewOptions
.showDecorationSelected
= true;
183 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
185 KCategorizedView::contextMenuEvent(event
);
186 m_controller
->triggerContextMenuRequest(event
->pos());
189 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
191 m_controller
->requestActivation();
192 if (!indexAt(event
->pos()).isValid()) {
193 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
194 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
199 KCategorizedView::mousePressEvent(event
);
202 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
204 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
205 // fix this in KDE 4.1
206 KCategorizedView::startDrag(supportedActions
);
207 DragAndDropHelper::startDrag(this, supportedActions
);
210 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
212 if (event
->mimeData()->hasUrls()) {
213 event
->acceptProposedAction();
218 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
220 KCategorizedView::dragLeaveEvent(event
);
222 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
224 setDirtyRegion(m_dropRect
);
227 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
229 KCategorizedView::dragMoveEvent(event
);
231 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
232 const QModelIndex index
= indexAt(event
->pos());
233 setDirtyRegion(m_dropRect
);
235 m_dropRect
.setSize(QSize()); // set as invalid
236 if (index
.isValid()) {
237 const KFileItem item
= itemForIndex(index
);
238 if (!item
.isNull() && item
.isDir()) {
239 m_dropRect
= visualRect(index
);
241 m_dropRect
.setSize(QSize()); // set as invalid
244 if (event
->mimeData()->hasUrls()) {
245 // accept url drops, independently from the destination item
246 event
->acceptProposedAction();
249 setDirtyRegion(m_dropRect
);
252 void DolphinIconsView::dropEvent(QDropEvent
* event
)
254 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
255 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
256 if (!urls
.isEmpty()) {
257 const QModelIndex index
= indexAt(event
->pos());
258 const KFileItem item
= itemForIndex(index
);
259 m_controller
->indicateDroppedUrls(urls
,
262 event
->acceptProposedAction();
266 KCategorizedView::dropEvent(event
);
271 void DolphinIconsView::paintEvent(QPaintEvent
* event
)
273 KCategorizedView::paintEvent(event
);
275 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
277 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
278 DragAndDropHelper::drawHoverIndication(this, m_dropRect
, brush
);
282 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
284 KCategorizedView::keyPressEvent(event
);
286 const QItemSelectionModel
* selModel
= selectionModel();
287 const QModelIndex currentIndex
= selModel
->currentIndex();
288 const bool trigger
= currentIndex
.isValid()
289 && (event
->key() == Qt::Key_Return
)
290 && (selModel
->selectedIndexes().count() <= 1);
292 triggerItem(currentIndex
);
296 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
298 KCategorizedView::wheelEvent(event
);
300 // if the icons are aligned left to right, the vertical wheel event should
301 // be applied to the horizontal scrollbar
302 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
303 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
304 (settings
->arrangement() == QListView::LeftToRight
);
305 if (scrollHorizontal
) {
306 QWheelEvent
horizEvent(event
->pos(),
311 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
315 void DolphinIconsView::triggerItem(const QModelIndex
& index
)
317 m_controller
->triggerItem(itemForIndex(index
));
320 void DolphinIconsView::slotEntered(const QModelIndex
& index
)
322 m_controller
->emitItemEntered(itemForIndex(index
));
325 void DolphinIconsView::slotShowPreviewChanged()
327 const DolphinView
* view
= m_controller
->dolphinView();
328 updateGridSize(view
->showPreview(), additionalInfoCount());
331 void DolphinIconsView::slotAdditionalInfoChanged()
333 const DolphinView
* view
= m_controller
->dolphinView();
334 const bool showPreview
= view
->showPreview();
335 updateGridSize(showPreview
, view
->additionalInfo().count());
338 void DolphinIconsView::zoomIn()
340 if (isZoomInPossible()) {
341 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
343 const int oldIconSize
= settings
->iconSize();
344 int newIconSize
= oldIconSize
;
346 const bool showPreview
= m_controller
->dolphinView()->showPreview();
348 const int previewSize
= increasedIconSize(settings
->previewSize());
349 settings
->setPreviewSize(previewSize
);
351 newIconSize
= increasedIconSize(oldIconSize
);
352 settings
->setIconSize(newIconSize
);
353 if (settings
->previewSize() < newIconSize
) {
354 // assure that the preview size is always >= the icon size
355 settings
->setPreviewSize(newIconSize
);
359 // increase also the grid size
360 const int diff
= newIconSize
- oldIconSize
;
361 settings
->setItemWidth(settings
->itemWidth() + diff
);
362 settings
->setItemHeight(settings
->itemHeight() + diff
);
364 updateGridSize(showPreview
, additionalInfoCount());
368 void DolphinIconsView::zoomOut()
370 if (isZoomOutPossible()) {
371 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
373 const int oldIconSize
= settings
->iconSize();
374 int newIconSize
= oldIconSize
;
376 const bool showPreview
= m_controller
->dolphinView()->showPreview();
378 const int previewSize
= decreasedIconSize(settings
->previewSize());
379 settings
->setPreviewSize(previewSize
);
380 if (settings
->iconSize() > previewSize
) {
381 // assure that the icon size is always <= the preview size
382 newIconSize
= previewSize
;
383 settings
->setIconSize(newIconSize
);
386 newIconSize
= decreasedIconSize(settings
->iconSize());
387 settings
->setIconSize(newIconSize
);
390 // decrease also the grid size
391 const int diff
= oldIconSize
- newIconSize
;
392 settings
->setItemWidth(settings
->itemWidth() - diff
);
393 settings
->setItemHeight(settings
->itemHeight() - diff
);
395 updateGridSize(showPreview
, additionalInfoCount());
399 void DolphinIconsView::requestActivation()
401 m_controller
->requestActivation();
404 bool DolphinIconsView::isZoomInPossible() const
406 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
407 const bool showPreview
= m_controller
->dolphinView()->showPreview();
408 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
409 return size
< KIconLoader::SizeEnormous
;
412 bool DolphinIconsView::isZoomOutPossible() const
414 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
415 const bool showPreview
= m_controller
->dolphinView()->showPreview();
416 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
417 return size
> KIconLoader::SizeSmall
;
420 int DolphinIconsView::increasedIconSize(int size
) const
424 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
425 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
426 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
427 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
428 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
429 default: Q_ASSERT(false); break;
434 int DolphinIconsView::decreasedIconSize(int size
) const
438 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
439 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
440 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
441 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
442 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
443 default: Q_ASSERT(false); break;
448 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
450 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
451 Q_ASSERT(settings
!= 0);
453 int itemWidth
= settings
->itemWidth();
454 int itemHeight
= settings
->itemHeight();
455 int size
= settings
->iconSize();
458 const int previewSize
= settings
->previewSize();
459 const int diff
= previewSize
- size
;
466 setIconSize(QSize(size
, size
));
468 Q_ASSERT(additionalInfoCount
>= 0);
469 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
471 if (settings
->arrangement() == QListView::TopToBottom
) {
472 // The decoration width indirectly defines the maximum
473 // width for the text wrapping. To use the maximum item width
474 // for text wrapping, it is used as decoration width.
475 m_decorationSize
= QSize(itemWidth
, size
);
477 m_decorationSize
= QSize(size
, size
);
480 m_itemSize
= QSize(itemWidth
, itemHeight
);
482 const int spacing
= settings
->gridSpacing();
483 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
485 m_controller
->setZoomInPossible(isZoomInPossible());
486 m_controller
->setZoomOutPossible(isZoomOutPossible());
489 KFileItem
DolphinIconsView::itemForIndex(const QModelIndex
& index
) const
491 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
492 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
493 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
494 return dirModel
->itemForIndex(dirIndex
);
497 int DolphinIconsView::additionalInfoCount() const
499 const DolphinView
* view
= m_controller
->dolphinView();
500 return view
->additionalInfo().count();
503 #include "dolphiniconsview.moc"