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