]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
Simplify DolphinController: don't remember the show-preview state in the controller...
[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 m_dropRect = visualRect(index);
192 setDirtyRegion(m_dropRect);
193 }
194
195 void DolphinIconsView::dropEvent(QDropEvent* event)
196 {
197 if (!selectionModel()->isSelected(indexAt(event->pos()))) {
198 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
199 if (!urls.isEmpty()) {
200 const QModelIndex index = indexAt(event->pos());
201 if (index.isValid()) {
202 const KFileItem item = itemForIndex(index);
203 m_controller->indicateDroppedUrls(urls,
204 m_controller->url(),
205 item,
206 event->source());
207 event->acceptProposedAction();
208 }
209 }
210 }
211
212 KCategorizedView::dropEvent(event);
213 m_dragging = false;
214 }
215
216 void DolphinIconsView::paintEvent(QPaintEvent* event)
217 {
218 KCategorizedView::paintEvent(event);
219
220 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
221 if (m_dragging) {
222 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
223 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
224 }
225 }
226
227 void DolphinIconsView::keyPressEvent(QKeyEvent* event)
228 {
229 KCategorizedView::keyPressEvent(event);
230
231 const QItemSelectionModel* selModel = selectionModel();
232 const QModelIndex currentIndex = selModel->currentIndex();
233 const bool trigger = currentIndex.isValid()
234 && (event->key() == Qt::Key_Return)
235 && (selModel->selectedIndexes().count() <= 1);
236 if (trigger) {
237 triggerItem(currentIndex);
238 }
239 }
240
241 void DolphinIconsView::triggerItem(const QModelIndex& index)
242 {
243 m_controller->triggerItem(itemForIndex(index));
244 }
245
246 void DolphinIconsView::slotEntered(const QModelIndex& index)
247 {
248 m_controller->emitItemEntered(itemForIndex(index));
249 }
250
251 void DolphinIconsView::slotShowPreviewChanged()
252 {
253 const DolphinView* view = m_controller->dolphinView();
254 const int infoCount = view->additionalInfo().count();
255 updateGridSize(view->showPreview(), infoCount);
256 }
257
258 void DolphinIconsView::slotAdditionalInfoChanged(const KFileItemDelegate::InformationList& info)
259 {
260 const bool showPreview = m_controller->dolphinView()->showPreview();
261 updateGridSize(showPreview, info.count());
262 }
263
264 void DolphinIconsView::zoomIn()
265 {
266 if (isZoomInPossible()) {
267 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
268
269 const int oldIconSize = settings->iconSize();
270 int newIconSize = oldIconSize;
271
272 const bool showPreview = m_controller->dolphinView()->showPreview();
273 if (showPreview) {
274 const int previewSize = increasedIconSize(settings->previewSize());
275 settings->setPreviewSize(previewSize);
276 } else {
277 newIconSize = increasedIconSize(oldIconSize);
278 settings->setIconSize(newIconSize);
279 if (settings->previewSize() < newIconSize) {
280 // assure that the preview size is always >= the icon size
281 settings->setPreviewSize(newIconSize);
282 }
283 }
284
285 // increase also the grid size
286 const int diff = newIconSize - oldIconSize;
287 settings->setItemWidth(settings->itemWidth() + diff);
288 settings->setItemHeight(settings->itemHeight() + diff);
289
290 const int infoCount = m_controller->dolphinView()->additionalInfo().count();
291 updateGridSize(showPreview, infoCount);
292 }
293 }
294
295 void DolphinIconsView::zoomOut()
296 {
297 if (isZoomOutPossible()) {
298 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
299
300 const int oldIconSize = settings->iconSize();
301 int newIconSize = oldIconSize;
302
303 const bool showPreview = m_controller->dolphinView()->showPreview();
304 if (showPreview) {
305 const int previewSize = decreasedIconSize(settings->previewSize());
306 settings->setPreviewSize(previewSize);
307 if (settings->iconSize() > previewSize) {
308 // assure that the icon size is always <= the preview size
309 newIconSize = previewSize;
310 settings->setIconSize(newIconSize);
311 }
312 } else {
313 newIconSize = decreasedIconSize(settings->iconSize());
314 settings->setIconSize(newIconSize);
315 }
316
317 // decrease also the grid size
318 const int diff = oldIconSize - newIconSize;
319 settings->setItemWidth(settings->itemWidth() - diff);
320 settings->setItemHeight(settings->itemHeight() - diff);
321
322 const int infoCount = m_controller->dolphinView()->additionalInfo().count();
323 updateGridSize(showPreview, infoCount);
324 }
325 }
326
327 bool DolphinIconsView::isZoomInPossible() const
328 {
329 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
330 const bool showPreview = m_controller->dolphinView()->showPreview();
331 const int size = showPreview ? settings->previewSize() : settings->iconSize();
332 return size < KIconLoader::SizeEnormous;
333 }
334
335 bool DolphinIconsView::isZoomOutPossible() const
336 {
337 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
338 const bool showPreview = m_controller->dolphinView()->showPreview();
339 const int size = showPreview ? settings->previewSize() : settings->iconSize();
340 return size > KIconLoader::SizeSmall;
341 }
342
343 int DolphinIconsView::increasedIconSize(int size) const
344 {
345 int incSize = 0;
346 switch (size) {
347 case KIconLoader::SizeSmall: incSize = KIconLoader::SizeSmallMedium; break;
348 case KIconLoader::SizeSmallMedium: incSize = KIconLoader::SizeMedium; break;
349 case KIconLoader::SizeMedium: incSize = KIconLoader::SizeLarge; break;
350 case KIconLoader::SizeLarge: incSize = KIconLoader::SizeHuge; break;
351 case KIconLoader::SizeHuge: incSize = KIconLoader::SizeEnormous; break;
352 default: Q_ASSERT(false); break;
353 }
354 return incSize;
355 }
356
357 int DolphinIconsView::decreasedIconSize(int size) const
358 {
359 int decSize = 0;
360 switch (size) {
361 case KIconLoader::SizeSmallMedium: decSize = KIconLoader::SizeSmall; break;
362 case KIconLoader::SizeMedium: decSize = KIconLoader::SizeSmallMedium; break;
363 case KIconLoader::SizeLarge: decSize = KIconLoader::SizeMedium; break;
364 case KIconLoader::SizeHuge: decSize = KIconLoader::SizeLarge; break;
365 case KIconLoader::SizeEnormous: decSize = KIconLoader::SizeHuge; break;
366 default: Q_ASSERT(false); break;
367 }
368 return decSize;
369 }
370
371 void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
372 {
373 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
374 Q_ASSERT(settings != 0);
375
376 int itemWidth = settings->itemWidth();
377 int itemHeight = settings->itemHeight();
378 int size = settings->iconSize();
379
380 if (showPreview) {
381 const int previewSize = settings->previewSize();
382 const int diff = previewSize - size;
383 Q_ASSERT(diff >= 0);
384 itemWidth += diff;
385 itemHeight += diff;
386
387 size = previewSize;
388 }
389
390 Q_ASSERT(additionalInfoCount >= 0);
391 itemHeight += additionalInfoCount * m_viewOptions.font.pointSize() * 2;
392
393 if (settings->arrangement() == QListView::TopToBottom) {
394 // The decoration width indirectly defines the maximum
395 // width for the text wrapping. To use the maximum item width
396 // for text wrapping, it is used as decoration width.
397 m_viewOptions.decorationSize = QSize(itemWidth, size);
398 } else {
399 m_viewOptions.decorationSize = QSize(size, size);
400 }
401
402 const int spacing = settings->gridSpacing();
403 setGridSize(QSize(itemWidth + spacing, itemHeight + spacing));
404
405 m_itemSize = QSize(itemWidth, itemHeight);
406
407 m_controller->setZoomInPossible(isZoomInPossible());
408 m_controller->setZoomOutPossible(isZoomOutPossible());
409 }
410
411 KFileItem DolphinIconsView::itemForIndex(const QModelIndex& index) const
412 {
413 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(model());
414 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
415 const QModelIndex dirIndex = proxyModel->mapToSource(index);
416 return dirModel->itemForIndex(dirIndex);
417 }
418
419
420 #include "dolphiniconsview.moc"