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