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 "selectionmanager.h"
29 #include "viewproperties.h"
31 #include "dolphin_detailsmodesettings.h"
32 #include "dolphin_generalsettings.h"
34 #include <kdirmodel.h>
38 #include <QAbstractProxyModel>
40 #include <QApplication>
41 #include <QHeaderView>
42 #include <QRubberBand>
46 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
, DolphinController
* controller
) :
49 m_controller(controller
),
53 m_showElasticBand(false),
54 m_elasticBandOrigin(),
55 m_elasticBandDestination()
57 Q_ASSERT(controller
!= 0);
60 setRootIsDecorated(false);
61 setSortingEnabled(true);
62 setUniformRowHeights(true);
63 setSelectionBehavior(SelectItems
);
64 setDragDropMode(QAbstractItemView::DragDrop
);
65 setDropIndicatorShown(false);
66 setAlternatingRowColors(true);
67 setItemsExpandable(false);
69 setMouseTracking(true);
70 viewport()->setAttribute(Qt::WA_Hover
);
72 const ViewProperties
props(controller
->url());
73 setSortIndicatorSection(props
.sorting());
74 setSortIndicatorOrder(props
.sortOrder());
76 QHeaderView
* headerView
= header();
77 connect(headerView
, SIGNAL(sectionClicked(int)),
78 this, SLOT(synchronizeSortingState(int)));
79 headerView
->setContextMenuPolicy(Qt::CustomContextMenu
);
80 connect(headerView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
81 this, SLOT(configureColumns(const QPoint
&)));
82 connect(headerView
, SIGNAL(sectionResized(int, int, int)),
83 this, SLOT(slotHeaderSectionResized(int, int, int)));
84 connect(headerView
, SIGNAL(sectionHandleDoubleClicked(int)),
85 this, SLOT(disableAutoResizing()));
87 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
88 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
89 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
90 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
92 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
93 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
94 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
95 // RETURN-key in keyPressEvent().
96 if (KGlobalSettings::singleClick()) {
97 connect(this, SIGNAL(clicked(const QModelIndex
&)),
98 this, SLOT(triggerItem(const QModelIndex
&)));
99 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
100 SelectionManager
* selManager
= new SelectionManager(this);
101 connect(selManager
, SIGNAL(selectionChanged()),
102 this, SLOT(requestActivation()));
105 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
106 this, SLOT(triggerItem(const QModelIndex
&)));
108 connect(this, SIGNAL(entered(const QModelIndex
&)),
109 this, SLOT(slotEntered(const QModelIndex
&)));
110 connect(this, SIGNAL(viewportEntered()),
111 controller
, SLOT(emitViewportEntered()));
112 connect(controller
, SIGNAL(zoomIn()),
113 this, SLOT(zoomIn()));
114 connect(controller
, SIGNAL(zoomOut()),
115 this, SLOT(zoomOut()));
116 connect(controller
->dolphinView(), SIGNAL(additionalInfoChanged()),
117 this, SLOT(updateColumnVisibility()));
119 // apply the details mode settings to the widget
120 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
121 Q_ASSERT(settings
!= 0);
123 m_font
= QFont(settings
->fontFamily(), settings
->fontSize());
125 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
126 // check avoids a division by zero happening on versions before 4.3.1.
127 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
129 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
130 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
131 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
134 updateDecorationSize();
139 DolphinDetailsView::~DolphinDetailsView()
143 bool DolphinDetailsView::event(QEvent
* event
)
145 if (event
->type() == QEvent::Polish
) {
146 QHeaderView
* headerView
= header();
147 headerView
->setResizeMode(QHeaderView::Interactive
);
148 headerView
->setMovable(false);
150 updateColumnVisibility();
152 hideColumn(DolphinModel::Rating
);
153 hideColumn(DolphinModel::Tags
);
155 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
156 // check avoids a division by zero happening on versions before 4.3.1.
157 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
159 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
160 else if (event
->type() == QEvent::UpdateRequest
) {
161 // a wheel movement will scroll 4 items
162 if (model()->rowCount() > 0) {
163 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
168 return QTreeView::event(event
);
171 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
173 QStyleOptionViewItem viewOptions
= QTreeView::viewOptions();
174 viewOptions
.font
= m_font
;
175 viewOptions
.showDecorationSelected
= true;
176 viewOptions
.decorationSize
= m_decorationSize
;
180 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
182 QTreeView::contextMenuEvent(event
);
183 m_controller
->triggerContextMenuRequest(event
->pos());
186 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
188 m_controller
->requestActivation();
190 QTreeView::mousePressEvent(event
);
192 const QModelIndex index
= indexAt(event
->pos());
193 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
194 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
195 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
200 if (event
->button() == Qt::LeftButton
) {
201 m_showElasticBand
= true;
203 const QPoint
pos(contentsPos());
204 m_elasticBandOrigin
= event
->pos();
205 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
206 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
207 m_elasticBandDestination
= event
->pos();
211 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
213 if (m_showElasticBand
) {
214 const QPoint mousePos
= event
->pos();
215 const QModelIndex index
= indexAt(mousePos
);
216 if (!index
.isValid()) {
217 // the destination of the selection rectangle is above the viewport. In this
218 // case QTreeView does no selection at all, which is not the wanted behavior
219 // in Dolphin -> select all items within the elastic band rectangle
222 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
223 QRect selRect
= QRect(m_elasticBandOrigin
, m_elasticBandDestination
).normalized();
224 const QRect
nameColumnsRect(0, 0, nameColumnWidth
, viewport()->height());
225 selRect
= nameColumnsRect
.intersected(selRect
);
227 setSelection(selRect
, QItemSelectionModel::Select
);
230 QTreeView::mouseMoveEvent(event
);
233 QTreeView::mouseMoveEvent(event
);
237 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
239 QTreeView::mouseReleaseEvent(event
);
240 if (m_showElasticBand
) {
242 m_showElasticBand
= false;
246 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions
)
248 DragAndDropHelper::startDrag(this, supportedActions
);
251 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
253 if (event
->mimeData()->hasUrls()) {
254 event
->acceptProposedAction();
257 if (m_showElasticBand
) {
259 m_showElasticBand
= false;
264 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
266 QTreeView::dragLeaveEvent(event
);
268 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
270 setDirtyRegion(m_dropRect
);
273 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
275 QTreeView::dragMoveEvent(event
);
277 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
278 setDirtyRegion(m_dropRect
);
279 const QModelIndex index
= indexAt(event
->pos());
280 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
284 const KFileItem item
= itemForIndex(index
);
285 if (!item
.isNull() && item
.isDir()) {
286 m_dropRect
= visualRect(index
);
288 m_dropRect
.setSize(QSize()); // set as invalid
290 setDirtyRegion(m_dropRect
);
293 if (event
->mimeData()->hasUrls()) {
294 // accept url drops, independently from the destination item
295 event
->acceptProposedAction();
299 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
301 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
302 if (!urls
.isEmpty()) {
303 event
->acceptProposedAction();
304 const QModelIndex index
= indexAt(event
->pos());
306 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
307 item
= itemForIndex(index
);
309 m_controller
->indicateDroppedUrls(urls
,
313 QTreeView::dropEvent(event
);
317 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
319 QTreeView::paintEvent(event
);
320 if (m_showElasticBand
) {
321 // The following code has been taken from QListView
322 // and adapted to DolphinDetailsView.
323 // (C) 1992-2007 Trolltech ASA
324 QStyleOptionRubberBand opt
;
326 opt
.shape
= QRubberBand::Rectangle
;
328 opt
.rect
= elasticBandRect();
330 QPainter
painter(viewport());
332 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
336 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
338 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
339 DragAndDropHelper::drawHoverIndication(this, m_dropRect
, brush
);
343 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
345 QTreeView::keyPressEvent(event
);
347 const QItemSelectionModel
* selModel
= selectionModel();
348 const QModelIndex currentIndex
= selModel
->currentIndex();
349 const bool trigger
= currentIndex
.isValid()
350 && (event
->key() == Qt::Key_Return
)
351 && (selModel
->selectedIndexes().count() <= 1);
353 triggerItem(currentIndex
);
357 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
359 QTreeView::resizeEvent(event
);
365 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
367 QHeaderView
* headerView
= header();
368 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
371 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
373 QHeaderView
* headerView
= header();
374 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
377 void DolphinDetailsView::synchronizeSortingState(int column
)
379 // The sorting has already been changed in QTreeView if this slot is
380 // invoked, but Dolphin is not informed about this.
381 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
382 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
383 m_controller
->indicateSortingChange(sorting
);
384 m_controller
->indicateSortOrderChange(sortOrder
);
387 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
389 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
390 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
391 if (pos
.x() < nameColumnWidth
) {
392 m_controller
->emitItemEntered(itemForIndex(index
));
395 m_controller
->emitViewportEntered();
399 void DolphinDetailsView::updateElasticBand()
401 if (m_showElasticBand
) {
402 QRect
dirtyRegion(elasticBandRect());
403 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
404 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
405 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 const KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
492 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
493 const KFileItemDelegate::Information info
= infoForColumn(i
);
494 const bool hide
= !list
.contains(info
);
495 if (isColumnHidden(i
) != hide
) {
496 setColumnHidden(i
, hide
);
503 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex
, int oldSize
, int newSize
)
505 Q_UNUSED(logicalIndex
);
508 if (QApplication::mouseButtons() & Qt::LeftButton
) {
509 disableAutoResizing();
513 void DolphinDetailsView::disableAutoResizing()
515 m_autoResize
= false;
518 void DolphinDetailsView::requestActivation()
520 m_controller
->requestActivation();
523 bool DolphinDetailsView::isZoomInPossible() const
525 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
526 return settings
->iconSize() < KIconLoader::SizeLarge
;
529 bool DolphinDetailsView::isZoomOutPossible() const
531 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
532 return settings
->iconSize() > KIconLoader::SizeSmall
;
535 void DolphinDetailsView::updateDecorationSize()
537 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
538 const int iconSize
= settings
->iconSize();
539 setIconSize(QSize(iconSize
, iconSize
));
540 m_decorationSize
= QSize(iconSize
, iconSize
);
542 m_controller
->setZoomInPossible(isZoomInPossible());
543 m_controller
->setZoomOutPossible(isZoomOutPossible());
548 QPoint
DolphinDetailsView::contentsPos() const
550 // implementation note: the horizonal position is ignored currently, as no
551 // horizontal scrolling is done anyway during a selection
552 const QScrollBar
* scrollbar
= verticalScrollBar();
553 Q_ASSERT(scrollbar
!= 0);
555 const int maxHeight
= maximumViewportSize().height();
556 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
557 const int visibleHeight
= model()->rowCount() + 1 - height
;
558 if (visibleHeight
<= 0) {
562 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
566 KFileItem
DolphinDetailsView::itemForIndex(const QModelIndex
& index
) const
568 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
569 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
570 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
571 return dirModel
->itemForIndex(dirIndex
);
574 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
576 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
578 switch (columnIndex
) {
579 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
580 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
581 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
582 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
583 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
584 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
591 void DolphinDetailsView::resizeColumns()
593 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
594 // around 3 seconds for each (!) resize operation when having > 10000 items).
595 // This gets a problem especially when opening large directories, where several
596 // resize operations are received for showing the currently available items during
597 // loading (the application hangs around 20 seconds when loading > 10000 items).
599 QHeaderView
* headerView
= header();
600 QFontMetrics
fontMetrics(viewport()->font());
602 int columnWidth
[KDirModel::ColumnCount
];
603 columnWidth
[KDirModel::Size
] = fontMetrics
.width("00000 Items");
604 columnWidth
[KDirModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
605 columnWidth
[KDirModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
606 columnWidth
[KDirModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
607 columnWidth
[KDirModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
608 columnWidth
[KDirModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
610 int requiredWidth
= 0;
611 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
612 if (!isColumnHidden(i
)) {
613 columnWidth
[i
] += 20; // provide a default gap
614 requiredWidth
+= columnWidth
[i
];
615 headerView
->resizeSection(i
, columnWidth
[i
]);
619 // resize the name column in a way that the whole available width is used
620 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
621 if (columnWidth
[KDirModel::Name
] < 120) {
622 columnWidth
[KDirModel::Name
] = 120;
624 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
627 #include "dolphindetailsview.moc"