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