]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
Assure that the selection is cleared if a click on the viewport is done.
[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 <QPoint>
35
36 DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controller) :
37 KListView(parent),
38 m_controller(controller)
39 {
40 Q_ASSERT(controller != 0);
41 setViewMode(QListView::IconMode);
42 setResizeMode(QListView::Adjust);
43
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());
107 }
108
109 void DolphinIconsView::mousePressEvent(QMouseEvent* event)
110 {
111 if (!indexAt(event->pos()).isValid()) {
112 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
113 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
114 clearSelection();
115 }
116 }
117
118 KListView::mousePressEvent(event);
119 }
120
121 void DolphinIconsView::mouseReleaseEvent(QMouseEvent* event)
122 {
123 KListView::mouseReleaseEvent(event);
124 m_controller->triggerActivation();
125 }
126
127 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
128 {
129 if (event->mimeData()->hasUrls()) {
130 event->acceptProposedAction();
131 }
132 }
133
134 void DolphinIconsView::dropEvent(QDropEvent* event)
135 {
136 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
137 if (!urls.isEmpty()) {
138 m_controller->indicateDroppedUrls(urls,
139 indexAt(event->pos()),
140 event->source());
141 event->acceptProposedAction();
142 }
143 KListView::dropEvent(event);
144 }
145
146 void DolphinIconsView::slotShowPreviewChanged(bool showPreview)
147 {
148 updateGridSize(showPreview, m_controller->showAdditionalInfo());
149 }
150
151 void DolphinIconsView::slotShowAdditionalInfoChanged(bool showAdditionalInfo)
152 {
153 updateGridSize(m_controller->showPreview(), showAdditionalInfo);
154 }
155
156 void DolphinIconsView::zoomIn()
157 {
158 if (isZoomInPossible()) {
159 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
160
161 const int oldIconSize = settings->iconSize();
162 int newIconSize = oldIconSize;
163
164 const bool showPreview = m_controller->showPreview();
165 if (showPreview) {
166 const int previewSize = increasedIconSize(settings->previewSize());
167 settings->setPreviewSize(previewSize);
168 } else {
169 newIconSize = increasedIconSize(oldIconSize);
170 settings->setIconSize(newIconSize);
171 if (settings->previewSize() < newIconSize) {
172 // assure that the preview size is always >= the icon size
173 settings->setPreviewSize(newIconSize);
174 }
175 }
176
177 // increase also the grid size
178 const int diff = newIconSize - oldIconSize;
179 settings->setItemWidth(settings->itemWidth() + diff);
180 settings->setItemHeight(settings->itemHeight() + diff);
181
182 updateGridSize(showPreview, m_controller->showAdditionalInfo());
183 }
184 }
185
186 void DolphinIconsView::zoomOut()
187 {
188 if (isZoomOutPossible()) {
189 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
190
191 const int oldIconSize = settings->iconSize();
192 int newIconSize = oldIconSize;
193
194 const bool showPreview = m_controller->showPreview();
195 if (showPreview) {
196 const int previewSize = decreasedIconSize(settings->previewSize());
197 settings->setPreviewSize(previewSize);
198 if (settings->iconSize() > previewSize) {
199 // assure that the icon size is always <= the preview size
200 newIconSize = previewSize;
201 settings->setIconSize(newIconSize);
202 }
203 } else {
204 newIconSize = decreasedIconSize(settings->iconSize());
205 settings->setIconSize(newIconSize);
206 }
207
208 // decrease also the grid size
209 const int diff = oldIconSize - newIconSize;
210 settings->setItemWidth(settings->itemWidth() - diff);
211 settings->setItemHeight(settings->itemHeight() - diff);
212
213 updateGridSize(showPreview, m_controller->showAdditionalInfo());
214 }
215 }
216
217 bool DolphinIconsView::isZoomInPossible() const
218 {
219 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
220 const int size = m_controller->showPreview() ? settings->previewSize() : settings->iconSize();
221 return size < K3Icon::SizeEnormous;
222 }
223
224 bool DolphinIconsView::isZoomOutPossible() const
225 {
226 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
227 const int size = m_controller->showPreview() ? settings->previewSize() : settings->iconSize();
228 return size > K3Icon::SizeSmall;
229 }
230
231 int DolphinIconsView::increasedIconSize(int size) const
232 {
233 // TODO: get rid of K3Icon sizes
234 int incSize = 0;
235 switch (size) {
236 case K3Icon::SizeSmall: incSize = K3Icon::SizeSmallMedium; break;
237 case K3Icon::SizeSmallMedium: incSize = K3Icon::SizeMedium; break;
238 case K3Icon::SizeMedium: incSize = K3Icon::SizeLarge; break;
239 case K3Icon::SizeLarge: incSize = K3Icon::SizeHuge; break;
240 case K3Icon::SizeHuge: incSize = K3Icon::SizeEnormous; break;
241 default: Q_ASSERT(false); break;
242 }
243 return incSize;
244 }
245
246 int DolphinIconsView::decreasedIconSize(int size) const
247 {
248 // TODO: get rid of K3Icon sizes
249 int decSize = 0;
250 switch (size) {
251 case K3Icon::SizeSmallMedium: decSize = K3Icon::SizeSmall; break;
252 case K3Icon::SizeMedium: decSize = K3Icon::SizeSmallMedium; break;
253 case K3Icon::SizeLarge: decSize = K3Icon::SizeMedium; break;
254 case K3Icon::SizeHuge: decSize = K3Icon::SizeLarge; break;
255 case K3Icon::SizeEnormous: decSize = K3Icon::SizeHuge; break;
256 default: Q_ASSERT(false); break;
257 }
258 return decSize;
259 }
260
261 void DolphinIconsView::updateGridSize(bool showPreview, bool showAdditionalInfo)
262 {
263 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
264 Q_ASSERT(settings != 0);
265
266 int itemWidth = settings->itemWidth();
267 int itemHeight = settings->itemHeight();
268 int size = settings->iconSize();
269
270 if (showPreview) {
271 const int previewSize = settings->previewSize();
272 const int diff = previewSize - size;
273 Q_ASSERT(diff >= 0);
274 itemWidth += diff;
275 itemHeight += diff;
276
277 size = previewSize;
278 }
279
280 if (showAdditionalInfo) {
281 itemHeight += m_viewOptions.font.pointSize() * 2;
282 }
283
284 if (settings->arrangement() == QListView::TopToBottom) {
285 // The decoration width indirectly defines the maximum
286 // width for the text wrapping. To use the maximum item width
287 // for text wrapping, it is used as decoration width.
288 m_viewOptions.decorationSize = QSize(itemWidth, size);
289 } else {
290 m_viewOptions.decorationSize = QSize(size, size);
291 }
292
293 const int spacing = settings->gridSpacing();
294 setGridSize(QSize(itemWidth + spacing, itemHeight + spacing));
295
296 m_controller->setZoomInPossible(isZoomInPossible());
297 m_controller->setZoomOutPossible(isZoomOutPossible());
298 }
299
300 #include "dolphiniconsview.moc"