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