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