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