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
&)));
104 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
105 controller
, SLOT(triggerItem(const QModelIndex
&)));
108 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
109 m_selectionManager
= new SelectionManager(this);
110 connect(m_selectionManager
, SIGNAL(selectionChanged()),
111 this, SLOT(requestActivation()));
112 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
113 m_selectionManager
, SLOT(reset()));
116 connect(this, SIGNAL(entered(const QModelIndex
&)),
117 this, SLOT(slotEntered(const QModelIndex
&)));
118 connect(this, SIGNAL(viewportEntered()),
119 controller
, SLOT(emitViewportEntered()));
120 connect(controller
, SIGNAL(zoomLevelChanged(int)),
121 this, SLOT(setZoomLevel(int)));
122 connect(controller
->dolphinView(), SIGNAL(additionalInfoChanged()),
123 this, SLOT(updateColumnVisibility()));
124 connect(controller
, SIGNAL(activationChanged(bool)),
125 this, SLOT(slotActivationChanged(bool)));
127 if (settings
->useSystemFont()) {
128 m_font
= KGlobalSettings::generalFont();
130 m_font
= QFont(settings
->fontFamily(),
131 settings
->fontSize(),
132 settings
->fontWeight(),
133 settings
->italicFont());
136 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
137 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
139 updateDecorationSize();
142 viewport()->installEventFilter(this);
144 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
145 this, SLOT(updateFont()));
148 DolphinDetailsView::~DolphinDetailsView()
152 bool DolphinDetailsView::event(QEvent
* event
)
154 if (event
->type() == QEvent::Polish
) {
155 QHeaderView
* headerView
= header();
156 headerView
->setResizeMode(QHeaderView::Interactive
);
157 headerView
->setMovable(false);
159 updateColumnVisibility();
161 hideColumn(DolphinModel::Rating
);
162 hideColumn(DolphinModel::Tags
);
163 } else if (event
->type() == QEvent::UpdateRequest
) {
164 // a wheel movement will scroll 4 items
165 if (model()->rowCount() > 0) {
166 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
170 return QTreeView::event(event
);
173 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
175 QStyleOptionViewItem viewOptions
= QTreeView::viewOptions();
176 viewOptions
.font
= m_font
;
177 viewOptions
.showDecorationSelected
= true;
178 viewOptions
.decorationSize
= m_decorationSize
;
182 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
184 QTreeView::contextMenuEvent(event
);
185 m_controller
->triggerContextMenuRequest(event
->pos());
188 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
190 m_controller
->requestActivation();
192 const QModelIndex current
= currentIndex();
193 QTreeView::mousePressEvent(event
);
195 m_expandingTogglePressed
= false;
196 const QModelIndex index
= indexAt(event
->pos());
197 const bool updateState
= index
.isValid() &&
198 (index
.column() == DolphinModel::Name
) &&
199 (event
->button() == Qt::LeftButton
);
201 // TODO: See comment in DolphinIconsView::mousePressEvent(). Only update
202 // the state if no expanding/collapsing area has been hit:
203 const QRect rect
= visualRect(index
);
204 if (event
->pos().x() >= rect
.x() + indentation()) {
205 setState(QAbstractItemView::DraggingState
);
207 m_expandingTogglePressed
= true;
211 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
212 if (QApplication::mouseButtons() & Qt::MidButton
) {
213 m_controller
->replaceUrlByClipboard();
216 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
217 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
221 // restore the current index, other columns are handled as viewport area
222 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
225 if ((event
->button() == Qt::LeftButton
) && !m_expandingTogglePressed
) {
226 m_showElasticBand
= true;
228 const QPoint
pos(contentsPos());
229 m_elasticBandOrigin
= event
->pos();
230 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
231 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
232 m_elasticBandDestination
= event
->pos();
236 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
238 if (m_showElasticBand
) {
239 const QPoint mousePos
= event
->pos();
240 const QModelIndex index
= indexAt(mousePos
);
241 if (!index
.isValid()) {
242 // the destination of the selection rectangle is above the viewport. In this
243 // case QTreeView does no selection at all, which is not the wanted behavior
244 // in Dolphin -> select all items within the elastic band rectangle
247 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
248 QRect selRect
= QRect(m_elasticBandOrigin
, m_elasticBandDestination
).normalized();
249 const QRect
nameColumnsRect(0, 0, nameColumnWidth
, viewport()->height());
250 selRect
= nameColumnsRect
.intersected(selRect
);
252 setSelection(selRect
, QItemSelectionModel::Select
);
255 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
256 // as the Qt-issue #199631 has been fixed.
257 // QTreeView::mouseMoveEvent(event);
258 QAbstractItemView::mouseMoveEvent(event
);
261 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
262 // as the Qt-issue #199631 has been fixed.
263 // QTreeView::mouseMoveEvent(event);
264 QAbstractItemView::mouseMoveEvent(event
);
267 if (m_expandingTogglePressed
) {
268 // Per default QTreeView starts either a selection or a drag operation when dragging
269 // the expanding toggle button (Qt-issue - see TODO comment in DolphinIconsView::mousePressEvent()).
270 // Turn off this behavior in Dolphin to stay predictable:
272 setState(QAbstractItemView::NoState
);
276 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
278 const QModelIndex index
= indexAt(event
->pos());
279 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
280 QTreeView::mouseReleaseEvent(event
);
282 // don't change the current index if the cursor is released
283 // above any other column than the name column, as the other
284 // columns act as viewport
285 const QModelIndex current
= currentIndex();
286 QTreeView::mouseReleaseEvent(event
);
287 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
290 m_expandingTogglePressed
= false;
291 if (m_showElasticBand
) {
293 m_showElasticBand
= false;
297 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions
)
299 DragAndDropHelper::startDrag(this, supportedActions
);
300 m_showElasticBand
= false;
303 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
305 if (event
->mimeData()->hasUrls()) {
306 event
->acceptProposedAction();
309 if (m_showElasticBand
) {
311 m_showElasticBand
= false;
315 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
317 QTreeView::dragLeaveEvent(event
);
318 setDirtyRegion(m_dropRect
);
321 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
323 QTreeView::dragMoveEvent(event
);
325 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
326 setDirtyRegion(m_dropRect
);
327 const QModelIndex index
= indexAt(event
->pos());
328 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
329 const KFileItem item
= m_controller
->itemForIndex(index
);
330 if (!item
.isNull() && item
.isDir()) {
331 m_dropRect
= visualRect(index
);
333 m_dropRect
.setSize(QSize()); // set as invalid
335 setDirtyRegion(m_dropRect
);
338 if (event
->mimeData()->hasUrls()) {
339 // accept url drops, independently from the destination item
340 event
->acceptProposedAction();
344 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
346 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
347 if (!urls
.isEmpty()) {
348 event
->acceptProposedAction();
349 const QModelIndex index
= indexAt(event
->pos());
351 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
352 item
= m_controller
->itemForIndex(index
);
354 m_controller
->indicateDroppedUrls(urls
,
358 QTreeView::dropEvent(event
);
361 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
363 QTreeView::paintEvent(event
);
364 if (m_showElasticBand
) {
365 // The following code has been taken from QListView
366 // and adapted to DolphinDetailsView.
367 // (C) 1992-2007 Trolltech ASA
368 QStyleOptionRubberBand opt
;
370 opt
.shape
= QRubberBand::Rectangle
;
372 opt
.rect
= elasticBandRect();
374 QPainter
painter(viewport());
376 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
381 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
383 // If the Control modifier is pressed, a multiple selection
384 // is done and DolphinDetailsView::currentChanged() may not
385 // not change the selection in a custom way.
386 m_keyPressed
= !(event
->modifiers() & Qt::ControlModifier
);
388 QTreeView::keyPressEvent(event
);
389 m_controller
->handleKeyPressEvent(event
);
392 void DolphinDetailsView::keyReleaseEvent(QKeyEvent
* event
)
394 QTreeView::keyReleaseEvent(event
);
395 m_keyPressed
= false;
398 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
403 QTreeView::resizeEvent(event
);
406 void DolphinDetailsView::wheelEvent(QWheelEvent
* event
)
408 if (m_selectionManager
!= 0) {
409 m_selectionManager
->reset();
412 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
413 if (event
->modifiers() & Qt::ControlModifier
) {
418 QTreeView::wheelEvent(event
);
421 void DolphinDetailsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
423 QTreeView::currentChanged(current
, previous
);
425 // Stay consistent with QListView: When changing the current index by key presses,
426 // also change the selection.
428 selectionModel()->select(current
, QItemSelectionModel::ClearAndSelect
);
432 bool DolphinDetailsView::eventFilter(QObject
* watched
, QEvent
* event
)
434 if ((watched
== viewport()) && (event
->type() == QEvent::Leave
)) {
435 // if the mouse is above an item and moved very fast outside the widget,
436 // no viewportEntered() signal might be emitted although the mouse has been moved
437 // above the viewport
438 m_controller
->emitViewportEntered();
441 return QTreeView::eventFilter(watched
, event
);
444 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
446 QHeaderView
* headerView
= header();
447 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
450 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
452 QHeaderView
* headerView
= header();
453 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
456 void DolphinDetailsView::synchronizeSortingState(int column
)
458 // The sorting has already been changed in QTreeView if this slot is
459 // invoked, but Dolphin is not informed about this.
460 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
461 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
462 m_controller
->indicateSortingChange(sorting
);
463 m_controller
->indicateSortOrderChange(sortOrder
);
466 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
468 if (index
.column() == DolphinModel::Name
) {
469 m_controller
->emitItemEntered(index
);
471 m_controller
->emitViewportEntered();
475 void DolphinDetailsView::updateElasticBand()
477 if (m_showElasticBand
) {
478 QRect
dirtyRegion(elasticBandRect());
479 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
480 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
481 setDirtyRegion(dirtyRegion
);
485 QRect
DolphinDetailsView::elasticBandRect() const
487 const QPoint
pos(contentsPos());
488 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
489 return QRect(topLeft
, m_elasticBandDestination
).normalized();
492 void DolphinDetailsView::setZoomLevel(int level
)
494 const int size
= DolphinController::iconSizeForZoomLevel(level
);
495 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
496 settings
->setIconSize(size
);
498 updateDecorationSize();
501 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
504 popup
.addTitle(i18nc("@title:menu", "Columns"));
506 QHeaderView
* headerView
= header();
507 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
508 const int logicalIndex
= headerView
->logicalIndex(i
);
509 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
510 QAction
* action
= popup
.addAction(text
);
511 action
->setCheckable(true);
512 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
516 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
517 if (activatedAction
!= 0) {
518 const bool show
= activatedAction
->isChecked();
519 const int columnIndex
= activatedAction
->data().toInt();
521 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
522 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
524 Q_ASSERT(!list
.contains(info
));
527 Q_ASSERT(list
.contains(info
));
528 const int index
= list
.indexOf(info
);
529 list
.removeAt(index
);
532 m_controller
->indicateAdditionalInfoChange(list
);
533 setColumnHidden(columnIndex
, !show
);
537 void DolphinDetailsView::updateColumnVisibility()
539 const KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
540 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
541 const KFileItemDelegate::Information info
= infoForColumn(i
);
542 const bool hide
= !list
.contains(info
);
543 if (isColumnHidden(i
) != hide
) {
544 setColumnHidden(i
, hide
);
551 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex
, int oldSize
, int newSize
)
553 Q_UNUSED(logicalIndex
);
556 if (QApplication::mouseButtons() & Qt::LeftButton
) {
557 disableAutoResizing();
561 void DolphinDetailsView::slotActivationChanged(bool active
)
563 setAlternatingRowColors(active
);
566 void DolphinDetailsView::disableAutoResizing()
568 m_autoResize
= false;
571 void DolphinDetailsView::requestActivation()
573 m_controller
->requestActivation();
576 void DolphinDetailsView::updateFont()
578 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
579 Q_ASSERT(settings
!= 0);
581 if (settings
->useSystemFont()) {
582 m_font
= KGlobalSettings::generalFont();
586 void DolphinDetailsView::updateDecorationSize()
588 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
589 const int iconSize
= settings
->iconSize();
590 setIconSize(QSize(iconSize
, iconSize
));
591 m_decorationSize
= QSize(iconSize
, iconSize
);
593 if (m_selectionManager
!= 0) {
594 m_selectionManager
->reset();
600 QPoint
DolphinDetailsView::contentsPos() const
602 // implementation note: the horizonal position is ignored currently, as no
603 // horizontal scrolling is done anyway during a selection
604 const QScrollBar
* scrollbar
= verticalScrollBar();
605 Q_ASSERT(scrollbar
!= 0);
607 const int maxHeight
= maximumViewportSize().height();
608 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
609 const int visibleHeight
= model()->rowCount() + 1 - height
;
610 if (visibleHeight
<= 0) {
614 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
618 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
620 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
622 switch (columnIndex
) {
623 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
624 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
625 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
626 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
627 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
628 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
635 void DolphinDetailsView::resizeColumns()
637 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
638 // around 3 seconds for each (!) resize operation when having > 10000 items).
639 // This gets a problem especially when opening large directories, where several
640 // resize operations are received for showing the currently available items during
641 // loading (the application hangs around 20 seconds when loading > 10000 items).
643 QHeaderView
* headerView
= header();
644 QFontMetrics
fontMetrics(viewport()->font());
646 int columnWidth
[KDirModel::ColumnCount
];
647 columnWidth
[KDirModel::Size
] = fontMetrics
.width("00000 Items");
648 columnWidth
[KDirModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
649 columnWidth
[KDirModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
650 columnWidth
[KDirModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
651 columnWidth
[KDirModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
652 columnWidth
[KDirModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
654 int requiredWidth
= 0;
655 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
656 if (!isColumnHidden(i
)) {
657 columnWidth
[i
] += 20; // provide a default gap
658 requiredWidth
+= columnWidth
[i
];
659 headerView
->resizeSection(i
, columnWidth
[i
]);
663 // resize the name column in a way that the whole available width is used
664 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
665 if (columnWidth
[KDirModel::Name
] < 120) {
666 columnWidth
[KDirModel::Name
] = 120;
668 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
671 #include "dolphindetailsview.moc"