]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
adapt Dolphin to kdelibs coding style (http://techbase.kde.org/Policies/Kdelibs_Codin...
[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 #include <QPoint>
33
34 DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controller) :
35 QListView(parent),
36 m_controller(controller)
37 {
38 Q_ASSERT(controller != 0);
39 setViewMode(QListView::IconMode);
40 setResizeMode(QListView::Adjust);
41
42 viewport()->setAttribute(Qt::WA_Hover);
43
44 connect(this, SIGNAL(clicked(const QModelIndex&)),
45 controller, SLOT(triggerItem(const QModelIndex&)));
46 connect(this, SIGNAL(activated(const QModelIndex&)),
47 controller, SLOT(triggerItem(const QModelIndex&)));
48 connect(controller, SIGNAL(showPreviewChanged(bool)),
49 this, SLOT(updateGridSize(bool)));
50 connect(controller, SIGNAL(zoomIn()),
51 this, SLOT(zoomIn()));
52 connect(controller, SIGNAL(zoomOut()),
53 this, SLOT(zoomOut()));
54
55 // apply the icons mode settings to the widget
56 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
57 Q_ASSERT(settings != 0);
58
59 m_viewOptions = QListView::viewOptions();
60
61 QFont font(settings->fontFamily(), settings->fontSize());
62 font.setItalic(settings->italicFont());
63 font.setBold(settings->boldFont());
64 m_viewOptions.font = font;
65
66 updateGridSize(controller->showPreview());
67
68 if (settings->arrangement() == QListView::TopToBottom) {
69 setFlow(QListView::LeftToRight);
70 m_viewOptions.decorationPosition = QStyleOptionViewItem::Top;
71 } else {
72 setFlow(QListView::TopToBottom);
73 m_viewOptions.decorationPosition = QStyleOptionViewItem::Left;
74 }
75 }
76
77 DolphinIconsView::~DolphinIconsView()
78 {}
79
80 QStyleOptionViewItem DolphinIconsView::viewOptions() const
81 {
82 return m_viewOptions;
83 }
84
85 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
86 {
87 QListView::contextMenuEvent(event);
88 m_controller->triggerContextMenuRequest(event->pos());
89 }
90
91 void DolphinIconsView::mouseReleaseEvent(QMouseEvent* event)
92 {
93 QListView::mouseReleaseEvent(event);
94 m_controller->triggerActivation();
95 }
96
97 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
98 {
99 if (event->mimeData()->hasUrls()) {
100 event->acceptProposedAction();
101 }
102 }
103
104 void DolphinIconsView::dropEvent(QDropEvent* event)
105 {
106 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
107 if (!urls.isEmpty()) {
108 m_controller->indicateDroppedUrls(urls,
109 indexAt(event->pos()),
110 event->source());
111 event->acceptProposedAction();
112 }
113 QListView::dropEvent(event);
114 }
115
116 void DolphinIconsView::updateGridSize(bool showPreview)
117 {
118 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
119 Q_ASSERT(settings != 0);
120
121 int gridWidth = settings->gridWidth();
122 int gridHeight = settings->gridHeight();
123 int size = settings->iconSize();
124
125 if (showPreview) {
126 const int previewSize = settings->previewSize();
127 const int diff = previewSize - size;
128 Q_ASSERT(diff >= 0);
129 gridWidth += diff;
130 gridHeight += diff;
131
132 size = previewSize;
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 int oldIconSize = settings->iconSize();
148 int newIconSize = oldIconSize;
149
150 const bool showPreview = m_controller->showPreview();
151 if (showPreview) {
152 const int previewSize = increasedIconSize(settings->previewSize());
153 settings->setPreviewSize(previewSize);
154 } else {
155 newIconSize = increasedIconSize(oldIconSize);
156 settings->setIconSize(newIconSize);
157 if (settings->previewSize() < newIconSize) {
158 // assure that the preview size is always >= the icon size
159 settings->setPreviewSize(newIconSize);
160 }
161 }
162
163 // increase also the grid size
164 const int diff = newIconSize - oldIconSize;
165 settings->setGridWidth(settings->gridWidth() + diff);
166 settings->setGridHeight(settings->gridHeight() + diff);
167
168 updateGridSize(showPreview);
169 }
170 }
171
172 void DolphinIconsView::zoomOut()
173 {
174 if (isZoomOutPossible()) {
175 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
176
177 const int oldIconSize = settings->iconSize();
178 int newIconSize = oldIconSize;
179
180 const bool showPreview = m_controller->showPreview();
181 if (showPreview) {
182 const int previewSize = decreasedIconSize(settings->previewSize());
183 settings->setPreviewSize(previewSize);
184 if (settings->iconSize() > previewSize) {
185 // assure that the icon size is always <= the preview size
186 newIconSize = previewSize;
187 settings->setIconSize(newIconSize);
188 }
189 } else {
190 newIconSize = decreasedIconSize(settings->iconSize());
191 settings->setIconSize(newIconSize);
192 }
193
194 // decrease also the grid size
195 const int diff = oldIconSize - newIconSize;
196 settings->setGridWidth(settings->gridWidth() - diff);
197 settings->setGridHeight(settings->gridHeight() - diff);
198
199 updateGridSize(showPreview);
200 }
201 }
202
203 bool DolphinIconsView::isZoomInPossible() const
204 {
205 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
206 const int size = m_controller->showPreview() ? settings->previewSize() : settings->iconSize();
207 return size < K3Icon::SizeEnormous;
208 }
209
210 bool DolphinIconsView::isZoomOutPossible() const
211 {
212 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
213 const int size = m_controller->showPreview() ? settings->previewSize() : settings->iconSize();
214 return size > K3Icon::SizeSmall;
215 }
216
217 int DolphinIconsView::increasedIconSize(int size) const
218 {
219 // TODO: get rid of K3Icon sizes
220 int incSize = 0;
221 switch (size) {
222 case K3Icon::SizeSmall: incSize = K3Icon::SizeSmallMedium; break;
223 case K3Icon::SizeSmallMedium: incSize = K3Icon::SizeMedium; break;
224 case K3Icon::SizeMedium: incSize = K3Icon::SizeLarge; break;
225 case K3Icon::SizeLarge: incSize = K3Icon::SizeHuge; break;
226 case K3Icon::SizeHuge: incSize = K3Icon::SizeEnormous; break;
227 default: Q_ASSERT(false); break;
228 }
229 return incSize;
230 }
231
232 int DolphinIconsView::decreasedIconSize(int size) const
233 {
234 // TODO: get rid of K3Icon sizes
235 int decSize = 0;
236 switch (size) {
237 case K3Icon::SizeSmallMedium: decSize = K3Icon::SizeSmall; break;
238 case K3Icon::SizeMedium: decSize = K3Icon::SizeSmallMedium; break;
239 case K3Icon::SizeLarge: decSize = K3Icon::SizeMedium; break;
240 case K3Icon::SizeHuge: decSize = K3Icon::SizeLarge; break;
241 case K3Icon::SizeEnormous: decSize = K3Icon::SizeHuge; break;
242 default: Q_ASSERT(false); break;
243 }
244 return decSize;
245 }
246
247 #include "dolphiniconsview.moc"