]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
* Fix triggering an assertion when the elastic band is already hidden.
[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 <QApplication>
34 #include <QHeaderView>
35 #include <QRubberBand>
36 #include <QPainter>
37 #include <QScrollBar>
38
39 DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* controller) :
40 QTreeView(parent),
41 m_controller(controller),
42 m_showElasticBand(false),
43 m_elasticBandOrigin(),
44 m_elasticBandDestination()
45 {
46 Q_ASSERT(controller != 0);
47
48 setAcceptDrops(true);
49 setRootIsDecorated(false);
50 setSortingEnabled(true);
51 setUniformRowHeights(true);
52 setSelectionBehavior(SelectItems);
53 setDragDropMode(QAbstractItemView::DragDrop);
54 setDropIndicatorShown(false);
55
56 setMouseTracking(true);
57 viewport()->setAttribute(Qt::WA_Hover);
58
59 const ViewProperties props(controller->url());
60 setSortIndicatorSection(props.sorting());
61 setSortIndicatorOrder(props.sortOrder());
62
63 connect(header(), SIGNAL(sectionClicked(int)),
64 this, SLOT(synchronizeSortingState(int)));
65
66 connect(parent, SIGNAL(sortingChanged(DolphinView::Sorting)),
67 this, SLOT(setSortIndicatorSection(DolphinView::Sorting)));
68 connect(parent, SIGNAL(sortOrderChanged(Qt::SortOrder)),
69 this, SLOT(setSortIndicatorOrder(Qt::SortOrder)));
70
71 if (KGlobalSettings::singleClick()) {
72 connect(this, SIGNAL(clicked(const QModelIndex&)),
73 controller, SLOT(triggerItem(const QModelIndex&)));
74 } else {
75 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
76 controller, SLOT(triggerItem(const QModelIndex&)));
77 }
78 connect(this, SIGNAL(activated(const QModelIndex&)),
79 controller, SLOT(triggerItem(const QModelIndex&)));
80 connect(this, SIGNAL(entered(const QModelIndex&)),
81 this, SLOT(slotEntered(const QModelIndex&)));
82 connect(this, SIGNAL(viewportEntered()),
83 controller, SLOT(emitViewportEntered()));
84 connect(controller, SIGNAL(zoomIn()),
85 this, SLOT(zoomIn()));
86 connect(controller, SIGNAL(zoomOut()),
87 this, SLOT(zoomOut()));
88
89 // apply the details mode settings to the widget
90 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
91 Q_ASSERT(settings != 0);
92
93 m_viewOptions = QTreeView::viewOptions();
94
95 QFont font(settings->fontFamily(), settings->fontSize());
96 font.setItalic(settings->italicFont());
97 font.setBold(settings->boldFont());
98 m_viewOptions.font = font;
99
100 updateDecorationSize();
101 }
102
103 DolphinDetailsView::~DolphinDetailsView()
104 {
105 }
106
107 bool DolphinDetailsView::event(QEvent* event)
108 {
109 if (event->type() == QEvent::Polish) {
110 // Assure that by respecting the available width that:
111 // - the 'Name' column is stretched as large as possible
112 // - the remaining columns are as small as possible
113 QHeaderView* headerView = header();
114 headerView->setStretchLastSection(false);
115 headerView->setResizeMode(QHeaderView::ResizeToContents);
116 headerView->setResizeMode(0, QHeaderView::Stretch);
117
118 // hide columns if this is indicated by the settings
119 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
120 Q_ASSERT(settings != 0);
121 if (!settings->showDate()) {
122 hideColumn(KDirModel::ModifiedTime);
123 }
124
125 if (!settings->showPermissions()) {
126 hideColumn(KDirModel::Permissions);
127 }
128
129 if (!settings->showOwner()) {
130 hideColumn(KDirModel::Owner);
131 }
132
133 if (!settings->showGroup()) {
134 hideColumn(KDirModel::Group);
135 }
136
137 if (!settings->showType()) {
138 hideColumn(KDirModel::Type);
139 }
140 }
141
142 return QTreeView::event(event);
143 }
144
145 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
146 {
147 return m_viewOptions;
148 }
149
150 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
151 {
152 QTreeView::contextMenuEvent(event);
153 m_controller->triggerContextMenuRequest(event->pos());
154 }
155
156 void DolphinDetailsView::mousePressEvent(QMouseEvent* event)
157 {
158 if (!indexAt(event->pos()).isValid()) {
159 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
160 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
161 clearSelection();
162 }
163 }
164
165 QTreeView::mousePressEvent(event);
166
167 if (event->button() == Qt::LeftButton) {
168 m_showElasticBand = true;
169
170 const QPoint pos(contentsPos());
171 m_elasticBandOrigin = event->pos();
172 m_elasticBandOrigin.setX(m_elasticBandOrigin.x() + pos.x());
173 m_elasticBandOrigin.setY(m_elasticBandOrigin.y() + pos.y());
174 m_elasticBandDestination = event->pos();
175 }
176 }
177
178 void DolphinDetailsView::mouseMoveEvent(QMouseEvent* event)
179 {
180 QTreeView::mouseMoveEvent(event);
181 if (m_showElasticBand) {
182 updateElasticBand();
183 }
184 }
185
186 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
187 {
188 QTreeView::mouseReleaseEvent(event);
189 if (m_showElasticBand) {
190 updateElasticBand();
191 m_showElasticBand = false;
192 }
193 m_controller->triggerActivation();
194 }
195
196 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
197 {
198 if (event->mimeData()->hasUrls()) {
199 event->acceptProposedAction();
200 }
201
202 if (m_showElasticBand) {
203 updateElasticBand();
204 m_showElasticBand = false;
205 }
206 }
207
208 void DolphinDetailsView::dropEvent(QDropEvent* event)
209 {
210 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
211 if (!urls.isEmpty()) {
212 event->acceptProposedAction();
213 m_controller->indicateDroppedUrls(urls,
214 indexAt(event->pos()),
215 event->source());
216 }
217 QTreeView::dropEvent(event);
218 }
219
220 void DolphinDetailsView::paintEvent(QPaintEvent* event)
221 {
222 QTreeView::paintEvent(event);
223 if (m_showElasticBand) {
224 // The following code has been taken from QListView
225 // and adapted to DolphinDetailsView.
226 // (C) 1992-2007 Trolltech ASA
227 QStyleOptionRubberBand opt;
228 opt.initFrom(this);
229 opt.shape = QRubberBand::Rectangle;
230 opt.opaque = false;
231 opt.rect = elasticBandRect();
232
233 QPainter painter(viewport());
234 painter.save();
235 style()->drawControl(QStyle::CE_RubberBand, &opt, &painter);
236 painter.restore();
237 }
238 }
239
240 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
241 {
242 QHeaderView* headerView = header();
243 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
244 }
245
246 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
247 {
248 QHeaderView* headerView = header();
249 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
250 }
251
252 void DolphinDetailsView::synchronizeSortingState(int column)
253 {
254 // The sorting has already been changed in QTreeView if this slot is
255 // invoked, but Dolphin is not informed about this.
256 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
257 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
258 m_controller->indicateSortingChange(sorting);
259 m_controller->indicateSortOrderChange(sortOrder);
260 }
261
262 void DolphinDetailsView::slotEntered(const QModelIndex& index)
263 {
264 const QPoint pos = viewport()->mapFromGlobal(QCursor::pos());
265 const int nameColumnWidth = header()->sectionSize(KDirModel::Name);
266 if (pos.x() < nameColumnWidth) {
267 m_controller->emitItemEntered(index);
268 }
269 else {
270 m_controller->emitViewportEntered();
271 }
272 }
273
274 void DolphinDetailsView::updateElasticBand()
275 {
276 Q_ASSERT(m_showElasticBand);
277 QRect dirtyRegion(elasticBandRect());
278 m_elasticBandDestination = viewport()->mapFromGlobal(QCursor::pos());
279 dirtyRegion = dirtyRegion.united(elasticBandRect());
280 setDirtyRegion(dirtyRegion);
281 }
282
283 void DolphinDetailsView::zoomIn()
284 {
285 if (isZoomInPossible()) {
286 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
287 // TODO: get rid of K3Icon sizes
288 switch (settings->iconSize()) {
289 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
290 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
291 default: Q_ASSERT(false); break;
292 }
293 updateDecorationSize();
294 }
295 }
296
297 void DolphinDetailsView::zoomOut()
298 {
299 if (isZoomOutPossible()) {
300 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
301 // TODO: get rid of K3Icon sizes
302 switch (settings->iconSize()) {
303 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
304 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
305 default: Q_ASSERT(false); break;
306 }
307 updateDecorationSize();
308 }
309 }
310
311 bool DolphinDetailsView::isZoomInPossible() const
312 {
313 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
314 return settings->iconSize() < K3Icon::SizeLarge;
315 }
316
317 bool DolphinDetailsView::isZoomOutPossible() const
318 {
319 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
320 return settings->iconSize() > K3Icon::SizeSmall;
321 }
322
323 void DolphinDetailsView::updateDecorationSize()
324 {
325 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
326 const int iconSize = settings->iconSize();
327 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
328
329 m_controller->setZoomInPossible(isZoomInPossible());
330 m_controller->setZoomOutPossible(isZoomOutPossible());
331
332 doItemsLayout();
333 }
334
335 QPoint DolphinDetailsView::contentsPos() const
336 {
337 // implementation note: the horizonal position is ignored currently, as no
338 // horizontal scrolling is done anyway during a selection
339 const QScrollBar* scrollbar = verticalScrollBar();
340 Q_ASSERT(scrollbar != 0);
341
342 const int maxHeight = maximumViewportSize().height();
343 const int height = scrollbar->maximum() - scrollbar->minimum() + 1;
344 const int visibleHeight = model()->rowCount() + 1 - height;
345 if (visibleHeight <= 0) {
346 return QPoint(0, 0);
347 }
348
349 const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight;
350 return QPoint(0, y);
351 }
352
353 QRect DolphinDetailsView::elasticBandRect() const
354 {
355 const QPoint pos(contentsPos());
356 const QPoint topLeft(m_elasticBandOrigin.x() - pos.x(), m_elasticBandOrigin.y() - pos.y());
357 return QRect(topLeft, m_elasticBandDestination).normalized();
358 }
359
360 #include "dolphindetailsview.moc"