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