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