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