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