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