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