]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
layout improvements in the icons view:
[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 <QtGui/QAbstractProxyModel>
33 #include <QtCore/QPoint>
34
35 DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controller) :
36 KListView(parent),
37 m_controller(controller)
38 {
39 Q_ASSERT(controller != 0);
40 setViewMode(QListView::IconMode);
41 setResizeMode(QListView::Adjust);
42
43 viewport()->setAttribute(Qt::WA_Hover);
44
45 if (KGlobalSettings::singleClick()) {
46 connect(this, SIGNAL(clicked(const QModelIndex&)),
47 controller, SLOT(triggerItem(const QModelIndex&)));
48 } else {
49 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
50 controller, SLOT(triggerItem(const QModelIndex&)));
51 }
52 connect(this, SIGNAL(activated(const QModelIndex&)),
53 controller, SLOT(triggerItem(const QModelIndex&)));
54 connect(controller, SIGNAL(showPreviewChanged(bool)),
55 this, SLOT(slotShowPreviewChanged(bool)));
56 connect(controller, SIGNAL(showAdditionalInfoChanged(bool)),
57 this, SLOT(slotShowAdditionalInfoChanged(bool)));
58 connect(controller, SIGNAL(zoomIn()),
59 this, SLOT(zoomIn()));
60 connect(controller, SIGNAL(zoomOut()),
61 this, SLOT(zoomOut()));
62
63 // apply the icons mode settings to the widget
64 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
65 Q_ASSERT(settings != 0);
66
67 m_viewOptions = KListView::viewOptions();
68 m_viewOptions.showDecorationSelected = true;
69
70 QFont font(settings->fontFamily(), settings->fontSize());
71 font.setItalic(settings->italicFont());
72 font.setBold(settings->boldFont());
73 m_viewOptions.font = font;
74 if (settings->numberOfTextlines() > 1) {
75 m_viewOptions.textElideMode = Qt::ElideNone;
76 m_viewOptions.features = QStyleOptionViewItemV2::WrapText;
77 }
78
79 updateGridSize(controller->showPreview(), controller->showAdditionalInfo());
80
81 if (settings->arrangement() == QListView::TopToBottom) {
82 setFlow(QListView::LeftToRight);
83 m_viewOptions.decorationPosition = QStyleOptionViewItem::Top;
84 } else {
85 setFlow(QListView::TopToBottom);
86 m_viewOptions.decorationPosition = QStyleOptionViewItem::Left;
87 }
88 }
89
90 DolphinIconsView::~DolphinIconsView()
91 {
92 }
93
94 QStyleOptionViewItem DolphinIconsView::viewOptions() const
95 {
96 return m_viewOptions;
97 }
98
99 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
100 {
101 KListView::contextMenuEvent(event);
102 m_controller->triggerContextMenuRequest(event->pos());
103 }
104
105 void DolphinIconsView::mouseReleaseEvent(QMouseEvent* event)
106 {
107 KListView::mouseReleaseEvent(event);
108 m_controller->triggerActivation();
109 }
110
111 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
112 {
113 if (event->mimeData()->hasUrls()) {
114 event->acceptProposedAction();
115 }
116 }
117
118 void DolphinIconsView::dropEvent(QDropEvent* event)
119 {
120 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
121 if (!urls.isEmpty()) {
122 m_controller->indicateDroppedUrls(urls,
123 indexAt(event->pos()),
124 event->source());
125 event->acceptProposedAction();
126 }
127 KListView::dropEvent(event);
128 }
129
130 void DolphinIconsView::slotShowPreviewChanged(bool showPreview)
131 {
132 updateGridSize(showPreview, m_controller->showAdditionalInfo());
133 }
134
135 void DolphinIconsView::slotShowAdditionalInfoChanged(bool showAdditionalInfo)
136 {
137 updateGridSize(m_controller->showPreview(), showAdditionalInfo);
138 }
139
140 void DolphinIconsView::zoomIn()
141 {
142 if (isZoomInPossible()) {
143 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
144
145 const int oldIconSize = settings->iconSize();
146 int newIconSize = oldIconSize;
147
148 const bool showPreview = m_controller->showPreview();
149 if (showPreview) {
150 const int previewSize = increasedIconSize(settings->previewSize());
151 settings->setPreviewSize(previewSize);
152 } else {
153 newIconSize = increasedIconSize(oldIconSize);
154 settings->setIconSize(newIconSize);
155 if (settings->previewSize() < newIconSize) {
156 // assure that the preview size is always >= the icon size
157 settings->setPreviewSize(newIconSize);
158 }
159 }
160
161 // increase also the grid size
162 const int diff = newIconSize - oldIconSize;
163 settings->setGridWidth(settings->gridWidth() + diff);
164 settings->setGridHeight(settings->gridHeight() + diff);
165
166 updateGridSize(showPreview, m_controller->showAdditionalInfo());
167 }
168 }
169
170 void DolphinIconsView::zoomOut()
171 {
172 if (isZoomOutPossible()) {
173 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
174
175 const int oldIconSize = settings->iconSize();
176 int newIconSize = oldIconSize;
177
178 const bool showPreview = m_controller->showPreview();
179 if (showPreview) {
180 const int previewSize = decreasedIconSize(settings->previewSize());
181 settings->setPreviewSize(previewSize);
182 if (settings->iconSize() > previewSize) {
183 // assure that the icon size is always <= the preview size
184 newIconSize = previewSize;
185 settings->setIconSize(newIconSize);
186 }
187 } else {
188 newIconSize = decreasedIconSize(settings->iconSize());
189 settings->setIconSize(newIconSize);
190 }
191
192 // decrease also the grid size
193 const int diff = oldIconSize - newIconSize;
194 settings->setGridWidth(settings->gridWidth() - diff);
195 settings->setGridHeight(settings->gridHeight() - diff);
196
197 updateGridSize(showPreview, m_controller->showAdditionalInfo());
198 }
199 }
200
201 bool DolphinIconsView::isZoomInPossible() const
202 {
203 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
204 const int size = m_controller->showPreview() ? settings->previewSize() : settings->iconSize();
205 return size < K3Icon::SizeEnormous;
206 }
207
208 bool DolphinIconsView::isZoomOutPossible() const
209 {
210 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
211 const int size = m_controller->showPreview() ? settings->previewSize() : settings->iconSize();
212 return size > K3Icon::SizeSmall;
213 }
214
215 int DolphinIconsView::increasedIconSize(int size) const
216 {
217 // TODO: get rid of K3Icon sizes
218 int incSize = 0;
219 switch (size) {
220 case K3Icon::SizeSmall: incSize = K3Icon::SizeSmallMedium; break;
221 case K3Icon::SizeSmallMedium: incSize = K3Icon::SizeMedium; break;
222 case K3Icon::SizeMedium: incSize = K3Icon::SizeLarge; break;
223 case K3Icon::SizeLarge: incSize = K3Icon::SizeHuge; break;
224 case K3Icon::SizeHuge: incSize = K3Icon::SizeEnormous; break;
225 default: Q_ASSERT(false); break;
226 }
227 return incSize;
228 }
229
230 int DolphinIconsView::decreasedIconSize(int size) const
231 {
232 // TODO: get rid of K3Icon sizes
233 int decSize = 0;
234 switch (size) {
235 case K3Icon::SizeSmallMedium: decSize = K3Icon::SizeSmall; break;
236 case K3Icon::SizeMedium: decSize = K3Icon::SizeSmallMedium; break;
237 case K3Icon::SizeLarge: decSize = K3Icon::SizeMedium; break;
238 case K3Icon::SizeHuge: decSize = K3Icon::SizeLarge; break;
239 case K3Icon::SizeEnormous: decSize = K3Icon::SizeHuge; break;
240 default: Q_ASSERT(false); break;
241 }
242 return decSize;
243 }
244
245 void DolphinIconsView::updateGridSize(bool showPreview, bool showAdditionalInfo)
246 {
247 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
248 Q_ASSERT(settings != 0);
249
250 int gridWidth = settings->gridWidth();
251 int gridHeight = settings->gridHeight();
252 int size = settings->iconSize();
253
254 if (showPreview) {
255 const int previewSize = settings->previewSize();
256 const int diff = previewSize - size;
257 Q_ASSERT(diff >= 0);
258 gridWidth += diff;
259 gridHeight += diff;
260
261 size = previewSize;
262 }
263
264 if (showAdditionalInfo) {
265 gridHeight += m_viewOptions.font.pointSize() * 2;
266 }
267
268 m_viewOptions.decorationSize = QSize(size, size);
269 setGridSize(QSize(gridWidth, gridHeight));
270
271 m_controller->setZoomInPossible(isZoomInPossible());
272 m_controller->setZoomOutPossible(isZoomOutPossible());
273 }
274
275 #include "dolphiniconsview.moc"