]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
allow zooming in and zooming out in the details view
[dolphin.git] / src / dolphiniconsview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "dolphiniconsview.h"
21
22 #include "dolphincontroller.h"
23 #include "dolphinsettings.h"
24
25 #include "dolphin_iconsmodesettings.h"
26
27 #include <kdirmodel.h>
28 #include <kfileitem.h>
29 #include <kfileitemdelegate.h>
30
31 #include <QAbstractProxyModel>
32
33 DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controller) :
34 QListView(parent),
35 m_controller(controller)
36 {
37 Q_ASSERT(controller != 0);
38 setViewMode(QListView::IconMode);
39 setResizeMode(QListView::Adjust);
40
41 connect(this, SIGNAL(clicked(const QModelIndex&)),
42 controller, SLOT(triggerItem(const QModelIndex&)));
43 connect(controller, SIGNAL(showPreviewChanged(bool)),
44 this, SLOT(updateGridSize(bool)));
45 connect(controller, SIGNAL(zoomIn()),
46 this, SLOT(zoomIn()));
47 connect(controller, SIGNAL(zoomOut()),
48 this, SLOT(zoomOut()));
49
50 // apply the icons mode settings to the widget
51 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
52 Q_ASSERT(settings != 0);
53
54 m_viewOptions = QListView::viewOptions();
55 m_viewOptions.font = QFont(settings->fontFamily(), settings->fontSize());
56
57 updateGridSize(controller->showPreview());
58
59 if (settings->arrangement() == QListView::TopToBottom) {
60 setFlow(QListView::LeftToRight);
61 m_viewOptions.decorationPosition = QStyleOptionViewItem::Top;
62 }
63 else {
64 setFlow(QListView::TopToBottom);
65 m_viewOptions.decorationPosition = QStyleOptionViewItem::Left;
66 }
67
68 KFileItemDelegate* delegate = new KFileItemDelegate(parent);
69 const KFileItemDelegate::AdditionalInformation info =
70 static_cast<KFileItemDelegate::AdditionalInformation>(settings->additionalInfo());
71 delegate->setAdditionalInformation(info);
72 setItemDelegate(delegate);
73 }
74
75 DolphinIconsView::~DolphinIconsView()
76 {
77 }
78
79 QStyleOptionViewItem DolphinIconsView::viewOptions() const
80 {
81 return m_viewOptions;
82 }
83
84 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
85 {
86 QListView::contextMenuEvent(event);
87 m_controller->triggerContextMenuRequest(event->pos());
88 }
89
90 void DolphinIconsView::mouseReleaseEvent(QMouseEvent* event)
91 {
92 QListView::mouseReleaseEvent(event);
93 m_controller->triggerActivation();
94 }
95
96 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
97 {
98 if (event->mimeData()->hasUrls()) {
99 event->acceptProposedAction();
100 }
101 }
102
103 void DolphinIconsView::dropEvent(QDropEvent* event)
104 {
105 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
106 if (urls.isEmpty() || (event->source() == this)) {
107 QListView::dropEvent(event);
108 }
109 else {
110 event->acceptProposedAction();
111 m_controller->indicateDroppedUrls(urls, event->pos());
112 }
113 }
114
115 void DolphinIconsView::updateGridSize(bool showPreview)
116 {
117 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
118 Q_ASSERT(settings != 0);
119
120 int gridWidth = settings->gridWidth();
121 int gridHeight = settings->gridHeight();
122 int size = settings->iconSize();
123
124 if (showPreview) {
125 const int previewSize = settings->previewSize();
126 const int diff = previewSize - size;
127 Q_ASSERT(diff >= 0);
128 gridWidth += diff;
129 gridHeight += diff;
130
131 size = previewSize;
132 }
133
134
135 m_viewOptions.decorationSize = QSize(size, size);
136 setGridSize(QSize(gridWidth, gridHeight));
137
138 m_controller->setZoomInPossible(isZoomInPossible());
139 m_controller->setZoomOutPossible(isZoomOutPossible());
140 }
141
142 void DolphinIconsView::zoomIn()
143 {
144 if (isZoomInPossible()) {
145 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
146
147 const bool showPreview = m_controller->showPreview();
148 if (showPreview) {
149 const int previewSize = increasedIconSize(settings->previewSize());
150 settings->setPreviewSize(previewSize);
151 }
152 else {
153 const int iconSize = increasedIconSize(settings->iconSize());
154 settings->setIconSize(iconSize);
155 if (settings->previewSize() < iconSize) {
156 // assure that the preview size is always >= the icon size
157 settings->setPreviewSize(iconSize);
158 }
159 }
160
161 updateGridSize(showPreview);
162 }
163 }
164
165 void DolphinIconsView::zoomOut()
166 {
167 if (isZoomOutPossible()) {
168 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
169
170 const bool showPreview = m_controller->showPreview();
171 if (showPreview) {
172 const int previewSize = decreasedIconSize(settings->previewSize());
173 settings->setPreviewSize(previewSize);
174 if (settings->iconSize() > previewSize) {
175 // assure that the icon size is always <= the preview size
176 settings->setIconSize(previewSize);
177 }
178 }
179 else {
180 const int iconSize = decreasedIconSize(settings->iconSize());
181 settings->setIconSize(iconSize);
182 }
183
184 updateGridSize(showPreview);
185 }
186 }
187
188 bool DolphinIconsView::isZoomInPossible() const
189 {
190 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
191 const int size = m_controller->showPreview() ? settings->previewSize() : settings->iconSize();
192 return size < K3Icon::SizeEnormous;
193 }
194
195 bool DolphinIconsView::isZoomOutPossible() const
196 {
197 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
198 const int size = m_controller->showPreview() ? settings->previewSize() : settings->iconSize();
199 return size > K3Icon::SizeSmall;
200 }
201
202 int DolphinIconsView::increasedIconSize(int size) const
203 {
204 // TODO: get rid of K3Icon sizes
205 int incSize = 0;
206 switch (size) {
207 case K3Icon::SizeSmall: incSize = K3Icon::SizeSmallMedium; break;
208 case K3Icon::SizeSmallMedium: incSize = K3Icon::SizeMedium; break;
209 case K3Icon::SizeMedium: incSize = K3Icon::SizeLarge; break;
210 case K3Icon::SizeLarge: incSize = K3Icon::SizeHuge; break;
211 case K3Icon::SizeHuge: incSize = K3Icon::SizeEnormous; break;
212 default: Q_ASSERT(false); break;
213 }
214 return incSize;
215 }
216
217 int DolphinIconsView::decreasedIconSize(int size) const
218 {
219 // TODO: get rid of K3Icon sizes
220 int decSize = 0;
221 switch (size) {
222 case K3Icon::SizeSmallMedium: decSize = K3Icon::SizeSmall; break;
223 case K3Icon::SizeMedium: decSize = K3Icon::SizeSmallMedium; break;
224 case K3Icon::SizeLarge: decSize = K3Icon::SizeMedium; break;
225 case K3Icon::SizeHuge: decSize = K3Icon::SizeLarge; break;
226 case K3Icon::SizeEnormous: decSize = K3Icon::SizeHuge; break;
227 default: Q_ASSERT(false); break;
228 }
229 return decSize;
230 }
231
232 #include "dolphiniconsview.moc"