]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
Provide a hover information when dragging items above other items. TODO: this code...
[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::dragMoveEvent(QDragMoveEvent* event)
211 {
212 QTreeView::dragMoveEvent(event);
213
214 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
215 const QPoint pos(0, event->pos().y());
216 const QModelIndex index = indexAt(pos);
217 setDirtyRegion(m_dropRect);
218 m_dropRect = visualRect(index);
219 setDirtyRegion(m_dropRect);
220 }
221
222 void DolphinDetailsView::dropEvent(QDropEvent* event)
223 {
224 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
225 if (!urls.isEmpty()) {
226 event->acceptProposedAction();
227 m_controller->indicateDroppedUrls(urls,
228 indexAt(event->pos()),
229 event->source());
230 }
231 QTreeView::dropEvent(event);
232 m_dragging = false;
233 }
234
235 void DolphinDetailsView::paintEvent(QPaintEvent* event)
236 {
237 QTreeView::paintEvent(event);
238 if (m_showElasticBand) {
239 // The following code has been taken from QListView
240 // and adapted to DolphinDetailsView.
241 // (C) 1992-2007 Trolltech ASA
242 QStyleOptionRubberBand opt;
243 opt.initFrom(this);
244 opt.shape = QRubberBand::Rectangle;
245 opt.opaque = false;
246 opt.rect = elasticBandRect();
247
248 QPainter painter(viewport());
249 painter.save();
250 style()->drawControl(QStyle::CE_RubberBand, &opt, &painter);
251 painter.restore();
252 }
253
254 if (m_dragging) {
255 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
256 QPainter painter(viewport());
257 painter.save();
258 QBrush brush(m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight));
259 QColor color = brush.color();
260 color.setAlpha(64);
261 brush.setColor(color);
262 painter.fillRect(m_dropRect, brush);
263 painter.restore();
264 }
265 }
266
267 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
268 {
269 QHeaderView* headerView = header();
270 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
271 }
272
273 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
274 {
275 QHeaderView* headerView = header();
276 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
277 }
278
279 void DolphinDetailsView::synchronizeSortingState(int column)
280 {
281 // The sorting has already been changed in QTreeView if this slot is
282 // invoked, but Dolphin is not informed about this.
283 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
284 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
285 m_controller->indicateSortingChange(sorting);
286 m_controller->indicateSortOrderChange(sortOrder);
287 }
288
289 void DolphinDetailsView::slotEntered(const QModelIndex& index)
290 {
291 const QPoint pos = viewport()->mapFromGlobal(QCursor::pos());
292 const int nameColumnWidth = header()->sectionSize(KDirModel::Name);
293 if (pos.x() < nameColumnWidth) {
294 m_controller->emitItemEntered(index);
295 }
296 else {
297 m_controller->emitViewportEntered();
298 }
299 }
300
301 void DolphinDetailsView::updateElasticBand()
302 {
303 Q_ASSERT(m_showElasticBand);
304 QRect dirtyRegion(elasticBandRect());
305 m_elasticBandDestination = viewport()->mapFromGlobal(QCursor::pos());
306 dirtyRegion = dirtyRegion.united(elasticBandRect());
307 setDirtyRegion(dirtyRegion);
308 }
309
310 void DolphinDetailsView::zoomIn()
311 {
312 if (isZoomInPossible()) {
313 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
314 // TODO: get rid of K3Icon sizes
315 switch (settings->iconSize()) {
316 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
317 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
318 default: Q_ASSERT(false); break;
319 }
320 updateDecorationSize();
321 }
322 }
323
324 void DolphinDetailsView::zoomOut()
325 {
326 if (isZoomOutPossible()) {
327 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
328 // TODO: get rid of K3Icon sizes
329 switch (settings->iconSize()) {
330 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
331 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
332 default: Q_ASSERT(false); break;
333 }
334 updateDecorationSize();
335 }
336 }
337
338 bool DolphinDetailsView::isZoomInPossible() const
339 {
340 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
341 return settings->iconSize() < K3Icon::SizeLarge;
342 }
343
344 bool DolphinDetailsView::isZoomOutPossible() const
345 {
346 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
347 return settings->iconSize() > K3Icon::SizeSmall;
348 }
349
350 void DolphinDetailsView::updateDecorationSize()
351 {
352 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
353 const int iconSize = settings->iconSize();
354 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
355
356 m_controller->setZoomInPossible(isZoomInPossible());
357 m_controller->setZoomOutPossible(isZoomOutPossible());
358
359 doItemsLayout();
360 }
361
362 QPoint DolphinDetailsView::contentsPos() const
363 {
364 // implementation note: the horizonal position is ignored currently, as no
365 // horizontal scrolling is done anyway during a selection
366 const QScrollBar* scrollbar = verticalScrollBar();
367 Q_ASSERT(scrollbar != 0);
368
369 const int maxHeight = maximumViewportSize().height();
370 const int height = scrollbar->maximum() - scrollbar->minimum() + 1;
371 const int visibleHeight = model()->rowCount() + 1 - height;
372 if (visibleHeight <= 0) {
373 return QPoint(0, 0);
374 }
375
376 const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight;
377 return QPoint(0, y);
378 }
379
380 QRect DolphinDetailsView::elasticBandRect() const
381 {
382 const QPoint pos(contentsPos());
383 const QPoint topLeft(m_elasticBandOrigin.x() - pos.x(), m_elasticBandOrigin.y() - pos.y());
384 return QRect(topLeft, m_elasticBandDestination).normalized();
385 }
386
387 #include "dolphindetailsview.moc"