]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
dd83de37ecb383fb9d1c552c21511527a70f86af
[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 void DolphinIconsView::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
137 {
138 KCategorizedView::dataChanged(topLeft, bottomRight);
139
140 KCategorizedSortFilterProxyModel* proxyModel = dynamic_cast<KCategorizedSortFilterProxyModel*>(model());
141 if ((flow() == QListView::LeftToRight) && !proxyModel->isCategorizedModel()) {
142 // bypass a QListView issue that items are not layout correctly if the decoration size of
143 // an index changes
144 scheduleDelayedItemsLayout();
145 }
146 }
147
148 QStyleOptionViewItem DolphinIconsView::viewOptions() const
149 {
150 QStyleOptionViewItem viewOptions = KCategorizedView::viewOptions();
151 viewOptions.font = m_font;
152 viewOptions.decorationPosition = m_decorationPosition;
153 viewOptions.decorationSize = m_decorationSize;
154 viewOptions.displayAlignment = m_displayAlignment;
155 viewOptions.showDecorationSelected = true;
156 return viewOptions;
157 }
158
159 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
160 {
161 KCategorizedView::contextMenuEvent(event);
162 m_controller->triggerContextMenuRequest(event->pos());
163 }
164
165 void DolphinIconsView::mousePressEvent(QMouseEvent* event)
166 {
167 m_controller->requestActivation();
168 if (!indexAt(event->pos()).isValid()) {
169 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
170 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
171 clearSelection();
172 }
173 }
174
175 KCategorizedView::mousePressEvent(event);
176 }
177
178 void DolphinIconsView::startDrag(Qt::DropActions supportedActions)
179 {
180 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
181 // fix this in KDE 4.1
182 KCategorizedView::startDrag(supportedActions);
183 DragAndDropHelper::startDrag(this, supportedActions);
184 }
185
186 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
187 {
188 if (event->mimeData()->hasUrls()) {
189 event->acceptProposedAction();
190 }
191 }
192
193 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent* event)
194 {
195 KCategorizedView::dragLeaveEvent(event);
196 setDirtyRegion(m_dropRect);
197 }
198
199 void DolphinIconsView::dragMoveEvent(QDragMoveEvent* event)
200 {
201 KCategorizedView::dragMoveEvent(event);
202
203 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
204 const QModelIndex index = indexAt(event->pos());
205 setDirtyRegion(m_dropRect);
206
207 m_dropRect.setSize(QSize()); // set as invalid
208 if (index.isValid()) {
209 const KFileItem item = m_controller->itemForIndex(index);
210 if (!item.isNull() && item.isDir()) {
211 m_dropRect = visualRect(index);
212 } else {
213 m_dropRect.setSize(QSize()); // set as invalid
214 }
215 }
216 if (event->mimeData()->hasUrls()) {
217 // accept url drops, independently from the destination item
218 event->acceptProposedAction();
219 }
220
221 setDirtyRegion(m_dropRect);
222 }
223
224 void DolphinIconsView::dropEvent(QDropEvent* event)
225 {
226 if (!selectionModel()->isSelected(indexAt(event->pos()))) {
227 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
228 if (!urls.isEmpty()) {
229 const QModelIndex index = indexAt(event->pos());
230 const KFileItem item = m_controller->itemForIndex(index);
231 m_controller->indicateDroppedUrls(urls,
232 m_controller->url(),
233 item);
234 event->acceptProposedAction();
235 }
236 }
237
238 KCategorizedView::dropEvent(event);
239 }
240
241 void DolphinIconsView::keyPressEvent(QKeyEvent* event)
242 {
243 KCategorizedView::keyPressEvent(event);
244 m_controller->handleKeyPressEvent(event);
245 }
246
247 void DolphinIconsView::wheelEvent(QWheelEvent* event)
248 {
249 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
250 if (event->modifiers() & Qt::ControlModifier) {
251 event->ignore();
252 return;
253 }
254 KCategorizedView::wheelEvent(event);
255 // if the icons are aligned left to right, the vertical wheel event should
256 // be applied to the horizontal scrollbar
257 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
258 const bool scrollHorizontal = (event->orientation() == Qt::Vertical) &&
259 (settings->arrangement() == QListView::LeftToRight);
260 if (scrollHorizontal) {
261 QWheelEvent horizEvent(event->pos(),
262 event->delta(),
263 event->buttons(),
264 event->modifiers(),
265 Qt::Horizontal);
266 QApplication::sendEvent(horizontalScrollBar(), &horizEvent);
267 }
268 }
269
270 void DolphinIconsView::showEvent(QShowEvent* event)
271 {
272 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
273 delegate->setMaximumSize(m_itemSize);
274
275 KCategorizedView::showEvent(event);
276 }
277
278 void DolphinIconsView::slotShowPreviewChanged()
279 {
280 const DolphinView* view = m_controller->dolphinView();
281 updateGridSize(view->showPreview(), additionalInfoCount());
282 }
283
284 void DolphinIconsView::slotAdditionalInfoChanged()
285 {
286 const DolphinView* view = m_controller->dolphinView();
287 const bool showPreview = view->showPreview();
288 updateGridSize(showPreview, view->additionalInfo().count());
289 }
290
291 void DolphinIconsView::zoomIn()
292 {
293 if (isZoomInPossible()) {
294 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
295
296 const int oldIconSize = settings->iconSize();
297 int newIconSize = oldIconSize;
298
299 const bool showPreview = m_controller->dolphinView()->showPreview();
300 if (showPreview) {
301 const int previewSize = increasedIconSize(settings->previewSize());
302 settings->setPreviewSize(previewSize);
303 } else {
304 newIconSize = increasedIconSize(oldIconSize);
305 settings->setIconSize(newIconSize);
306 }
307
308 // increase also the grid size
309 const int diff = newIconSize - oldIconSize;
310 settings->setItemWidth(settings->itemWidth() + diff);
311 settings->setItemHeight(settings->itemHeight() + diff);
312
313 updateGridSize(showPreview, additionalInfoCount());
314 }
315 }
316
317 void DolphinIconsView::zoomOut()
318 {
319 if (isZoomOutPossible()) {
320 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
321
322 const int oldIconSize = settings->iconSize();
323 int newIconSize = oldIconSize;
324
325 const bool showPreview = m_controller->dolphinView()->showPreview();
326 if (showPreview) {
327 const int previewSize = decreasedIconSize(settings->previewSize());
328 settings->setPreviewSize(previewSize);
329 } else {
330 newIconSize = decreasedIconSize(settings->iconSize());
331 settings->setIconSize(newIconSize);
332 }
333
334 // decrease also the grid size
335 const int diff = oldIconSize - newIconSize;
336 settings->setItemWidth(settings->itemWidth() - diff);
337 settings->setItemHeight(settings->itemHeight() - diff);
338
339 updateGridSize(showPreview, additionalInfoCount());
340 }
341 }
342
343 void DolphinIconsView::requestActivation()
344 {
345 m_controller->requestActivation();
346 }
347
348 void DolphinIconsView::updateFont()
349 {
350 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
351 Q_ASSERT(settings != 0);
352
353 if (settings->useSystemFont()) {
354 m_font = KGlobalSettings::generalFont();
355 }
356 }
357
358 bool DolphinIconsView::isZoomInPossible() const
359 {
360 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
361 const bool showPreview = m_controller->dolphinView()->showPreview();
362 const int size = showPreview ? settings->previewSize() : settings->iconSize();
363 return size < KIconLoader::SizeEnormous;
364 }
365
366 bool DolphinIconsView::isZoomOutPossible() const
367 {
368 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
369 const bool showPreview = m_controller->dolphinView()->showPreview();
370 const int size = showPreview ? settings->previewSize() : settings->iconSize();
371 return size > KIconLoader::SizeSmall;
372 }
373
374 int DolphinIconsView::increasedIconSize(int size) const
375 {
376 int incSize = 0;
377 switch (size) {
378 case KIconLoader::SizeSmall: incSize = KIconLoader::SizeSmallMedium; break;
379 case KIconLoader::SizeSmallMedium: incSize = KIconLoader::SizeMedium; break;
380 case KIconLoader::SizeMedium: incSize = KIconLoader::SizeLarge; break;
381 case KIconLoader::SizeLarge: incSize = KIconLoader::SizeHuge; break;
382 case KIconLoader::SizeHuge: incSize = KIconLoader::SizeEnormous; break;
383 default: Q_ASSERT(false); break;
384 }
385 return incSize;
386 }
387
388 int DolphinIconsView::decreasedIconSize(int size) const
389 {
390 int decSize = 0;
391 switch (size) {
392 case KIconLoader::SizeSmallMedium: decSize = KIconLoader::SizeSmall; break;
393 case KIconLoader::SizeMedium: decSize = KIconLoader::SizeSmallMedium; break;
394 case KIconLoader::SizeLarge: decSize = KIconLoader::SizeMedium; break;
395 case KIconLoader::SizeHuge: decSize = KIconLoader::SizeLarge; break;
396 case KIconLoader::SizeEnormous: decSize = KIconLoader::SizeHuge; break;
397 default: Q_ASSERT(false); break;
398 }
399 return decSize;
400 }
401
402 void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
403 {
404 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
405 Q_ASSERT(settings != 0);
406
407 int itemWidth = settings->itemWidth();
408 int itemHeight = settings->itemHeight();
409 int size = settings->iconSize();
410
411 if (showPreview) {
412 const int previewSize = settings->previewSize();
413 const int diff = previewSize - size;
414 itemWidth += diff;
415 itemHeight += diff;
416
417 size = previewSize;
418 }
419
420 Q_ASSERT(additionalInfoCount >= 0);
421 itemHeight += additionalInfoCount * m_font.pointSize() * 2;
422
423 if (settings->arrangement() == QListView::TopToBottom) {
424 // The decoration width indirectly defines the maximum
425 // width for the text wrapping. To use the maximum item width
426 // for text wrapping, it is used as decoration width.
427 m_decorationSize = QSize(itemWidth, size);
428 setIconSize(QSize(itemWidth, size));
429 } else {
430 m_decorationSize = QSize(size, size);
431 setIconSize(QSize(size, size));
432 }
433
434 m_itemSize = QSize(itemWidth, itemHeight);
435
436 const int spacing = settings->gridSpacing();
437 setGridSize(QSize(itemWidth + spacing * 2, itemHeight + spacing));
438
439 m_controller->setZoomInPossible(isZoomInPossible());
440 m_controller->setZoomOutPossible(isZoomOutPossible());
441
442 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
443 if (delegate != 0) {
444 delegate->setMaximumSize(m_itemSize);
445 }
446 }
447
448 int DolphinIconsView::additionalInfoCount() const
449 {
450 const DolphinView* view = m_controller->dolphinView();
451 return view->additionalInfo().count();
452 }
453
454 #include "dolphiniconsview.moc"