]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
SVN_SILENT made messages (.desktop file)
[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 #include "dolphinitemcategorizer.h"
25
26 #include "dolphin_iconsmodesettings.h"
27
28 #include <QAbstractProxyModel>
29 #include <QApplication>
30 #include <QPainter>
31 #include <QPoint>
32
33 DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controller) :
34 KListView(parent),
35 m_controller(controller),
36 m_dragging(false)
37 {
38 Q_ASSERT(controller != 0);
39 setViewMode(QListView::IconMode);
40 setResizeMode(QListView::Adjust);
41
42 setMouseTracking(true);
43 viewport()->setAttribute(Qt::WA_Hover);
44
45 if (KGlobalSettings::singleClick()) {
46 connect(this, SIGNAL(clicked(const QModelIndex&)),
47 controller, SLOT(triggerItem(const QModelIndex&)));
48 } else {
49 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
50 controller, SLOT(triggerItem(const QModelIndex&)));
51 }
52 connect(this, SIGNAL(activated(const QModelIndex&)),
53 controller, SLOT(triggerItem(const QModelIndex&)));
54 connect(this, SIGNAL(entered(const QModelIndex&)),
55 controller, SLOT(emitItemEntered(const QModelIndex&)));
56 connect(this, SIGNAL(viewportEntered()),
57 controller, SLOT(emitViewportEntered()));
58 connect(controller, SIGNAL(showPreviewChanged(bool)),
59 this, SLOT(slotShowPreviewChanged(bool)));
60 connect(controller, SIGNAL(showAdditionalInfoChanged(bool)),
61 this, SLOT(slotShowAdditionalInfoChanged(bool)));
62 connect(controller, SIGNAL(zoomIn()),
63 this, SLOT(zoomIn()));
64 connect(controller, SIGNAL(zoomOut()),
65 this, SLOT(zoomOut()));
66
67 // apply the icons mode settings to the widget
68 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
69 Q_ASSERT(settings != 0);
70
71 m_viewOptions = KListView::viewOptions();
72 m_viewOptions.showDecorationSelected = true;
73
74 QFont font(settings->fontFamily(), settings->fontSize());
75 font.setItalic(settings->italicFont());
76 font.setBold(settings->boldFont());
77 m_viewOptions.font = font;
78
79 setWordWrap(settings->numberOfTextlines() > 1);
80 updateGridSize(controller->showPreview(), controller->showAdditionalInfo());
81
82 if (settings->arrangement() == QListView::TopToBottom) {
83 setFlow(QListView::LeftToRight);
84 m_viewOptions.decorationPosition = QStyleOptionViewItem::Top;
85 } else {
86 setFlow(QListView::TopToBottom);
87 m_viewOptions.decorationPosition = QStyleOptionViewItem::Left;
88 m_viewOptions.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
89 }
90 }
91
92 DolphinIconsView::~DolphinIconsView()
93 {
94 }
95
96 QStyleOptionViewItem DolphinIconsView::viewOptions() const
97 {
98 return m_viewOptions;
99 }
100
101 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
102 {
103 KListView::contextMenuEvent(event);
104 m_controller->triggerContextMenuRequest(event->pos());
105 }
106
107 void DolphinIconsView::mousePressEvent(QMouseEvent* event)
108 {
109 if (!indexAt(event->pos()).isValid()) {
110 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
111 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
112 clearSelection();
113 }
114 }
115
116 KListView::mousePressEvent(event);
117 }
118
119 void DolphinIconsView::mouseReleaseEvent(QMouseEvent* event)
120 {
121 KListView::mouseReleaseEvent(event);
122 m_controller->triggerActivation();
123 }
124
125 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
126 {
127 if (event->mimeData()->hasUrls()) {
128 event->acceptProposedAction();
129 }
130 m_dragging = true;
131 }
132
133 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent* event)
134 {
135 KListView::dragLeaveEvent(event);
136
137 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
138 m_dragging = false;
139 setDirtyRegion(m_dropRect);
140 }
141
142 void DolphinIconsView::dragMoveEvent(QDragMoveEvent* event)
143 {
144 KListView::dragMoveEvent(event);
145
146 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
147 const QModelIndex index = indexAt(event->pos());
148 setDirtyRegion(m_dropRect);
149 m_dropRect = visualRect(index);
150 setDirtyRegion(m_dropRect);
151 }
152
153 void DolphinIconsView::dropEvent(QDropEvent* event)
154 {
155 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
156 if (!urls.isEmpty()) {
157 m_controller->indicateDroppedUrls(urls,
158 indexAt(event->pos()),
159 event->source());
160 event->acceptProposedAction();
161 }
162 KListView::dropEvent(event);
163 m_dragging = false;
164 }
165
166 void DolphinIconsView::paintEvent(QPaintEvent* event)
167 {
168 KListView::paintEvent(event);
169
170 if (m_dragging) {
171 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
172 QPainter painter(viewport());
173 painter.save();
174 QBrush brush(m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight));
175 QColor color = brush.color();
176 color.setAlpha(64);
177 brush.setColor(color);
178 painter.fillRect(m_dropRect, brush);
179 painter.restore();
180 }
181 }
182
183 void DolphinIconsView::slotShowPreviewChanged(bool showPreview)
184 {
185 updateGridSize(showPreview, m_controller->showAdditionalInfo());
186 }
187
188 void DolphinIconsView::slotShowAdditionalInfoChanged(bool showAdditionalInfo)
189 {
190 updateGridSize(m_controller->showPreview(), showAdditionalInfo);
191 }
192
193 void DolphinIconsView::zoomIn()
194 {
195 if (isZoomInPossible()) {
196 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
197
198 const int oldIconSize = settings->iconSize();
199 int newIconSize = oldIconSize;
200
201 const bool showPreview = m_controller->showPreview();
202 if (showPreview) {
203 const int previewSize = increasedIconSize(settings->previewSize());
204 settings->setPreviewSize(previewSize);
205 } else {
206 newIconSize = increasedIconSize(oldIconSize);
207 settings->setIconSize(newIconSize);
208 if (settings->previewSize() < newIconSize) {
209 // assure that the preview size is always >= the icon size
210 settings->setPreviewSize(newIconSize);
211 }
212 }
213
214 // increase also the grid size
215 const int diff = newIconSize - oldIconSize;
216 settings->setItemWidth(settings->itemWidth() + diff);
217 settings->setItemHeight(settings->itemHeight() + diff);
218
219 updateGridSize(showPreview, m_controller->showAdditionalInfo());
220 }
221 }
222
223 void DolphinIconsView::zoomOut()
224 {
225 if (isZoomOutPossible()) {
226 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
227
228 const int oldIconSize = settings->iconSize();
229 int newIconSize = oldIconSize;
230
231 const bool showPreview = m_controller->showPreview();
232 if (showPreview) {
233 const int previewSize = decreasedIconSize(settings->previewSize());
234 settings->setPreviewSize(previewSize);
235 if (settings->iconSize() > previewSize) {
236 // assure that the icon size is always <= the preview size
237 newIconSize = previewSize;
238 settings->setIconSize(newIconSize);
239 }
240 } else {
241 newIconSize = decreasedIconSize(settings->iconSize());
242 settings->setIconSize(newIconSize);
243 }
244
245 // decrease also the grid size
246 const int diff = oldIconSize - newIconSize;
247 settings->setItemWidth(settings->itemWidth() - diff);
248 settings->setItemHeight(settings->itemHeight() - diff);
249
250 updateGridSize(showPreview, m_controller->showAdditionalInfo());
251 }
252 }
253
254 bool DolphinIconsView::isZoomInPossible() const
255 {
256 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
257 const int size = m_controller->showPreview() ? settings->previewSize() : settings->iconSize();
258 return size < K3Icon::SizeEnormous;
259 }
260
261 bool DolphinIconsView::isZoomOutPossible() const
262 {
263 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
264 const int size = m_controller->showPreview() ? settings->previewSize() : settings->iconSize();
265 return size > K3Icon::SizeSmall;
266 }
267
268 int DolphinIconsView::increasedIconSize(int size) const
269 {
270 // TODO: get rid of K3Icon sizes
271 int incSize = 0;
272 switch (size) {
273 case K3Icon::SizeSmall: incSize = K3Icon::SizeSmallMedium; break;
274 case K3Icon::SizeSmallMedium: incSize = K3Icon::SizeMedium; break;
275 case K3Icon::SizeMedium: incSize = K3Icon::SizeLarge; break;
276 case K3Icon::SizeLarge: incSize = K3Icon::SizeHuge; break;
277 case K3Icon::SizeHuge: incSize = K3Icon::SizeEnormous; break;
278 default: Q_ASSERT(false); break;
279 }
280 return incSize;
281 }
282
283 int DolphinIconsView::decreasedIconSize(int size) const
284 {
285 // TODO: get rid of K3Icon sizes
286 int decSize = 0;
287 switch (size) {
288 case K3Icon::SizeSmallMedium: decSize = K3Icon::SizeSmall; break;
289 case K3Icon::SizeMedium: decSize = K3Icon::SizeSmallMedium; break;
290 case K3Icon::SizeLarge: decSize = K3Icon::SizeMedium; break;
291 case K3Icon::SizeHuge: decSize = K3Icon::SizeLarge; break;
292 case K3Icon::SizeEnormous: decSize = K3Icon::SizeHuge; break;
293 default: Q_ASSERT(false); break;
294 }
295 return decSize;
296 }
297
298 void DolphinIconsView::updateGridSize(bool showPreview, bool showAdditionalInfo)
299 {
300 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
301 Q_ASSERT(settings != 0);
302
303 int itemWidth = settings->itemWidth();
304 int itemHeight = settings->itemHeight();
305 int size = settings->iconSize();
306
307 if (showPreview) {
308 const int previewSize = settings->previewSize();
309 const int diff = previewSize - size;
310 Q_ASSERT(diff >= 0);
311 itemWidth += diff;
312 itemHeight += diff;
313
314 size = previewSize;
315 }
316
317 if (showAdditionalInfo) {
318 itemHeight += m_viewOptions.font.pointSize() * 2;
319 }
320
321 if (settings->arrangement() == QListView::TopToBottom) {
322 // The decoration width indirectly defines the maximum
323 // width for the text wrapping. To use the maximum item width
324 // for text wrapping, it is used as decoration width.
325 m_viewOptions.decorationSize = QSize(itemWidth, size);
326 } else {
327 m_viewOptions.decorationSize = QSize(size, size);
328 }
329
330 const int spacing = settings->gridSpacing();
331 setGridSize(QSize(itemWidth + spacing, itemHeight + spacing));
332
333 m_controller->setZoomInPossible(isZoomInPossible());
334 m_controller->setZoomOutPossible(isZoomOutPossible());
335 }
336
337 #include "dolphiniconsview.moc"