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>
37 DolphinIconsView::DolphinIconsView(QWidget
* parent
, DolphinController
* controller
) :
39 m_controller(controller
),
42 Q_ASSERT(controller
!= 0);
43 setViewMode(QListView::IconMode
);
44 setResizeMode(QListView::Adjust
);
46 setMouseTracking(true);
47 viewport()->setAttribute(Qt::WA_Hover
);
49 if (KGlobalSettings::singleClick()) {
50 connect(this, SIGNAL(clicked(const QModelIndex
&)),
51 controller
, SLOT(triggerItem(const QModelIndex
&)));
53 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
54 controller
, SLOT(triggerItem(const QModelIndex
&)));
56 connect(this, SIGNAL(activated(const QModelIndex
&)),
57 controller
, SLOT(triggerItem(const QModelIndex
&)));
58 connect(this, SIGNAL(entered(const QModelIndex
&)),
59 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
60 connect(this, SIGNAL(viewportEntered()),
61 controller
, SLOT(emitViewportEntered()));
62 connect(controller
, SIGNAL(showPreviewChanged(bool)),
63 this, SLOT(slotShowPreviewChanged(bool)));
64 connect(controller
, SIGNAL(showAdditionalInfoChanged(bool)),
65 this, SLOT(slotShowAdditionalInfoChanged(bool)));
66 connect(controller
, SIGNAL(zoomIn()),
67 this, SLOT(zoomIn()));
68 connect(controller
, SIGNAL(zoomOut()),
69 this, SLOT(zoomOut()));
71 // apply the icons mode settings to the widget
72 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
73 Q_ASSERT(settings
!= 0);
75 m_viewOptions
= KListView::viewOptions();
76 m_viewOptions
.showDecorationSelected
= true;
78 QFont
font(settings
->fontFamily(), settings
->fontSize());
79 font
.setItalic(settings
->italicFont());
80 font
.setBold(settings
->boldFont());
81 m_viewOptions
.font
= font
;
83 setWordWrap(settings
->numberOfTextlines() > 1);
84 updateGridSize(controller
->showPreview(), controller
->showAdditionalInfo());
86 if (settings
->arrangement() == QListView::TopToBottom
) {
87 setFlow(QListView::LeftToRight
);
88 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Top
;
90 setFlow(QListView::TopToBottom
);
91 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Left
;
92 m_viewOptions
.displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
96 DolphinIconsView::~DolphinIconsView()
100 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
102 return m_viewOptions
;
105 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
107 KListView::contextMenuEvent(event
);
108 m_controller
->triggerContextMenuRequest(event
->pos());
111 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
113 if (!indexAt(event
->pos()).isValid()) {
114 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
115 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
120 KListView::mousePressEvent(event
);
123 void DolphinIconsView::mouseReleaseEvent(QMouseEvent
* event
)
125 KListView::mouseReleaseEvent(event
);
126 m_controller
->triggerActivation();
129 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
131 if (event
->mimeData()->hasUrls()) {
132 event
->acceptProposedAction();
137 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
139 KListView::dragLeaveEvent(event
);
141 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
143 setDirtyRegion(m_dropRect
);
146 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
148 KListView::dragMoveEvent(event
);
150 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
151 const QModelIndex index
= indexAt(event
->pos());
152 setDirtyRegion(m_dropRect
);
153 m_dropRect
= visualRect(index
);
154 setDirtyRegion(m_dropRect
);
157 void DolphinIconsView::dropEvent(QDropEvent
* event
)
159 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
160 if (!urls
.isEmpty()) {
161 m_controller
->indicateDroppedUrls(urls
,
162 indexAt(event
->pos()),
164 event
->acceptProposedAction();
166 KListView::dropEvent(event
);
170 void DolphinIconsView::paintEvent(QPaintEvent
* event
)
172 KListView::paintEvent(event
);
175 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
176 QPainter
painter(viewport());
178 QBrush
brush(m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
));
179 QColor color
= brush
.color();
181 brush
.setColor(color
);
182 painter
.fillRect(m_dropRect
, brush
);
187 void DolphinIconsView::slotShowPreviewChanged(bool showPreview
)
189 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
192 void DolphinIconsView::slotShowAdditionalInfoChanged(bool showAdditionalInfo
)
194 updateGridSize(m_controller
->showPreview(), showAdditionalInfo
);
197 void DolphinIconsView::zoomIn()
199 if (isZoomInPossible()) {
200 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
202 const int oldIconSize
= settings
->iconSize();
203 int newIconSize
= oldIconSize
;
205 const bool showPreview
= m_controller
->showPreview();
207 const int previewSize
= increasedIconSize(settings
->previewSize());
208 settings
->setPreviewSize(previewSize
);
210 newIconSize
= increasedIconSize(oldIconSize
);
211 settings
->setIconSize(newIconSize
);
212 if (settings
->previewSize() < newIconSize
) {
213 // assure that the preview size is always >= the icon size
214 settings
->setPreviewSize(newIconSize
);
218 // increase also the grid size
219 const int diff
= newIconSize
- oldIconSize
;
220 settings
->setItemWidth(settings
->itemWidth() + diff
);
221 settings
->setItemHeight(settings
->itemHeight() + diff
);
223 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
227 void DolphinIconsView::zoomOut()
229 if (isZoomOutPossible()) {
230 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
232 const int oldIconSize
= settings
->iconSize();
233 int newIconSize
= oldIconSize
;
235 const bool showPreview
= m_controller
->showPreview();
237 const int previewSize
= decreasedIconSize(settings
->previewSize());
238 settings
->setPreviewSize(previewSize
);
239 if (settings
->iconSize() > previewSize
) {
240 // assure that the icon size is always <= the preview size
241 newIconSize
= previewSize
;
242 settings
->setIconSize(newIconSize
);
245 newIconSize
= decreasedIconSize(settings
->iconSize());
246 settings
->setIconSize(newIconSize
);
249 // decrease also the grid size
250 const int diff
= oldIconSize
- newIconSize
;
251 settings
->setItemWidth(settings
->itemWidth() - diff
);
252 settings
->setItemHeight(settings
->itemHeight() - diff
);
254 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
258 bool DolphinIconsView::isZoomInPossible() const
260 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
261 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
262 return size
< K3Icon::SizeEnormous
;
265 bool DolphinIconsView::isZoomOutPossible() const
267 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
268 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
269 return size
> K3Icon::SizeSmall
;
272 int DolphinIconsView::increasedIconSize(int size
) const
274 // TODO: get rid of K3Icon sizes
277 case K3Icon::SizeSmall
: incSize
= K3Icon::SizeSmallMedium
; break;
278 case K3Icon::SizeSmallMedium
: incSize
= K3Icon::SizeMedium
; break;
279 case K3Icon::SizeMedium
: incSize
= K3Icon::SizeLarge
; break;
280 case K3Icon::SizeLarge
: incSize
= K3Icon::SizeHuge
; break;
281 case K3Icon::SizeHuge
: incSize
= K3Icon::SizeEnormous
; break;
282 default: Q_ASSERT(false); break;
287 int DolphinIconsView::decreasedIconSize(int size
) const
289 // TODO: get rid of K3Icon sizes
292 case K3Icon::SizeSmallMedium
: decSize
= K3Icon::SizeSmall
; break;
293 case K3Icon::SizeMedium
: decSize
= K3Icon::SizeSmallMedium
; break;
294 case K3Icon::SizeLarge
: decSize
= K3Icon::SizeMedium
; break;
295 case K3Icon::SizeHuge
: decSize
= K3Icon::SizeLarge
; break;
296 case K3Icon::SizeEnormous
: decSize
= K3Icon::SizeHuge
; break;
297 default: Q_ASSERT(false); break;
302 void DolphinIconsView::updateGridSize(bool showPreview
, bool showAdditionalInfo
)
304 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
305 Q_ASSERT(settings
!= 0);
307 int itemWidth
= settings
->itemWidth();
308 int itemHeight
= settings
->itemHeight();
309 int size
= settings
->iconSize();
312 const int previewSize
= settings
->previewSize();
313 const int diff
= previewSize
- size
;
321 if (showAdditionalInfo
) {
322 itemHeight
+= m_viewOptions
.font
.pointSize() * 2;
325 if (settings
->arrangement() == QListView::TopToBottom
) {
326 // The decoration width indirectly defines the maximum
327 // width for the text wrapping. To use the maximum item width
328 // for text wrapping, it is used as decoration width.
329 m_viewOptions
.decorationSize
= QSize(itemWidth
, size
);
331 m_viewOptions
.decorationSize
= QSize(size
, size
);
334 const int spacing
= settings
->gridSpacing();
335 setGridSize(QSize(itemWidth
+ spacing
, itemHeight
+ spacing
));
337 m_controller
->setZoomInPossible(isZoomInPossible());
338 m_controller
->setZoomOutPossible(isZoomOutPossible());
341 #include "dolphiniconsview.moc"