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
),
43 m_decorationPosition(QStyleOptionViewItem::Top
),
44 m_displayAlignment(Qt::AlignHCenter
),
49 Q_ASSERT(controller
!= 0);
50 setViewMode(QListView::IconMode
);
51 setResizeMode(QListView::Adjust
);
52 setSpacing(KDialog::spacingHint());
53 setMovement(QListView::Static
);
55 viewport()->setAcceptDrops(true);
57 setMouseTracking(true);
58 viewport()->setAttribute(Qt::WA_Hover
);
60 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
61 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
62 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
63 // RETURN-key in keyPressEvent().
64 if (KGlobalSettings::singleClick()) {
65 connect(this, SIGNAL(clicked(const QModelIndex
&)),
66 this, SLOT(triggerItem(const QModelIndex
&)));
68 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
69 this, SLOT(triggerItem(const QModelIndex
&)));
71 connect(this, SIGNAL(viewportEntered()),
72 controller
, SLOT(emitViewportEntered()));
73 connect(controller
, SIGNAL(zoomIn()),
74 this, SLOT(zoomIn()));
75 connect(controller
, SIGNAL(zoomOut()),
76 this, SLOT(zoomOut()));
78 const DolphinView
* view
= controller
->dolphinView();
79 connect(view
, SIGNAL(showPreviewChanged()),
80 this, SLOT(slotShowPreviewChanged()));
81 connect(view
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
82 this, SLOT(slotAdditionalInfoChanged(const KFileItemDelegate::InformationList
&)));
84 connect(this, SIGNAL(entered(const QModelIndex
&)),
85 this, SLOT(slotEntered(const QModelIndex
&)));
87 // apply the icons mode settings to the widget
88 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
89 Q_ASSERT(settings
!= 0);
91 m_font
= QFont(settings
->fontFamily(), settings
->fontSize());
92 m_font
.setItalic(settings
->italicFont());
93 m_font
.setBold(settings
->boldFont());
95 setWordWrap(settings
->numberOfTextlines() > 1);
96 updateGridSize(view
->showPreview(), 0);
98 if (settings
->arrangement() == QListView::TopToBottom
) {
99 setFlow(QListView::LeftToRight
);
100 m_decorationPosition
= QStyleOptionViewItem::Top
;
101 m_displayAlignment
= Qt::AlignHCenter
;
103 setFlow(QListView::TopToBottom
);
104 m_decorationPosition
= QStyleOptionViewItem::Left
;
105 m_displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
108 m_categoryDrawer
= new DolphinCategoryDrawer();
109 setCategoryDrawer(m_categoryDrawer
);
114 DolphinIconsView::~DolphinIconsView()
116 delete m_categoryDrawer
;
117 m_categoryDrawer
= 0;
120 QRect
DolphinIconsView::visualRect(const QModelIndex
& index
) const
122 const bool leftToRightFlow
= (flow() == QListView::LeftToRight
);
124 QRect itemRect
= KCategorizedView::visualRect(index
);
126 const int maxWidth
= m_itemSize
.width();
127 const int maxHeight
= m_itemSize
.height();
129 if (itemRect
.width() > maxWidth
) {
130 // assure that the maximum item width is not exceeded
131 if (leftToRightFlow
) {
132 const int left
= itemRect
.left() + (itemRect
.width() - maxWidth
) / 2;
133 itemRect
.setLeft(left
);
135 itemRect
.setWidth(maxWidth
);
138 if (itemRect
.height() > maxHeight
) {
139 // assure that the maximum item height is not exceeded
140 if (!leftToRightFlow
) {
141 const int top
= itemRect
.top() + (itemRect
.height() - maxHeight
) / 2;
142 itemRect
.setTop(top
);
144 itemRect
.setHeight(maxHeight
);
147 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
148 if (leftToRightFlow
&& !proxyModel
->isCategorizedModel()) {
149 // TODO: QListView::visualRect() calculates a wrong position of the items under
150 // certain circumstances (e. g. if the text is too long). This issue is bypassed
151 // by the following code (I'll try create a patch for Qt but as Dolphin must also work with
152 // Qt 4.3.0 this workaround must get applied at least for KDE 4.0).
153 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
154 const int margin
= settings
->gridSpacing();
155 const int gridWidth
= gridSize().width();
156 const int gridIndex
= (itemRect
.left() - margin
+ 1) / gridWidth
;
157 const int centerInc
= (maxWidth
- itemRect
.width()) / 2;
158 itemRect
.moveLeft((gridIndex
* gridWidth
) + margin
+ centerInc
);
164 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
166 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
167 viewOptions
.font
= m_font
;
168 viewOptions
.decorationPosition
= m_decorationPosition
;
169 viewOptions
.decorationSize
= m_decorationSize
;
170 viewOptions
.displayAlignment
= m_displayAlignment
;
171 viewOptions
.showDecorationSelected
= true;
175 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
177 KCategorizedView::contextMenuEvent(event
);
178 m_controller
->triggerContextMenuRequest(event
->pos());
181 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
183 m_controller
->requestActivation();
184 if (!indexAt(event
->pos()).isValid()) {
185 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
186 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
191 KCategorizedView::mousePressEvent(event
);
194 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
196 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
197 // fix this in KDE 4.1
198 KCategorizedView::startDrag(supportedActions
);
199 DragAndDropHelper::startDrag(this, supportedActions
);
202 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
204 if (event
->mimeData()->hasUrls()) {
205 event
->acceptProposedAction();
210 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
212 KCategorizedView::dragLeaveEvent(event
);
214 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
216 setDirtyRegion(m_dropRect
);
219 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
221 KCategorizedView::dragMoveEvent(event
);
223 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
224 const QModelIndex index
= indexAt(event
->pos());
225 setDirtyRegion(m_dropRect
);
227 m_dropRect
.setSize(QSize()); // set as invalid
228 bool destIsDir
= false;
229 if (index
.isValid()) {
230 const KFileItem item
= itemForIndex(index
);
231 if (!item
.isNull() && item
.isDir()) {
232 m_dropRect
= visualRect(index
);
235 } else { // dropping on viewport
238 if (destIsDir
&& event
->mimeData()->hasUrls()) {
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(viewport(), 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::triggerItem(const QModelIndex
& index
)
291 m_controller
->triggerItem(itemForIndex(index
));
294 void DolphinIconsView::slotEntered(const QModelIndex
& index
)
296 m_controller
->emitItemEntered(itemForIndex(index
));
299 void DolphinIconsView::slotShowPreviewChanged()
301 const DolphinView
* view
= m_controller
->dolphinView();
302 updateGridSize(view
->showPreview(), additionalInfoCount());
305 void DolphinIconsView::slotAdditionalInfoChanged(const KFileItemDelegate::InformationList
& info
)
307 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
308 if (!settings
->showAdditionalInfo()) {
312 const bool showPreview
= m_controller
->dolphinView()->showPreview();
313 updateGridSize(showPreview
, info
.count());
316 void DolphinIconsView::zoomIn()
318 if (isZoomInPossible()) {
319 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
321 const int oldIconSize
= settings
->iconSize();
322 int newIconSize
= oldIconSize
;
324 const bool showPreview
= m_controller
->dolphinView()->showPreview();
326 const int previewSize
= increasedIconSize(settings
->previewSize());
327 settings
->setPreviewSize(previewSize
);
329 newIconSize
= increasedIconSize(oldIconSize
);
330 settings
->setIconSize(newIconSize
);
331 if (settings
->previewSize() < newIconSize
) {
332 // assure that the preview size is always >= the icon size
333 settings
->setPreviewSize(newIconSize
);
337 // increase also the grid size
338 const int diff
= newIconSize
- oldIconSize
;
339 settings
->setItemWidth(settings
->itemWidth() + diff
);
340 settings
->setItemHeight(settings
->itemHeight() + diff
);
342 updateGridSize(showPreview
, additionalInfoCount());
346 void DolphinIconsView::zoomOut()
348 if (isZoomOutPossible()) {
349 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
351 const int oldIconSize
= settings
->iconSize();
352 int newIconSize
= oldIconSize
;
354 const bool showPreview
= m_controller
->dolphinView()->showPreview();
356 const int previewSize
= decreasedIconSize(settings
->previewSize());
357 settings
->setPreviewSize(previewSize
);
358 if (settings
->iconSize() > previewSize
) {
359 // assure that the icon size is always <= the preview size
360 newIconSize
= previewSize
;
361 settings
->setIconSize(newIconSize
);
364 newIconSize
= decreasedIconSize(settings
->iconSize());
365 settings
->setIconSize(newIconSize
);
368 // decrease also the grid size
369 const int diff
= oldIconSize
- newIconSize
;
370 settings
->setItemWidth(settings
->itemWidth() - diff
);
371 settings
->setItemHeight(settings
->itemHeight() - diff
);
373 updateGridSize(showPreview
, additionalInfoCount());
377 bool DolphinIconsView::isZoomInPossible() const
379 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
380 const bool showPreview
= m_controller
->dolphinView()->showPreview();
381 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
382 return size
< KIconLoader::SizeEnormous
;
385 bool DolphinIconsView::isZoomOutPossible() const
387 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
388 const bool showPreview
= m_controller
->dolphinView()->showPreview();
389 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
390 return size
> KIconLoader::SizeSmall
;
393 int DolphinIconsView::increasedIconSize(int size
) const
397 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
398 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
399 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
400 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
401 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
402 default: Q_ASSERT(false); break;
407 int DolphinIconsView::decreasedIconSize(int size
) const
411 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
412 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
413 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
414 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
415 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
416 default: Q_ASSERT(false); break;
421 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
423 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
424 Q_ASSERT(settings
!= 0);
426 int itemWidth
= settings
->itemWidth();
427 int itemHeight
= settings
->itemHeight();
428 int size
= settings
->iconSize();
431 const int previewSize
= settings
->previewSize();
432 const int diff
= previewSize
- size
;
440 Q_ASSERT(additionalInfoCount
>= 0);
441 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
443 if (settings
->arrangement() == QListView::TopToBottom
) {
444 // The decoration width indirectly defines the maximum
445 // width for the text wrapping. To use the maximum item width
446 // for text wrapping, it is used as decoration width.
447 m_decorationSize
= QSize(itemWidth
, size
);
449 m_decorationSize
= QSize(size
, size
);
452 m_itemSize
= QSize(itemWidth
, itemHeight
);
454 const int spacing
= settings
->gridSpacing();
455 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
457 m_controller
->setZoomInPossible(isZoomInPossible());
458 m_controller
->setZoomOutPossible(isZoomOutPossible());
461 KFileItem
DolphinIconsView::itemForIndex(const QModelIndex
& index
) const
463 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
464 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
465 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
466 return dirModel
->itemForIndex(dirIndex
);
469 int DolphinIconsView::additionalInfoCount() const
471 const DolphinView
* view
= m_controller
->dolphinView();
472 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
473 return settings
->showAdditionalInfo() ? view
->additionalInfo().count() : 0;
476 #include "dolphiniconsview.moc"