]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
As requested by Peter: upgrade version to 1.0
[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 "draganddrophelper.h"
27
28 #include <kcategorizedsortfilterproxymodel.h>
29 #include <kdialog.h>
30 #include <kdirmodel.h>
31
32 #include <QAbstractProxyModel>
33 #include <QApplication>
34 #include <QPainter>
35 #include <QPoint>
36
37 DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controller) :
38 KCategorizedView(parent),
39 m_controller(controller),
40 m_categoryDrawer(0),
41 m_font(),
42 m_decorationSize(),
43 m_decorationPosition(QStyleOptionViewItem::Top),
44 m_displayAlignment(Qt::AlignHCenter),
45 m_itemSize(),
46 m_dragging(false),
47 m_dropRect()
48 {
49 Q_ASSERT(controller != 0);
50 setViewMode(QListView::IconMode);
51 setResizeMode(QListView::Adjust);
52 setSpacing(KDialog::spacingHint());
53 setMovement(QListView::Static);
54 setDragEnabled(true);
55 viewport()->setAcceptDrops(true);
56
57 setMouseTracking(true);
58 viewport()->setAttribute(Qt::WA_Hover);
59
60 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
61 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
62 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
63 // RETURN-key in keyPressEvent().
64 if (KGlobalSettings::singleClick()) {
65 connect(this, SIGNAL(clicked(const QModelIndex&)),
66 this, SLOT(triggerItem(const QModelIndex&)));
67 } else {
68 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
69 this, SLOT(triggerItem(const QModelIndex&)));
70 }
71 connect(this, SIGNAL(viewportEntered()),
72 controller, SLOT(emitViewportEntered()));
73 connect(controller, SIGNAL(zoomIn()),
74 this, SLOT(zoomIn()));
75 connect(controller, SIGNAL(zoomOut()),
76 this, SLOT(zoomOut()));
77
78 const DolphinView* view = controller->dolphinView();
79 connect(view, SIGNAL(showPreviewChanged()),
80 this, SLOT(slotShowPreviewChanged()));
81 connect(view, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList&)),
82 this, SLOT(slotAdditionalInfoChanged(const KFileItemDelegate::InformationList&)));
83
84 connect(this, SIGNAL(entered(const QModelIndex&)),
85 this, SLOT(slotEntered(const QModelIndex&)));
86
87 // apply the icons mode settings to the widget
88 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
89 Q_ASSERT(settings != 0);
90
91 m_font = QFont(settings->fontFamily(), settings->fontSize());
92 m_font.setItalic(settings->italicFont());
93 m_font.setBold(settings->boldFont());
94
95 setWordWrap(settings->numberOfTextlines() > 1);
96 updateGridSize(view->showPreview(), 0);
97
98 if (settings->arrangement() == QListView::TopToBottom) {
99 setFlow(QListView::LeftToRight);
100 m_decorationPosition = QStyleOptionViewItem::Top;
101 m_displayAlignment = Qt::AlignHCenter;
102 } else {
103 setFlow(QListView::TopToBottom);
104 m_decorationPosition = QStyleOptionViewItem::Left;
105 m_displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
106 }
107
108 m_categoryDrawer = new DolphinCategoryDrawer();
109 setCategoryDrawer(m_categoryDrawer);
110
111 setFocus();
112 }
113
114 DolphinIconsView::~DolphinIconsView()
115 {
116 delete m_categoryDrawer;
117 m_categoryDrawer = 0;
118 }
119
120 QRect DolphinIconsView::visualRect(const QModelIndex& index) const
121 {
122 const bool leftToRightFlow = (flow() == QListView::LeftToRight);
123
124 QRect itemRect = KCategorizedView::visualRect(index);
125
126 const int maxWidth = m_itemSize.width();
127 const int maxHeight = m_itemSize.height();
128
129 if (itemRect.width() > maxWidth) {
130 // assure that the maximum item width is not exceeded
131 if (leftToRightFlow) {
132 const int left = itemRect.left() + (itemRect.width() - maxWidth) / 2;
133 itemRect.setLeft(left);
134 }
135 itemRect.setWidth(maxWidth);
136 }
137
138 if (itemRect.height() > maxHeight) {
139 // assure that the maximum item height is not exceeded
140 if (!leftToRightFlow) {
141 const int top = itemRect.top() + (itemRect.height() - maxHeight) / 2;
142 itemRect.setTop(top);
143 }
144 itemRect.setHeight(maxHeight);
145 }
146
147 KCategorizedSortFilterProxyModel* proxyModel = dynamic_cast<KCategorizedSortFilterProxyModel*>(model());
148 if (leftToRightFlow && !proxyModel->isCategorizedModel()) {
149 // TODO: QListView::visualRect() calculates a wrong position of the items under
150 // certain circumstances (e. g. if the text is too long). This issue is bypassed
151 // by the following code (I'll try create a patch for Qt but as Dolphin must also work with
152 // Qt 4.3.0 this workaround must get applied at least for KDE 4.0).
153 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
154 const int margin = settings->gridSpacing();
155 const int gridWidth = gridSize().width();
156 const int gridIndex = (itemRect.left() - margin + 1) / gridWidth;
157 const int centerInc = (maxWidth - itemRect.width()) / 2;
158 itemRect.moveLeft((gridIndex * gridWidth) + margin + centerInc);
159 }
160
161 return itemRect;
162 }
163
164 QStyleOptionViewItem DolphinIconsView::viewOptions() const
165 {
166 QStyleOptionViewItem viewOptions = KCategorizedView::viewOptions();
167 viewOptions.font = m_font;
168 viewOptions.decorationPosition = m_decorationPosition;
169 viewOptions.decorationSize = m_decorationSize;
170 viewOptions.displayAlignment = m_displayAlignment;
171 viewOptions.showDecorationSelected = true;
172 return viewOptions;
173 }
174
175 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
176 {
177 KCategorizedView::contextMenuEvent(event);
178 m_controller->triggerContextMenuRequest(event->pos());
179 }
180
181 void DolphinIconsView::mousePressEvent(QMouseEvent* event)
182 {
183 m_controller->requestActivation();
184 if (!indexAt(event->pos()).isValid()) {
185 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
186 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
187 clearSelection();
188 }
189 }
190
191 KCategorizedView::mousePressEvent(event);
192 }
193
194 void DolphinIconsView::startDrag(Qt::DropActions supportedActions)
195 {
196 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
197 // fix this in KDE 4.1
198 KCategorizedView::startDrag(supportedActions);
199 DragAndDropHelper::startDrag(this, supportedActions);
200 }
201
202 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
203 {
204 if (event->mimeData()->hasUrls()) {
205 event->acceptProposedAction();
206 }
207 m_dragging = true;
208 }
209
210 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent* event)
211 {
212 KCategorizedView::dragLeaveEvent(event);
213
214 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
215 m_dragging = false;
216 setDirtyRegion(m_dropRect);
217 }
218
219 void DolphinIconsView::dragMoveEvent(QDragMoveEvent* event)
220 {
221 KCategorizedView::dragMoveEvent(event);
222
223 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
224 const QModelIndex index = indexAt(event->pos());
225 setDirtyRegion(m_dropRect);
226
227 m_dropRect.setSize(QSize()); // set as invalid
228 if (index.isValid()) {
229 const KFileItem item = itemForIndex(index);
230 if (!item.isNull() && item.isDir()) {
231 m_dropRect = visualRect(index);
232 } else {
233 m_dropRect.setSize(QSize()); // set as invalid
234 }
235 }
236 if (event->mimeData()->hasUrls()) {
237 // accept url drops, independently from the destination item
238 event->acceptProposedAction();
239 }
240
241 setDirtyRegion(m_dropRect);
242 }
243
244 void DolphinIconsView::dropEvent(QDropEvent* event)
245 {
246 if (!selectionModel()->isSelected(indexAt(event->pos()))) {
247 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
248 if (!urls.isEmpty()) {
249 const QModelIndex index = indexAt(event->pos());
250 const KFileItem item = itemForIndex(index);
251 m_controller->indicateDroppedUrls(urls,
252 m_controller->url(),
253 item);
254 event->acceptProposedAction();
255 }
256 }
257
258 KCategorizedView::dropEvent(event);
259
260 m_dragging = false;
261 }
262
263 void DolphinIconsView::paintEvent(QPaintEvent* event)
264 {
265 KCategorizedView::paintEvent(event);
266
267 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
268 if (m_dragging) {
269 const QBrush& brush = viewOptions().palette.brush(QPalette::Normal, QPalette::Highlight);
270 DragAndDropHelper::drawHoverIndication(this, m_dropRect, brush);
271 }
272 }
273
274 void DolphinIconsView::keyPressEvent(QKeyEvent* event)
275 {
276 KCategorizedView::keyPressEvent(event);
277
278 const QItemSelectionModel* selModel = selectionModel();
279 const QModelIndex currentIndex = selModel->currentIndex();
280 const bool trigger = currentIndex.isValid()
281 && (event->key() == Qt::Key_Return)
282 && (selModel->selectedIndexes().count() <= 1);
283 if (trigger) {
284 triggerItem(currentIndex);
285 }
286 }
287
288 void DolphinIconsView::triggerItem(const QModelIndex& index)
289 {
290 m_controller->triggerItem(itemForIndex(index));
291 }
292
293 void DolphinIconsView::slotEntered(const QModelIndex& index)
294 {
295 m_controller->emitItemEntered(itemForIndex(index));
296 }
297
298 void DolphinIconsView::slotShowPreviewChanged()
299 {
300 const DolphinView* view = m_controller->dolphinView();
301 updateGridSize(view->showPreview(), additionalInfoCount());
302 }
303
304 void DolphinIconsView::slotAdditionalInfoChanged(const KFileItemDelegate::InformationList& info)
305 {
306 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
307 if (!settings->showAdditionalInfo()) {
308 return;
309 }
310
311 const bool showPreview = m_controller->dolphinView()->showPreview();
312 updateGridSize(showPreview, info.count());
313 }
314
315 void DolphinIconsView::zoomIn()
316 {
317 if (isZoomInPossible()) {
318 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
319
320 const int oldIconSize = settings->iconSize();
321 int newIconSize = oldIconSize;
322
323 const bool showPreview = m_controller->dolphinView()->showPreview();
324 if (showPreview) {
325 const int previewSize = increasedIconSize(settings->previewSize());
326 settings->setPreviewSize(previewSize);
327 } else {
328 newIconSize = increasedIconSize(oldIconSize);
329 settings->setIconSize(newIconSize);
330 if (settings->previewSize() < newIconSize) {
331 // assure that the preview size is always >= the icon size
332 settings->setPreviewSize(newIconSize);
333 }
334 }
335
336 // increase also the grid size
337 const int diff = newIconSize - oldIconSize;
338 settings->setItemWidth(settings->itemWidth() + diff);
339 settings->setItemHeight(settings->itemHeight() + diff);
340
341 updateGridSize(showPreview, additionalInfoCount());
342 }
343 }
344
345 void DolphinIconsView::zoomOut()
346 {
347 if (isZoomOutPossible()) {
348 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
349
350 const int oldIconSize = settings->iconSize();
351 int newIconSize = oldIconSize;
352
353 const bool showPreview = m_controller->dolphinView()->showPreview();
354 if (showPreview) {
355 const int previewSize = decreasedIconSize(settings->previewSize());
356 settings->setPreviewSize(previewSize);
357 if (settings->iconSize() > previewSize) {
358 // assure that the icon size is always <= the preview size
359 newIconSize = previewSize;
360 settings->setIconSize(newIconSize);
361 }
362 } else {
363 newIconSize = decreasedIconSize(settings->iconSize());
364 settings->setIconSize(newIconSize);
365 }
366
367 // decrease also the grid size
368 const int diff = oldIconSize - newIconSize;
369 settings->setItemWidth(settings->itemWidth() - diff);
370 settings->setItemHeight(settings->itemHeight() - diff);
371
372 updateGridSize(showPreview, additionalInfoCount());
373 }
374 }
375
376 bool DolphinIconsView::isZoomInPossible() const
377 {
378 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
379 const bool showPreview = m_controller->dolphinView()->showPreview();
380 const int size = showPreview ? settings->previewSize() : settings->iconSize();
381 return size < KIconLoader::SizeEnormous;
382 }
383
384 bool DolphinIconsView::isZoomOutPossible() const
385 {
386 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
387 const bool showPreview = m_controller->dolphinView()->showPreview();
388 const int size = showPreview ? settings->previewSize() : settings->iconSize();
389 return size > KIconLoader::SizeSmall;
390 }
391
392 int DolphinIconsView::increasedIconSize(int size) const
393 {
394 int incSize = 0;
395 switch (size) {
396 case KIconLoader::SizeSmall: incSize = KIconLoader::SizeSmallMedium; break;
397 case KIconLoader::SizeSmallMedium: incSize = KIconLoader::SizeMedium; break;
398 case KIconLoader::SizeMedium: incSize = KIconLoader::SizeLarge; break;
399 case KIconLoader::SizeLarge: incSize = KIconLoader::SizeHuge; break;
400 case KIconLoader::SizeHuge: incSize = KIconLoader::SizeEnormous; break;
401 default: Q_ASSERT(false); break;
402 }
403 return incSize;
404 }
405
406 int DolphinIconsView::decreasedIconSize(int size) const
407 {
408 int decSize = 0;
409 switch (size) {
410 case KIconLoader::SizeSmallMedium: decSize = KIconLoader::SizeSmall; break;
411 case KIconLoader::SizeMedium: decSize = KIconLoader::SizeSmallMedium; break;
412 case KIconLoader::SizeLarge: decSize = KIconLoader::SizeMedium; break;
413 case KIconLoader::SizeHuge: decSize = KIconLoader::SizeLarge; break;
414 case KIconLoader::SizeEnormous: decSize = KIconLoader::SizeHuge; break;
415 default: Q_ASSERT(false); break;
416 }
417 return decSize;
418 }
419
420 void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
421 {
422 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
423 Q_ASSERT(settings != 0);
424
425 int itemWidth = settings->itemWidth();
426 int itemHeight = settings->itemHeight();
427 int size = settings->iconSize();
428
429 if (showPreview) {
430 const int previewSize = settings->previewSize();
431 const int diff = previewSize - size;
432 Q_ASSERT(diff >= 0);
433 itemWidth += diff;
434 itemHeight += diff;
435
436 size = previewSize;
437 }
438
439 Q_ASSERT(additionalInfoCount >= 0);
440 itemHeight += additionalInfoCount * m_font.pointSize() * 2;
441
442 if (settings->arrangement() == QListView::TopToBottom) {
443 // The decoration width indirectly defines the maximum
444 // width for the text wrapping. To use the maximum item width
445 // for text wrapping, it is used as decoration width.
446 m_decorationSize = QSize(itemWidth, size);
447 } else {
448 m_decorationSize = QSize(size, size);
449 }
450
451 m_itemSize = QSize(itemWidth, itemHeight);
452
453 const int spacing = settings->gridSpacing();
454 setGridSize(QSize(itemWidth + spacing * 2, itemHeight + spacing));
455
456 m_controller->setZoomInPossible(isZoomInPossible());
457 m_controller->setZoomOutPossible(isZoomOutPossible());
458 }
459
460 KFileItem DolphinIconsView::itemForIndex(const QModelIndex& index) const
461 {
462 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(model());
463 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
464 const QModelIndex dirIndex = proxyModel->mapToSource(index);
465 return dirModel->itemForIndex(dirIndex);
466 }
467
468 int DolphinIconsView::additionalInfoCount() const
469 {
470 const DolphinView* view = m_controller->dolphinView();
471 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
472 return settings->showAdditionalInfo() ? view->additionalInfo().count() : 0;
473 }
474
475 #include "dolphiniconsview.moc"