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),
51 m_controller(controller
),
52 m_selectionManager(0),
55 m_showElasticBand(false),
56 m_elasticBandOrigin(),
57 m_elasticBandDestination()
59 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
60 Q_ASSERT(settings
!= 0);
61 Q_ASSERT(controller
!= 0);
64 setSortingEnabled(true);
65 setUniformRowHeights(true);
66 setSelectionBehavior(SelectItems
);
67 setDragDropMode(QAbstractItemView::DragDrop
);
68 setDropIndicatorShown(false);
69 setAlternatingRowColors(true);
70 setRootIsDecorated(settings
->expandableFolders());
71 setItemsExpandable(settings
->expandableFolders());
72 setEditTriggers(QAbstractItemView::NoEditTriggers
);
74 setMouseTracking(true);
76 const ViewProperties
props(controller
->url());
77 setSortIndicatorSection(props
.sorting());
78 setSortIndicatorOrder(props
.sortOrder());
80 QHeaderView
* headerView
= header();
81 connect(headerView
, SIGNAL(sectionClicked(int)),
82 this, SLOT(synchronizeSortingState(int)));
83 headerView
->setContextMenuPolicy(Qt::CustomContextMenu
);
84 connect(headerView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
85 this, SLOT(configureColumns(const QPoint
&)));
86 connect(headerView
, SIGNAL(sectionResized(int, int, int)),
87 this, SLOT(slotHeaderSectionResized(int, int, int)));
88 connect(headerView
, SIGNAL(sectionHandleDoubleClicked(int)),
89 this, SLOT(disableAutoResizing()));
91 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
92 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
93 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
94 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
96 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
97 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
98 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
99 // RETURN-key in keyPressEvent().
100 if (KGlobalSettings::singleClick()) {
101 connect(this, SIGNAL(clicked(const QModelIndex
&)),
102 controller
, SLOT(triggerItem(const QModelIndex
&)));
103 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
104 m_selectionManager
= new SelectionManager(this);
105 connect(m_selectionManager
, SIGNAL(selectionChanged()),
106 this, SLOT(requestActivation()));
107 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
108 m_selectionManager
, SLOT(reset()));
111 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
112 controller
, SLOT(triggerItem(const QModelIndex
&)));
114 connect(this, SIGNAL(entered(const QModelIndex
&)),
115 this, SLOT(slotEntered(const QModelIndex
&)));
116 connect(this, SIGNAL(viewportEntered()),
117 controller
, SLOT(emitViewportEntered()));
118 connect(controller
, SIGNAL(zoomIn()),
119 this, SLOT(zoomIn()));
120 connect(controller
, SIGNAL(zoomOut()),
121 this, SLOT(zoomOut()));
122 connect(controller
->dolphinView(), SIGNAL(additionalInfoChanged()),
123 this, SLOT(updateColumnVisibility()));
125 if (settings
->useSystemFont()) {
126 m_font
= KGlobalSettings::generalFont();
128 m_font
= QFont(settings
->fontFamily(),
129 settings
->fontSize(),
130 settings
->fontWeight(),
131 settings
->italicFont());
134 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
135 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
137 updateDecorationSize();
140 viewport()->installEventFilter(this);
142 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
143 this, SLOT(updateFont()));
146 DolphinDetailsView::~DolphinDetailsView()
150 bool DolphinDetailsView::event(QEvent
* event
)
152 if (event
->type() == QEvent::Polish
) {
153 QHeaderView
* headerView
= header();
154 headerView
->setResizeMode(QHeaderView::Interactive
);
155 headerView
->setMovable(false);
157 updateColumnVisibility();
159 hideColumn(DolphinModel::Rating
);
160 hideColumn(DolphinModel::Tags
);
161 } else if (event
->type() == QEvent::UpdateRequest
) {
162 // a wheel movement will scroll 4 items
163 if (model()->rowCount() > 0) {
164 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 m_expandingTogglePressed
= false;
193 const QModelIndex index
= indexAt(event
->pos());
194 const bool updateState
= index
.isValid() &&
195 (index
.column() == DolphinModel::Name
) &&
196 (event
->button() == Qt::LeftButton
);
198 // TODO: See comment in DolphinIconsView::mousePressEvent(). Only update
199 // the state if no expanding/collapsing area has been hit:
200 const QRect rect
= visualRect(index
);
201 if (event
->pos().x() >= rect
.x() + indentation()) {
202 setState(QAbstractItemView::DraggingState
);
204 m_expandingTogglePressed
= true;
208 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
209 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
210 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
215 if ((event
->button() == Qt::LeftButton
) && !m_expandingTogglePressed
) {
216 m_showElasticBand
= true;
218 const QPoint
pos(contentsPos());
219 m_elasticBandOrigin
= event
->pos();
220 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
221 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
222 m_elasticBandDestination
= event
->pos();
226 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
228 if (m_showElasticBand
) {
229 const QPoint mousePos
= event
->pos();
230 const QModelIndex index
= indexAt(mousePos
);
231 if (!index
.isValid()) {
232 // the destination of the selection rectangle is above the viewport. In this
233 // case QTreeView does no selection at all, which is not the wanted behavior
234 // in Dolphin -> select all items within the elastic band rectangle
237 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
238 QRect selRect
= QRect(m_elasticBandOrigin
, m_elasticBandDestination
).normalized();
239 const QRect
nameColumnsRect(0, 0, nameColumnWidth
, viewport()->height());
240 selRect
= nameColumnsRect
.intersected(selRect
);
242 setSelection(selRect
, QItemSelectionModel::Select
);
245 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
246 // as the Qt-issue #199631 has been fixed.
247 // QTreeView::mouseMoveEvent(event);
248 QAbstractItemView::mouseMoveEvent(event
);
251 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
252 // as the Qt-issue #199631 has been fixed.
253 // QTreeView::mouseMoveEvent(event);
254 QAbstractItemView::mouseMoveEvent(event
);
257 if (m_expandingTogglePressed
) {
258 // Per default QTreeView starts either a selection or a drag operation when dragging
259 // the expanding toggle button (Qt-issue - see TODO comment in DolphinIconsView::mousePressEvent()).
260 // Turn off this behavior in Dolphin to stay predictable:
262 setState(QAbstractItemView::NoState
);
266 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
268 QTreeView::mouseReleaseEvent(event
);
269 m_expandingTogglePressed
= false;
270 if (m_showElasticBand
) {
272 m_showElasticBand
= false;
276 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions
)
278 DragAndDropHelper::startDrag(this, supportedActions
);
279 m_showElasticBand
= false;
282 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
284 if (event
->mimeData()->hasUrls()) {
285 event
->acceptProposedAction();
288 if (m_showElasticBand
) {
290 m_showElasticBand
= false;
294 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
296 QTreeView::dragLeaveEvent(event
);
297 setDirtyRegion(m_dropRect
);
300 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
302 QTreeView::dragMoveEvent(event
);
304 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
305 setDirtyRegion(m_dropRect
);
306 const QModelIndex index
= indexAt(event
->pos());
307 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
308 const KFileItem item
= m_controller
->itemForIndex(index
);
309 if (!item
.isNull() && item
.isDir()) {
310 m_dropRect
= visualRect(index
);
312 m_dropRect
.setSize(QSize()); // set as invalid
314 setDirtyRegion(m_dropRect
);
317 if (event
->mimeData()->hasUrls()) {
318 // accept url drops, independently from the destination item
319 event
->acceptProposedAction();
323 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
325 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
326 if (!urls
.isEmpty()) {
327 event
->acceptProposedAction();
328 const QModelIndex index
= indexAt(event
->pos());
330 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
331 item
= m_controller
->itemForIndex(index
);
333 m_controller
->indicateDroppedUrls(urls
,
337 QTreeView::dropEvent(event
);
340 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
342 QTreeView::paintEvent(event
);
343 if (m_showElasticBand
) {
344 // The following code has been taken from QListView
345 // and adapted to DolphinDetailsView.
346 // (C) 1992-2007 Trolltech ASA
347 QStyleOptionRubberBand opt
;
349 opt
.shape
= QRubberBand::Rectangle
;
351 opt
.rect
= elasticBandRect();
353 QPainter
painter(viewport());
355 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
360 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
362 QTreeView::keyPressEvent(event
);
363 m_controller
->handleKeyPressEvent(event
);
367 void DolphinDetailsView::keyReleaseEvent(QKeyEvent
* event
)
369 QTreeView::keyReleaseEvent(event
);
370 m_keyPressed
= false;
373 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
378 QTreeView::resizeEvent(event
);
381 void DolphinDetailsView::wheelEvent(QWheelEvent
* event
)
383 if (m_selectionManager
!= 0) {
384 m_selectionManager
->reset();
387 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
388 if (event
->modifiers() & Qt::ControlModifier
) {
393 QTreeView::wheelEvent(event
);
396 void DolphinDetailsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
398 QTreeView::currentChanged(current
, previous
);
400 // Stay consistent with QListView: When changing the current index by key presses,
401 // also change the selection.
403 selectionModel()->select(current
, QItemSelectionModel::ClearAndSelect
);
407 bool DolphinDetailsView::eventFilter(QObject
* watched
, QEvent
* event
)
409 if ((watched
== viewport()) && (event
->type() == QEvent::Leave
)) {
410 // if the mouse is above an item and moved very fast outside the widget,
411 // no viewportEntered() signal might be emitted although the mouse has been moved
412 // above the viewport
413 m_controller
->emitViewportEntered();
416 return QTreeView::eventFilter(watched
, event
);
419 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
421 QHeaderView
* headerView
= header();
422 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
425 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
427 QHeaderView
* headerView
= header();
428 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
431 void DolphinDetailsView::synchronizeSortingState(int column
)
433 // The sorting has already been changed in QTreeView if this slot is
434 // invoked, but Dolphin is not informed about this.
435 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
436 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
437 m_controller
->indicateSortingChange(sorting
);
438 m_controller
->indicateSortOrderChange(sortOrder
);
441 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
443 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
444 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
445 if (pos
.x() < nameColumnWidth
) {
446 m_controller
->emitItemEntered(index
);
449 m_controller
->emitViewportEntered();
453 void DolphinDetailsView::updateElasticBand()
455 if (m_showElasticBand
) {
456 QRect
dirtyRegion(elasticBandRect());
457 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
458 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
459 setDirtyRegion(dirtyRegion
);
463 QRect
DolphinDetailsView::elasticBandRect() const
465 const QPoint
pos(contentsPos());
466 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
467 return QRect(topLeft
, m_elasticBandDestination
).normalized();
470 void DolphinDetailsView::zoomIn()
472 if (isZoomInPossible()) {
473 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
474 switch (settings
->iconSize()) {
475 case KIconLoader::SizeSmall
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
476 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeLarge
); break;
477 default: Q_ASSERT(false); break;
479 updateDecorationSize();
483 void DolphinDetailsView::zoomOut()
485 if (isZoomOutPossible()) {
486 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
487 switch (settings
->iconSize()) {
488 case KIconLoader::SizeLarge
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
489 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeSmall
); break;
490 default: Q_ASSERT(false); break;
492 updateDecorationSize();
496 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
499 popup
.addTitle(i18nc("@title:menu", "Columns"));
501 QHeaderView
* headerView
= header();
502 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
503 const int logicalIndex
= headerView
->logicalIndex(i
);
504 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
505 QAction
* action
= popup
.addAction(text
);
506 action
->setCheckable(true);
507 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
511 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
512 if (activatedAction
!= 0) {
513 const bool show
= activatedAction
->isChecked();
514 const int columnIndex
= activatedAction
->data().toInt();
516 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
517 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
519 Q_ASSERT(!list
.contains(info
));
522 Q_ASSERT(list
.contains(info
));
523 const int index
= list
.indexOf(info
);
524 list
.removeAt(index
);
527 m_controller
->indicateAdditionalInfoChange(list
);
528 setColumnHidden(columnIndex
, !show
);
532 void DolphinDetailsView::updateColumnVisibility()
534 const KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
535 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
536 const KFileItemDelegate::Information info
= infoForColumn(i
);
537 const bool hide
= !list
.contains(info
);
538 if (isColumnHidden(i
) != hide
) {
539 setColumnHidden(i
, hide
);
546 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex
, int oldSize
, int newSize
)
548 Q_UNUSED(logicalIndex
);
551 if (QApplication::mouseButtons() & Qt::LeftButton
) {
552 disableAutoResizing();
556 void DolphinDetailsView::disableAutoResizing()
558 m_autoResize
= false;
561 void DolphinDetailsView::requestActivation()
563 m_controller
->requestActivation();
566 void DolphinDetailsView::updateFont()
568 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
569 Q_ASSERT(settings
!= 0);
571 if (settings
->useSystemFont()) {
572 m_font
= KGlobalSettings::generalFont();
576 bool DolphinDetailsView::isZoomInPossible() const
578 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
579 return settings
->iconSize() < KIconLoader::SizeLarge
;
582 bool DolphinDetailsView::isZoomOutPossible() const
584 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
585 return settings
->iconSize() > KIconLoader::SizeSmall
;
588 void DolphinDetailsView::updateDecorationSize()
590 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
591 const int iconSize
= settings
->iconSize();
592 setIconSize(QSize(iconSize
, iconSize
));
593 m_decorationSize
= QSize(iconSize
, iconSize
);
595 m_controller
->setZoomInPossible(isZoomInPossible());
596 m_controller
->setZoomOutPossible(isZoomOutPossible());
598 if (m_selectionManager
!= 0) {
599 m_selectionManager
->reset();
605 QPoint
DolphinDetailsView::contentsPos() const
607 // implementation note: the horizonal position is ignored currently, as no
608 // horizontal scrolling is done anyway during a selection
609 const QScrollBar
* scrollbar
= verticalScrollBar();
610 Q_ASSERT(scrollbar
!= 0);
612 const int maxHeight
= maximumViewportSize().height();
613 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
614 const int visibleHeight
= model()->rowCount() + 1 - height
;
615 if (visibleHeight
<= 0) {
619 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
623 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
625 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
627 switch (columnIndex
) {
628 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
629 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
630 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
631 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
632 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
633 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
640 void DolphinDetailsView::resizeColumns()
642 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
643 // around 3 seconds for each (!) resize operation when having > 10000 items).
644 // This gets a problem especially when opening large directories, where several
645 // resize operations are received for showing the currently available items during
646 // loading (the application hangs around 20 seconds when loading > 10000 items).
648 QHeaderView
* headerView
= header();
649 QFontMetrics
fontMetrics(viewport()->font());
651 int columnWidth
[KDirModel::ColumnCount
];
652 columnWidth
[KDirModel::Size
] = fontMetrics
.width("00000 Items");
653 columnWidth
[KDirModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
654 columnWidth
[KDirModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
655 columnWidth
[KDirModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
656 columnWidth
[KDirModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
657 columnWidth
[KDirModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
659 int requiredWidth
= 0;
660 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
661 if (!isColumnHidden(i
)) {
662 columnWidth
[i
] += 20; // provide a default gap
663 requiredWidth
+= columnWidth
[i
];
664 headerView
->resizeSection(i
, columnWidth
[i
]);
668 // resize the name column in a way that the whole available width is used
669 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
670 if (columnWidth
[KDirModel::Name
] < 120) {
671 columnWidth
[KDirModel::Name
] = 120;
673 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
676 #include "dolphindetailsview.moc"