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