]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
Respect global single/double click setting for the icons- and details view (thanks...
[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 <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(updateGridSize(bool)));
56 connect(controller, SIGNAL(zoomIn()),
57 this, SLOT(zoomIn()));
58 connect(controller, SIGNAL(zoomOut()),
59 this, SLOT(zoomOut()));
60
61 // apply the icons mode settings to the widget
62 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
63 Q_ASSERT(settings != 0);
64
65 m_viewOptions = KListView::viewOptions();
66 m_viewOptions.showDecorationSelected = true;
67
68 QFont font(settings->fontFamily(), settings->fontSize());
69 font.setItalic(settings->italicFont());
70 font.setBold(settings->boldFont());
71 m_viewOptions.font = font;
72
73 updateGridSize(controller->showPreview());
74
75 if (settings->arrangement() == QListView::TopToBottom) {
76 setFlow(QListView::LeftToRight);
77 m_viewOptions.decorationPosition = QStyleOptionViewItem::Top;
78 } else {
79 setFlow(QListView::TopToBottom);
80 m_viewOptions.decorationPosition = QStyleOptionViewItem::Left;
81 }
82 }
83
84 DolphinIconsView::~DolphinIconsView()
85 {
86 }
87
88 QStyleOptionViewItem DolphinIconsView::viewOptions() const
89 {
90 return m_viewOptions;
91 }
92
93 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
94 {
95 KListView::contextMenuEvent(event);
96 m_controller->triggerContextMenuRequest(event->pos());
97 }
98
99 void DolphinIconsView::mouseReleaseEvent(QMouseEvent* event)
100 {
101 KListView::mouseReleaseEvent(event);
102 m_controller->triggerActivation();
103 }
104
105 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
106 {
107 if (event->mimeData()->hasUrls()) {
108 event->acceptProposedAction();
109 }
110 }
111
112 void DolphinIconsView::dropEvent(QDropEvent* event)
113 {
114 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
115 if (!urls.isEmpty()) {
116 m_controller->indicateDroppedUrls(urls,
117 indexAt(event->pos()),
118 event->source());
119 event->acceptProposedAction();
120 }
121 KListView::dropEvent(event);
122 }
123
124 void DolphinIconsView::updateGridSize(bool showPreview)
125 {
126 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
127 Q_ASSERT(settings != 0);
128
129 int gridWidth = settings->gridWidth();
130 int gridHeight = settings->gridHeight();
131 int size = settings->iconSize();
132
133 if (showPreview) {
134 const int previewSize = settings->previewSize();
135 const int diff = previewSize - size;
136 Q_ASSERT(diff >= 0);
137 gridWidth += diff;
138 gridHeight += diff;
139
140 size = previewSize;
141 }
142
143 m_viewOptions.decorationSize = QSize(size, size);
144 setGridSize(QSize(gridWidth, gridHeight));
145
146 m_controller->setZoomInPossible(isZoomInPossible());
147 m_controller->setZoomOutPossible(isZoomOutPossible());
148 }
149
150 void DolphinIconsView::zoomIn()
151 {
152 if (isZoomInPossible()) {
153 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
154
155 const int oldIconSize = settings->iconSize();
156 int newIconSize = oldIconSize;
157
158 const bool showPreview = m_controller->showPreview();
159 if (showPreview) {
160 const int previewSize = increasedIconSize(settings->previewSize());
161 settings->setPreviewSize(previewSize);
162 } else {
163 newIconSize = increasedIconSize(oldIconSize);
164 settings->setIconSize(newIconSize);
165 if (settings->previewSize() < newIconSize) {
166 // assure that the preview size is always >= the icon size
167 settings->setPreviewSize(newIconSize);
168 }
169 }
170
171 // increase also the grid size
172 const int diff = newIconSize - oldIconSize;
173 settings->setGridWidth(settings->gridWidth() + diff);
174 settings->setGridHeight(settings->gridHeight() + diff);
175
176 updateGridSize(showPreview);
177 }
178 }
179
180 void DolphinIconsView::zoomOut()
181 {
182 if (isZoomOutPossible()) {
183 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
184
185 const int oldIconSize = settings->iconSize();
186 int newIconSize = oldIconSize;
187
188 const bool showPreview = m_controller->showPreview();
189 if (showPreview) {
190 const int previewSize = decreasedIconSize(settings->previewSize());
191 settings->setPreviewSize(previewSize);
192 if (settings->iconSize() > previewSize) {
193 // assure that the icon size is always <= the preview size
194 newIconSize = previewSize;
195 settings->setIconSize(newIconSize);
196 }
197 } else {
198 newIconSize = decreasedIconSize(settings->iconSize());
199 settings->setIconSize(newIconSize);
200 }
201
202 // decrease also the grid size
203 const int diff = oldIconSize - newIconSize;
204 settings->setGridWidth(settings->gridWidth() - diff);
205 settings->setGridHeight(settings->gridHeight() - diff);
206
207 updateGridSize(showPreview);
208 }
209 }
210
211 bool DolphinIconsView::isZoomInPossible() const
212 {
213 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
214 const int size = m_controller->showPreview() ? settings->previewSize() : settings->iconSize();
215 return size < K3Icon::SizeEnormous;
216 }
217
218 bool DolphinIconsView::isZoomOutPossible() const
219 {
220 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
221 const int size = m_controller->showPreview() ? settings->previewSize() : settings->iconSize();
222 return size > K3Icon::SizeSmall;
223 }
224
225 int DolphinIconsView::increasedIconSize(int size) const
226 {
227 // TODO: get rid of K3Icon sizes
228 int incSize = 0;
229 switch (size) {
230 case K3Icon::SizeSmall: incSize = K3Icon::SizeSmallMedium; break;
231 case K3Icon::SizeSmallMedium: incSize = K3Icon::SizeMedium; break;
232 case K3Icon::SizeMedium: incSize = K3Icon::SizeLarge; break;
233 case K3Icon::SizeLarge: incSize = K3Icon::SizeHuge; break;
234 case K3Icon::SizeHuge: incSize = K3Icon::SizeEnormous; break;
235 default: Q_ASSERT(false); break;
236 }
237 return incSize;
238 }
239
240 int DolphinIconsView::decreasedIconSize(int size) const
241 {
242 // TODO: get rid of K3Icon sizes
243 int decSize = 0;
244 switch (size) {
245 case K3Icon::SizeSmallMedium: decSize = K3Icon::SizeSmall; break;
246 case K3Icon::SizeMedium: decSize = K3Icon::SizeSmallMedium; break;
247 case K3Icon::SizeLarge: decSize = K3Icon::SizeMedium; break;
248 case K3Icon::SizeHuge: decSize = K3Icon::SizeLarge; break;
249 case K3Icon::SizeEnormous: decSize = K3Icon::SizeHuge; break;
250 default: Q_ASSERT(false); break;
251 }
252 return decSize;
253 }
254
255 #include "dolphiniconsview.moc"