]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
there's no need having 2-liners inside their own method when this method is only...
[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 "draganddrophelper.h"
28 #include "viewproperties.h"
29
30 #include "dolphin_detailsmodesettings.h"
31
32 #include <kdirmodel.h>
33 #include <klocale.h>
34 #include <kmenu.h>
35
36 #include <QAbstractProxyModel>
37 #include <QAction>
38 #include <QApplication>
39 #include <QHeaderView>
40 #include <QRubberBand>
41 #include <QPainter>
42 #include <QScrollBar>
43
44 DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* controller) :
45 QTreeView(parent),
46 m_controller(controller),
47 m_clearAdditionalInfo(false),
48 m_dragging(false),
49 m_showElasticBand(false),
50 m_elasticBandOrigin(),
51 m_elasticBandDestination()
52 {
53 Q_ASSERT(controller != 0);
54
55 setAcceptDrops(true);
56 setRootIsDecorated(false);
57 setSortingEnabled(true);
58 setUniformRowHeights(true);
59 setSelectionBehavior(SelectItems);
60 setDragDropMode(QAbstractItemView::DragDrop);
61 setDropIndicatorShown(false);
62 setAlternatingRowColors(true);
63
64 setMouseTracking(true);
65 viewport()->setAttribute(Qt::WA_Hover);
66
67 const ViewProperties props(controller->url());
68 setSortIndicatorSection(props.sorting());
69 setSortIndicatorOrder(props.sortOrder());
70
71 QHeaderView* headerView = header();
72 connect(headerView, SIGNAL(sectionClicked(int)),
73 this, SLOT(synchronizeSortingState(int)));
74 headerView->setContextMenuPolicy(Qt::CustomContextMenu);
75 connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
76 this, SLOT(configureColumns(const QPoint&)));
77
78 connect(parent, SIGNAL(sortingChanged(DolphinView::Sorting)),
79 this, SLOT(setSortIndicatorSection(DolphinView::Sorting)));
80 connect(parent, SIGNAL(sortOrderChanged(Qt::SortOrder)),
81 this, SLOT(setSortIndicatorOrder(Qt::SortOrder)));
82
83 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
84 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
85 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
86 // RETURN-key in keyPressEvent().
87 if (KGlobalSettings::singleClick()) {
88 connect(this, SIGNAL(clicked(const QModelIndex&)),
89 this, SLOT(triggerItem(const QModelIndex&)));
90 } else {
91 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
92 this, SLOT(triggerItem(const QModelIndex&)));
93 }
94 connect(this, SIGNAL(entered(const QModelIndex&)),
95 this, SLOT(slotEntered(const QModelIndex&)));
96 connect(this, SIGNAL(viewportEntered()),
97 controller, SLOT(emitViewportEntered()));
98 connect(controller, SIGNAL(zoomIn()),
99 this, SLOT(zoomIn()));
100 connect(controller, SIGNAL(zoomOut()),
101 this, SLOT(zoomOut()));
102 connect(controller->dolphinView(), SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList&)),
103 this, SLOT(updateColumnVisibility()));
104
105 // apply the details mode settings to the widget
106 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
107 Q_ASSERT(settings != 0);
108
109 m_viewOptions = QTreeView::viewOptions();
110
111 QFont font(settings->fontFamily(), settings->fontSize());
112 font.setItalic(settings->italicFont());
113 font.setBold(settings->boldFont());
114 m_viewOptions.font = font;
115 m_viewOptions.showDecorationSelected = true;
116
117 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
118 // check avoids a division by zero happening on versions before 4.3.1.
119 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
120 // ereslibre
121 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
122 setVerticalScrollMode(QTreeView::ScrollPerPixel);
123 setHorizontalScrollMode(QTreeView::ScrollPerPixel);
124 #endif
125
126 updateDecorationSize();
127
128 setFocus();
129 }
130
131 DolphinDetailsView::~DolphinDetailsView()
132 {
133 }
134
135 bool DolphinDetailsView::event(QEvent* event)
136 {
137 if (event->type() == QEvent::Polish) {
138 QHeaderView* headerView = header();
139 headerView->setResizeMode(QHeaderView::Fixed);
140 headerView->setMovable(false);
141
142 updateColumnVisibility();
143
144 hideColumn(DolphinModel::Rating);
145 hideColumn(DolphinModel::Tags);
146 }
147 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
148 // check avoids a division by zero happening on versions before 4.3.1.
149 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
150 // ereslibre
151 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
152 else if (event->type() == QEvent::UpdateRequest) {
153 // a wheel movement will scroll 4 items
154 if (model()->rowCount() > 0) {
155 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
156 }
157 }
158 #endif
159
160 return QTreeView::event(event);
161 }
162
163 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
164 {
165 return m_viewOptions;
166 }
167
168 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
169 {
170 QTreeView::contextMenuEvent(event);
171 m_controller->triggerContextMenuRequest(event->pos());
172 }
173
174 void DolphinDetailsView::mousePressEvent(QMouseEvent* event)
175 {
176 m_controller->requestActivation();
177
178 QTreeView::mousePressEvent(event);
179
180 const QModelIndex index = indexAt(event->pos());
181 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
182 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
183 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
184 clearSelection();
185 }
186 }
187
188 if (event->button() == Qt::LeftButton) {
189 m_showElasticBand = true;
190
191 const QPoint pos(contentsPos());
192 m_elasticBandOrigin = event->pos();
193 m_elasticBandOrigin.setX(m_elasticBandOrigin.x() + pos.x());
194 m_elasticBandOrigin.setY(m_elasticBandOrigin.y() + pos.y());
195 m_elasticBandDestination = event->pos();
196 }
197 }
198
199 void DolphinDetailsView::mouseMoveEvent(QMouseEvent* event)
200 {
201 QTreeView::mouseMoveEvent(event);
202 if (m_showElasticBand) {
203 updateElasticBand();
204 }
205 }
206
207 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
208 {
209 QTreeView::mouseReleaseEvent(event);
210 if (m_showElasticBand) {
211 updateElasticBand();
212 m_showElasticBand = false;
213 }
214 }
215
216 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions)
217 {
218 DragAndDropHelper::startDrag(this, supportedActions);
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 if (itemForIndex(index).isDir()) {
255 m_dropRect = visualRect(index);
256 } else {
257 m_dropRect.setSize(QSize()); // set as invalid
258 }
259 setDirtyRegion(m_dropRect);
260 }
261 }
262
263 void DolphinDetailsView::dropEvent(QDropEvent* event)
264 {
265 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
266 if (!urls.isEmpty()) {
267 event->acceptProposedAction();
268 const QModelIndex index = indexAt(event->pos());
269 KFileItem item;
270 if (index.isValid() && (index.column() == DolphinModel::Name)) {
271 item = itemForIndex(index);
272 }
273 m_controller->indicateDroppedUrls(urls,
274 m_controller->url(),
275 item);
276 }
277 QTreeView::dropEvent(event);
278 m_dragging = false;
279 }
280
281 void DolphinDetailsView::paintEvent(QPaintEvent* event)
282 {
283 QTreeView::paintEvent(event);
284 if (m_showElasticBand) {
285 // The following code has been taken from QListView
286 // and adapted to DolphinDetailsView.
287 // (C) 1992-2007 Trolltech ASA
288 QStyleOptionRubberBand opt;
289 opt.initFrom(this);
290 opt.shape = QRubberBand::Rectangle;
291 opt.opaque = false;
292 opt.rect = elasticBandRect();
293
294 QPainter painter(viewport());
295 painter.save();
296 style()->drawControl(QStyle::CE_RubberBand, &opt, &painter);
297 painter.restore();
298 }
299
300 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
301 if (m_dragging) {
302 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
303 DragAndDropHelper::drawHoverIndication(viewport(), m_dropRect, brush);
304 }
305 }
306
307 void DolphinDetailsView::keyPressEvent(QKeyEvent* event)
308 {
309 QTreeView::keyPressEvent(event);
310
311 const QItemSelectionModel* selModel = selectionModel();
312 const QModelIndex currentIndex = selModel->currentIndex();
313 const bool trigger = currentIndex.isValid()
314 && (event->key() == Qt::Key_Return)
315 && (selModel->selectedIndexes().count() <= 1);
316 if (trigger) {
317 triggerItem(currentIndex);
318 }
319 }
320
321 void DolphinDetailsView::resizeEvent(QResizeEvent* event)
322 {
323 QTreeView::resizeEvent(event);
324 resizeColumns();
325 }
326
327 void DolphinDetailsView::closeEvent(QCloseEvent* event)
328 {
329 if (m_clearAdditionalInfo) {
330 disconnect(m_controller->dolphinView(), SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList&)),
331 this, SLOT(updateColumnVisibility()));
332
333 KFileItemDelegate::InformationList info;
334 info.append(KFileItemDelegate::NoInformation);
335 m_controller->indicateAdditionalInfoChange(info);
336 m_clearAdditionalInfo = false;
337 }
338 QTreeView::closeEvent(event);
339 }
340
341 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
342 {
343 QHeaderView* headerView = header();
344 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
345 }
346
347 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
348 {
349 QHeaderView* headerView = header();
350 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
351 }
352
353 void DolphinDetailsView::synchronizeSortingState(int column)
354 {
355 // The sorting has already been changed in QTreeView if this slot is
356 // invoked, but Dolphin is not informed about this.
357 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
358 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
359 m_controller->indicateSortingChange(sorting);
360 m_controller->indicateSortOrderChange(sortOrder);
361 }
362
363 void DolphinDetailsView::slotEntered(const QModelIndex& index)
364 {
365 const QPoint pos = viewport()->mapFromGlobal(QCursor::pos());
366 const int nameColumnWidth = header()->sectionSize(DolphinModel::Name);
367 if (pos.x() < nameColumnWidth) {
368 m_controller->emitItemEntered(itemForIndex(index));
369 }
370 else {
371 m_controller->emitViewportEntered();
372 }
373 }
374
375 void DolphinDetailsView::updateElasticBand()
376 {
377 Q_ASSERT(m_showElasticBand);
378 QRect dirtyRegion(elasticBandRect());
379 m_elasticBandDestination = viewport()->mapFromGlobal(QCursor::pos());
380 dirtyRegion = dirtyRegion.united(elasticBandRect());
381 setDirtyRegion(dirtyRegion);
382 }
383
384 QRect DolphinDetailsView::elasticBandRect() const
385 {
386 const QPoint pos(contentsPos());
387 const QPoint topLeft(m_elasticBandOrigin.x() - pos.x(), m_elasticBandOrigin.y() - pos.y());
388 return QRect(topLeft, m_elasticBandDestination).normalized();
389 }
390
391 void DolphinDetailsView::zoomIn()
392 {
393 if (isZoomInPossible()) {
394 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
395 switch (settings->iconSize()) {
396 case KIconLoader::SizeSmall: settings->setIconSize(KIconLoader::SizeMedium); break;
397 case KIconLoader::SizeMedium: settings->setIconSize(KIconLoader::SizeLarge); break;
398 default: Q_ASSERT(false); break;
399 }
400 updateDecorationSize();
401 }
402 }
403
404 void DolphinDetailsView::zoomOut()
405 {
406 if (isZoomOutPossible()) {
407 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
408 switch (settings->iconSize()) {
409 case KIconLoader::SizeLarge: settings->setIconSize(KIconLoader::SizeMedium); break;
410 case KIconLoader::SizeMedium: settings->setIconSize(KIconLoader::SizeSmall); break;
411 default: Q_ASSERT(false); break;
412 }
413 updateDecorationSize();
414 }
415 }
416
417 void DolphinDetailsView::triggerItem(const QModelIndex& index)
418 {
419 const KFileItem item = itemForIndex(index);
420 if (index.isValid() && (index.column() == KDirModel::Name)) {
421 m_controller->triggerItem(item);
422 } else {
423 clearSelection();
424 m_controller->emitItemEntered(item);
425 }
426 }
427
428 void DolphinDetailsView::configureColumns(const QPoint& pos)
429 {
430 KMenu popup(this);
431 popup.addTitle(i18nc("@title:menu", "Columns"));
432
433 QHeaderView* headerView = header();
434 for (int i = DolphinModel::Size; i <= DolphinModel::Type; ++i) {
435 const int logicalIndex = headerView->logicalIndex(i);
436 const QString text = model()->headerData(i, Qt::Horizontal).toString();
437 QAction* action = popup.addAction(text);
438 action->setCheckable(true);
439 action->setChecked(!headerView->isSectionHidden(logicalIndex));
440 action->setData(i);
441 }
442
443 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
444 if (activatedAction != 0) {
445 const bool show = activatedAction->isChecked();
446 const int columnIndex = activatedAction->data().toInt();
447
448 KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
449 const KFileItemDelegate::Information info = infoForColumn(columnIndex);
450 if (show) {
451 Q_ASSERT(!list.contains(info));
452 list.append(info);
453 } else {
454 Q_ASSERT(list.contains(info));
455 const int index = list.indexOf(info);
456 list.removeAt(index);
457 }
458
459 m_controller->indicateAdditionalInfoChange(list);
460 setColumnHidden(columnIndex, !show);
461 }
462 }
463
464 void DolphinDetailsView::updateColumnVisibility()
465 {
466 KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
467 const bool useDefaultColumns = !isVisible() &&
468 (list.isEmpty() ||
469 list.contains(KFileItemDelegate::NoInformation));
470 if (useDefaultColumns) {
471 // Using the details view without any additional information (-> additional column)
472 // makes no sense and leads to a usability problem as no viewport area is available
473 // anymore. Hence as fallback provide at least a size and date column.
474 list.clear();
475 list.append(KFileItemDelegate::Size);
476 list.append(KFileItemDelegate::ModificationTime);
477 m_controller->indicateAdditionalInfoChange(list);
478 m_clearAdditionalInfo = true;
479 }
480
481 for (int i = DolphinModel::Size; i <= DolphinModel::Type; ++i) {
482 const KFileItemDelegate::Information info = infoForColumn(i);
483 const bool hide = !list.contains(info);
484 if (isColumnHidden(i) != hide) {
485 setColumnHidden(i, hide);
486 m_clearAdditionalInfo = false;
487 }
488 }
489
490 resizeColumns();
491 }
492
493 bool DolphinDetailsView::isZoomInPossible() const
494 {
495 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
496 return settings->iconSize() < KIconLoader::SizeLarge;
497 }
498
499 bool DolphinDetailsView::isZoomOutPossible() const
500 {
501 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
502 return settings->iconSize() > KIconLoader::SizeSmall;
503 }
504
505 void DolphinDetailsView::updateDecorationSize()
506 {
507 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
508 const int iconSize = settings->iconSize();
509 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
510
511 m_controller->setZoomInPossible(isZoomInPossible());
512 m_controller->setZoomOutPossible(isZoomOutPossible());
513
514 doItemsLayout();
515 }
516
517 QPoint DolphinDetailsView::contentsPos() const
518 {
519 // implementation note: the horizonal position is ignored currently, as no
520 // horizontal scrolling is done anyway during a selection
521 const QScrollBar* scrollbar = verticalScrollBar();
522 Q_ASSERT(scrollbar != 0);
523
524 const int maxHeight = maximumViewportSize().height();
525 const int height = scrollbar->maximum() - scrollbar->minimum() + 1;
526 const int visibleHeight = model()->rowCount() + 1 - height;
527 if (visibleHeight <= 0) {
528 return QPoint(0, 0);
529 }
530
531 const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight;
532 return QPoint(0, y);
533 }
534
535 KFileItem DolphinDetailsView::itemForIndex(const QModelIndex& index) const
536 {
537 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(model());
538 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
539 const QModelIndex dirIndex = proxyModel->mapToSource(index);
540 return dirModel->itemForIndex(dirIndex);
541 }
542
543 KFileItemDelegate::Information DolphinDetailsView::infoForColumn(int columnIndex) const
544 {
545 KFileItemDelegate::Information info = KFileItemDelegate::NoInformation;
546
547 switch (columnIndex) {
548 case DolphinModel::Size: info = KFileItemDelegate::Size; break;
549 case DolphinModel::ModifiedTime: info = KFileItemDelegate::ModificationTime; break;
550 case DolphinModel::Permissions: info = KFileItemDelegate::Permissions; break;
551 case DolphinModel::Owner: info = KFileItemDelegate::Owner; break;
552 case DolphinModel::Group: info = KFileItemDelegate::OwnerAndGroup; break;
553 case DolphinModel::Type: info = KFileItemDelegate::FriendlyMimeType; break;
554 default: break;
555 }
556
557 return info;
558 }
559
560 void DolphinDetailsView::resizeColumns()
561 {
562 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
563 // around 3 seconds for each (!) resize operation when having > 10000 items).
564 // This gets a problem especially when opening large directories, where several
565 // resize operations are received for showing the currently available items during
566 // loading (the application hangs around 20 seconds when loading > 10000 items).
567
568 QHeaderView* headerView = header();
569 QFontMetrics fontMetrics(viewport()->font());
570
571 int columnWidth[KDirModel::ColumnCount];
572 columnWidth[KDirModel::Size] = fontMetrics.width("00000 Items");
573 columnWidth[KDirModel::ModifiedTime] = fontMetrics.width("0000-00-00 00:00");
574 columnWidth[KDirModel::Permissions] = fontMetrics.width("xxxxxxxxxx");
575 columnWidth[KDirModel::Owner] = fontMetrics.width("xxxxxxxxxx");
576 columnWidth[KDirModel::Group] = fontMetrics.width("xxxxxxxxxx");
577 columnWidth[KDirModel::Type] = fontMetrics.width("XXXX Xxxxxxx");
578
579 int requiredWidth = 0;
580 for (int i = KDirModel::Size; i <= KDirModel::Type; ++i) {
581 if (!isColumnHidden(i)) {
582 columnWidth[i] += 20; // provide a default gap
583 requiredWidth += columnWidth[i];
584 headerView->resizeSection(i, columnWidth[i]);
585 }
586 }
587
588 // resize the name column in a way that the whole available width is used
589 columnWidth[KDirModel::Name] = viewport()->width() - requiredWidth;
590 if (columnWidth[KDirModel::Name] < 120) {
591 columnWidth[KDirModel::Name] = 120;
592 }
593 headerView->resizeSection(KDirModel::Name, columnWidth[KDirModel::Name]);
594 }
595
596 #include "dolphindetailsview.moc"