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"
26 #include "dolphin_iconsmodesettings.h"
29 #include <kdirmodel.h>
31 #include <QAbstractProxyModel>
32 #include <QApplication>
36 DolphinIconsView::DolphinIconsView(QWidget
* parent
, DolphinController
* controller
) :
37 KCategorizedView(parent
),
38 m_controller(controller
),
44 Q_ASSERT(controller
!= 0);
45 setViewMode(QListView::IconMode
);
46 setResizeMode(QListView::Adjust
);
47 setSpacing(KDialog::spacingHint());
48 setMouseTracking(true);
49 viewport()->setAttribute(Qt::WA_Hover
);
51 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
52 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
53 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
54 // RETURN-key in keyPressEvent().
55 if (KGlobalSettings::singleClick()) {
56 connect(this, SIGNAL(clicked(const QModelIndex
&)),
57 this, SLOT(triggerItem(const QModelIndex
&)));
59 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
60 this, SLOT(triggerItem(const QModelIndex
&)));
62 connect(this, SIGNAL(viewportEntered()),
63 controller
, SLOT(emitViewportEntered()));
64 connect(controller
, SIGNAL(showPreviewChanged(bool)),
65 this, SLOT(slotShowPreviewChanged(bool)));
66 connect(controller
->dolphinView(), SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
67 this, SLOT(slotAdditionalInfoChanged(const KFileItemDelegate::InformationList
&)));
68 connect(controller
, SIGNAL(zoomIn()),
69 this, SLOT(zoomIn()));
70 connect(controller
, SIGNAL(zoomOut()),
71 this, SLOT(zoomOut()));
73 connect(this, SIGNAL(entered(const QModelIndex
&)),
74 this, SLOT(slotEntered(const QModelIndex
&)));
76 // apply the icons mode settings to the widget
77 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
78 Q_ASSERT(settings
!= 0);
80 m_viewOptions
= KCategorizedView::viewOptions();
81 m_viewOptions
.showDecorationSelected
= true;
83 QFont
font(settings
->fontFamily(), settings
->fontSize());
84 font
.setItalic(settings
->italicFont());
85 font
.setBold(settings
->boldFont());
86 m_viewOptions
.font
= font
;
88 setWordWrap(settings
->numberOfTextlines() > 1);
89 updateGridSize(controller
->showPreview(), 0);
91 if (settings
->arrangement() == QListView::TopToBottom
) {
92 setFlow(QListView::LeftToRight
);
93 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Top
;
95 setFlow(QListView::TopToBottom
);
96 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Left
;
97 m_viewOptions
.displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
100 m_categoryDrawer
= new DolphinCategoryDrawer();
101 setCategoryDrawer(m_categoryDrawer
);
106 DolphinIconsView::~DolphinIconsView()
108 delete m_categoryDrawer
;
109 m_categoryDrawer
= 0;
112 QRect
DolphinIconsView::visualRect(const QModelIndex
& index
) const
114 const bool leftToRightFlow
= (flow() == QListView::LeftToRight
);
116 QRect itemRect
= KCategorizedView::visualRect(index
);
117 const int maxWidth
= m_itemSize
.width();
118 const int maxHeight
= m_itemSize
.height();
120 if (itemRect
.width() > maxWidth
) {
121 // assure that the maximum item width is not exceeded
122 if (leftToRightFlow
) {
123 const int left
= itemRect
.left() + (itemRect
.width() - maxWidth
) / 2;
124 itemRect
.setLeft(left
);
126 itemRect
.setWidth(maxWidth
);
129 if (itemRect
.height() > maxHeight
) {
130 // assure that the maximum item height is not exceeded
131 if (!leftToRightFlow
) {
132 const int top
= itemRect
.top() + (itemRect
.height() - maxHeight
) / 2;
133 itemRect
.setTop(top
);
135 itemRect
.setHeight(maxHeight
);
141 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
143 return m_viewOptions
;
146 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
148 KCategorizedView::contextMenuEvent(event
);
149 m_controller
->triggerContextMenuRequest(event
->pos());
152 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
154 m_controller
->requestActivation();
155 if (!indexAt(event
->pos()).isValid()) {
156 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
157 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
162 KCategorizedView::mousePressEvent(event
);
165 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
167 if (event
->mimeData()->hasUrls()) {
168 event
->acceptProposedAction();
173 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
175 KCategorizedView::dragLeaveEvent(event
);
177 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
179 setDirtyRegion(m_dropRect
);
182 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
184 KCategorizedView::dragMoveEvent(event
);
186 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
187 const QModelIndex index
= indexAt(event
->pos());
188 setDirtyRegion(m_dropRect
);
189 m_dropRect
= visualRect(index
);
190 setDirtyRegion(m_dropRect
);
193 void DolphinIconsView::dropEvent(QDropEvent
* event
)
195 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
196 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
197 if (!urls
.isEmpty()) {
198 const QModelIndex index
= indexAt(event
->pos());
199 if (index
.isValid()) {
200 const KFileItem item
= itemForIndex(index
);
201 m_controller
->indicateDroppedUrls(urls
,
205 event
->acceptProposedAction();
210 KCategorizedView::dropEvent(event
);
214 void DolphinIconsView::paintEvent(QPaintEvent
* event
)
216 KCategorizedView::paintEvent(event
);
218 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
220 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
221 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
225 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
227 KCategorizedView::keyPressEvent(event
);
229 const QItemSelectionModel
* selModel
= selectionModel();
230 const QModelIndex currentIndex
= selModel
->currentIndex();
231 const bool trigger
= currentIndex
.isValid()
232 && (event
->key() == Qt::Key_Return
)
233 && (selModel
->selectedIndexes().count() <= 1);
235 triggerItem(currentIndex
);
239 void DolphinIconsView::triggerItem(const QModelIndex
& index
)
241 m_controller
->triggerItem(itemForIndex(index
));
244 void DolphinIconsView::slotEntered(const QModelIndex
& index
)
246 m_controller
->emitItemEntered(itemForIndex(index
));
249 void DolphinIconsView::slotShowPreviewChanged(bool showPreview
)
251 const int infoCount
= m_controller
->dolphinView()->additionalInfo().count();
252 updateGridSize(showPreview
, infoCount
);
255 void DolphinIconsView::slotAdditionalInfoChanged(const KFileItemDelegate::InformationList
& info
)
257 updateGridSize(m_controller
->showPreview(), info
.count());
260 void DolphinIconsView::zoomIn()
262 if (isZoomInPossible()) {
263 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
265 const int oldIconSize
= settings
->iconSize();
266 int newIconSize
= oldIconSize
;
268 const bool showPreview
= m_controller
->showPreview();
270 const int previewSize
= increasedIconSize(settings
->previewSize());
271 settings
->setPreviewSize(previewSize
);
273 newIconSize
= increasedIconSize(oldIconSize
);
274 settings
->setIconSize(newIconSize
);
275 if (settings
->previewSize() < newIconSize
) {
276 // assure that the preview size is always >= the icon size
277 settings
->setPreviewSize(newIconSize
);
281 // increase also the grid size
282 const int diff
= newIconSize
- oldIconSize
;
283 settings
->setItemWidth(settings
->itemWidth() + diff
);
284 settings
->setItemHeight(settings
->itemHeight() + diff
);
286 const int infoCount
= m_controller
->dolphinView()->additionalInfo().count();
287 updateGridSize(showPreview
, infoCount
);
291 void DolphinIconsView::zoomOut()
293 if (isZoomOutPossible()) {
294 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
296 const int oldIconSize
= settings
->iconSize();
297 int newIconSize
= oldIconSize
;
299 const bool showPreview
= m_controller
->showPreview();
301 const int previewSize
= decreasedIconSize(settings
->previewSize());
302 settings
->setPreviewSize(previewSize
);
303 if (settings
->iconSize() > previewSize
) {
304 // assure that the icon size is always <= the preview size
305 newIconSize
= previewSize
;
306 settings
->setIconSize(newIconSize
);
309 newIconSize
= decreasedIconSize(settings
->iconSize());
310 settings
->setIconSize(newIconSize
);
313 // decrease also the grid size
314 const int diff
= oldIconSize
- newIconSize
;
315 settings
->setItemWidth(settings
->itemWidth() - diff
);
316 settings
->setItemHeight(settings
->itemHeight() - diff
);
318 const int infoCount
= m_controller
->dolphinView()->additionalInfo().count();
319 updateGridSize(showPreview
, infoCount
);
323 bool DolphinIconsView::isZoomInPossible() const
325 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
326 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
327 return size
< KIconLoader::SizeEnormous
;
330 bool DolphinIconsView::isZoomOutPossible() const
332 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
333 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
334 return size
> KIconLoader::SizeSmall
;
337 int DolphinIconsView::increasedIconSize(int size
) const
341 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
342 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
343 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
344 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
345 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
346 default: Q_ASSERT(false); break;
351 int DolphinIconsView::decreasedIconSize(int size
) const
355 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
356 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
357 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
358 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
359 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
360 default: Q_ASSERT(false); break;
365 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
367 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
368 Q_ASSERT(settings
!= 0);
370 int itemWidth
= settings
->itemWidth();
371 int itemHeight
= settings
->itemHeight();
372 int size
= settings
->iconSize();
375 const int previewSize
= settings
->previewSize();
376 const int diff
= previewSize
- size
;
384 Q_ASSERT(additionalInfoCount
>= 0);
385 itemHeight
+= additionalInfoCount
* m_viewOptions
.font
.pointSize() * 2;
387 if (settings
->arrangement() == QListView::TopToBottom
) {
388 // The decoration width indirectly defines the maximum
389 // width for the text wrapping. To use the maximum item width
390 // for text wrapping, it is used as decoration width.
391 m_viewOptions
.decorationSize
= QSize(itemWidth
, size
);
393 m_viewOptions
.decorationSize
= QSize(size
, size
);
396 const int spacing
= settings
->gridSpacing();
397 setGridSize(QSize(itemWidth
+ spacing
, itemHeight
+ spacing
));
399 m_itemSize
= QSize(itemWidth
, itemHeight
);
401 m_controller
->setZoomInPossible(isZoomInPossible());
402 m_controller
->setZoomOutPossible(isZoomOutPossible());
405 KFileItem
DolphinIconsView::itemForIndex(const QModelIndex
& index
) const
407 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
408 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
409 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
410 return dirModel
->itemForIndex(dirIndex
);
414 #include "dolphiniconsview.moc"