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