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"
25 #include "dolphin_iconsmodesettings.h"
27 #include <kdirmodel.h>
28 #include <kfileitem.h>
29 #include <kfileitemdelegate.h>
31 #include <QAbstractProxyModel>
33 DolphinIconsView::DolphinIconsView(QWidget
* parent
, DolphinController
* controller
) :
35 m_controller(controller
)
37 Q_ASSERT(controller
!= 0);
38 setViewMode(QListView::IconMode
);
39 setResizeMode(QListView::Adjust
);
41 viewport()->setAttribute(Qt::WA_Hover
);
43 connect(this, SIGNAL(clicked(const QModelIndex
&)),
44 controller
, SLOT(triggerItem(const QModelIndex
&)));
45 connect(controller
, SIGNAL(showPreviewChanged(bool)),
46 this, SLOT(updateGridSize(bool)));
47 connect(controller
, SIGNAL(zoomIn()),
48 this, SLOT(zoomIn()));
49 connect(controller
, SIGNAL(zoomOut()),
50 this, SLOT(zoomOut()));
52 // apply the icons mode settings to the widget
53 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
54 Q_ASSERT(settings
!= 0);
56 m_viewOptions
= QListView::viewOptions();
57 m_viewOptions
.font
= QFont(settings
->fontFamily(), settings
->fontSize());
59 updateGridSize(controller
->showPreview());
61 if (settings
->arrangement() == QListView::TopToBottom
) {
62 setFlow(QListView::LeftToRight
);
63 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Top
;
66 setFlow(QListView::TopToBottom
);
67 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Left
;
70 KFileItemDelegate
* delegate
= new KFileItemDelegate(parent
);
71 const KFileItemDelegate::AdditionalInformation info
=
72 static_cast<KFileItemDelegate::AdditionalInformation
>(settings
->additionalInfo());
73 delegate
->setAdditionalInformation(info
);
74 setItemDelegate(delegate
);
77 DolphinIconsView::~DolphinIconsView()
81 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
86 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
88 QListView::contextMenuEvent(event
);
89 m_controller
->triggerContextMenuRequest(event
->pos());
92 void DolphinIconsView::mouseReleaseEvent(QMouseEvent
* event
)
94 QListView::mouseReleaseEvent(event
);
95 m_controller
->triggerActivation();
98 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
100 if (event
->mimeData()->hasUrls()) {
101 event
->acceptProposedAction();
105 void DolphinIconsView::dropEvent(QDropEvent
* event
)
107 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
108 if (urls
.isEmpty() || (event
->source() == this)) {
109 QListView::dropEvent(event
);
112 event
->acceptProposedAction();
113 m_controller
->indicateDroppedUrls(urls
, event
->pos());
117 void DolphinIconsView::updateGridSize(bool showPreview
)
119 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
120 Q_ASSERT(settings
!= 0);
122 int gridWidth
= settings
->gridWidth();
123 int gridHeight
= settings
->gridHeight();
124 int size
= settings
->iconSize();
127 const int previewSize
= settings
->previewSize();
128 const int diff
= previewSize
- size
;
137 m_viewOptions
.decorationSize
= QSize(size
, size
);
138 setGridSize(QSize(gridWidth
, gridHeight
));
140 m_controller
->setZoomInPossible(isZoomInPossible());
141 m_controller
->setZoomOutPossible(isZoomOutPossible());
144 void DolphinIconsView::zoomIn()
146 if (isZoomInPossible()) {
147 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
149 const bool showPreview
= m_controller
->showPreview();
151 const int previewSize
= increasedIconSize(settings
->previewSize());
152 settings
->setPreviewSize(previewSize
);
155 const int iconSize
= increasedIconSize(settings
->iconSize());
156 settings
->setIconSize(iconSize
);
157 if (settings
->previewSize() < iconSize
) {
158 // assure that the preview size is always >= the icon size
159 settings
->setPreviewSize(iconSize
);
163 updateGridSize(showPreview
);
167 void DolphinIconsView::zoomOut()
169 if (isZoomOutPossible()) {
170 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
172 const bool showPreview
= m_controller
->showPreview();
174 const int previewSize
= decreasedIconSize(settings
->previewSize());
175 settings
->setPreviewSize(previewSize
);
176 if (settings
->iconSize() > previewSize
) {
177 // assure that the icon size is always <= the preview size
178 settings
->setIconSize(previewSize
);
182 const int iconSize
= decreasedIconSize(settings
->iconSize());
183 settings
->setIconSize(iconSize
);
186 updateGridSize(showPreview
);
190 bool DolphinIconsView::isZoomInPossible() const
192 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
193 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
194 return size
< K3Icon::SizeEnormous
;
197 bool DolphinIconsView::isZoomOutPossible() const
199 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
200 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
201 return size
> K3Icon::SizeSmall
;
204 int DolphinIconsView::increasedIconSize(int size
) const
206 // TODO: get rid of K3Icon sizes
209 case K3Icon::SizeSmall
: incSize
= K3Icon::SizeSmallMedium
; break;
210 case K3Icon::SizeSmallMedium
: incSize
= K3Icon::SizeMedium
; break;
211 case K3Icon::SizeMedium
: incSize
= K3Icon::SizeLarge
; break;
212 case K3Icon::SizeLarge
: incSize
= K3Icon::SizeHuge
; break;
213 case K3Icon::SizeHuge
: incSize
= K3Icon::SizeEnormous
; break;
214 default: Q_ASSERT(false); break;
219 int DolphinIconsView::decreasedIconSize(int size
) const
221 // TODO: get rid of K3Icon sizes
224 case K3Icon::SizeSmallMedium
: decSize
= K3Icon::SizeSmall
; break;
225 case K3Icon::SizeMedium
: decSize
= K3Icon::SizeSmallMedium
; break;
226 case K3Icon::SizeLarge
: decSize
= K3Icon::SizeMedium
; break;
227 case K3Icon::SizeHuge
: decSize
= K3Icon::SizeLarge
; break;
228 case K3Icon::SizeEnormous
: decSize
= K3Icon::SizeHuge
; break;
229 default: Q_ASSERT(false); break;
234 #include "dolphiniconsview.moc"