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(this, SIGNAL(activated(const QModelIndex
&)),
46 controller
, SLOT(triggerItem(const QModelIndex
&)));
47 connect(controller
, SIGNAL(showPreviewChanged(bool)),
48 this, SLOT(updateGridSize(bool)));
49 connect(controller
, SIGNAL(zoomIn()),
50 this, SLOT(zoomIn()));
51 connect(controller
, SIGNAL(zoomOut()),
52 this, SLOT(zoomOut()));
54 // apply the icons mode settings to the widget
55 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
56 Q_ASSERT(settings
!= 0);
58 m_viewOptions
= QListView::viewOptions();
59 m_viewOptions
.font
= QFont(settings
->fontFamily(), settings
->fontSize());
61 updateGridSize(controller
->showPreview());
63 if (settings
->arrangement() == QListView::TopToBottom
) {
64 setFlow(QListView::LeftToRight
);
65 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Top
;
68 setFlow(QListView::TopToBottom
);
69 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Left
;
72 KFileItemDelegate
* delegate
= new KFileItemDelegate(parent
);
73 const KFileItemDelegate::AdditionalInformation info
=
74 static_cast<KFileItemDelegate::AdditionalInformation
>(settings
->additionalInfo());
75 delegate
->setAdditionalInformation(info
);
76 setItemDelegate(delegate
);
79 DolphinIconsView::~DolphinIconsView()
83 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
88 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
90 QListView::contextMenuEvent(event
);
91 m_controller
->triggerContextMenuRequest(event
->pos());
94 void DolphinIconsView::mouseReleaseEvent(QMouseEvent
* event
)
96 QListView::mouseReleaseEvent(event
);
97 m_controller
->triggerActivation();
100 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
102 if (event
->mimeData()->hasUrls()) {
103 event
->acceptProposedAction();
107 void DolphinIconsView::dropEvent(QDropEvent
* event
)
109 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
110 if (urls
.isEmpty() || (event
->source() == this)) {
111 QListView::dropEvent(event
);
114 event
->acceptProposedAction();
115 m_controller
->indicateDroppedUrls(urls
, event
->pos());
119 void DolphinIconsView::updateGridSize(bool showPreview
)
121 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
122 Q_ASSERT(settings
!= 0);
124 int gridWidth
= settings
->gridWidth();
125 int gridHeight
= settings
->gridHeight();
126 int size
= settings
->iconSize();
129 const int previewSize
= settings
->previewSize();
130 const int diff
= previewSize
- size
;
139 m_viewOptions
.decorationSize
= QSize(size
, size
);
140 setGridSize(QSize(gridWidth
, gridHeight
));
142 m_controller
->setZoomInPossible(isZoomInPossible());
143 m_controller
->setZoomOutPossible(isZoomOutPossible());
146 void DolphinIconsView::zoomIn()
148 if (isZoomInPossible()) {
149 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
151 const bool showPreview
= m_controller
->showPreview();
153 const int previewSize
= increasedIconSize(settings
->previewSize());
154 settings
->setPreviewSize(previewSize
);
157 const int iconSize
= increasedIconSize(settings
->iconSize());
158 settings
->setIconSize(iconSize
);
159 if (settings
->previewSize() < iconSize
) {
160 // assure that the preview size is always >= the icon size
161 settings
->setPreviewSize(iconSize
);
165 updateGridSize(showPreview
);
169 void DolphinIconsView::zoomOut()
171 if (isZoomOutPossible()) {
172 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
174 const bool showPreview
= m_controller
->showPreview();
176 const int previewSize
= decreasedIconSize(settings
->previewSize());
177 settings
->setPreviewSize(previewSize
);
178 if (settings
->iconSize() > previewSize
) {
179 // assure that the icon size is always <= the preview size
180 settings
->setIconSize(previewSize
);
184 const int iconSize
= decreasedIconSize(settings
->iconSize());
185 settings
->setIconSize(iconSize
);
188 updateGridSize(showPreview
);
192 bool DolphinIconsView::isZoomInPossible() const
194 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
195 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
196 return size
< K3Icon::SizeEnormous
;
199 bool DolphinIconsView::isZoomOutPossible() const
201 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
202 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
203 return size
> K3Icon::SizeSmall
;
206 int DolphinIconsView::increasedIconSize(int size
) const
208 // TODO: get rid of K3Icon sizes
211 case K3Icon::SizeSmall
: incSize
= K3Icon::SizeSmallMedium
; break;
212 case K3Icon::SizeSmallMedium
: incSize
= K3Icon::SizeMedium
; break;
213 case K3Icon::SizeMedium
: incSize
= K3Icon::SizeLarge
; break;
214 case K3Icon::SizeLarge
: incSize
= K3Icon::SizeHuge
; break;
215 case K3Icon::SizeHuge
: incSize
= K3Icon::SizeEnormous
; break;
216 default: Q_ASSERT(false); break;
221 int DolphinIconsView::decreasedIconSize(int size
) const
223 // TODO: get rid of K3Icon sizes
226 case K3Icon::SizeSmallMedium
: decSize
= K3Icon::SizeSmall
; break;
227 case K3Icon::SizeMedium
: decSize
= K3Icon::SizeSmallMedium
; break;
228 case K3Icon::SizeLarge
: decSize
= K3Icon::SizeMedium
; break;
229 case K3Icon::SizeHuge
: decSize
= K3Icon::SizeLarge
; break;
230 case K3Icon::SizeEnormous
: decSize
= K3Icon::SizeHuge
; break;
231 default: Q_ASSERT(false); break;
236 #include "dolphiniconsview.moc"