]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
0b9b1e6fa3c768d6e203ff39fb48a76110df7f7b
[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 }
72 else {
73 setFlow(QListView::TopToBottom);
74 m_viewOptions.decorationPosition = QStyleOptionViewItem::Left;
75 }
76 }
77
78 DolphinIconsView::~DolphinIconsView()
79 {
80 }
81
82 QStyleOptionViewItem DolphinIconsView::viewOptions() const
83 {
84 return m_viewOptions;
85 }
86
87 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
88 {
89 QListView::contextMenuEvent(event);
90 m_controller->triggerContextMenuRequest(event->pos());
91 }
92
93 void DolphinIconsView::mouseReleaseEvent(QMouseEvent* event)
94 {
95 QListView::mouseReleaseEvent(event);
96 m_controller->triggerActivation();
97 }
98
99 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
100 {
101 if (event->mimeData()->hasUrls()) {
102 event->acceptProposedAction();
103 }
104 }
105
106 void DolphinIconsView::dropEvent(QDropEvent* event)
107 {
108 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
109 if (!urls.isEmpty()) {
110 m_controller->indicateDroppedUrls(urls,
111 indexAt(event->pos()),
112 event->source());
113 event->acceptProposedAction();
114 }
115 QListView::dropEvent(event);
116 }
117
118 void DolphinIconsView::updateGridSize(bool showPreview)
119 {
120 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
121 Q_ASSERT(settings != 0);
122
123 int gridWidth = settings->gridWidth();
124 int gridHeight = settings->gridHeight();
125 int size = settings->iconSize();
126
127 if (showPreview) {
128 const int previewSize = settings->previewSize();
129 const int diff = previewSize - size;
130 Q_ASSERT(diff >= 0);
131 gridWidth += diff;
132 gridHeight += diff;
133
134 size = previewSize;
135 }
136
137 m_viewOptions.decorationSize = QSize(size, size);
138 setGridSize(QSize(gridWidth, gridHeight));
139
140 m_controller->setZoomInPossible(isZoomInPossible());
141 m_controller->setZoomOutPossible(isZoomOutPossible());
142 }
143
144 void DolphinIconsView::zoomIn()
145 {
146 if (isZoomInPossible()) {
147 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
148
149 const int oldIconSize = settings->iconSize();
150 int newIconSize = oldIconSize;
151
152 const bool showPreview = m_controller->showPreview();
153 if (showPreview) {
154 const int previewSize = increasedIconSize(settings->previewSize());
155 settings->setPreviewSize(previewSize);
156 }
157 else {
158 newIconSize = increasedIconSize(oldIconSize);
159 settings->setIconSize(newIconSize);
160 if (settings->previewSize() < newIconSize) {
161 // assure that the preview size is always >= the icon size
162 settings->setPreviewSize(newIconSize);
163 }
164 }
165
166 // increase also the grid size
167 const int diff = newIconSize - oldIconSize;
168 settings->setGridWidth(settings->gridWidth() + diff);
169 settings->setGridHeight(settings->gridHeight() + diff);
170
171 updateGridSize(showPreview);
172 }
173 }
174
175 void DolphinIconsView::zoomOut()
176 {
177 if (isZoomOutPossible()) {
178 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
179
180 const int oldIconSize = settings->iconSize();
181 int newIconSize = oldIconSize;
182
183 const bool showPreview = m_controller->showPreview();
184 if (showPreview) {
185 const int previewSize = decreasedIconSize(settings->previewSize());
186 settings->setPreviewSize(previewSize);
187 if (settings->iconSize() > previewSize) {
188 // assure that the icon size is always <= the preview size
189 newIconSize = previewSize;
190 settings->setIconSize(newIconSize);
191 }
192 }
193 else {
194 newIconSize = decreasedIconSize(settings->iconSize());
195 settings->setIconSize(newIconSize);
196 }
197
198 // decrease also the grid size
199 const int diff = oldIconSize - newIconSize;
200 settings->setGridWidth(settings->gridWidth() - diff);
201 settings->setGridHeight(settings->gridHeight() - diff);
202
203 updateGridSize(showPreview);
204 }
205 }
206
207 bool DolphinIconsView::isZoomInPossible() const
208 {
209 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
210 const int size = m_controller->showPreview() ? settings->previewSize() : settings->iconSize();
211 return size < K3Icon::SizeEnormous;
212 }
213
214 bool DolphinIconsView::isZoomOutPossible() const
215 {
216 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
217 const int size = m_controller->showPreview() ? settings->previewSize() : settings->iconSize();
218 return size > K3Icon::SizeSmall;
219 }
220
221 int DolphinIconsView::increasedIconSize(int size) const
222 {
223 // TODO: get rid of K3Icon sizes
224 int incSize = 0;
225 switch (size) {
226 case K3Icon::SizeSmall: incSize = K3Icon::SizeSmallMedium; break;
227 case K3Icon::SizeSmallMedium: incSize = K3Icon::SizeMedium; break;
228 case K3Icon::SizeMedium: incSize = K3Icon::SizeLarge; break;
229 case K3Icon::SizeLarge: incSize = K3Icon::SizeHuge; break;
230 case K3Icon::SizeHuge: incSize = K3Icon::SizeEnormous; break;
231 default: Q_ASSERT(false); break;
232 }
233 return incSize;
234 }
235
236 int DolphinIconsView::decreasedIconSize(int size) const
237 {
238 // TODO: get rid of K3Icon sizes
239 int decSize = 0;
240 switch (size) {
241 case K3Icon::SizeSmallMedium: decSize = K3Icon::SizeSmall; break;
242 case K3Icon::SizeMedium: decSize = K3Icon::SizeSmallMedium; break;
243 case K3Icon::SizeLarge: decSize = K3Icon::SizeMedium; break;
244 case K3Icon::SizeHuge: decSize = K3Icon::SizeLarge; break;
245 case K3Icon::SizeEnormous: decSize = K3Icon::SizeHuge; break;
246 default: Q_ASSERT(false); break;
247 }
248 return decSize;
249 }
250
251 #include "dolphiniconsview.moc"