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