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