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_expandingTogglePressed(false),
50 m_controller(controller
),
51 m_selectionManager(0),
54 m_showElasticBand(false),
55 m_elasticBandOrigin(),
56 m_elasticBandDestination()
58 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
59 Q_ASSERT(settings
!= 0);
60 Q_ASSERT(controller
!= 0);
63 setSortingEnabled(true);
64 setUniformRowHeights(true);
65 setSelectionBehavior(SelectItems
);
66 setDragDropMode(QAbstractItemView::DragDrop
);
67 setDropIndicatorShown(false);
68 setAlternatingRowColors(true);
69 setRootIsDecorated(settings
->expandableFolders());
70 setItemsExpandable(settings
->expandableFolders());
71 setEditTriggers(QAbstractItemView::NoEditTriggers
);
73 setMouseTracking(true);
75 const ViewProperties
props(controller
->url());
76 setSortIndicatorSection(props
.sorting());
77 setSortIndicatorOrder(props
.sortOrder());
79 QHeaderView
* headerView
= header();
80 connect(headerView
, SIGNAL(sectionClicked(int)),
81 this, SLOT(synchronizeSortingState(int)));
82 headerView
->setContextMenuPolicy(Qt::CustomContextMenu
);
83 connect(headerView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
84 this, SLOT(configureColumns(const QPoint
&)));
85 connect(headerView
, SIGNAL(sectionResized(int, int, int)),
86 this, SLOT(slotHeaderSectionResized(int, int, int)));
87 connect(headerView
, SIGNAL(sectionHandleDoubleClicked(int)),
88 this, SLOT(disableAutoResizing()));
90 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
91 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
92 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
93 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
95 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
96 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
97 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
98 // RETURN-key in keyPressEvent().
99 if (KGlobalSettings::singleClick()) {
100 connect(this, SIGNAL(clicked(const QModelIndex
&)),
101 controller
, SLOT(triggerItem(const QModelIndex
&)));
102 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
103 m_selectionManager
= new SelectionManager(this);
104 connect(m_selectionManager
, SIGNAL(selectionChanged()),
105 this, SLOT(requestActivation()));
106 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
107 m_selectionManager
, SLOT(reset()));
110 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
111 controller
, SLOT(triggerItem(const QModelIndex
&)));
113 connect(this, SIGNAL(entered(const QModelIndex
&)),
114 this, SLOT(slotEntered(const QModelIndex
&)));
115 connect(this, SIGNAL(viewportEntered()),
116 controller
, SLOT(emitViewportEntered()));
117 connect(controller
, SIGNAL(zoomIn()),
118 this, SLOT(zoomIn()));
119 connect(controller
, SIGNAL(zoomOut()),
120 this, SLOT(zoomOut()));
121 connect(controller
->dolphinView(), SIGNAL(additionalInfoChanged()),
122 this, SLOT(updateColumnVisibility()));
124 if (settings
->useSystemFont()) {
125 m_font
= KGlobalSettings::generalFont();
127 m_font
= QFont(settings
->fontFamily(),
128 settings
->fontSize(),
129 settings
->fontWeight(),
130 settings
->italicFont());
133 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
134 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
136 updateDecorationSize();
139 viewport()->installEventFilter(this);
141 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
142 this, SLOT(updateFont()));
145 DolphinDetailsView::~DolphinDetailsView()
149 bool DolphinDetailsView::event(QEvent
* event
)
151 if (event
->type() == QEvent::Polish
) {
152 QHeaderView
* headerView
= header();
153 headerView
->setResizeMode(QHeaderView::Interactive
);
154 headerView
->setMovable(false);
156 updateColumnVisibility();
158 hideColumn(DolphinModel::Rating
);
159 hideColumn(DolphinModel::Tags
);
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);
167 return QTreeView::event(event
);
170 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
172 QStyleOptionViewItem viewOptions
= QTreeView::viewOptions();
173 viewOptions
.font
= m_font
;
174 viewOptions
.showDecorationSelected
= true;
175 viewOptions
.decorationSize
= m_decorationSize
;
179 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
181 QTreeView::contextMenuEvent(event
);
182 m_controller
->triggerContextMenuRequest(event
->pos());
185 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
187 m_controller
->requestActivation();
189 QTreeView::mousePressEvent(event
);
191 m_expandingTogglePressed
= false;
192 const QModelIndex index
= indexAt(event
->pos());
193 const bool updateState
= index
.isValid() &&
194 (index
.column() == DolphinModel::Name
) &&
195 (event
->button() == Qt::LeftButton
);
197 // TODO: See comment in DolphinIconsView::mousePressEvent(). Only update
198 // the state if no expanding/collapsing area has been hit:
199 const QRect rect
= visualRect(index
);
200 if (event
->pos().x() >= rect
.x() + indentation()) {
201 setState(QAbstractItemView::DraggingState
);
203 m_expandingTogglePressed
= true;
207 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
208 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
209 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
214 if ((event
->button() == Qt::LeftButton
) && !m_expandingTogglePressed
) {
215 m_showElasticBand
= true;
217 const QPoint
pos(contentsPos());
218 m_elasticBandOrigin
= event
->pos();
219 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
220 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
221 m_elasticBandDestination
= event
->pos();
225 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
227 if (m_showElasticBand
) {
228 const QPoint mousePos
= event
->pos();
229 const QModelIndex index
= indexAt(mousePos
);
230 if (!index
.isValid()) {
231 // the destination of the selection rectangle is above the viewport. In this
232 // case QTreeView does no selection at all, which is not the wanted behavior
233 // in Dolphin -> select all items within the elastic band rectangle
236 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
237 QRect selRect
= QRect(m_elasticBandOrigin
, m_elasticBandDestination
).normalized();
238 const QRect
nameColumnsRect(0, 0, nameColumnWidth
, viewport()->height());
239 selRect
= nameColumnsRect
.intersected(selRect
);
241 setSelection(selRect
, QItemSelectionModel::Select
);
244 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
245 // as the Qt-issue #199631 has been fixed.
246 // QTreeView::mouseMoveEvent(event);
247 QAbstractItemView::mouseMoveEvent(event
);
250 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
251 // as the Qt-issue #199631 has been fixed.
252 // QTreeView::mouseMoveEvent(event);
253 QAbstractItemView::mouseMoveEvent(event
);
256 if (m_expandingTogglePressed
) {
257 // Per default QTreeView starts either a selection or a drag operation when dragging
258 // the expanding toggle button (Qt-issue - see TODO comment in DolphinIconsView::mousePressEvent()).
259 // Turn off this behavior in Dolphin to stay predictable:
261 setState(QAbstractItemView::NoState
);
265 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
267 QTreeView::mouseReleaseEvent(event
);
268 m_expandingTogglePressed
= false;
269 if (m_showElasticBand
) {
271 m_showElasticBand
= false;
275 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions
)
277 DragAndDropHelper::startDrag(this, supportedActions
);
278 m_showElasticBand
= false;
281 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
283 if (event
->mimeData()->hasUrls()) {
284 event
->acceptProposedAction();
287 if (m_showElasticBand
) {
289 m_showElasticBand
= false;
293 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
295 QTreeView::dragLeaveEvent(event
);
296 setDirtyRegion(m_dropRect
);
299 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
301 QTreeView::dragMoveEvent(event
);
303 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
304 setDirtyRegion(m_dropRect
);
305 const QModelIndex index
= indexAt(event
->pos());
306 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
307 const KFileItem item
= m_controller
->itemForIndex(index
);
308 if (!item
.isNull() && item
.isDir()) {
309 m_dropRect
= visualRect(index
);
311 m_dropRect
.setSize(QSize()); // set as invalid
313 setDirtyRegion(m_dropRect
);
316 if (event
->mimeData()->hasUrls()) {
317 // accept url drops, independently from the destination item
318 event
->acceptProposedAction();
322 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
324 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
325 if (!urls
.isEmpty()) {
326 event
->acceptProposedAction();
327 const QModelIndex index
= indexAt(event
->pos());
329 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
330 item
= m_controller
->itemForIndex(index
);
332 m_controller
->indicateDroppedUrls(urls
,
336 QTreeView::dropEvent(event
);
339 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
341 QTreeView::paintEvent(event
);
342 if (m_showElasticBand
) {
343 // The following code has been taken from QListView
344 // and adapted to DolphinDetailsView.
345 // (C) 1992-2007 Trolltech ASA
346 QStyleOptionRubberBand opt
;
348 opt
.shape
= QRubberBand::Rectangle
;
350 opt
.rect
= elasticBandRect();
352 QPainter
painter(viewport());
354 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
359 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
361 QTreeView::keyPressEvent(event
);
362 m_controller
->handleKeyPressEvent(event
);
365 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
370 QTreeView::resizeEvent(event
);
373 void DolphinDetailsView::wheelEvent(QWheelEvent
* event
)
375 if (m_selectionManager
!= 0) {
376 m_selectionManager
->reset();
379 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
380 if (event
->modifiers() & Qt::ControlModifier
) {
385 QTreeView::wheelEvent(event
);
388 void DolphinDetailsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
390 QTreeView::currentChanged(current
, previous
);
392 // Stay consistent with QListView: When changing the current index by key presses,
393 // also change the selection.
394 if (QApplication::mouseButtons() == Qt::NoButton
) {
395 selectionModel()->select(current
, QItemSelectionModel::ClearAndSelect
);
399 bool DolphinDetailsView::eventFilter(QObject
* watched
, QEvent
* event
)
401 if ((watched
== viewport()) && (event
->type() == QEvent::Leave
)) {
402 // if the mouse is above an item and moved very fast outside the widget,
403 // no viewportEntered() signal might be emitted although the mouse has been moved
404 // above the viewport
405 m_controller
->emitViewportEntered();
408 return QTreeView::eventFilter(watched
, event
);
411 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
413 QHeaderView
* headerView
= header();
414 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
417 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
419 QHeaderView
* headerView
= header();
420 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
423 void DolphinDetailsView::synchronizeSortingState(int column
)
425 // The sorting has already been changed in QTreeView if this slot is
426 // invoked, but Dolphin is not informed about this.
427 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
428 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
429 m_controller
->indicateSortingChange(sorting
);
430 m_controller
->indicateSortOrderChange(sortOrder
);
433 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
435 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
436 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
437 if (pos
.x() < nameColumnWidth
) {
438 m_controller
->emitItemEntered(index
);
441 m_controller
->emitViewportEntered();
445 void DolphinDetailsView::updateElasticBand()
447 if (m_showElasticBand
) {
448 QRect
dirtyRegion(elasticBandRect());
449 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
450 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
451 setDirtyRegion(dirtyRegion
);
455 QRect
DolphinDetailsView::elasticBandRect() const
457 const QPoint
pos(contentsPos());
458 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
459 return QRect(topLeft
, m_elasticBandDestination
).normalized();
462 void DolphinDetailsView::zoomIn()
464 if (isZoomInPossible()) {
465 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
466 switch (settings
->iconSize()) {
467 case KIconLoader::SizeSmall
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
468 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeLarge
); break;
469 default: Q_ASSERT(false); break;
471 updateDecorationSize();
475 void DolphinDetailsView::zoomOut()
477 if (isZoomOutPossible()) {
478 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
479 switch (settings
->iconSize()) {
480 case KIconLoader::SizeLarge
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
481 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeSmall
); break;
482 default: Q_ASSERT(false); break;
484 updateDecorationSize();
488 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
491 popup
.addTitle(i18nc("@title:menu", "Columns"));
493 QHeaderView
* headerView
= header();
494 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
495 const int logicalIndex
= headerView
->logicalIndex(i
);
496 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
497 QAction
* action
= popup
.addAction(text
);
498 action
->setCheckable(true);
499 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
503 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
504 if (activatedAction
!= 0) {
505 const bool show
= activatedAction
->isChecked();
506 const int columnIndex
= activatedAction
->data().toInt();
508 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
509 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
511 Q_ASSERT(!list
.contains(info
));
514 Q_ASSERT(list
.contains(info
));
515 const int index
= list
.indexOf(info
);
516 list
.removeAt(index
);
519 m_controller
->indicateAdditionalInfoChange(list
);
520 setColumnHidden(columnIndex
, !show
);
524 void DolphinDetailsView::updateColumnVisibility()
526 const KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
527 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
528 const KFileItemDelegate::Information info
= infoForColumn(i
);
529 const bool hide
= !list
.contains(info
);
530 if (isColumnHidden(i
) != hide
) {
531 setColumnHidden(i
, hide
);
538 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex
, int oldSize
, int newSize
)
540 Q_UNUSED(logicalIndex
);
543 if (QApplication::mouseButtons() & Qt::LeftButton
) {
544 disableAutoResizing();
548 void DolphinDetailsView::disableAutoResizing()
550 m_autoResize
= false;
553 void DolphinDetailsView::requestActivation()
555 m_controller
->requestActivation();
558 void DolphinDetailsView::updateFont()
560 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
561 Q_ASSERT(settings
!= 0);
563 if (settings
->useSystemFont()) {
564 m_font
= KGlobalSettings::generalFont();
568 bool DolphinDetailsView::isZoomInPossible() const
570 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
571 return settings
->iconSize() < KIconLoader::SizeLarge
;
574 bool DolphinDetailsView::isZoomOutPossible() const
576 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
577 return settings
->iconSize() > KIconLoader::SizeSmall
;
580 void DolphinDetailsView::updateDecorationSize()
582 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
583 const int iconSize
= settings
->iconSize();
584 setIconSize(QSize(iconSize
, iconSize
));
585 m_decorationSize
= QSize(iconSize
, iconSize
);
587 m_controller
->setZoomInPossible(isZoomInPossible());
588 m_controller
->setZoomOutPossible(isZoomOutPossible());
590 if (m_selectionManager
!= 0) {
591 m_selectionManager
->reset();
597 QPoint
DolphinDetailsView::contentsPos() const
599 // implementation note: the horizonal position is ignored currently, as no
600 // horizontal scrolling is done anyway during a selection
601 const QScrollBar
* scrollbar
= verticalScrollBar();
602 Q_ASSERT(scrollbar
!= 0);
604 const int maxHeight
= maximumViewportSize().height();
605 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
606 const int visibleHeight
= model()->rowCount() + 1 - height
;
607 if (visibleHeight
<= 0) {
611 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
615 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
617 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
619 switch (columnIndex
) {
620 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
621 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
622 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
623 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
624 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
625 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
632 void DolphinDetailsView::resizeColumns()
634 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
635 // around 3 seconds for each (!) resize operation when having > 10000 items).
636 // This gets a problem especially when opening large directories, where several
637 // resize operations are received for showing the currently available items during
638 // loading (the application hangs around 20 seconds when loading > 10000 items).
640 QHeaderView
* headerView
= header();
641 QFontMetrics
fontMetrics(viewport()->font());
643 int columnWidth
[KDirModel::ColumnCount
];
644 columnWidth
[KDirModel::Size
] = fontMetrics
.width("00000 Items");
645 columnWidth
[KDirModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
646 columnWidth
[KDirModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
647 columnWidth
[KDirModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
648 columnWidth
[KDirModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
649 columnWidth
[KDirModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
651 int requiredWidth
= 0;
652 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
653 if (!isColumnHidden(i
)) {
654 columnWidth
[i
] += 20; // provide a default gap
655 requiredWidth
+= columnWidth
[i
];
656 headerView
->resizeSection(i
, columnWidth
[i
]);
660 // resize the name column in a way that the whole available width is used
661 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
662 if (columnWidth
[KDirModel::Name
] < 120) {
663 columnWidth
[KDirModel::Name
] = 120;
665 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
668 #include "dolphindetailsview.moc"