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