]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
Provide a rubberband for the Details View when selecting items. This assures a consis...
[dolphin.git] / src / dolphindetailsview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
3 * peter.penz@gmx.at *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphindetailsview.h"
22
23 #include "dolphincontroller.h"
24 #include "dolphinsettings.h"
25 #include "dolphinsortfilterproxymodel.h"
26 #include "viewproperties.h"
27
28 #include "dolphin_detailsmodesettings.h"
29
30 #include <kdirmodel.h>
31 #include <kfileitemdelegate.h>
32
33 #include <QHeaderView>
34 #include <QRubberBand>
35 #include <QScrollBar>
36
37 DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* controller) :
38 QTreeView(parent),
39 m_controller(controller),
40 m_rubberBand(0),
41 m_origin()
42 {
43 Q_ASSERT(controller != 0);
44
45 setAcceptDrops(true);
46 setRootIsDecorated(false);
47 setSortingEnabled(true);
48 setUniformRowHeights(true);
49 setSelectionBehavior(SelectItems);
50 setDragDropMode(QAbstractItemView::DragDrop);
51 setDropIndicatorShown(false);
52
53 setMouseTracking(true);
54 viewport()->setAttribute(Qt::WA_Hover);
55
56 const ViewProperties props(controller->url());
57 setSortIndicatorSection(props.sorting());
58 setSortIndicatorOrder(props.sortOrder());
59
60 connect(header(), SIGNAL(sectionClicked(int)),
61 this, SLOT(synchronizeSortingState(int)));
62
63 connect(parent, SIGNAL(sortingChanged(DolphinView::Sorting)),
64 this, SLOT(setSortIndicatorSection(DolphinView::Sorting)));
65 connect(parent, SIGNAL(sortOrderChanged(Qt::SortOrder)),
66 this, SLOT(setSortIndicatorOrder(Qt::SortOrder)));
67
68 if (KGlobalSettings::singleClick()) {
69 connect(this, SIGNAL(clicked(const QModelIndex&)),
70 controller, SLOT(triggerItem(const QModelIndex&)));
71 } else {
72 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
73 controller, SLOT(triggerItem(const QModelIndex&)));
74 }
75 connect(this, SIGNAL(activated(const QModelIndex&)),
76 controller, SLOT(triggerItem(const QModelIndex&)));
77 connect(this, SIGNAL(entered(const QModelIndex&)),
78 this, SLOT(slotEntered(const QModelIndex&)));
79 connect(this, SIGNAL(viewportEntered()),
80 controller, SLOT(emitViewportEntered()));
81 connect(controller, SIGNAL(zoomIn()),
82 this, SLOT(zoomIn()));
83 connect(controller, SIGNAL(zoomOut()),
84 this, SLOT(zoomOut()));
85
86 // apply the details mode settings to the widget
87 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
88 Q_ASSERT(settings != 0);
89
90 m_viewOptions = QTreeView::viewOptions();
91
92 QFont font(settings->fontFamily(), settings->fontSize());
93 font.setItalic(settings->italicFont());
94 font.setBold(settings->boldFont());
95 m_viewOptions.font = font;
96
97 updateDecorationSize();
98 }
99
100 DolphinDetailsView::~DolphinDetailsView()
101 {
102 }
103
104 bool DolphinDetailsView::event(QEvent* event)
105 {
106 if (event->type() == QEvent::Polish) {
107 // Assure that by respecting the available width that:
108 // - the 'Name' column is stretched as large as possible
109 // - the remaining columns are as small as possible
110 QHeaderView* headerView = header();
111 headerView->setStretchLastSection(false);
112 headerView->setResizeMode(QHeaderView::ResizeToContents);
113 headerView->setResizeMode(0, QHeaderView::Stretch);
114
115 // hide columns if this is indicated by the settings
116 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
117 Q_ASSERT(settings != 0);
118 if (!settings->showDate()) {
119 hideColumn(KDirModel::ModifiedTime);
120 }
121
122 if (!settings->showPermissions()) {
123 hideColumn(KDirModel::Permissions);
124 }
125
126 if (!settings->showOwner()) {
127 hideColumn(KDirModel::Owner);
128 }
129
130 if (!settings->showGroup()) {
131 hideColumn(KDirModel::Group);
132 }
133
134 if (!settings->showType()) {
135 hideColumn(KDirModel::Type);
136 }
137 }
138
139 return QTreeView::event(event);
140 }
141
142 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
143 {
144 return m_viewOptions;
145 }
146
147 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
148 {
149 QTreeView::contextMenuEvent(event);
150 m_controller->triggerContextMenuRequest(event->pos());
151 }
152
153 void DolphinDetailsView::mousePressEvent(QMouseEvent* event)
154 {
155 QTreeView::mousePressEvent(event);
156
157 if (event->button() == Qt::LeftButton) {
158 // initialize rubberband for the selection
159 if (m_rubberBand == 0) {
160 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, viewport());
161 }
162
163 const QPoint pos(contentsPos());
164 m_origin = event->pos();
165 m_origin.setX(m_origin.x() + pos.x());
166 m_origin.setY(m_origin.y() + pos.y());
167 updateRubberBandGeometry();
168 m_rubberBand->show();
169 }
170 }
171
172 void DolphinDetailsView::mouseMoveEvent(QMouseEvent* event)
173 {
174 QTreeView::mouseMoveEvent(event);
175 if (m_rubberBand != 0) {
176 updateRubberBandGeometry();
177 }
178 }
179
180 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
181 {
182 QTreeView::mouseReleaseEvent(event);
183 if (m_rubberBand != 0) {
184 m_rubberBand->hide();
185 }
186 m_controller->triggerActivation();
187 }
188
189 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
190 {
191 if (event->mimeData()->hasUrls()) {
192 event->acceptProposedAction();
193 }
194 if (m_rubberBand != 0) {
195 m_rubberBand->hide();
196 }
197 }
198
199 void DolphinDetailsView::dropEvent(QDropEvent* event)
200 {
201 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
202 if (!urls.isEmpty()) {
203 event->acceptProposedAction();
204 m_controller->indicateDroppedUrls(urls,
205 indexAt(event->pos()),
206 event->source());
207 }
208 QTreeView::dropEvent(event);
209 }
210
211 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
212 {
213 QHeaderView* headerView = header();
214 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
215 }
216
217 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
218 {
219 QHeaderView* headerView = header();
220 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
221 }
222
223 void DolphinDetailsView::synchronizeSortingState(int column)
224 {
225 // The sorting has already been changed in QTreeView if this slot is
226 // invoked, but Dolphin is not informed about this.
227 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
228 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
229 m_controller->indicateSortingChange(sorting);
230 m_controller->indicateSortOrderChange(sortOrder);
231 }
232
233 void DolphinDetailsView::slotEntered(const QModelIndex& index)
234 {
235 const QPoint pos = viewport()->mapFromGlobal(QCursor::pos());
236 const int nameColumnWidth = header()->sectionSize(KDirModel::Name);
237 if (pos.x() < nameColumnWidth) {
238 m_controller->emitItemEntered(index);
239 }
240 else {
241 m_controller->emitViewportEntered();
242 }
243 }
244
245 void DolphinDetailsView::updateRubberBandGeometry()
246 {
247 if (m_rubberBand != 0) {
248 const QPoint pos(contentsPos());
249 const QPoint origin(m_origin.x() - pos.x(), m_origin.y() - pos.y());
250 const QPoint dest(viewport()->mapFromGlobal(QCursor::pos()));
251 m_rubberBand->setGeometry(QRect(origin, dest).normalized());
252 }
253 }
254
255 void DolphinDetailsView::zoomIn()
256 {
257 if (isZoomInPossible()) {
258 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
259 // TODO: get rid of K3Icon sizes
260 switch (settings->iconSize()) {
261 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
262 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
263 default: Q_ASSERT(false); break;
264 }
265 updateDecorationSize();
266 }
267 }
268
269 void DolphinDetailsView::zoomOut()
270 {
271 if (isZoomOutPossible()) {
272 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
273 // TODO: get rid of K3Icon sizes
274 switch (settings->iconSize()) {
275 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
276 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
277 default: Q_ASSERT(false); break;
278 }
279 updateDecorationSize();
280 }
281 }
282
283 bool DolphinDetailsView::isZoomInPossible() const
284 {
285 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
286 return settings->iconSize() < K3Icon::SizeLarge;
287 }
288
289 bool DolphinDetailsView::isZoomOutPossible() const
290 {
291 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
292 return settings->iconSize() > K3Icon::SizeSmall;
293 }
294
295 void DolphinDetailsView::updateDecorationSize()
296 {
297 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
298 const int iconSize = settings->iconSize();
299 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
300
301 m_controller->setZoomInPossible(isZoomInPossible());
302 m_controller->setZoomOutPossible(isZoomOutPossible());
303
304 doItemsLayout();
305 }
306
307 QPoint DolphinDetailsView::contentsPos() const
308 {
309 // implementation note: the horizonal position is ignored currently, as no
310 // horizontal scrolling is done anyway during a selection
311 const QScrollBar* scrollbar = verticalScrollBar();
312 Q_ASSERT(scrollbar != 0);
313
314 const int maxHeight = maximumViewportSize().height();
315 const int height = scrollbar->maximum() - scrollbar->minimum() + 1;
316 const int visibleHeight = model()->rowCount() + 1 - height;
317 if (visibleHeight <= 0) {
318 return QPoint(0, 0);
319 }
320
321 const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight;
322 return QPoint(0, y);
323 }
324
325 #include "dolphindetailsview.moc"