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