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 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
146 if (leftToRightFlow
&& !proxyModel
->isCategorizedModel()) {
147 // TODO: QListView::visualRect() calculates a wrong position of the items under
148 // certain circumstances (e. g. if the text is too long). This issue is bypassed
149 // by the following code (I'll try create a patch for Qt but as Dolphin must also work with
150 // Qt 4.3.0 this workaround must get applied at least for KDE 4.0).
151 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
152 const int margin
= settings
->gridSpacing();
153 const int gridWidth
= gridSize().width();
154 const int gridIndex
= (itemRect
.left() - margin
+ 1) / gridWidth
;
155 itemRect
.moveLeft(gridIndex
* gridWidth
+ margin
);
161 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
163 return m_viewOptions
;
166 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
168 KCategorizedView::contextMenuEvent(event
);
169 m_controller
->triggerContextMenuRequest(event
->pos());
172 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
174 m_controller
->requestActivation();
175 if (!indexAt(event
->pos()).isValid()) {
176 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
177 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
182 KCategorizedView::mousePressEvent(event
);
185 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
187 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
188 // fix this in KDE 4.1
189 KCategorizedView::startDrag(supportedActions
);
190 DragAndDropHelper::startDrag(this, supportedActions
);
193 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
195 if (event
->mimeData()->hasUrls()) {
196 event
->acceptProposedAction();
201 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
203 KCategorizedView::dragLeaveEvent(event
);
205 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
207 setDirtyRegion(m_dropRect
);
210 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
212 KCategorizedView::dragMoveEvent(event
);
214 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
215 const QModelIndex index
= indexAt(event
->pos());
216 setDirtyRegion(m_dropRect
);
218 if(!index
.isValid()) {
219 m_dropRect
.setSize(QSize()); // set as invalid
221 KFileItem item
= itemForIndex(index
);
223 kWarning(7007) << "Invalid item returned for index";
224 } else if (itemForIndex(index
).isDir()) {
225 m_dropRect
= visualRect(index
);
227 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 KCategorizedView::dropEvent(event
);
252 void DolphinIconsView::paintEvent(QPaintEvent
* event
)
254 KCategorizedView::paintEvent(event
);
256 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
258 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
259 DragAndDropHelper::drawHoverIndication(viewport(), m_dropRect
, brush
);
263 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
265 KCategorizedView::keyPressEvent(event
);
267 const QItemSelectionModel
* selModel
= selectionModel();
268 const QModelIndex currentIndex
= selModel
->currentIndex();
269 const bool trigger
= currentIndex
.isValid()
270 && (event
->key() == Qt::Key_Return
)
271 && (selModel
->selectedIndexes().count() <= 1);
273 triggerItem(currentIndex
);
277 void DolphinIconsView::triggerItem(const QModelIndex
& index
)
279 m_controller
->triggerItem(itemForIndex(index
));
282 void DolphinIconsView::slotEntered(const QModelIndex
& index
)
284 m_controller
->emitItemEntered(itemForIndex(index
));
287 void DolphinIconsView::slotShowPreviewChanged()
289 const DolphinView
* view
= m_controller
->dolphinView();
290 const int infoCount
= view
->additionalInfo().count();
291 updateGridSize(view
->showPreview(), infoCount
);
294 void DolphinIconsView::slotAdditionalInfoChanged(const KFileItemDelegate::InformationList
& info
)
296 const bool showPreview
= m_controller
->dolphinView()->showPreview();
297 updateGridSize(showPreview
, info
.count());
300 void DolphinIconsView::zoomIn()
302 if (isZoomInPossible()) {
303 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
305 const int oldIconSize
= settings
->iconSize();
306 int newIconSize
= oldIconSize
;
308 const bool showPreview
= m_controller
->dolphinView()->showPreview();
310 const int previewSize
= increasedIconSize(settings
->previewSize());
311 settings
->setPreviewSize(previewSize
);
313 newIconSize
= increasedIconSize(oldIconSize
);
314 settings
->setIconSize(newIconSize
);
315 if (settings
->previewSize() < newIconSize
) {
316 // assure that the preview size is always >= the icon size
317 settings
->setPreviewSize(newIconSize
);
321 // increase also the grid size
322 const int diff
= newIconSize
- oldIconSize
;
323 settings
->setItemWidth(settings
->itemWidth() + diff
);
324 settings
->setItemHeight(settings
->itemHeight() + diff
);
326 const int infoCount
= m_controller
->dolphinView()->additionalInfo().count();
327 updateGridSize(showPreview
, infoCount
);
331 void DolphinIconsView::zoomOut()
333 if (isZoomOutPossible()) {
334 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
336 const int oldIconSize
= settings
->iconSize();
337 int newIconSize
= oldIconSize
;
339 const bool showPreview
= m_controller
->dolphinView()->showPreview();
341 const int previewSize
= decreasedIconSize(settings
->previewSize());
342 settings
->setPreviewSize(previewSize
);
343 if (settings
->iconSize() > previewSize
) {
344 // assure that the icon size is always <= the preview size
345 newIconSize
= previewSize
;
346 settings
->setIconSize(newIconSize
);
349 newIconSize
= decreasedIconSize(settings
->iconSize());
350 settings
->setIconSize(newIconSize
);
353 // decrease also the grid size
354 const int diff
= oldIconSize
- newIconSize
;
355 settings
->setItemWidth(settings
->itemWidth() - diff
);
356 settings
->setItemHeight(settings
->itemHeight() - diff
);
358 const int infoCount
= m_controller
->dolphinView()->additionalInfo().count();
359 updateGridSize(showPreview
, infoCount
);
363 bool DolphinIconsView::isZoomInPossible() const
365 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
366 const bool showPreview
= m_controller
->dolphinView()->showPreview();
367 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
368 return size
< KIconLoader::SizeEnormous
;
371 bool DolphinIconsView::isZoomOutPossible() const
373 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
374 const bool showPreview
= m_controller
->dolphinView()->showPreview();
375 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
376 return size
> KIconLoader::SizeSmall
;
379 int DolphinIconsView::increasedIconSize(int size
) const
383 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
384 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
385 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
386 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
387 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
388 default: Q_ASSERT(false); break;
393 int DolphinIconsView::decreasedIconSize(int size
) const
397 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
398 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
399 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
400 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
401 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
402 default: Q_ASSERT(false); break;
407 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
409 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
410 Q_ASSERT(settings
!= 0);
412 int itemWidth
= settings
->itemWidth();
413 int itemHeight
= settings
->itemHeight();
414 int size
= settings
->iconSize();
417 const int previewSize
= settings
->previewSize();
418 const int diff
= previewSize
- size
;
426 Q_ASSERT(additionalInfoCount
>= 0);
427 itemHeight
+= additionalInfoCount
* m_viewOptions
.font
.pointSize() * 2;
429 if (settings
->arrangement() == QListView::TopToBottom
) {
430 // The decoration width indirectly defines the maximum
431 // width for the text wrapping. To use the maximum item width
432 // for text wrapping, it is used as decoration width.
433 m_viewOptions
.decorationSize
= QSize(itemWidth
, size
);
435 m_viewOptions
.decorationSize
= QSize(size
, size
);
438 const int spacing
= settings
->gridSpacing();
439 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
441 m_itemSize
= QSize(itemWidth
, itemHeight
);
443 m_controller
->setZoomInPossible(isZoomInPossible());
444 m_controller
->setZoomOutPossible(isZoomOutPossible());
447 KFileItem
DolphinIconsView::itemForIndex(const QModelIndex
& index
) const
449 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
450 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
451 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
452 return dirModel
->itemForIndex(dirIndex
);
455 #include "dolphiniconsview.moc"