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