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