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