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