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