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