]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
Revert last commit; the issue seems to be reproducible only in combination with ...
[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 #include "dolphin_iconsmodesettings.h"
26 #include "dolphin_generalsettings.h"
27 #include "draganddrophelper.h"
28 #include "selectionmanager.h"
29
30 #include <kcategorizedsortfilterproxymodel.h>
31 #include <kdialog.h>
32 #include <kdirmodel.h>
33 #include <kfileitemdelegate.h>
34
35 #include <QAbstractProxyModel>
36 #include <QApplication>
37 #include <QPainter>
38 #include <QPoint>
39 #include <QScrollBar>
40
41 DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controller) :
42 KCategorizedView(parent),
43 m_controller(controller),
44 m_categoryDrawer(0),
45 m_font(),
46 m_decorationSize(),
47 m_decorationPosition(QStyleOptionViewItem::Top),
48 m_displayAlignment(Qt::AlignHCenter),
49 m_itemSize(),
50 m_dropRect()
51 {
52 Q_ASSERT(controller != 0);
53 setViewMode(QListView::IconMode);
54 setResizeMode(QListView::Adjust);
55 setSpacing(KDialog::spacingHint());
56 setMovement(QListView::Static);
57 setDragEnabled(true);
58 viewport()->setAcceptDrops(true);
59
60 setMouseTracking(true);
61
62 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
63 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
64 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
65 // RETURN-key in keyPressEvent().
66 if (KGlobalSettings::singleClick()) {
67 connect(this, SIGNAL(clicked(const QModelIndex&)),
68 controller, SLOT(triggerItem(const QModelIndex&)));
69 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
70 SelectionManager* selManager = new SelectionManager(this);
71 connect(selManager, SIGNAL(selectionChanged()),
72 this, SLOT(requestActivation()));
73 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
74 selManager, SLOT(reset()));
75 }
76 } else {
77 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
78 controller, SLOT(triggerItem(const QModelIndex&)));
79 }
80 connect(this, SIGNAL(entered(const QModelIndex&)),
81 controller, SLOT(emitItemEntered(const QModelIndex&)));
82 connect(this, SIGNAL(viewportEntered()),
83 controller, SLOT(emitViewportEntered()));
84 connect(controller, SIGNAL(zoomIn()),
85 this, SLOT(zoomIn()));
86 connect(controller, SIGNAL(zoomOut()),
87 this, SLOT(zoomOut()));
88
89 const DolphinView* view = controller->dolphinView();
90 connect(view, SIGNAL(showPreviewChanged()),
91 this, SLOT(slotShowPreviewChanged()));
92 connect(view, SIGNAL(additionalInfoChanged()),
93 this, SLOT(slotAdditionalInfoChanged()));
94
95 // apply the icons mode settings to the widget
96 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
97 Q_ASSERT(settings != 0);
98
99 if (settings->useSystemFont()) {
100 m_font = KGlobalSettings::generalFont();
101 } else {
102 m_font = QFont(settings->fontFamily(),
103 settings->fontSize(),
104 settings->fontWeight(),
105 settings->italicFont());
106 }
107
108 setWordWrap(settings->numberOfTextlines() > 1);
109 updateGridSize(view->showPreview(), 0);
110
111 if (settings->arrangement() == QListView::TopToBottom) {
112 setFlow(QListView::LeftToRight);
113 m_decorationPosition = QStyleOptionViewItem::Top;
114 m_displayAlignment = Qt::AlignHCenter;
115 } else {
116 setFlow(QListView::TopToBottom);
117 m_decorationPosition = QStyleOptionViewItem::Left;
118 m_displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
119 }
120
121 m_categoryDrawer = new DolphinCategoryDrawer();
122 setCategoryDrawer(m_categoryDrawer);
123
124 setFocus();
125
126 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
127 this, SLOT(updateFont()));
128 }
129
130 DolphinIconsView::~DolphinIconsView()
131 {
132 delete m_categoryDrawer;
133 m_categoryDrawer = 0;
134 }
135
136 QStyleOptionViewItem DolphinIconsView::viewOptions() const
137 {
138 QStyleOptionViewItem viewOptions = KCategorizedView::viewOptions();
139 viewOptions.font = m_font;
140 viewOptions.decorationPosition = m_decorationPosition;
141 viewOptions.decorationSize = m_decorationSize;
142 viewOptions.displayAlignment = m_displayAlignment;
143 viewOptions.showDecorationSelected = true;
144 return viewOptions;
145 }
146
147 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
148 {
149 KCategorizedView::contextMenuEvent(event);
150 m_controller->triggerContextMenuRequest(event->pos());
151 }
152
153 void DolphinIconsView::mousePressEvent(QMouseEvent* event)
154 {
155 m_controller->requestActivation();
156 if (!indexAt(event->pos()).isValid()) {
157 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
158 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
159 clearSelection();
160 }
161 }
162
163 KCategorizedView::mousePressEvent(event);
164 }
165
166 void DolphinIconsView::startDrag(Qt::DropActions supportedActions)
167 {
168 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
169 // fix this in KDE 4.1
170 KCategorizedView::startDrag(supportedActions);
171 DragAndDropHelper::startDrag(this, supportedActions);
172 }
173
174 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
175 {
176 if (event->mimeData()->hasUrls()) {
177 event->acceptProposedAction();
178 }
179 }
180
181 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent* event)
182 {
183 KCategorizedView::dragLeaveEvent(event);
184 setDirtyRegion(m_dropRect);
185 }
186
187 void DolphinIconsView::dragMoveEvent(QDragMoveEvent* event)
188 {
189 KCategorizedView::dragMoveEvent(event);
190
191 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
192 const QModelIndex index = indexAt(event->pos());
193 setDirtyRegion(m_dropRect);
194
195 m_dropRect.setSize(QSize()); // set as invalid
196 if (index.isValid()) {
197 const KFileItem item = m_controller->itemForIndex(index);
198 if (!item.isNull() && item.isDir()) {
199 m_dropRect = visualRect(index);
200 } else {
201 m_dropRect.setSize(QSize()); // set as invalid
202 }
203 }
204 if (event->mimeData()->hasUrls()) {
205 // accept url drops, independently from the destination item
206 event->acceptProposedAction();
207 }
208
209 setDirtyRegion(m_dropRect);
210 }
211
212 void DolphinIconsView::dropEvent(QDropEvent* event)
213 {
214 if (!selectionModel()->isSelected(indexAt(event->pos()))) {
215 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
216 if (!urls.isEmpty()) {
217 const QModelIndex index = indexAt(event->pos());
218 const KFileItem item = m_controller->itemForIndex(index);
219 m_controller->indicateDroppedUrls(urls,
220 m_controller->url(),
221 item);
222 event->acceptProposedAction();
223 }
224 }
225
226 KCategorizedView::dropEvent(event);
227 }
228
229 void DolphinIconsView::keyPressEvent(QKeyEvent* event)
230 {
231 KCategorizedView::keyPressEvent(event);
232 m_controller->handleKeyPressEvent(event);
233 }
234
235 void DolphinIconsView::wheelEvent(QWheelEvent* event)
236 {
237 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
238 if (event->modifiers() & Qt::ControlModifier) {
239 event->ignore();
240 return;
241 }
242 KCategorizedView::wheelEvent(event);
243 // if the icons are aligned left to right, the vertical wheel event should
244 // be applied to the horizontal scrollbar
245 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
246 const bool scrollHorizontal = (event->orientation() == Qt::Vertical) &&
247 (settings->arrangement() == QListView::LeftToRight);
248 if (scrollHorizontal) {
249 QWheelEvent horizEvent(event->pos(),
250 event->delta(),
251 event->buttons(),
252 event->modifiers(),
253 Qt::Horizontal);
254 QApplication::sendEvent(horizontalScrollBar(), &horizEvent);
255 }
256 }
257
258 void DolphinIconsView::showEvent(QShowEvent* event)
259 {
260 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
261 delegate->setMaximumSize(m_itemSize);
262
263 KCategorizedView::showEvent(event);
264 }
265
266 void DolphinIconsView::slotShowPreviewChanged()
267 {
268 const DolphinView* view = m_controller->dolphinView();
269 updateGridSize(view->showPreview(), additionalInfoCount());
270 }
271
272 void DolphinIconsView::slotAdditionalInfoChanged()
273 {
274 const DolphinView* view = m_controller->dolphinView();
275 const bool showPreview = view->showPreview();
276 updateGridSize(showPreview, view->additionalInfo().count());
277 }
278
279 void DolphinIconsView::zoomIn()
280 {
281 if (isZoomInPossible()) {
282 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
283
284 const int oldIconSize = settings->iconSize();
285 int newIconSize = oldIconSize;
286
287 const bool showPreview = m_controller->dolphinView()->showPreview();
288 if (showPreview) {
289 const int previewSize = increasedIconSize(settings->previewSize());
290 settings->setPreviewSize(previewSize);
291 } else {
292 newIconSize = increasedIconSize(oldIconSize);
293 settings->setIconSize(newIconSize);
294 }
295
296 // increase also the grid size
297 const int diff = newIconSize - oldIconSize;
298 settings->setItemWidth(settings->itemWidth() + diff);
299 settings->setItemHeight(settings->itemHeight() + diff);
300
301 updateGridSize(showPreview, additionalInfoCount());
302 }
303 }
304
305 void DolphinIconsView::zoomOut()
306 {
307 if (isZoomOutPossible()) {
308 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
309
310 const int oldIconSize = settings->iconSize();
311 int newIconSize = oldIconSize;
312
313 const bool showPreview = m_controller->dolphinView()->showPreview();
314 if (showPreview) {
315 const int previewSize = decreasedIconSize(settings->previewSize());
316 settings->setPreviewSize(previewSize);
317 } else {
318 newIconSize = decreasedIconSize(settings->iconSize());
319 settings->setIconSize(newIconSize);
320 }
321
322 // decrease also the grid size
323 const int diff = oldIconSize - newIconSize;
324 settings->setItemWidth(settings->itemWidth() - diff);
325 settings->setItemHeight(settings->itemHeight() - diff);
326
327 updateGridSize(showPreview, additionalInfoCount());
328 }
329 }
330
331 void DolphinIconsView::requestActivation()
332 {
333 m_controller->requestActivation();
334 }
335
336 void DolphinIconsView::updateFont()
337 {
338 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
339 Q_ASSERT(settings != 0);
340
341 if (settings->useSystemFont()) {
342 m_font = KGlobalSettings::generalFont();
343 }
344 }
345
346 bool DolphinIconsView::isZoomInPossible() const
347 {
348 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
349 const bool showPreview = m_controller->dolphinView()->showPreview();
350 const int size = showPreview ? settings->previewSize() : settings->iconSize();
351 return size < KIconLoader::SizeEnormous;
352 }
353
354 bool DolphinIconsView::isZoomOutPossible() const
355 {
356 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
357 const bool showPreview = m_controller->dolphinView()->showPreview();
358 const int size = showPreview ? settings->previewSize() : settings->iconSize();
359 return size > KIconLoader::SizeSmall;
360 }
361
362 int DolphinIconsView::increasedIconSize(int size) const
363 {
364 int incSize = 0;
365 switch (size) {
366 case KIconLoader::SizeSmall: incSize = KIconLoader::SizeSmallMedium; break;
367 case KIconLoader::SizeSmallMedium: incSize = KIconLoader::SizeMedium; break;
368 case KIconLoader::SizeMedium: incSize = KIconLoader::SizeLarge; break;
369 case KIconLoader::SizeLarge: incSize = KIconLoader::SizeHuge; break;
370 case KIconLoader::SizeHuge: incSize = KIconLoader::SizeEnormous; break;
371 default: Q_ASSERT(false); break;
372 }
373 return incSize;
374 }
375
376 int DolphinIconsView::decreasedIconSize(int size) const
377 {
378 int decSize = 0;
379 switch (size) {
380 case KIconLoader::SizeSmallMedium: decSize = KIconLoader::SizeSmall; break;
381 case KIconLoader::SizeMedium: decSize = KIconLoader::SizeSmallMedium; break;
382 case KIconLoader::SizeLarge: decSize = KIconLoader::SizeMedium; break;
383 case KIconLoader::SizeHuge: decSize = KIconLoader::SizeLarge; break;
384 case KIconLoader::SizeEnormous: decSize = KIconLoader::SizeHuge; break;
385 default: Q_ASSERT(false); break;
386 }
387 return decSize;
388 }
389
390 void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
391 {
392 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
393 Q_ASSERT(settings != 0);
394
395 int itemWidth = settings->itemWidth();
396 int itemHeight = settings->itemHeight();
397 int size = settings->iconSize();
398
399 if (showPreview) {
400 const int previewSize = settings->previewSize();
401 const int diff = previewSize - size;
402 itemWidth += diff;
403 itemHeight += diff;
404
405 size = previewSize;
406 }
407
408 Q_ASSERT(additionalInfoCount >= 0);
409 itemHeight += additionalInfoCount * m_font.pointSize() * 2;
410
411 if (settings->arrangement() == QListView::TopToBottom) {
412 // The decoration width indirectly defines the maximum
413 // width for the text wrapping. To use the maximum item width
414 // for text wrapping, it is used as decoration width.
415 m_decorationSize = QSize(itemWidth, size);
416 setIconSize(QSize(itemWidth, size));
417 } else {
418 m_decorationSize = QSize(size, size);
419 setIconSize(QSize(size, size));
420 }
421
422 m_itemSize = QSize(itemWidth, itemHeight);
423
424 const int spacing = settings->gridSpacing();
425 setGridSize(QSize(itemWidth + spacing * 2, itemHeight + spacing));
426
427 m_controller->setZoomInPossible(isZoomInPossible());
428 m_controller->setZoomOutPossible(isZoomOutPossible());
429
430 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
431 if (delegate != 0) {
432 delegate->setMaximumSize(m_itemSize);
433 }
434 }
435
436 int DolphinIconsView::additionalInfoCount() const
437 {
438 const DolphinView* view = m_controller->dolphinView();
439 return view->additionalInfo().count();
440 }
441
442 #include "dolphiniconsview.moc"