]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
Assure that the item size is not exceeded -> the spacing is also respected for texts...
[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 if (itemCategorizer() == 0) {
101 const bool leftToRightFlow = (flow() == QListView::LeftToRight);
102
103 QRect itemRect = KCategorizedView::visualRect(index);
104 const int maxWidth = m_itemSize.width();
105 const int maxHeight = m_itemSize.height();
106
107 if (itemRect.width() > maxWidth) {
108 // assure that the maximum item width is not exceeded
109 if (leftToRightFlow) {
110 const int left = itemRect.left() + (itemRect.width() - maxWidth) / 2;
111 itemRect.setLeft(left);
112 }
113 itemRect.setWidth(maxWidth);
114 }
115
116 if (itemRect.height() > maxHeight) {
117 // assure that the maximum item height is not exceeded
118 if (!leftToRightFlow) {
119 const int top = itemRect.top() + (itemRect.height() - maxHeight) / 2;
120 itemRect.setTop(top);
121 }
122 itemRect.setHeight(maxHeight);
123 }
124
125 return itemRect;
126 }
127
128 return KCategorizedView::visualRect(index);
129 }
130
131 QStyleOptionViewItem DolphinIconsView::viewOptions() const
132 {
133 return m_viewOptions;
134 }
135
136 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
137 {
138 KCategorizedView::contextMenuEvent(event);
139 m_controller->triggerContextMenuRequest(event->pos());
140 }
141
142 void DolphinIconsView::mousePressEvent(QMouseEvent* event)
143 {
144 m_controller->triggerActivation();
145 if (!indexAt(event->pos()).isValid()) {
146 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
147 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
148 clearSelection();
149 }
150 }
151
152 KCategorizedView::mousePressEvent(event);
153 }
154
155 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
156 {
157 if (event->mimeData()->hasUrls()) {
158 event->acceptProposedAction();
159 }
160 m_dragging = true;
161 }
162
163 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent* event)
164 {
165 KCategorizedView::dragLeaveEvent(event);
166
167 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
168 m_dragging = false;
169 setDirtyRegion(m_dropRect);
170 }
171
172 void DolphinIconsView::dragMoveEvent(QDragMoveEvent* event)
173 {
174 KCategorizedView::dragMoveEvent(event);
175
176 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
177 const QModelIndex index = indexAt(event->pos());
178 setDirtyRegion(m_dropRect);
179 m_dropRect = visualRect(index);
180 setDirtyRegion(m_dropRect);
181 }
182
183 void DolphinIconsView::dropEvent(QDropEvent* event)
184 {
185 if (!selectionModel()->isSelected(indexAt(event->pos()))) {
186 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
187 if (!urls.isEmpty()) {
188 m_controller->indicateDroppedUrls(urls,
189 indexAt(event->pos()),
190 event->source());
191 event->acceptProposedAction();
192 }
193 }
194 KCategorizedView::dropEvent(event);
195 m_dragging = false;
196 }
197
198 void DolphinIconsView::paintEvent(QPaintEvent* event)
199 {
200 KCategorizedView::paintEvent(event);
201
202 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
203 if (m_dragging) {
204 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
205 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
206 }
207 }
208
209 void DolphinIconsView::slotShowPreviewChanged(bool showPreview)
210 {
211 updateGridSize(showPreview, m_controller->showAdditionalInfo());
212 }
213
214 void DolphinIconsView::slotShowAdditionalInfoChanged(bool showAdditionalInfo)
215 {
216 updateGridSize(m_controller->showPreview(), showAdditionalInfo);
217 }
218
219 void DolphinIconsView::zoomIn()
220 {
221 if (isZoomInPossible()) {
222 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
223
224 const int oldIconSize = settings->iconSize();
225 int newIconSize = oldIconSize;
226
227 const bool showPreview = m_controller->showPreview();
228 if (showPreview) {
229 const int previewSize = increasedIconSize(settings->previewSize());
230 settings->setPreviewSize(previewSize);
231 } else {
232 newIconSize = increasedIconSize(oldIconSize);
233 settings->setIconSize(newIconSize);
234 if (settings->previewSize() < newIconSize) {
235 // assure that the preview size is always >= the icon size
236 settings->setPreviewSize(newIconSize);
237 }
238 }
239
240 // increase also the grid size
241 const int diff = newIconSize - oldIconSize;
242 settings->setItemWidth(settings->itemWidth() + diff);
243 settings->setItemHeight(settings->itemHeight() + diff);
244
245 updateGridSize(showPreview, m_controller->showAdditionalInfo());
246 }
247 }
248
249 void DolphinIconsView::zoomOut()
250 {
251 if (isZoomOutPossible()) {
252 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
253
254 const int oldIconSize = settings->iconSize();
255 int newIconSize = oldIconSize;
256
257 const bool showPreview = m_controller->showPreview();
258 if (showPreview) {
259 const int previewSize = decreasedIconSize(settings->previewSize());
260 settings->setPreviewSize(previewSize);
261 if (settings->iconSize() > previewSize) {
262 // assure that the icon size is always <= the preview size
263 newIconSize = previewSize;
264 settings->setIconSize(newIconSize);
265 }
266 } else {
267 newIconSize = decreasedIconSize(settings->iconSize());
268 settings->setIconSize(newIconSize);
269 }
270
271 // decrease also the grid size
272 const int diff = oldIconSize - newIconSize;
273 settings->setItemWidth(settings->itemWidth() - diff);
274 settings->setItemHeight(settings->itemHeight() - diff);
275
276 updateGridSize(showPreview, m_controller->showAdditionalInfo());
277 }
278 }
279
280 bool DolphinIconsView::isZoomInPossible() const
281 {
282 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
283 const int size = m_controller->showPreview() ? settings->previewSize() : settings->iconSize();
284 return size < K3Icon::SizeEnormous;
285 }
286
287 bool DolphinIconsView::isZoomOutPossible() const
288 {
289 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
290 const int size = m_controller->showPreview() ? settings->previewSize() : settings->iconSize();
291 return size > K3Icon::SizeSmall;
292 }
293
294 int DolphinIconsView::increasedIconSize(int size) const
295 {
296 // TODO: get rid of K3Icon sizes
297 int incSize = 0;
298 switch (size) {
299 case K3Icon::SizeSmall: incSize = K3Icon::SizeSmallMedium; break;
300 case K3Icon::SizeSmallMedium: incSize = K3Icon::SizeMedium; break;
301 case K3Icon::SizeMedium: incSize = K3Icon::SizeLarge; break;
302 case K3Icon::SizeLarge: incSize = K3Icon::SizeHuge; break;
303 case K3Icon::SizeHuge: incSize = K3Icon::SizeEnormous; break;
304 default: Q_ASSERT(false); break;
305 }
306 return incSize;
307 }
308
309 int DolphinIconsView::decreasedIconSize(int size) const
310 {
311 // TODO: get rid of K3Icon sizes
312 int decSize = 0;
313 switch (size) {
314 case K3Icon::SizeSmallMedium: decSize = K3Icon::SizeSmall; break;
315 case K3Icon::SizeMedium: decSize = K3Icon::SizeSmallMedium; break;
316 case K3Icon::SizeLarge: decSize = K3Icon::SizeMedium; break;
317 case K3Icon::SizeHuge: decSize = K3Icon::SizeLarge; break;
318 case K3Icon::SizeEnormous: decSize = K3Icon::SizeHuge; break;
319 default: Q_ASSERT(false); break;
320 }
321 return decSize;
322 }
323
324 void DolphinIconsView::updateGridSize(bool showPreview, bool showAdditionalInfo)
325 {
326 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
327 Q_ASSERT(settings != 0);
328
329 int itemWidth = settings->itemWidth();
330 int itemHeight = settings->itemHeight();
331 int size = settings->iconSize();
332
333 if (showPreview) {
334 const int previewSize = settings->previewSize();
335 const int diff = previewSize - size;
336 Q_ASSERT(diff >= 0);
337 itemWidth += diff;
338 itemHeight += diff;
339
340 size = previewSize;
341 }
342
343 if (showAdditionalInfo) {
344 itemHeight += m_viewOptions.font.pointSize() * 2;
345 }
346
347 if (settings->arrangement() == QListView::TopToBottom) {
348 // The decoration width indirectly defines the maximum
349 // width for the text wrapping. To use the maximum item width
350 // for text wrapping, it is used as decoration width.
351 m_viewOptions.decorationSize = QSize(itemWidth, size);
352 } else {
353 m_viewOptions.decorationSize = QSize(size, size);
354 }
355
356 const int spacing = settings->gridSpacing();
357 setGridSize(QSize(itemWidth + spacing, itemHeight + spacing));
358
359 m_itemSize = QSize(itemWidth, itemHeight);
360
361 m_controller->setZoomInPossible(isZoomInPossible());
362 m_controller->setZoomOutPossible(isZoomOutPossible());
363 }
364
365 #include "dolphiniconsview.moc"