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"
30 #include "zoomlevelinfo.h"
32 #include "dolphin_detailsmodesettings.h"
33 #include "dolphin_generalsettings.h"
35 #include <kdirmodel.h>
39 #include <QAbstractProxyModel>
41 #include <QApplication>
42 #include <QHeaderView>
43 #include <QRubberBand>
47 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
, DolphinController
* controller
) :
50 m_expandingTogglePressed(false),
52 m_controller(controller
),
53 m_selectionManager(0),
56 m_showElasticBand(false),
57 m_elasticBandOrigin(),
58 m_elasticBandDestination()
60 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
61 Q_ASSERT(settings
!= 0);
62 Q_ASSERT(controller
!= 0);
64 setLayoutDirection(Qt::LeftToRight
);
66 setSortingEnabled(true);
67 setUniformRowHeights(true);
68 setSelectionBehavior(SelectItems
);
69 setDragDropMode(QAbstractItemView::DragDrop
);
70 setDropIndicatorShown(false);
71 setAlternatingRowColors(true);
72 setRootIsDecorated(settings
->expandableFolders());
73 setItemsExpandable(settings
->expandableFolders());
74 setEditTriggers(QAbstractItemView::NoEditTriggers
);
76 setMouseTracking(true);
78 const ViewProperties
props(controller
->url());
79 setSortIndicatorSection(props
.sorting());
80 setSortIndicatorOrder(props
.sortOrder());
82 QHeaderView
* headerView
= header();
83 connect(headerView
, SIGNAL(sectionClicked(int)),
84 this, SLOT(synchronizeSortingState(int)));
85 headerView
->setContextMenuPolicy(Qt::CustomContextMenu
);
86 connect(headerView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
87 this, SLOT(configureColumns(const QPoint
&)));
88 connect(headerView
, SIGNAL(sectionResized(int, int, int)),
89 this, SLOT(slotHeaderSectionResized(int, int, int)));
90 connect(headerView
, SIGNAL(sectionHandleDoubleClicked(int)),
91 this, SLOT(disableAutoResizing()));
93 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
94 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
95 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
96 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
98 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
99 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
100 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
101 // RETURN-key in keyPressEvent().
102 if (KGlobalSettings::singleClick()) {
103 connect(this, SIGNAL(clicked(const QModelIndex
&)),
104 controller
, SLOT(triggerItem(const QModelIndex
&)));
106 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
107 controller
, SLOT(triggerItem(const QModelIndex
&)));
110 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
111 m_selectionManager
= new SelectionManager(this);
112 connect(m_selectionManager
, SIGNAL(selectionChanged()),
113 this, SLOT(requestActivation()));
114 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
115 m_selectionManager
, SLOT(reset()));
118 connect(this, SIGNAL(entered(const QModelIndex
&)),
119 this, SLOT(slotEntered(const QModelIndex
&)));
120 connect(this, SIGNAL(viewportEntered()),
121 controller
, SLOT(emitViewportEntered()));
122 connect(controller
, SIGNAL(zoomLevelChanged(int)),
123 this, SLOT(setZoomLevel(int)));
124 connect(controller
->dolphinView(), SIGNAL(additionalInfoChanged()),
125 this, SLOT(updateColumnVisibility()));
126 connect(controller
, SIGNAL(activationChanged(bool)),
127 this, SLOT(slotActivationChanged(bool)));
129 if (settings
->useSystemFont()) {
130 m_font
= KGlobalSettings::generalFont();
132 m_font
= QFont(settings
->fontFamily(),
133 settings
->fontSize(),
134 settings
->fontWeight(),
135 settings
->italicFont());
138 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
139 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
141 const DolphinView
* view
= controller
->dolphinView();
142 connect(view
, SIGNAL(showPreviewChanged()),
143 this, SLOT(slotShowPreviewChanged()));
145 updateDecorationSize(view
->showPreview());
148 viewport()->installEventFilter(this);
150 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
151 this, SLOT(updateFont()));
154 DolphinDetailsView::~DolphinDetailsView()
158 bool DolphinDetailsView::event(QEvent
* event
)
160 if (event
->type() == QEvent::Polish
) {
161 QHeaderView
* headerView
= header();
162 headerView
->setResizeMode(QHeaderView::Interactive
);
163 headerView
->setMovable(false);
165 updateColumnVisibility();
167 hideColumn(DolphinModel::Rating
);
168 hideColumn(DolphinModel::Tags
);
169 } else if (event
->type() == QEvent::UpdateRequest
) {
170 // a wheel movement will scroll 4 items
171 if (model()->rowCount() > 0) {
172 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
176 return QTreeView::event(event
);
179 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
181 QStyleOptionViewItem viewOptions
= QTreeView::viewOptions();
182 viewOptions
.font
= m_font
;
183 viewOptions
.showDecorationSelected
= true;
184 viewOptions
.decorationSize
= m_decorationSize
;
188 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
190 QTreeView::contextMenuEvent(event
);
191 m_controller
->triggerContextMenuRequest(event
->pos());
194 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
196 m_controller
->requestActivation();
198 const QModelIndex current
= currentIndex();
199 QTreeView::mousePressEvent(event
);
201 m_expandingTogglePressed
= false;
202 const QModelIndex index
= indexAt(event
->pos());
203 const bool updateState
= index
.isValid() &&
204 (index
.column() == DolphinModel::Name
) &&
205 (event
->button() == Qt::LeftButton
);
207 // TODO: See comment in DolphinIconsView::mousePressEvent(). Only update
208 // the state if no expanding/collapsing area has been hit:
209 const QRect rect
= visualRect(index
);
210 if (event
->pos().x() >= rect
.x() + indentation()) {
211 setState(QAbstractItemView::DraggingState
);
213 m_expandingTogglePressed
= true;
217 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
218 if (QApplication::mouseButtons() & Qt::MidButton
) {
219 m_controller
->replaceUrlByClipboard();
222 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
223 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
227 // restore the current index, other columns are handled as viewport area
228 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
231 if ((event
->button() == Qt::LeftButton
) && !m_expandingTogglePressed
) {
232 m_showElasticBand
= true;
234 const QPoint
pos(contentsPos());
235 m_elasticBandOrigin
= event
->pos();
236 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
237 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
238 m_elasticBandDestination
= event
->pos();
242 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
244 if (m_showElasticBand
) {
245 const QPoint mousePos
= event
->pos();
246 const QModelIndex index
= indexAt(mousePos
);
247 if (!index
.isValid()) {
248 // the destination of the selection rectangle is above the viewport. In this
249 // case QTreeView does no selection at all, which is not the wanted behavior
250 // in Dolphin -> select all items within the elastic band rectangle
253 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
254 QRect selRect
= QRect(m_elasticBandOrigin
, m_elasticBandDestination
).normalized();
255 const QRect
nameColumnsRect(0, 0, nameColumnWidth
, viewport()->height());
256 selRect
= nameColumnsRect
.intersected(selRect
);
258 setSelection(selRect
, QItemSelectionModel::Select
);
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 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
268 // as the Qt-issue #199631 has been fixed.
269 // QTreeView::mouseMoveEvent(event);
270 QAbstractItemView::mouseMoveEvent(event
);
273 if (m_expandingTogglePressed
) {
274 // Per default QTreeView starts either a selection or a drag operation when dragging
275 // the expanding toggle button (Qt-issue - see TODO comment in DolphinIconsView::mousePressEvent()).
276 // Turn off this behavior in Dolphin to stay predictable:
278 setState(QAbstractItemView::NoState
);
282 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
284 const QModelIndex index
= indexAt(event
->pos());
285 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
286 QTreeView::mouseReleaseEvent(event
);
288 // don't change the current index if the cursor is released
289 // above any other column than the name column, as the other
290 // columns act as viewport
291 const QModelIndex current
= currentIndex();
292 QTreeView::mouseReleaseEvent(event
);
293 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
296 m_expandingTogglePressed
= false;
297 if (m_showElasticBand
) {
299 m_showElasticBand
= false;
303 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions
)
305 DragAndDropHelper::startDrag(this, supportedActions
);
306 m_showElasticBand
= false;
309 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
311 if (event
->mimeData()->hasUrls()) {
312 event
->acceptProposedAction();
315 if (m_showElasticBand
) {
317 m_showElasticBand
= false;
321 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
323 QTreeView::dragLeaveEvent(event
);
324 setDirtyRegion(m_dropRect
);
327 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
329 QTreeView::dragMoveEvent(event
);
331 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
332 setDirtyRegion(m_dropRect
);
333 const QModelIndex index
= indexAt(event
->pos());
334 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
335 const KFileItem item
= m_controller
->itemForIndex(index
);
336 if (!item
.isNull() && item
.isDir()) {
337 m_dropRect
= visualRect(index
);
339 m_dropRect
.setSize(QSize()); // set as invalid
341 setDirtyRegion(m_dropRect
);
344 if (event
->mimeData()->hasUrls()) {
345 // accept url drops, independently from the destination item
346 event
->acceptProposedAction();
350 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
352 const QModelIndex index
= indexAt(event
->pos());
354 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
355 item
= m_controller
->itemForIndex(index
);
357 m_controller
->indicateDroppedUrls(item
, m_controller
->url(), event
);
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
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
495 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
497 const bool showPreview
= m_controller
->dolphinView()->showPreview();
499 settings
->setPreviewSize(size
);
501 settings
->setIconSize(size
);
504 updateDecorationSize(showPreview
);
508 void DolphinDetailsView::slotShowPreviewChanged()
510 const DolphinView
* view
= m_controller
->dolphinView();
511 updateDecorationSize(view
->showPreview());
514 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
517 popup
.addTitle(i18nc("@title:menu", "Columns"));
519 QHeaderView
* headerView
= header();
520 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
521 const int logicalIndex
= headerView
->logicalIndex(i
);
522 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
523 QAction
* action
= popup
.addAction(text
);
524 action
->setCheckable(true);
525 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
529 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
530 if (activatedAction
!= 0) {
531 const bool show
= activatedAction
->isChecked();
532 const int columnIndex
= activatedAction
->data().toInt();
534 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
535 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
537 Q_ASSERT(!list
.contains(info
));
540 Q_ASSERT(list
.contains(info
));
541 const int index
= list
.indexOf(info
);
542 list
.removeAt(index
);
545 m_controller
->indicateAdditionalInfoChange(list
);
546 setColumnHidden(columnIndex
, !show
);
550 void DolphinDetailsView::updateColumnVisibility()
552 const KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
553 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
554 const KFileItemDelegate::Information info
= infoForColumn(i
);
555 const bool hide
= !list
.contains(info
);
556 if (isColumnHidden(i
) != hide
) {
557 setColumnHidden(i
, hide
);
564 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex
, int oldSize
, int newSize
)
566 Q_UNUSED(logicalIndex
);
569 if (QApplication::mouseButtons() & Qt::LeftButton
) {
570 disableAutoResizing();
574 void DolphinDetailsView::slotActivationChanged(bool active
)
576 setAlternatingRowColors(active
);
579 void DolphinDetailsView::disableAutoResizing()
581 m_autoResize
= false;
584 void DolphinDetailsView::requestActivation()
586 m_controller
->requestActivation();
589 void DolphinDetailsView::updateFont()
591 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
592 Q_ASSERT(settings
!= 0);
594 if (settings
->useSystemFont()) {
595 m_font
= KGlobalSettings::generalFont();
599 void DolphinDetailsView::updateDecorationSize(bool showPreview
)
601 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
602 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
603 setIconSize(QSize(iconSize
, iconSize
));
604 m_decorationSize
= QSize(iconSize
, iconSize
);
606 if (m_selectionManager
!= 0) {
607 m_selectionManager
->reset();
613 QPoint
DolphinDetailsView::contentsPos() const
615 // implementation note: the horizonal position is ignored currently, as no
616 // horizontal scrolling is done anyway during a selection
617 const QScrollBar
* scrollbar
= verticalScrollBar();
618 Q_ASSERT(scrollbar
!= 0);
620 const int maxHeight
= maximumViewportSize().height();
621 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
622 const int visibleHeight
= model()->rowCount() + 1 - height
;
623 if (visibleHeight
<= 0) {
627 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
631 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
633 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
635 switch (columnIndex
) {
636 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
637 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
638 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
639 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
640 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
641 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
648 void DolphinDetailsView::resizeColumns()
650 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
651 // around 3 seconds for each (!) resize operation when having > 10000 items).
652 // This gets a problem especially when opening large directories, where several
653 // resize operations are received for showing the currently available items during
654 // loading (the application hangs around 20 seconds when loading > 10000 items).
656 QHeaderView
* headerView
= header();
657 QFontMetrics
fontMetrics(viewport()->font());
659 int columnWidth
[KDirModel::ColumnCount
];
660 columnWidth
[KDirModel::Size
] = fontMetrics
.width("00000 Items");
661 columnWidth
[KDirModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
662 columnWidth
[KDirModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
663 columnWidth
[KDirModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
664 columnWidth
[KDirModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
665 columnWidth
[KDirModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
667 int requiredWidth
= 0;
668 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
669 if (!isColumnHidden(i
)) {
670 columnWidth
[i
] += 20; // provide a default gap
671 requiredWidth
+= columnWidth
[i
];
672 headerView
->resizeSection(i
, columnWidth
[i
]);
676 // resize the name column in a way that the whole available width is used
677 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
678 if (columnWidth
[KDirModel::Name
] < 120) {
679 columnWidth
[KDirModel::Name
] = 120;
681 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
684 #include "dolphindetailsview.moc"