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 "dolphincontroller.h"
23 #include "dolphinsettings.h"
24 #include "dolphinitemcategorizer.h"
26 #include "dolphin_iconsmodesettings.h"
28 #include <kdirmodel.h>
29 #include <kfileitem.h>
30 #include <kfileitemdelegate.h>
32 #include <QAbstractProxyModel>
33 #include <QApplication>
36 DolphinIconsView::DolphinIconsView(QWidget
* parent
, DolphinController
* controller
) :
38 m_controller(controller
)
40 Q_ASSERT(controller
!= 0);
41 setViewMode(QListView::IconMode
);
42 setResizeMode(QListView::Adjust
);
44 setMouseTracking(true);
45 viewport()->setAttribute(Qt::WA_Hover
);
47 if (KGlobalSettings::singleClick()) {
48 connect(this, SIGNAL(clicked(const QModelIndex
&)),
49 controller
, SLOT(triggerItem(const QModelIndex
&)));
51 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
52 controller
, SLOT(triggerItem(const QModelIndex
&)));
54 connect(this, SIGNAL(activated(const QModelIndex
&)),
55 controller
, SLOT(triggerItem(const QModelIndex
&)));
56 connect(this, SIGNAL(entered(const QModelIndex
&)),
57 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
58 connect(this, SIGNAL(viewportEntered()),
59 controller
, SLOT(emitViewportEntered()));
60 connect(controller
, SIGNAL(showPreviewChanged(bool)),
61 this, SLOT(slotShowPreviewChanged(bool)));
62 connect(controller
, SIGNAL(showAdditionalInfoChanged(bool)),
63 this, SLOT(slotShowAdditionalInfoChanged(bool)));
64 connect(controller
, SIGNAL(zoomIn()),
65 this, SLOT(zoomIn()));
66 connect(controller
, SIGNAL(zoomOut()),
67 this, SLOT(zoomOut()));
69 // apply the icons mode settings to the widget
70 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
71 Q_ASSERT(settings
!= 0);
73 m_viewOptions
= KListView::viewOptions();
74 m_viewOptions
.showDecorationSelected
= true;
76 QFont
font(settings
->fontFamily(), settings
->fontSize());
77 font
.setItalic(settings
->italicFont());
78 font
.setBold(settings
->boldFont());
79 m_viewOptions
.font
= font
;
81 setWordWrap(settings
->numberOfTextlines() > 1);
82 updateGridSize(controller
->showPreview(), controller
->showAdditionalInfo());
84 if (settings
->arrangement() == QListView::TopToBottom
) {
85 setFlow(QListView::LeftToRight
);
86 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Top
;
88 setFlow(QListView::TopToBottom
);
89 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Left
;
90 m_viewOptions
.displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
94 DolphinIconsView::~DolphinIconsView()
98 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
100 return m_viewOptions
;
103 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
105 KListView::contextMenuEvent(event
);
106 m_controller
->triggerContextMenuRequest(event
->pos());
109 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
111 if (!indexAt(event
->pos()).isValid()) {
112 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
113 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
118 KListView::mousePressEvent(event
);
121 void DolphinIconsView::mouseReleaseEvent(QMouseEvent
* event
)
123 KListView::mouseReleaseEvent(event
);
124 m_controller
->triggerActivation();
127 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
129 if (event
->mimeData()->hasUrls()) {
130 event
->acceptProposedAction();
134 void DolphinIconsView::dropEvent(QDropEvent
* event
)
136 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
137 if (!urls
.isEmpty()) {
138 m_controller
->indicateDroppedUrls(urls
,
139 indexAt(event
->pos()),
141 event
->acceptProposedAction();
143 KListView::dropEvent(event
);
146 void DolphinIconsView::slotShowPreviewChanged(bool showPreview
)
148 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
151 void DolphinIconsView::slotShowAdditionalInfoChanged(bool showAdditionalInfo
)
153 updateGridSize(m_controller
->showPreview(), showAdditionalInfo
);
156 void DolphinIconsView::zoomIn()
158 if (isZoomInPossible()) {
159 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
161 const int oldIconSize
= settings
->iconSize();
162 int newIconSize
= oldIconSize
;
164 const bool showPreview
= m_controller
->showPreview();
166 const int previewSize
= increasedIconSize(settings
->previewSize());
167 settings
->setPreviewSize(previewSize
);
169 newIconSize
= increasedIconSize(oldIconSize
);
170 settings
->setIconSize(newIconSize
);
171 if (settings
->previewSize() < newIconSize
) {
172 // assure that the preview size is always >= the icon size
173 settings
->setPreviewSize(newIconSize
);
177 // increase also the grid size
178 const int diff
= newIconSize
- oldIconSize
;
179 settings
->setItemWidth(settings
->itemWidth() + diff
);
180 settings
->setItemHeight(settings
->itemHeight() + diff
);
182 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
186 void DolphinIconsView::zoomOut()
188 if (isZoomOutPossible()) {
189 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
191 const int oldIconSize
= settings
->iconSize();
192 int newIconSize
= oldIconSize
;
194 const bool showPreview
= m_controller
->showPreview();
196 const int previewSize
= decreasedIconSize(settings
->previewSize());
197 settings
->setPreviewSize(previewSize
);
198 if (settings
->iconSize() > previewSize
) {
199 // assure that the icon size is always <= the preview size
200 newIconSize
= previewSize
;
201 settings
->setIconSize(newIconSize
);
204 newIconSize
= decreasedIconSize(settings
->iconSize());
205 settings
->setIconSize(newIconSize
);
208 // decrease also the grid size
209 const int diff
= oldIconSize
- newIconSize
;
210 settings
->setItemWidth(settings
->itemWidth() - diff
);
211 settings
->setItemHeight(settings
->itemHeight() - diff
);
213 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
217 bool DolphinIconsView::isZoomInPossible() const
219 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
220 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
221 return size
< K3Icon::SizeEnormous
;
224 bool DolphinIconsView::isZoomOutPossible() const
226 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
227 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
228 return size
> K3Icon::SizeSmall
;
231 int DolphinIconsView::increasedIconSize(int size
) const
233 // TODO: get rid of K3Icon sizes
236 case K3Icon::SizeSmall
: incSize
= K3Icon::SizeSmallMedium
; break;
237 case K3Icon::SizeSmallMedium
: incSize
= K3Icon::SizeMedium
; break;
238 case K3Icon::SizeMedium
: incSize
= K3Icon::SizeLarge
; break;
239 case K3Icon::SizeLarge
: incSize
= K3Icon::SizeHuge
; break;
240 case K3Icon::SizeHuge
: incSize
= K3Icon::SizeEnormous
; break;
241 default: Q_ASSERT(false); break;
246 int DolphinIconsView::decreasedIconSize(int size
) const
248 // TODO: get rid of K3Icon sizes
251 case K3Icon::SizeSmallMedium
: decSize
= K3Icon::SizeSmall
; break;
252 case K3Icon::SizeMedium
: decSize
= K3Icon::SizeSmallMedium
; break;
253 case K3Icon::SizeLarge
: decSize
= K3Icon::SizeMedium
; break;
254 case K3Icon::SizeHuge
: decSize
= K3Icon::SizeLarge
; break;
255 case K3Icon::SizeEnormous
: decSize
= K3Icon::SizeHuge
; break;
256 default: Q_ASSERT(false); break;
261 void DolphinIconsView::updateGridSize(bool showPreview
, bool showAdditionalInfo
)
263 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
264 Q_ASSERT(settings
!= 0);
266 int itemWidth
= settings
->itemWidth();
267 int itemHeight
= settings
->itemHeight();
268 int size
= settings
->iconSize();
271 const int previewSize
= settings
->previewSize();
272 const int diff
= previewSize
- size
;
280 if (showAdditionalInfo
) {
281 itemHeight
+= m_viewOptions
.font
.pointSize() * 2;
284 if (settings
->arrangement() == QListView::TopToBottom
) {
285 // The decoration width indirectly defines the maximum
286 // width for the text wrapping. To use the maximum item width
287 // for text wrapping, it is used as decoration width.
288 m_viewOptions
.decorationSize
= QSize(itemWidth
, size
);
290 m_viewOptions
.decorationSize
= QSize(size
, size
);
293 const int spacing
= settings
->gridSpacing();
294 setGridSize(QSize(itemWidth
+ spacing
, itemHeight
+ spacing
));
296 m_controller
->setZoomInPossible(isZoomInPossible());
297 m_controller
->setZoomOutPossible(isZoomOutPossible());
300 #include "dolphiniconsview.moc"