]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
Rename KListView to KCategorizedView as decided
[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 KCategorizedView(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 = KCategorizedView::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 KCategorizedView::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 KCategorizedView::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 KCategorizedView::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 KCategorizedView::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 if (!selectionModel()->isSelected(indexAt(event->pos()))) {
151 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
152 if (!urls.isEmpty()) {
153 m_controller->indicateDroppedUrls(urls,
154 indexAt(event->pos()),
155 event->source());
156 event->acceptProposedAction();
157 }
158 }
159 KCategorizedView::dropEvent(event);
160 m_dragging = false;
161 }
162
163 void DolphinIconsView::paintEvent(QPaintEvent* event)
164 {
165 KCategorizedView::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"