]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
DocPath => X-DocPath
[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() / 2;
149 const int gridWidth = gridSize().width();
150 int left = itemRect.left();
151 left = ((left - margin + 1) / gridWidth) * gridWidth + margin;
152 itemRect.moveLeft(left);
153 }
154
155 return itemRect;
156 }
157
158 QStyleOptionViewItem DolphinIconsView::viewOptions() const
159 {
160 return m_viewOptions;
161 }
162
163 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
164 {
165 KCategorizedView::contextMenuEvent(event);
166 m_controller->triggerContextMenuRequest(event->pos());
167 }
168
169 void DolphinIconsView::mousePressEvent(QMouseEvent* event)
170 {
171 m_controller->requestActivation();
172 if (!indexAt(event->pos()).isValid()) {
173 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
174 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
175 clearSelection();
176 }
177 }
178
179 KCategorizedView::mousePressEvent(event);
180 }
181
182 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
183 {
184 if (event->mimeData()->hasUrls()) {
185 event->acceptProposedAction();
186 }
187 m_dragging = true;
188 }
189
190 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent* event)
191 {
192 KCategorizedView::dragLeaveEvent(event);
193
194 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
195 m_dragging = false;
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 if (itemForIndex(index).isDir()) {
207 m_dropRect = visualRect(index);
208 } else {
209 m_dropRect.setSize(QSize()); // set as invalid
210 }
211 setDirtyRegion(m_dropRect);
212 }
213
214 void DolphinIconsView::dropEvent(QDropEvent* event)
215 {
216 if (!selectionModel()->isSelected(indexAt(event->pos()))) {
217 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
218 if (!urls.isEmpty()) {
219 const QModelIndex index = indexAt(event->pos());
220 const KFileItem item = itemForIndex(index);
221 m_controller->indicateDroppedUrls(urls,
222 m_controller->url(),
223 item);
224 event->acceptProposedAction();
225 }
226 }
227
228 KCategorizedView::dropEvent(event);
229 m_dragging = false;
230 }
231
232 void DolphinIconsView::paintEvent(QPaintEvent* event)
233 {
234 KCategorizedView::paintEvent(event);
235
236 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
237 if (m_dragging) {
238 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
239 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
240 }
241 }
242
243 void DolphinIconsView::keyPressEvent(QKeyEvent* event)
244 {
245 KCategorizedView::keyPressEvent(event);
246
247 const QItemSelectionModel* selModel = selectionModel();
248 const QModelIndex currentIndex = selModel->currentIndex();
249 const bool trigger = currentIndex.isValid()
250 && (event->key() == Qt::Key_Return)
251 && (selModel->selectedIndexes().count() <= 1);
252 if (trigger) {
253 triggerItem(currentIndex);
254 }
255 }
256
257 void DolphinIconsView::triggerItem(const QModelIndex& index)
258 {
259 m_controller->triggerItem(itemForIndex(index));
260 }
261
262 void DolphinIconsView::slotEntered(const QModelIndex& index)
263 {
264 m_controller->emitItemEntered(itemForIndex(index));
265 }
266
267 void DolphinIconsView::slotShowPreviewChanged()
268 {
269 const DolphinView* view = m_controller->dolphinView();
270 const int infoCount = view->additionalInfo().count();
271 updateGridSize(view->showPreview(), infoCount);
272 }
273
274 void DolphinIconsView::slotAdditionalInfoChanged(const KFileItemDelegate::InformationList& info)
275 {
276 const bool showPreview = m_controller->dolphinView()->showPreview();
277 updateGridSize(showPreview, info.count());
278 }
279
280 void DolphinIconsView::zoomIn()
281 {
282 if (isZoomInPossible()) {
283 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
284
285 const int oldIconSize = settings->iconSize();
286 int newIconSize = oldIconSize;
287
288 const bool showPreview = m_controller->dolphinView()->showPreview();
289 if (showPreview) {
290 const int previewSize = increasedIconSize(settings->previewSize());
291 settings->setPreviewSize(previewSize);
292 } else {
293 newIconSize = increasedIconSize(oldIconSize);
294 settings->setIconSize(newIconSize);
295 if (settings->previewSize() < newIconSize) {
296 // assure that the preview size is always >= the icon size
297 settings->setPreviewSize(newIconSize);
298 }
299 }
300
301 // increase also the grid size
302 const int diff = newIconSize - oldIconSize;
303 settings->setItemWidth(settings->itemWidth() + diff);
304 settings->setItemHeight(settings->itemHeight() + diff);
305
306 const int infoCount = m_controller->dolphinView()->additionalInfo().count();
307 updateGridSize(showPreview, infoCount);
308 }
309 }
310
311 void DolphinIconsView::zoomOut()
312 {
313 if (isZoomOutPossible()) {
314 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
315
316 const int oldIconSize = settings->iconSize();
317 int newIconSize = oldIconSize;
318
319 const bool showPreview = m_controller->dolphinView()->showPreview();
320 if (showPreview) {
321 const int previewSize = decreasedIconSize(settings->previewSize());
322 settings->setPreviewSize(previewSize);
323 if (settings->iconSize() > previewSize) {
324 // assure that the icon size is always <= the preview size
325 newIconSize = previewSize;
326 settings->setIconSize(newIconSize);
327 }
328 } else {
329 newIconSize = decreasedIconSize(settings->iconSize());
330 settings->setIconSize(newIconSize);
331 }
332
333 // decrease also the grid size
334 const int diff = oldIconSize - newIconSize;
335 settings->setItemWidth(settings->itemWidth() - diff);
336 settings->setItemHeight(settings->itemHeight() - diff);
337
338 const int infoCount = m_controller->dolphinView()->additionalInfo().count();
339 updateGridSize(showPreview, infoCount);
340 }
341 }
342
343 bool DolphinIconsView::isZoomInPossible() const
344 {
345 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
346 const bool showPreview = m_controller->dolphinView()->showPreview();
347 const int size = showPreview ? settings->previewSize() : settings->iconSize();
348 return size < KIconLoader::SizeEnormous;
349 }
350
351 bool DolphinIconsView::isZoomOutPossible() const
352 {
353 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
354 const bool showPreview = m_controller->dolphinView()->showPreview();
355 const int size = showPreview ? settings->previewSize() : settings->iconSize();
356 return size > KIconLoader::SizeSmall;
357 }
358
359 int DolphinIconsView::increasedIconSize(int size) const
360 {
361 int incSize = 0;
362 switch (size) {
363 case KIconLoader::SizeSmall: incSize = KIconLoader::SizeSmallMedium; break;
364 case KIconLoader::SizeSmallMedium: incSize = KIconLoader::SizeMedium; break;
365 case KIconLoader::SizeMedium: incSize = KIconLoader::SizeLarge; break;
366 case KIconLoader::SizeLarge: incSize = KIconLoader::SizeHuge; break;
367 case KIconLoader::SizeHuge: incSize = KIconLoader::SizeEnormous; break;
368 default: Q_ASSERT(false); break;
369 }
370 return incSize;
371 }
372
373 int DolphinIconsView::decreasedIconSize(int size) const
374 {
375 int decSize = 0;
376 switch (size) {
377 case KIconLoader::SizeSmallMedium: decSize = KIconLoader::SizeSmall; break;
378 case KIconLoader::SizeMedium: decSize = KIconLoader::SizeSmallMedium; break;
379 case KIconLoader::SizeLarge: decSize = KIconLoader::SizeMedium; break;
380 case KIconLoader::SizeHuge: decSize = KIconLoader::SizeLarge; break;
381 case KIconLoader::SizeEnormous: decSize = KIconLoader::SizeHuge; break;
382 default: Q_ASSERT(false); break;
383 }
384 return decSize;
385 }
386
387 void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
388 {
389 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
390 Q_ASSERT(settings != 0);
391
392 int itemWidth = settings->itemWidth();
393 int itemHeight = settings->itemHeight();
394 int size = settings->iconSize();
395
396 if (showPreview) {
397 const int previewSize = settings->previewSize();
398 const int diff = previewSize - size;
399 Q_ASSERT(diff >= 0);
400 itemWidth += diff;
401 itemHeight += diff;
402
403 size = previewSize;
404 }
405
406 Q_ASSERT(additionalInfoCount >= 0);
407 itemHeight += additionalInfoCount * m_viewOptions.font.pointSize() * 2;
408
409 if (settings->arrangement() == QListView::TopToBottom) {
410 // The decoration width indirectly defines the maximum
411 // width for the text wrapping. To use the maximum item width
412 // for text wrapping, it is used as decoration width.
413 m_viewOptions.decorationSize = QSize(itemWidth, size);
414 } else {
415 m_viewOptions.decorationSize = QSize(size, size);
416 }
417
418 const int spacing = settings->gridSpacing();
419 setGridSize(QSize(itemWidth + spacing, itemHeight + spacing));
420
421 m_itemSize = QSize(itemWidth, itemHeight);
422
423 m_controller->setZoomInPossible(isZoomInPossible());
424 m_controller->setZoomOutPossible(isZoomOutPossible());
425 }
426
427 KFileItem DolphinIconsView::itemForIndex(const QModelIndex& index) const
428 {
429 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(model());
430 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
431 const QModelIndex dirIndex = proxyModel->mapToSource(index);
432 return dirModel->itemForIndex(dirIndex);
433 }
434
435
436 #include "dolphiniconsview.moc"