]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
reactivate ScrollPerPixel again, as the Qt-patch for "divide by zero" has been applie...
[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 "dolphinmodel.h"
24 #include "dolphincontroller.h"
25 #include "dolphinsettings.h"
26 #include "dolphinsortfilterproxymodel.h"
27 #include "viewproperties.h"
28
29 #include "dolphin_detailsmodesettings.h"
30
31 #include <QApplication>
32 #include <QHeaderView>
33 #include <QRubberBand>
34 #include <QPainter>
35 #include <QScrollBar>
36
37 DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* controller) :
38 QTreeView(parent),
39 m_controller(controller),
40 m_dragging(false),
41 m_showElasticBand(false),
42 m_elasticBandOrigin(),
43 m_elasticBandDestination()
44 {
45 Q_ASSERT(controller != 0);
46
47 setAcceptDrops(true);
48 setRootIsDecorated(false);
49 setSortingEnabled(true);
50 setUniformRowHeights(true);
51 setSelectionBehavior(SelectItems);
52 setDragDropMode(QAbstractItemView::DragDrop);
53 setDropIndicatorShown(false);
54 setAlternatingRowColors(true);
55 setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
56 setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
57
58 setMouseTracking(true);
59 viewport()->setAttribute(Qt::WA_Hover);
60
61 const ViewProperties props(controller->url());
62 setSortIndicatorSection(props.sorting());
63 setSortIndicatorOrder(props.sortOrder());
64
65 connect(header(), SIGNAL(sectionClicked(int)),
66 this, SLOT(synchronizeSortingState(int)));
67
68 connect(parent, SIGNAL(sortingChanged(DolphinView::Sorting)),
69 this, SLOT(setSortIndicatorSection(DolphinView::Sorting)));
70 connect(parent, SIGNAL(sortOrderChanged(Qt::SortOrder)),
71 this, SLOT(setSortIndicatorOrder(Qt::SortOrder)));
72
73 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
74 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
75 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
76 // RETURN-key in keyPressEvent().
77 if (KGlobalSettings::singleClick()) {
78 connect(this, SIGNAL(clicked(const QModelIndex&)),
79 this, SLOT(slotItemActivated(const QModelIndex&)));
80 } else {
81 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
82 this, SLOT(slotItemActivated(const QModelIndex&)));
83 }
84 connect(this, SIGNAL(entered(const QModelIndex&)),
85 this, SLOT(slotEntered(const QModelIndex&)));
86 connect(this, SIGNAL(viewportEntered()),
87 controller, SLOT(emitViewportEntered()));
88 connect(controller, SIGNAL(zoomIn()),
89 this, SLOT(zoomIn()));
90 connect(controller, SIGNAL(zoomOut()),
91 this, SLOT(zoomOut()));
92
93 // apply the details mode settings to the widget
94 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
95 Q_ASSERT(settings != 0);
96
97 m_viewOptions = QTreeView::viewOptions();
98
99 QFont font(settings->fontFamily(), settings->fontSize());
100 font.setItalic(settings->italicFont());
101 font.setBold(settings->boldFont());
102 m_viewOptions.font = font;
103
104 updateDecorationSize();
105 }
106
107 DolphinDetailsView::~DolphinDetailsView()
108 {
109 }
110
111 bool DolphinDetailsView::event(QEvent* event)
112 {
113 if (event->type() == QEvent::Polish) {
114 // Assure that by respecting the available width that:
115 // - the 'Name' column is stretched as large as possible
116 // - the remaining columns are as small as possible
117 QHeaderView* headerView = header();
118 headerView->setStretchLastSection(false);
119 headerView->setResizeMode(QHeaderView::ResizeToContents);
120 headerView->setResizeMode(0, QHeaderView::Stretch);
121
122 // hide columns if this is indicated by the settings
123 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
124 Q_ASSERT(settings != 0);
125 if (!settings->showDate()) {
126 hideColumn(DolphinModel::ModifiedTime);
127 }
128
129 if (!settings->showPermissions()) {
130 hideColumn(DolphinModel::Permissions);
131 }
132
133 if (!settings->showOwner()) {
134 hideColumn(DolphinModel::Owner);
135 }
136
137 if (!settings->showGroup()) {
138 hideColumn(DolphinModel::Group);
139 }
140
141 if (!settings->showType()) {
142 hideColumn(DolphinModel::Type);
143 }
144
145 hideColumn(DolphinModel::Rating);
146 hideColumn(DolphinModel::Tags);
147 }
148
149 return QTreeView::event(event);
150 }
151
152 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
153 {
154 return m_viewOptions;
155 }
156
157 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
158 {
159 QTreeView::contextMenuEvent(event);
160 m_controller->triggerContextMenuRequest(event->pos());
161 }
162
163 void DolphinDetailsView::mousePressEvent(QMouseEvent* event)
164 {
165 m_controller->triggerActivation();
166
167 QTreeView::mousePressEvent(event);
168
169 const QModelIndex index = indexAt(event->pos());
170 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
171 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
172 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
173 clearSelection();
174 }
175 }
176
177 if (event->button() == Qt::LeftButton) {
178 m_showElasticBand = true;
179
180 const QPoint pos(contentsPos());
181 m_elasticBandOrigin = event->pos();
182 m_elasticBandOrigin.setX(m_elasticBandOrigin.x() + pos.x());
183 m_elasticBandOrigin.setY(m_elasticBandOrigin.y() + pos.y());
184 m_elasticBandDestination = event->pos();
185 }
186 }
187
188 void DolphinDetailsView::mouseMoveEvent(QMouseEvent* event)
189 {
190 QTreeView::mouseMoveEvent(event);
191 if (m_showElasticBand) {
192 updateElasticBand();
193 }
194 }
195
196 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
197 {
198 QTreeView::mouseReleaseEvent(event);
199 if (m_showElasticBand) {
200 updateElasticBand();
201 m_showElasticBand = false;
202 }
203 }
204
205 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
206 {
207 if (event->mimeData()->hasUrls()) {
208 event->acceptProposedAction();
209 }
210
211 if (m_showElasticBand) {
212 updateElasticBand();
213 m_showElasticBand = false;
214 }
215 m_dragging = true;
216 }
217
218 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent* event)
219 {
220 QTreeView::dragLeaveEvent(event);
221
222 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
223 m_dragging = false;
224 setDirtyRegion(m_dropRect);
225 }
226
227 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent* event)
228 {
229 QTreeView::dragMoveEvent(event);
230
231 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
232 setDirtyRegion(m_dropRect);
233 const QModelIndex index = indexAt(event->pos());
234 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
235 m_dragging = false;
236 } else {
237 m_dragging = true;
238 m_dropRect = visualRect(index);
239 setDirtyRegion(m_dropRect);
240 }
241 }
242
243 void DolphinDetailsView::dropEvent(QDropEvent* event)
244 {
245 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
246 if (!urls.isEmpty()) {
247 event->acceptProposedAction();
248 m_controller->indicateDroppedUrls(urls,
249 m_controller->url(),
250 indexAt(event->pos()),
251 event->source());
252 }
253 QTreeView::dropEvent(event);
254 m_dragging = false;
255 }
256
257 void DolphinDetailsView::paintEvent(QPaintEvent* event)
258 {
259 QTreeView::paintEvent(event);
260 if (m_showElasticBand) {
261 // The following code has been taken from QListView
262 // and adapted to DolphinDetailsView.
263 // (C) 1992-2007 Trolltech ASA
264 QStyleOptionRubberBand opt;
265 opt.initFrom(this);
266 opt.shape = QRubberBand::Rectangle;
267 opt.opaque = false;
268 opt.rect = elasticBandRect();
269
270 QPainter painter(viewport());
271 painter.save();
272 style()->drawControl(QStyle::CE_RubberBand, &opt, &painter);
273 painter.restore();
274 }
275
276 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
277 if (m_dragging) {
278 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
279 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
280 }
281 }
282
283 void DolphinDetailsView::keyPressEvent(QKeyEvent* event)
284 {
285 QTreeView::keyPressEvent(event);
286
287 const QItemSelectionModel* selModel = selectionModel();
288 const QModelIndex currentIndex = selModel->currentIndex();
289 const bool triggerItem = currentIndex.isValid()
290 && (event->key() == Qt::Key_Return)
291 && (selModel->selectedIndexes().count() <= 1);
292 if (triggerItem) {
293 m_controller->triggerItem(currentIndex);
294 }
295 }
296
297 void DolphinDetailsView::resizeEvent(QResizeEvent* event)
298 {
299 QTreeView::resizeEvent(event);
300
301 // assure that the width of the name-column does not get too small
302 const int minWidth = 120;
303 QHeaderView* headerView = header();
304 bool useFixedWidth = (headerView->sectionSize(KDirModel::Name) <= minWidth)
305 && (headerView->resizeMode(0) != QHeaderView::Fixed);
306 if (useFixedWidth) {
307 // the current width of the name-column is too small, hence
308 // use a fixed size
309 headerView->setResizeMode(QHeaderView::Fixed);
310 headerView->setResizeMode(0, QHeaderView::Fixed);
311 headerView->resizeSection(KDirModel::Name, minWidth);
312 } else if (headerView->resizeMode(0) != QHeaderView::Stretch) {
313 // check whether there is enough available viewport width
314 // to automatically resize the columns
315 const int availableWidth = viewport()->width();
316
317 int headerWidth = 0;
318 const int count = headerView->count();
319 for (int i = 0; i < count; ++i) {
320 headerWidth += headerView->sectionSize(i);
321 }
322
323 if (headerWidth < availableWidth) {
324 headerView->setResizeMode(QHeaderView::ResizeToContents);
325 headerView->setResizeMode(0, QHeaderView::Stretch);
326 }
327 }
328 }
329
330 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
331 {
332 QHeaderView* headerView = header();
333 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
334 }
335
336 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
337 {
338 QHeaderView* headerView = header();
339 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
340 }
341
342 void DolphinDetailsView::synchronizeSortingState(int column)
343 {
344 // The sorting has already been changed in QTreeView if this slot is
345 // invoked, but Dolphin is not informed about this.
346 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
347 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
348 m_controller->indicateSortingChange(sorting);
349 m_controller->indicateSortOrderChange(sortOrder);
350 }
351
352 void DolphinDetailsView::slotEntered(const QModelIndex& index)
353 {
354 const QPoint pos = viewport()->mapFromGlobal(QCursor::pos());
355 const int nameColumnWidth = header()->sectionSize(DolphinModel::Name);
356 if (pos.x() < nameColumnWidth) {
357 m_controller->emitItemEntered(index);
358 }
359 else {
360 m_controller->emitViewportEntered();
361 }
362 }
363
364 void DolphinDetailsView::updateElasticBand()
365 {
366 Q_ASSERT(m_showElasticBand);
367 QRect dirtyRegion(elasticBandRect());
368 m_elasticBandDestination = viewport()->mapFromGlobal(QCursor::pos());
369 dirtyRegion = dirtyRegion.united(elasticBandRect());
370 setDirtyRegion(dirtyRegion);
371 }
372
373 void DolphinDetailsView::zoomIn()
374 {
375 if (isZoomInPossible()) {
376 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
377 // TODO: get rid of K3Icon sizes
378 switch (settings->iconSize()) {
379 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
380 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
381 default: Q_ASSERT(false); break;
382 }
383 updateDecorationSize();
384 }
385 }
386
387 void DolphinDetailsView::zoomOut()
388 {
389 if (isZoomOutPossible()) {
390 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
391 // TODO: get rid of K3Icon sizes
392 switch (settings->iconSize()) {
393 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
394 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
395 default: Q_ASSERT(false); break;
396 }
397 updateDecorationSize();
398 }
399 }
400
401 void DolphinDetailsView::slotItemActivated(const QModelIndex& index)
402 {
403 if (index.isValid() && (index.column() == KDirModel::Name)) {
404 m_controller->triggerItem(index);
405 } else {
406 clearSelection();
407 m_controller->emitItemEntered(index);
408 }
409 }
410
411 bool DolphinDetailsView::isZoomInPossible() const
412 {
413 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
414 return settings->iconSize() < K3Icon::SizeLarge;
415 }
416
417 bool DolphinDetailsView::isZoomOutPossible() const
418 {
419 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
420 return settings->iconSize() > K3Icon::SizeSmall;
421 }
422
423 void DolphinDetailsView::updateDecorationSize()
424 {
425 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
426 const int iconSize = settings->iconSize();
427 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
428
429 m_controller->setZoomInPossible(isZoomInPossible());
430 m_controller->setZoomOutPossible(isZoomOutPossible());
431
432 doItemsLayout();
433 }
434
435 QPoint DolphinDetailsView::contentsPos() const
436 {
437 // implementation note: the horizonal position is ignored currently, as no
438 // horizontal scrolling is done anyway during a selection
439 const QScrollBar* scrollbar = verticalScrollBar();
440 Q_ASSERT(scrollbar != 0);
441
442 const int maxHeight = maximumViewportSize().height();
443 const int height = scrollbar->maximum() - scrollbar->minimum() + 1;
444 const int visibleHeight = model()->rowCount() + 1 - height;
445 if (visibleHeight <= 0) {
446 return QPoint(0, 0);
447 }
448
449 const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight;
450 return QPoint(0, y);
451 }
452
453 QRect DolphinDetailsView::elasticBandRect() const
454 {
455 const QPoint pos(contentsPos());
456 const QPoint topLeft(m_elasticBandOrigin.x() - pos.x(), m_elasticBandOrigin.y() - pos.y());
457 return QRect(topLeft, m_elasticBandDestination).normalized();
458 }
459
460 static bool isValidNameIndex(const QModelIndex& index)
461 {
462 return index.isValid() && (index.column() == KDirModel::Name);
463 }
464
465 #include "dolphindetailsview.moc"