1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
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. *
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. *
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 ***************************************************************************/
21 #include "dolphindetailsview.h"
23 #include "dolphinmodel.h"
24 #include "dolphincontroller.h"
25 #include "dolphinsettings.h"
26 #include "dolphinsortfilterproxymodel.h"
27 #include "draganddrophelper.h"
28 #include "viewproperties.h"
30 #include "dolphin_detailsmodesettings.h"
32 #include <kdirmodel.h>
36 #include <QAbstractProxyModel>
38 #include <QApplication>
39 #include <QHeaderView>
40 #include <QRubberBand>
44 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
, DolphinController
* controller
) :
46 m_controller(controller
),
49 m_clearAdditionalInfo(false),
51 m_showElasticBand(false),
52 m_elasticBandOrigin(),
53 m_elasticBandDestination()
55 Q_ASSERT(controller
!= 0);
58 setRootIsDecorated(false);
59 setSortingEnabled(true);
60 setUniformRowHeights(true);
61 setSelectionBehavior(SelectItems
);
62 setDragDropMode(QAbstractItemView::DragDrop
);
63 setDropIndicatorShown(false);
64 setAlternatingRowColors(true);
65 setItemsExpandable(false);
67 setMouseTracking(true);
68 viewport()->setAttribute(Qt::WA_Hover
);
70 const ViewProperties
props(controller
->url());
71 setSortIndicatorSection(props
.sorting());
72 setSortIndicatorOrder(props
.sortOrder());
74 QHeaderView
* headerView
= header();
75 connect(headerView
, SIGNAL(sectionClicked(int)),
76 this, SLOT(synchronizeSortingState(int)));
77 headerView
->setContextMenuPolicy(Qt::CustomContextMenu
);
78 connect(headerView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
79 this, SLOT(configureColumns(const QPoint
&)));
81 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
82 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
83 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
84 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
86 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
87 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
88 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
89 // RETURN-key in keyPressEvent().
90 if (KGlobalSettings::singleClick()) {
91 connect(this, SIGNAL(clicked(const QModelIndex
&)),
92 this, SLOT(triggerItem(const QModelIndex
&)));
94 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
95 this, SLOT(triggerItem(const QModelIndex
&)));
97 connect(this, SIGNAL(entered(const QModelIndex
&)),
98 this, SLOT(slotEntered(const QModelIndex
&)));
99 connect(this, SIGNAL(viewportEntered()),
100 controller
, SLOT(emitViewportEntered()));
101 connect(controller
, SIGNAL(zoomIn()),
102 this, SLOT(zoomIn()));
103 connect(controller
, SIGNAL(zoomOut()),
104 this, SLOT(zoomOut()));
105 connect(controller
->dolphinView(), SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
106 this, SLOT(updateColumnVisibility()));
108 // apply the details mode settings to the widget
109 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
110 Q_ASSERT(settings
!= 0);
112 m_font
= QFont(settings
->fontFamily(), settings
->fontSize());
114 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
115 // check avoids a division by zero happening on versions before 4.3.1.
116 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
118 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
119 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
120 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
123 updateDecorationSize();
128 DolphinDetailsView::~DolphinDetailsView()
132 bool DolphinDetailsView::event(QEvent
* event
)
134 if (event
->type() == QEvent::Polish
) {
135 QHeaderView
* headerView
= header();
136 headerView
->setResizeMode(QHeaderView::Interactive
);
137 headerView
->setMovable(false);
139 updateColumnVisibility();
141 hideColumn(DolphinModel::Rating
);
142 hideColumn(DolphinModel::Tags
);
144 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
145 // check avoids a division by zero happening on versions before 4.3.1.
146 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
148 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
149 else if (event
->type() == QEvent::UpdateRequest
) {
150 // a wheel movement will scroll 4 items
151 if (model()->rowCount() > 0) {
152 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
157 return QTreeView::event(event
);
160 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
162 QStyleOptionViewItem viewOptions
= QTreeView::viewOptions();
163 viewOptions
.font
= m_font
;
164 viewOptions
.showDecorationSelected
= true;
165 viewOptions
.decorationSize
= m_decorationSize
;
169 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
171 QTreeView::contextMenuEvent(event
);
172 m_controller
->triggerContextMenuRequest(event
->pos());
175 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
177 m_controller
->requestActivation();
179 QTreeView::mousePressEvent(event
);
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
)) {
189 if (event
->button() == Qt::LeftButton
) {
190 m_showElasticBand
= true;
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();
200 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
202 if (m_showElasticBand
) {
203 const QPoint mousePos
= event
->pos();
204 const QModelIndex index
= indexAt(mousePos
);
205 if (!index
.isValid()) {
206 // the destination of the selection rectangle is above the viewport. In this
207 // case QTreeView does no selection at all, which is not the wanted behavior
208 // in Dolphin -> select all items within the elastic band rectangle
210 if (mousePos
.x() < header()->sectionSize(DolphinModel::Name
)) {
211 setSelection(QRect(m_elasticBandOrigin
, m_elasticBandDestination
),
212 QItemSelectionModel::Select
);
216 QTreeView::mouseMoveEvent(event
);
219 QTreeView::mouseMoveEvent(event
);
223 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
225 QTreeView::mouseReleaseEvent(event
);
226 if (m_showElasticBand
) {
228 m_showElasticBand
= false;
232 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions
)
234 DragAndDropHelper::startDrag(this, supportedActions
);
237 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
239 if (event
->mimeData()->hasUrls()) {
240 event
->acceptProposedAction();
243 if (m_showElasticBand
) {
245 m_showElasticBand
= false;
250 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
252 QTreeView::dragLeaveEvent(event
);
254 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
256 setDirtyRegion(m_dropRect
);
259 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
261 QTreeView::dragMoveEvent(event
);
263 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
264 setDirtyRegion(m_dropRect
);
265 const QModelIndex index
= indexAt(event
->pos());
266 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
270 const KFileItem item
= itemForIndex(index
);
271 if (!item
.isNull() && item
.isDir()) {
272 m_dropRect
= visualRect(index
);
274 m_dropRect
.setSize(QSize()); // set as invalid
276 setDirtyRegion(m_dropRect
);
280 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
282 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
283 if (!urls
.isEmpty()) {
284 event
->acceptProposedAction();
285 const QModelIndex index
= indexAt(event
->pos());
287 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
288 item
= itemForIndex(index
);
290 m_controller
->indicateDroppedUrls(urls
,
294 QTreeView::dropEvent(event
);
298 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
300 QTreeView::paintEvent(event
);
301 if (m_showElasticBand
) {
302 // The following code has been taken from QListView
303 // and adapted to DolphinDetailsView.
304 // (C) 1992-2007 Trolltech ASA
305 QStyleOptionRubberBand opt
;
307 opt
.shape
= QRubberBand::Rectangle
;
309 opt
.rect
= elasticBandRect();
311 QPainter
painter(viewport());
313 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
317 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
319 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
320 DragAndDropHelper::drawHoverIndication(viewport(), m_dropRect
, brush
);
324 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
326 QTreeView::keyPressEvent(event
);
328 const QItemSelectionModel
* selModel
= selectionModel();
329 const QModelIndex currentIndex
= selModel
->currentIndex();
330 const bool trigger
= currentIndex
.isValid()
331 && (event
->key() == Qt::Key_Return
)
332 && (selModel
->selectedIndexes().count() <= 1);
334 triggerItem(currentIndex
);
338 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
340 QTreeView::resizeEvent(event
);
342 // TODO: There seems to be no easy way to find out whether the resize event
343 // has been triggered because of resizing the window or by adjusting the column-width
344 // by a left mouse-click (the columns should only be resized automatically when the window
345 // size is adjusted). The following workaround works well, but it should be
346 // considered solving this in a more transparent way.
347 if (!(QApplication::mouseButtons() & Qt::LeftButton
)) {
352 void DolphinDetailsView::closeEvent(QCloseEvent
* event
)
354 if (m_clearAdditionalInfo
) {
355 disconnect(m_controller
->dolphinView(), SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
356 this, SLOT(updateColumnVisibility()));
358 KFileItemDelegate::InformationList info
;
359 info
.append(KFileItemDelegate::NoInformation
);
360 m_controller
->indicateAdditionalInfoChange(info
);
361 m_clearAdditionalInfo
= false;
363 QTreeView::closeEvent(event
);
366 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
368 QHeaderView
* headerView
= header();
369 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
372 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
374 QHeaderView
* headerView
= header();
375 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
378 void DolphinDetailsView::synchronizeSortingState(int column
)
380 // The sorting has already been changed in QTreeView if this slot is
381 // invoked, but Dolphin is not informed about this.
382 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
383 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
384 m_controller
->indicateSortingChange(sorting
);
385 m_controller
->indicateSortOrderChange(sortOrder
);
388 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
390 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
391 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
392 if (pos
.x() < nameColumnWidth
) {
393 m_controller
->emitItemEntered(itemForIndex(index
));
396 m_controller
->emitViewportEntered();
400 void DolphinDetailsView::updateElasticBand()
402 Q_ASSERT(m_showElasticBand
);
403 QRect
dirtyRegion(elasticBandRect());
404 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
405 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
406 setDirtyRegion(dirtyRegion
);
409 QRect
DolphinDetailsView::elasticBandRect() const
411 const QPoint
pos(contentsPos());
412 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
413 return QRect(topLeft
, m_elasticBandDestination
).normalized();
416 void DolphinDetailsView::zoomIn()
418 if (isZoomInPossible()) {
419 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
420 switch (settings
->iconSize()) {
421 case KIconLoader::SizeSmall
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
422 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeLarge
); break;
423 default: Q_ASSERT(false); break;
425 updateDecorationSize();
429 void DolphinDetailsView::zoomOut()
431 if (isZoomOutPossible()) {
432 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
433 switch (settings
->iconSize()) {
434 case KIconLoader::SizeLarge
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
435 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeSmall
); break;
436 default: Q_ASSERT(false); break;
438 updateDecorationSize();
442 void DolphinDetailsView::triggerItem(const QModelIndex
& index
)
444 const KFileItem item
= itemForIndex(index
);
445 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
446 m_controller
->triggerItem(item
);
449 m_controller
->emitItemEntered(item
);
453 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
456 popup
.addTitle(i18nc("@title:menu", "Columns"));
458 QHeaderView
* headerView
= header();
459 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
460 const int logicalIndex
= headerView
->logicalIndex(i
);
461 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
462 QAction
* action
= popup
.addAction(text
);
463 action
->setCheckable(true);
464 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
468 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
469 if (activatedAction
!= 0) {
470 const bool show
= activatedAction
->isChecked();
471 const int columnIndex
= activatedAction
->data().toInt();
473 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
474 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
476 Q_ASSERT(!list
.contains(info
));
479 Q_ASSERT(list
.contains(info
));
480 const int index
= list
.indexOf(info
);
481 list
.removeAt(index
);
484 m_controller
->indicateAdditionalInfoChange(list
);
485 setColumnHidden(columnIndex
, !show
);
489 void DolphinDetailsView::updateColumnVisibility()
491 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
492 const bool useDefaultColumns
= !isVisible() &&
494 list
.contains(KFileItemDelegate::NoInformation
));
495 if (useDefaultColumns
) {
496 // Using the details view without any additional information (-> additional column)
497 // makes no sense and leads to a usability problem as no viewport area is available
498 // anymore. Hence as fallback provide at least a size and date column.
500 list
.append(KFileItemDelegate::Size
);
501 list
.append(KFileItemDelegate::ModificationTime
);
502 m_controller
->indicateAdditionalInfoChange(list
);
503 m_clearAdditionalInfo
= true;
506 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
507 const KFileItemDelegate::Information info
= infoForColumn(i
);
508 const bool hide
= !list
.contains(info
);
509 if (isColumnHidden(i
) != hide
) {
510 setColumnHidden(i
, hide
);
511 m_clearAdditionalInfo
= false;
518 bool DolphinDetailsView::isZoomInPossible() const
520 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
521 return settings
->iconSize() < KIconLoader::SizeLarge
;
524 bool DolphinDetailsView::isZoomOutPossible() const
526 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
527 return settings
->iconSize() > KIconLoader::SizeSmall
;
530 void DolphinDetailsView::updateDecorationSize()
532 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
533 const int iconSize
= settings
->iconSize();
534 m_decorationSize
= QSize(iconSize
, iconSize
);
536 m_controller
->setZoomInPossible(isZoomInPossible());
537 m_controller
->setZoomOutPossible(isZoomOutPossible());
542 QPoint
DolphinDetailsView::contentsPos() const
544 // implementation note: the horizonal position is ignored currently, as no
545 // horizontal scrolling is done anyway during a selection
546 const QScrollBar
* scrollbar
= verticalScrollBar();
547 Q_ASSERT(scrollbar
!= 0);
549 const int maxHeight
= maximumViewportSize().height();
550 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
551 const int visibleHeight
= model()->rowCount() + 1 - height
;
552 if (visibleHeight
<= 0) {
556 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
560 KFileItem
DolphinDetailsView::itemForIndex(const QModelIndex
& index
) const
562 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
563 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
564 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
565 return dirModel
->itemForIndex(dirIndex
);
568 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
570 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
572 switch (columnIndex
) {
573 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
574 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
575 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
576 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
577 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
578 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
585 void DolphinDetailsView::resizeColumns()
587 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
588 // around 3 seconds for each (!) resize operation when having > 10000 items).
589 // This gets a problem especially when opening large directories, where several
590 // resize operations are received for showing the currently available items during
591 // loading (the application hangs around 20 seconds when loading > 10000 items).
593 QHeaderView
* headerView
= header();
594 QFontMetrics
fontMetrics(viewport()->font());
596 int columnWidth
[KDirModel::ColumnCount
];
597 columnWidth
[KDirModel::Size
] = fontMetrics
.width("00000 Items");
598 columnWidth
[KDirModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
599 columnWidth
[KDirModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
600 columnWidth
[KDirModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
601 columnWidth
[KDirModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
602 columnWidth
[KDirModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
604 int requiredWidth
= 0;
605 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
606 if (!isColumnHidden(i
)) {
607 columnWidth
[i
] += 20; // provide a default gap
608 requiredWidth
+= columnWidth
[i
];
609 headerView
->resizeSection(i
, columnWidth
[i
]);
613 // resize the name column in a way that the whole available width is used
614 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
615 if (columnWidth
[KDirModel::Name
] < 120) {
616 columnWidth
[KDirModel::Name
] = 120;
618 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
621 #include "dolphindetailsview.moc"