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