1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) *
3 * Copyright (C) 2008 by Simon St. James (kdedevel@etotheipiplusone.com) *
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 "dolphinfileitemdelegate.h"
26 #include "settings/dolphinsettings.h"
27 #include "dolphinsortfilterproxymodel.h"
28 #include "dolphinviewautoscroller.h"
29 #include "draganddrophelper.h"
30 #include "selectionmanager.h"
31 #include "viewproperties.h"
32 #include "zoomlevelinfo.h"
34 #include "dolphin_detailsmodesettings.h"
35 #include "dolphin_generalsettings.h"
37 #include <kdirmodel.h>
41 #include <QAbstractProxyModel>
43 #include <QApplication>
44 #include <QHeaderView>
45 #include <QRubberBand>
49 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
, DolphinController
* controller
) :
52 m_expandingTogglePressed(false),
54 m_useDefaultIndexAt(true),
55 m_ignoreScrollTo(false),
56 m_controller(controller
),
57 m_selectionManager(0),
59 m_expandableFoldersAction(0),
64 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
65 Q_ASSERT(settings
!= 0);
66 Q_ASSERT(controller
!= 0);
68 setLayoutDirection(Qt::LeftToRight
);
70 setSortingEnabled(true);
71 setUniformRowHeights(true);
72 setSelectionBehavior(SelectItems
);
73 setDragDropMode(QAbstractItemView::DragDrop
);
74 setDropIndicatorShown(false);
75 setAlternatingRowColors(true);
76 setRootIsDecorated(settings
->expandableFolders());
77 setItemsExpandable(settings
->expandableFolders());
78 setEditTriggers(QAbstractItemView::NoEditTriggers
);
80 setMouseTracking(true);
81 m_autoScroller
= new DolphinViewAutoScroller(this);
83 const ViewProperties
props(controller
->url());
84 setSortIndicatorSection(props
.sorting());
85 setSortIndicatorOrder(props
.sortOrder());
87 QHeaderView
* headerView
= header();
88 connect(headerView
, SIGNAL(sectionClicked(int)),
89 this, SLOT(synchronizeSortingState(int)));
90 headerView
->setContextMenuPolicy(Qt::CustomContextMenu
);
91 connect(headerView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
92 this, SLOT(configureSettings(const QPoint
&)));
93 connect(headerView
, SIGNAL(sectionResized(int, int, int)),
94 this, SLOT(slotHeaderSectionResized(int, int, int)));
95 connect(headerView
, SIGNAL(sectionHandleDoubleClicked(int)),
96 this, SLOT(disableAutoResizing()));
98 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
99 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
100 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
101 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
103 connect(this, SIGNAL(clicked(const QModelIndex
&)),
104 controller
, SLOT(requestTab(const QModelIndex
&)));
105 if (KGlobalSettings::singleClick()) {
106 connect(this, SIGNAL(clicked(const QModelIndex
&)),
107 controller
, SLOT(triggerItem(const QModelIndex
&)));
109 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
110 controller
, SLOT(triggerItem(const QModelIndex
&)));
113 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
114 m_selectionManager
= new SelectionManager(this);
115 connect(m_selectionManager
, SIGNAL(selectionChanged()),
116 this, SLOT(requestActivation()));
117 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
118 m_selectionManager
, SLOT(reset()));
121 connect(this, SIGNAL(entered(const QModelIndex
&)),
122 this, SLOT(slotEntered(const QModelIndex
&)));
123 connect(this, SIGNAL(viewportEntered()),
124 controller
, SLOT(emitViewportEntered()));
125 connect(controller
, SIGNAL(zoomLevelChanged(int)),
126 this, SLOT(setZoomLevel(int)));
127 connect(controller
->dolphinView(), SIGNAL(additionalInfoChanged()),
128 this, SLOT(updateColumnVisibility()));
129 connect(controller
, SIGNAL(activationChanged(bool)),
130 this, SLOT(slotActivationChanged(bool)));
132 if (settings
->useSystemFont()) {
133 m_font
= KGlobalSettings::generalFont();
135 m_font
= QFont(settings
->fontFamily(),
136 settings
->fontSize(),
137 settings
->fontWeight(),
138 settings
->italicFont());
141 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
142 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
144 const DolphinView
* view
= controller
->dolphinView();
145 connect(view
, SIGNAL(showPreviewChanged()),
146 this, SLOT(slotShowPreviewChanged()));
148 updateDecorationSize(view
->showPreview());
151 viewport()->installEventFilter(this);
153 connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)),
154 this, SLOT(slotGlobalSettingsChanged(int)));
156 m_useDefaultIndexAt
= false;
158 m_expandableFoldersAction
= new QAction(i18nc("@option:check", "Expandable Folders"), this);
159 m_expandableFoldersAction
->setCheckable(true);
160 connect(m_expandableFoldersAction
, SIGNAL(toggled(bool)),
161 this, SLOT(setFoldersExpandable(bool)));
164 DolphinDetailsView::~DolphinDetailsView()
168 bool DolphinDetailsView::event(QEvent
* event
)
170 if (event
->type() == QEvent::Polish
) {
171 QHeaderView
* headerView
= header();
172 headerView
->setResizeMode(QHeaderView::Interactive
);
173 headerView
->setMovable(false);
175 updateColumnVisibility();
177 hideColumn(DolphinModel::Rating
);
178 hideColumn(DolphinModel::Tags
);
181 return QTreeView::event(event
);
184 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
186 QStyleOptionViewItem viewOptions
= QTreeView::viewOptions();
187 viewOptions
.font
= m_font
;
188 viewOptions
.showDecorationSelected
= true;
189 viewOptions
.decorationSize
= m_decorationSize
;
193 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
195 QTreeView::contextMenuEvent(event
);
197 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
198 m_expandableFoldersAction
->setChecked(settings
->expandableFolders());
199 m_controller
->triggerContextMenuRequest(event
->pos(),
200 QList
<QAction
*>() << m_expandableFoldersAction
);
203 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
205 m_controller
->requestActivation();
207 const QModelIndex current
= currentIndex();
208 QTreeView::mousePressEvent(event
);
210 m_expandingTogglePressed
= isAboveExpandingToggle(event
->pos());
212 const QModelIndex index
= indexAt(event
->pos());
213 const bool updateState
= index
.isValid() &&
214 (index
.column() == DolphinModel::Name
) &&
215 (event
->button() == Qt::LeftButton
);
217 setState(QAbstractItemView::DraggingState
);
220 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
221 // the mouse press is done somewhere outside the filename column
222 if (QApplication::mouseButtons() & Qt::MidButton
) {
223 m_controller
->replaceUrlByClipboard();
226 const Qt::KeyboardModifiers mod
= QApplication::keyboardModifiers();
227 if (!m_expandingTogglePressed
&& !(mod
& Qt::ShiftModifier
) && !(mod
& Qt::ControlModifier
)) {
231 // restore the current index, other columns are handled as viewport area.
232 // setCurrentIndex(...) implicitly calls scrollTo(...), which we want to ignore.
233 m_ignoreScrollTo
= true;
234 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
235 m_ignoreScrollTo
= false;
237 if ((event
->button() == Qt::LeftButton
) && !m_expandingTogglePressed
) {
238 // Inform Qt about what we are doing - otherwise it starts dragging items around!
239 setState(DragSelectingState
);
241 // Incremental update data will not be useful - start from scratch.
242 m_band
.ignoreOldInfo
= true;
243 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
244 m_band
.origin
= event
->pos() + scrollPos
;
245 m_band
.destination
= m_band
.origin
;
246 m_band
.originalSelection
= selectionModel()->selection();
251 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
253 if (m_expandingTogglePressed
) {
254 // Per default QTreeView starts either a selection or a drag operation when dragging
255 // the expanding toggle button (Qt-issue - see TODO comment in DolphinIconsView::mousePressEvent()).
256 // Turn off this behavior in Dolphin to stay predictable:
257 setState(QAbstractItemView::NoState
);
262 const QPoint mousePos
= event
->pos();
263 const QModelIndex index
= indexAt(mousePos
);
264 if (!index
.isValid()) {
265 // the destination of the selection rectangle is above the viewport. In this
266 // case QTreeView does no selection at all, which is not the wanted behavior
267 // in Dolphin -> select all items within the elastic band rectangle
268 updateElasticBandSelection();
271 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
272 // as the Qt-issue #199631 has been fixed.
273 // QTreeView::mouseMoveEvent(event);
274 QAbstractItemView::mouseMoveEvent(event
);
277 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
278 // as the Qt-issue #199631 has been fixed.
279 // QTreeView::mouseMoveEvent(event);
280 QAbstractItemView::mouseMoveEvent(event
);
284 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
286 if (!m_expandingTogglePressed
) {
287 const QModelIndex index
= indexAt(event
->pos());
288 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
289 QTreeView::mouseReleaseEvent(event
);
291 // don't change the current index if the cursor is released
292 // above any other column than the name column, as the other
293 // columns act as viewport
294 const QModelIndex current
= currentIndex();
295 QTreeView::mouseReleaseEvent(event
);
296 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
299 m_expandingTogglePressed
= false;
308 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions
)
310 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_controller
);
314 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
316 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
317 event
->acceptProposedAction();
326 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
328 QTreeView::dragLeaveEvent(event
);
329 setDirtyRegion(m_dropRect
);
332 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
334 QTreeView::dragMoveEvent(event
);
336 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
337 setDirtyRegion(m_dropRect
);
338 const QModelIndex index
= indexAt(event
->pos());
339 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
340 const KFileItem item
= m_controller
->itemForIndex(index
);
341 if (!item
.isNull() && item
.isDir()) {
342 m_dropRect
= visualRect(index
);
344 m_dropRect
.setSize(QSize()); // set as invalid
346 setDirtyRegion(m_dropRect
);
349 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
350 // accept url drops, independently from the destination item
351 event
->acceptProposedAction();
355 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
357 const QModelIndex index
= indexAt(event
->pos());
359 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
360 item
= m_controller
->itemForIndex(index
);
362 m_controller
->indicateDroppedUrls(item
, m_controller
->url(), event
);
363 QTreeView::dropEvent(event
);
366 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
368 QTreeView::paintEvent(event
);
370 // The following code has been taken from QListView
371 // and adapted to DolphinDetailsView.
372 // (C) 1992-2007 Trolltech ASA
373 QStyleOptionRubberBand opt
;
375 opt
.shape
= QRubberBand::Rectangle
;
377 opt
.rect
= elasticBandRect();
379 QPainter
painter(viewport());
381 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
386 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
388 // If the Control modifier is pressed, a multiple selection
389 // is done and DolphinDetailsView::currentChanged() may not
390 // not change the selection in a custom way.
391 m_keyPressed
= !(event
->modifiers() & Qt::ControlModifier
);
393 QTreeView::keyPressEvent(event
);
394 m_controller
->handleKeyPressEvent(event
);
397 void DolphinDetailsView::keyReleaseEvent(QKeyEvent
* event
)
399 QTreeView::keyReleaseEvent(event
);
400 m_keyPressed
= false;
403 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
405 QTreeView::resizeEvent(event
);
411 void DolphinDetailsView::wheelEvent(QWheelEvent
* event
)
413 if (m_selectionManager
!= 0) {
414 m_selectionManager
->reset();
417 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
418 if (event
->modifiers() & Qt::ControlModifier
) {
423 const int height
= m_decorationSize
.height();
424 const int step
= (height
>= KIconLoader::SizeHuge
) ? height
/ 10 : (KIconLoader::SizeHuge
- height
) / 2;
425 verticalScrollBar()->setSingleStep(step
);
426 QTreeView::wheelEvent(event
);
429 void DolphinDetailsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
431 QTreeView::currentChanged(current
, previous
);
432 m_autoScroller
->handleCurrentIndexChange(current
, previous
);
434 // Stay consistent with QListView: When changing the current index by key presses,
435 // also change the selection.
437 selectionModel()->select(current
, QItemSelectionModel::ClearAndSelect
);
441 bool DolphinDetailsView::eventFilter(QObject
* watched
, QEvent
* event
)
443 if ((watched
== viewport()) && (event
->type() == QEvent::Leave
)) {
444 // if the mouse is above an item and moved very fast outside the widget,
445 // no viewportEntered() signal might be emitted although the mouse has been moved
446 // above the viewport
447 m_controller
->emitViewportEntered();
450 return QTreeView::eventFilter(watched
, event
);
453 QModelIndex
DolphinDetailsView::indexAt(const QPoint
& point
) const
455 // the blank portion of the name column counts as empty space
456 const QModelIndex index
= QTreeView::indexAt(point
);
457 const bool isAboveEmptySpace
= !m_useDefaultIndexAt
&&
458 (index
.column() == KDirModel::Name
) && !nameColumnRect(index
).contains(point
);
459 return isAboveEmptySpace
? QModelIndex() : index
;
462 void DolphinDetailsView::setSelection(const QRect
& rect
, QItemSelectionModel::SelectionFlags command
)
464 // We must override setSelection() as Qt calls it internally and when this happens
465 // we must ensure that the default indexAt() is used.
467 m_useDefaultIndexAt
= true;
468 QTreeView::setSelection(rect
, command
);
469 m_useDefaultIndexAt
= false;
471 // Use our own elastic band selection algorithm
472 updateElasticBandSelection();
476 void DolphinDetailsView::scrollTo(const QModelIndex
& index
, ScrollHint hint
)
478 if (!m_ignoreScrollTo
) {
479 QTreeView::scrollTo(index
, hint
);
483 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
485 header()->setSortIndicator(sorting
, header()->sortIndicatorOrder());
488 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
490 header()->setSortIndicator(header()->sortIndicatorSection(), sortOrder
);
493 void DolphinDetailsView::synchronizeSortingState(int column
)
495 // The sorting has already been changed in QTreeView if this slot is
496 // invoked, but Dolphin is not informed about this.
497 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
498 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
499 m_controller
->indicateSortingChange(sorting
);
500 m_controller
->indicateSortOrderChange(sortOrder
);
503 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
505 if (index
.column() == DolphinModel::Name
) {
506 m_controller
->emitItemEntered(index
);
508 m_controller
->emitViewportEntered();
512 void DolphinDetailsView::updateElasticBand()
515 QRect
dirtyRegion(elasticBandRect());
516 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
517 m_band
.destination
= viewport()->mapFromGlobal(QCursor::pos()) + scrollPos
;
518 // Going above the (logical) top-left of the view causes complications during selection;
519 // we may as well prevent it.
520 if (m_band
.destination
.y() < 0) {
521 m_band
.destination
.setY(0);
523 if (m_band
.destination
.x() < 0) {
524 m_band
.destination
.setX(0);
526 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
527 setDirtyRegion(dirtyRegion
);
531 QRect
DolphinDetailsView::elasticBandRect() const
533 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
535 const QPoint topLeft
= m_band
.origin
- scrollPos
;
536 const QPoint bottomRight
= m_band
.destination
- scrollPos
;
537 return QRect(topLeft
, bottomRight
).normalized();
540 void DolphinDetailsView::setZoomLevel(int level
)
542 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
543 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
545 const bool showPreview
= m_controller
->dolphinView()->showPreview();
547 settings
->setPreviewSize(size
);
549 settings
->setIconSize(size
);
552 updateDecorationSize(showPreview
);
556 void DolphinDetailsView::slotShowPreviewChanged()
558 const DolphinView
* view
= m_controller
->dolphinView();
559 updateDecorationSize(view
->showPreview());
562 void DolphinDetailsView::configureSettings(const QPoint
& pos
)
565 popup
.addTitle(i18nc("@title:menu", "Columns"));
567 // add checkbox items for each column
568 QHeaderView
* headerView
= header();
569 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
570 const int logicalIndex
= headerView
->logicalIndex(i
);
571 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
572 QAction
* action
= popup
.addAction(text
);
573 action
->setCheckable(true);
574 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
577 popup
.addSeparator();
579 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
580 if (activatedAction
!= 0) {
581 const bool show
= activatedAction
->isChecked();
582 const int columnIndex
= activatedAction
->data().toInt();
584 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
585 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
587 Q_ASSERT(!list
.contains(info
));
590 Q_ASSERT(list
.contains(info
));
591 const int index
= list
.indexOf(info
);
592 list
.removeAt(index
);
595 m_controller
->indicateAdditionalInfoChange(list
);
596 setColumnHidden(columnIndex
, !show
);
601 void DolphinDetailsView::updateColumnVisibility()
603 const KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
604 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
605 const KFileItemDelegate::Information info
= infoForColumn(i
);
606 const bool hide
= !list
.contains(info
);
607 if (isColumnHidden(i
) != hide
) {
608 setColumnHidden(i
, hide
);
615 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex
, int oldSize
, int newSize
)
617 Q_UNUSED(logicalIndex
);
620 // If the user changes the size of the headers, the autoresize feature should be
621 // turned off. As there is no dedicated interface to find out whether the header
622 // section has been resized by the user or by a resize event, another approach is used.
623 // Attention: Take care when changing the if-condition to verify that there is no
624 // regression in combination with bug 178630 (see fix in comment #8).
625 if ((QApplication::mouseButtons() & Qt::LeftButton
) && header()->underMouse()) {
626 disableAutoResizing();
630 void DolphinDetailsView::slotActivationChanged(bool active
)
632 setAlternatingRowColors(active
);
635 void DolphinDetailsView::disableAutoResizing()
637 m_autoResize
= false;
640 void DolphinDetailsView::requestActivation()
642 m_controller
->requestActivation();
645 void DolphinDetailsView::slotGlobalSettingsChanged(int category
)
649 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
650 Q_ASSERT(settings
!= 0);
651 if (settings
->useSystemFont()) {
652 m_font
= KGlobalSettings::generalFont();
654 //Disconnect then reconnect, since the settings have been changed, the connection requirements may have also.
655 disconnect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
656 disconnect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
657 if (KGlobalSettings::singleClick()) {
658 connect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
660 connect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
664 void DolphinDetailsView::updateElasticBandSelection()
670 // Ensure the elastic band itself is up-to-date, in
671 // case we are being called due to e.g. a drag event.
674 // Clip horizontally to the name column, as some filenames will be
675 // longer than the column. We don't clip vertically as origin
676 // may be above or below the current viewport area.
677 const int nameColumnX
= header()->sectionPosition(DolphinModel::Name
);
678 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
679 QRect selRect
= elasticBandRect().normalized();
680 QRect
nameColumnArea(nameColumnX
, selRect
.y(), nameColumnWidth
, selRect
.height());
681 selRect
= nameColumnArea
.intersect(selRect
).normalized();
682 // Get the last elastic band rectangle, expressed in viewpoint coordinates.
683 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
684 QRect oldSelRect
= QRect(m_band
.lastSelectionOrigin
- scrollPos
, m_band
.lastSelectionDestination
- scrollPos
).normalized();
686 if (selRect
.isNull()) {
687 selectionModel()->select(m_band
.originalSelection
, QItemSelectionModel::ClearAndSelect
);
688 m_band
.ignoreOldInfo
= true;
692 if (!m_band
.ignoreOldInfo
) {
693 // Do some quick checks to see if we can rule out the need to
694 // update the selection.
695 Q_ASSERT(uniformRowHeights());
696 QModelIndex dummyIndex
= model()->index(0, 0);
697 if (!dummyIndex
.isValid()) {
698 // No items in the model presumably.
702 // If the elastic band does not cover the same rows as before, we'll
703 // need to re-check, and also invalidate the old item distances.
704 const int rowHeight
= QTreeView::rowHeight(dummyIndex
);
705 const bool coveringSameRows
=
706 (selRect
.top() / rowHeight
== oldSelRect
.top() / rowHeight
) &&
707 (selRect
.bottom() / rowHeight
== oldSelRect
.bottom() / rowHeight
);
708 if (coveringSameRows
) {
709 // Covering the same rows, but have we moved far enough horizontally
710 // that we might have (de)selected some other items?
711 const bool itemSelectionChanged
=
712 ((selRect
.left() > oldSelRect
.left()) &&
713 (selRect
.left() > m_band
.insideNearestLeftEdge
)) ||
714 ((selRect
.left() < oldSelRect
.left()) &&
715 (selRect
.left() <= m_band
.outsideNearestLeftEdge
)) ||
716 ((selRect
.right() < oldSelRect
.right()) &&
717 (selRect
.left() >= m_band
.insideNearestRightEdge
)) ||
718 ((selRect
.right() > oldSelRect
.right()) &&
719 (selRect
.right() >= m_band
.outsideNearestRightEdge
));
721 if (!itemSelectionChanged
) {
727 // This is the only piece of optimization data that needs to be explicitly
729 m_band
.lastSelectionOrigin
= QPoint();
730 m_band
.lastSelectionDestination
= QPoint();
731 oldSelRect
= selRect
;
734 // Do the selection from scratch. Force a update of the horizontal distances info.
735 m_band
.insideNearestLeftEdge
= nameColumnX
+ nameColumnWidth
+ 1;
736 m_band
.insideNearestRightEdge
= nameColumnX
- 1;
737 m_band
.outsideNearestLeftEdge
= nameColumnX
- 1;
738 m_band
.outsideNearestRightEdge
= nameColumnX
+ nameColumnWidth
+ 1;
740 // Include the old selection rect as well, so we can deselect
741 // items that were inside it but not in the new selRect.
742 const QRect boundingRect
= selRect
.united(oldSelRect
).normalized();
743 if (boundingRect
.isNull()) {
747 // Get the index of the item in this row in the name column.
748 // TODO - would this still work if the columns could be re-ordered?
749 QModelIndex startIndex
= QTreeView::indexAt(boundingRect
.topLeft());
750 if (startIndex
.parent().isValid()) {
751 startIndex
= startIndex
.parent().child(startIndex
.row(), KDirModel::Name
);
753 startIndex
= model()->index(startIndex
.row(), KDirModel::Name
);
755 if (!startIndex
.isValid()) {
756 selectionModel()->select(m_band
.originalSelection
, QItemSelectionModel::ClearAndSelect
);
757 m_band
.ignoreOldInfo
= true;
761 // Go through all indexes between the top and bottom of boundingRect, and
762 // update the selection.
763 const int verticalCutoff
= boundingRect
.bottom();
764 QModelIndex currIndex
= startIndex
;
765 QModelIndex lastIndex
;
766 bool allItemsInBoundDone
= false;
768 // Calling selectionModel()->select(...) for each item that needs to be
769 // toggled is slow as each call emits selectionChanged(...) so store them
770 // and do the selection toggle in one batch.
771 QItemSelection itemsToToggle
;
772 // QItemSelection's deal with continuous ranges of indexes better than
773 // single indexes, so try to portion items that need to be toggled into ranges.
774 bool formingToggleIndexRange
= false;
775 QModelIndex toggleIndexRangeBegin
= QModelIndex();
778 QRect currIndexRect
= nameColumnRect(currIndex
);
780 // Update some optimization info as we go.
781 const int cr
= currIndexRect
.right();
782 const int cl
= currIndexRect
.left();
783 const int sl
= selRect
.left();
784 const int sr
= selRect
.right();
785 // "The right edge of the name is outside of the rect but nearer than m_outsideNearestLeft", etc
786 if ((cr
< sl
&& cr
> m_band
.outsideNearestLeftEdge
)) {
787 m_band
.outsideNearestLeftEdge
= cr
;
789 if ((cl
> sr
&& cl
< m_band
.outsideNearestRightEdge
)) {
790 m_band
.outsideNearestRightEdge
= cl
;
792 if ((cl
>= sl
&& cl
<= sr
&& cl
> m_band
.insideNearestRightEdge
)) {
793 m_band
.insideNearestRightEdge
= cl
;
795 if ((cr
>= sl
&& cr
<= sr
&& cr
< m_band
.insideNearestLeftEdge
)) {
796 m_band
.insideNearestLeftEdge
= cr
;
799 bool currentlySelected
= selectionModel()->isSelected(currIndex
);
800 bool originallySelected
= m_band
.originalSelection
.contains(currIndex
);
801 bool intersectsSelectedRect
= currIndexRect
.intersects(selRect
);
802 bool shouldBeSelected
= (intersectsSelectedRect
&& !originallySelected
) || (!intersectsSelectedRect
&& originallySelected
);
803 bool needToToggleItem
= (currentlySelected
&& !shouldBeSelected
) || (!currentlySelected
&& shouldBeSelected
);
804 if (needToToggleItem
&& !formingToggleIndexRange
) {
805 toggleIndexRangeBegin
= currIndex
;
806 formingToggleIndexRange
= true;
809 // NOTE: indexBelow actually walks up and down expanded trees for us.
810 QModelIndex nextIndex
= indexBelow(currIndex
);
811 allItemsInBoundDone
= !nextIndex
.isValid() || currIndexRect
.top() > verticalCutoff
;
813 const bool commitToggleIndexRange
= formingToggleIndexRange
&&
814 (!needToToggleItem
||
815 allItemsInBoundDone
||
816 currIndex
.parent() != toggleIndexRangeBegin
.parent());
817 if (commitToggleIndexRange
) {
818 formingToggleIndexRange
= false;
819 // If this is the last item in the bounds and it is also the beginning of a range,
820 // don't toggle lastIndex - it will already have been dealt with.
821 if (!allItemsInBoundDone
|| toggleIndexRangeBegin
!= currIndex
) {
822 itemsToToggle
.select(toggleIndexRangeBegin
, lastIndex
);
824 // Need to start a new range immediately with currIndex?
825 if (needToToggleItem
) {
826 toggleIndexRangeBegin
= currIndex
;
827 formingToggleIndexRange
= true;
829 if (allItemsInBoundDone
&& needToToggleItem
) {
830 // Toggle the very last item in the bounds.
831 itemsToToggle
.select(currIndex
, currIndex
);
836 lastIndex
= currIndex
;
837 currIndex
= nextIndex
;
838 } while (!allItemsInBoundDone
);
840 selectionModel()->select(itemsToToggle
, QItemSelectionModel::Toggle
);
842 m_band
.lastSelectionOrigin
= m_band
.origin
;
843 m_band
.lastSelectionDestination
= m_band
.destination
;
844 m_band
.ignoreOldInfo
= false;
847 void DolphinDetailsView::setFoldersExpandable(bool expandable
)
850 // collapse all expanded folders, as QTreeView::setItemsExpandable(false)
851 // does not do this task
852 const int rowCount
= model()->rowCount();
853 for (int row
= 0; row
< rowCount
; ++row
) {
854 setExpanded(model()->index(row
, 0), false);
857 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
858 settings
->setExpandableFolders(expandable
);
859 setRootIsDecorated(expandable
);
860 setItemsExpandable(expandable
);
863 void DolphinDetailsView::updateDecorationSize(bool showPreview
)
865 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
866 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
867 setIconSize(QSize(iconSize
, iconSize
));
868 m_decorationSize
= QSize(iconSize
, iconSize
);
870 if (m_selectionManager
!= 0) {
871 m_selectionManager
->reset();
877 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
879 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
881 switch (columnIndex
) {
882 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
883 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
884 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
885 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
886 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
887 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
894 void DolphinDetailsView::resizeColumns()
896 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
897 // around 3 seconds for each (!) resize operation when having > 10000 items).
898 // This gets a problem especially when opening large directories, where several
899 // resize operations are received for showing the currently available items during
900 // loading (the application hangs around 20 seconds when loading > 10000 items).
902 QHeaderView
* headerView
= header();
903 QFontMetrics
fontMetrics(viewport()->font());
905 int columnWidth
[KDirModel::ColumnCount
];
906 columnWidth
[KDirModel::Size
] = fontMetrics
.width("00000 Items");
907 columnWidth
[KDirModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
908 columnWidth
[KDirModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
909 columnWidth
[KDirModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
910 columnWidth
[KDirModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
911 columnWidth
[KDirModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
913 int requiredWidth
= 0;
914 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
915 if (!isColumnHidden(i
)) {
916 columnWidth
[i
] += 20; // provide a default gap
917 requiredWidth
+= columnWidth
[i
];
918 headerView
->resizeSection(i
, columnWidth
[i
]);
922 // resize the name column in a way that the whole available width is used
923 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
925 const int minNameWidth
= 300;
926 if (columnWidth
[KDirModel::Name
] < minNameWidth
) {
927 columnWidth
[KDirModel::Name
] = minNameWidth
;
929 // It might be possible that the name column width can be
930 // decreased without clipping any text. For performance
931 // reasons the exact necessary width for full visible names is
932 // only checked for up to 200 items:
933 const int rowCount
= model()->rowCount();
934 if (rowCount
> 0 && rowCount
< 200) {
935 const int nameWidth
= sizeHintForColumn(DolphinModel::Name
);
936 if (nameWidth
+ requiredWidth
<= viewport()->width()) {
937 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
938 } else if (nameWidth
< minNameWidth
) {
939 columnWidth
[KDirModel::Name
] = nameWidth
;
944 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
947 QRect
DolphinDetailsView::nameColumnRect(const QModelIndex
& index
) const
949 QRect rect
= visualRect(index
);
950 const KFileItem item
= m_controller
->itemForIndex(index
);
951 if (!item
.isNull()) {
952 const int width
= DolphinFileItemDelegate::nameColumnWidth(item
.text(), viewOptions());
953 rect
.setWidth(width
);
959 bool DolphinDetailsView::isAboveExpandingToggle(const QPoint
& pos
) const
961 // QTreeView offers no public API to get the information whether an index has an
962 // expanding toggle and what boundaries the toggle has. The following approach
963 // also assumes a toggle for file items.
964 if (itemsExpandable()) {
965 const QModelIndex index
= QTreeView::indexAt(pos
);
966 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
967 QRect rect
= nameColumnRect(index
);
968 const int toggleSize
= rect
.height();
969 if (isRightToLeft()) {
970 rect
.moveRight(rect
.right());
972 rect
.moveLeft(rect
.x() - toggleSize
);
974 rect
.setWidth(toggleSize
);
979 rect
= style()->subElementRect(QStyle::SE_TreeViewDisclosureItem
, &opt
, this);
981 return rect
.contains(pos
);
987 DolphinDetailsView::ElasticBand::ElasticBand() :
991 lastSelectionOrigin(),
992 lastSelectionDestination(),
994 outsideNearestLeftEdge(0),
995 outsideNearestRightEdge(0),
996 insideNearestLeftEdge(0),
997 insideNearestRightEdge(0)
1001 #include "dolphindetailsview.moc"