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();
178 return QTreeView::event(event
);
181 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
183 QStyleOptionViewItem viewOptions
= QTreeView::viewOptions();
184 viewOptions
.font
= m_font
;
185 viewOptions
.showDecorationSelected
= true;
186 viewOptions
.decorationSize
= m_decorationSize
;
190 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
192 QTreeView::contextMenuEvent(event
);
194 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
195 m_expandableFoldersAction
->setChecked(settings
->expandableFolders());
196 m_controller
->triggerContextMenuRequest(event
->pos(),
197 QList
<QAction
*>() << m_expandableFoldersAction
);
200 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
202 m_controller
->requestActivation();
204 const QModelIndex current
= currentIndex();
205 QTreeView::mousePressEvent(event
);
207 m_expandingTogglePressed
= isAboveExpandingToggle(event
->pos());
209 const QModelIndex index
= indexAt(event
->pos());
210 const bool updateState
= index
.isValid() &&
211 (index
.column() == DolphinModel::Name
) &&
212 (event
->button() == Qt::LeftButton
);
214 setState(QAbstractItemView::DraggingState
);
217 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
218 // the mouse press is done somewhere outside the filename column
219 if (QApplication::mouseButtons() & Qt::MidButton
) {
220 m_controller
->replaceUrlByClipboard();
223 const Qt::KeyboardModifiers mod
= QApplication::keyboardModifiers();
224 if (!m_expandingTogglePressed
&& !(mod
& Qt::ShiftModifier
) && !(mod
& Qt::ControlModifier
)) {
228 // restore the current index, other columns are handled as viewport area.
229 // setCurrentIndex(...) implicitly calls scrollTo(...), which we want to ignore.
230 m_ignoreScrollTo
= true;
231 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
232 m_ignoreScrollTo
= false;
234 if ((event
->button() == Qt::LeftButton
) && !m_expandingTogglePressed
) {
235 // Inform Qt about what we are doing - otherwise it starts dragging items around!
236 setState(DragSelectingState
);
238 // Incremental update data will not be useful - start from scratch.
239 m_band
.ignoreOldInfo
= true;
240 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
241 m_band
.origin
= event
->pos() + scrollPos
;
242 m_band
.destination
= m_band
.origin
;
243 m_band
.originalSelection
= selectionModel()->selection();
248 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
250 if (m_expandingTogglePressed
) {
251 // Per default QTreeView starts either a selection or a drag operation when dragging
252 // the expanding toggle button (Qt-issue - see TODO comment in DolphinIconsView::mousePressEvent()).
253 // Turn off this behavior in Dolphin to stay predictable:
254 setState(QAbstractItemView::NoState
);
259 const QPoint mousePos
= event
->pos();
260 const QModelIndex index
= indexAt(mousePos
);
261 if (!index
.isValid()) {
262 // the destination of the selection rectangle is above the viewport. In this
263 // case QTreeView does no selection at all, which is not the wanted behavior
264 // in Dolphin -> select all items within the elastic band rectangle
265 updateElasticBandSelection();
268 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
269 // as the Qt-issue #199631 has been fixed.
270 // QTreeView::mouseMoveEvent(event);
271 QAbstractItemView::mouseMoveEvent(event
);
274 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
275 // as the Qt-issue #199631 has been fixed.
276 // QTreeView::mouseMoveEvent(event);
277 QAbstractItemView::mouseMoveEvent(event
);
281 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
283 if (!m_expandingTogglePressed
) {
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;
305 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions
)
307 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_controller
);
311 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
313 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
314 event
->acceptProposedAction();
323 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
325 QTreeView::dragLeaveEvent(event
);
326 setDirtyRegion(m_dropRect
);
329 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
331 QTreeView::dragMoveEvent(event
);
333 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
334 setDirtyRegion(m_dropRect
);
335 const QModelIndex index
= indexAt(event
->pos());
336 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
337 const KFileItem item
= m_controller
->itemForIndex(index
);
338 if (!item
.isNull() && item
.isDir()) {
339 m_dropRect
= visualRect(index
);
341 m_dropRect
.setSize(QSize()); // set as invalid
343 setDirtyRegion(m_dropRect
);
346 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
347 // accept url drops, independently from the destination item
348 event
->acceptProposedAction();
352 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
354 const QModelIndex index
= indexAt(event
->pos());
356 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
357 item
= m_controller
->itemForIndex(index
);
359 m_controller
->indicateDroppedUrls(item
, m_controller
->url(), event
);
360 QTreeView::dropEvent(event
);
363 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
365 QTreeView::paintEvent(event
);
367 // The following code has been taken from QListView
368 // and adapted to DolphinDetailsView.
369 // (C) 1992-2007 Trolltech ASA
370 QStyleOptionRubberBand opt
;
372 opt
.shape
= QRubberBand::Rectangle
;
374 opt
.rect
= elasticBandRect();
376 QPainter
painter(viewport());
378 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
383 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
385 // If the Control modifier is pressed, a multiple selection
386 // is done and DolphinDetailsView::currentChanged() may not
387 // not change the selection in a custom way.
388 m_keyPressed
= !(event
->modifiers() & Qt::ControlModifier
);
390 QTreeView::keyPressEvent(event
);
391 m_controller
->handleKeyPressEvent(event
);
394 void DolphinDetailsView::keyReleaseEvent(QKeyEvent
* event
)
396 QTreeView::keyReleaseEvent(event
);
397 m_keyPressed
= false;
400 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
402 QTreeView::resizeEvent(event
);
408 void DolphinDetailsView::wheelEvent(QWheelEvent
* event
)
410 if (m_selectionManager
!= 0) {
411 m_selectionManager
->reset();
414 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
415 if (event
->modifiers() & Qt::ControlModifier
) {
420 const int height
= m_decorationSize
.height();
421 const int step
= (height
>= KIconLoader::SizeHuge
) ? height
/ 10 : (KIconLoader::SizeHuge
- height
) / 2;
422 verticalScrollBar()->setSingleStep(step
);
423 QTreeView::wheelEvent(event
);
426 void DolphinDetailsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
428 QTreeView::currentChanged(current
, previous
);
429 m_autoScroller
->handleCurrentIndexChange(current
, previous
);
431 // Stay consistent with QListView: When changing the current index by key presses,
432 // also change the selection.
434 selectionModel()->select(current
, QItemSelectionModel::ClearAndSelect
);
438 bool DolphinDetailsView::eventFilter(QObject
* watched
, QEvent
* event
)
440 if ((watched
== viewport()) && (event
->type() == QEvent::Leave
)) {
441 // if the mouse is above an item and moved very fast outside the widget,
442 // no viewportEntered() signal might be emitted although the mouse has been moved
443 // above the viewport
444 m_controller
->emitViewportEntered();
447 return QTreeView::eventFilter(watched
, event
);
450 QModelIndex
DolphinDetailsView::indexAt(const QPoint
& point
) const
452 // the blank portion of the name column counts as empty space
453 const QModelIndex index
= QTreeView::indexAt(point
);
454 const bool isAboveEmptySpace
= !m_useDefaultIndexAt
&&
455 (index
.column() == KDirModel::Name
) && !nameColumnRect(index
).contains(point
);
456 return isAboveEmptySpace
? QModelIndex() : index
;
459 void DolphinDetailsView::setSelection(const QRect
& rect
, QItemSelectionModel::SelectionFlags command
)
461 // We must override setSelection() as Qt calls it internally and when this happens
462 // we must ensure that the default indexAt() is used.
464 m_useDefaultIndexAt
= true;
465 QTreeView::setSelection(rect
, command
);
466 m_useDefaultIndexAt
= false;
468 // Use our own elastic band selection algorithm
469 updateElasticBandSelection();
473 void DolphinDetailsView::scrollTo(const QModelIndex
& index
, ScrollHint hint
)
475 if (!m_ignoreScrollTo
) {
476 QTreeView::scrollTo(index
, hint
);
480 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
482 header()->setSortIndicator(sorting
, header()->sortIndicatorOrder());
485 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
487 header()->setSortIndicator(header()->sortIndicatorSection(), sortOrder
);
490 void DolphinDetailsView::synchronizeSortingState(int column
)
492 // The sorting has already been changed in QTreeView if this slot is
493 // invoked, but Dolphin is not informed about this.
494 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
495 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
496 m_controller
->indicateSortingChange(sorting
);
497 m_controller
->indicateSortOrderChange(sortOrder
);
500 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
502 if (index
.column() == DolphinModel::Name
) {
503 m_controller
->emitItemEntered(index
);
505 m_controller
->emitViewportEntered();
509 void DolphinDetailsView::updateElasticBand()
512 QRect
dirtyRegion(elasticBandRect());
513 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
514 m_band
.destination
= viewport()->mapFromGlobal(QCursor::pos()) + scrollPos
;
515 // Going above the (logical) top-left of the view causes complications during selection;
516 // we may as well prevent it.
517 if (m_band
.destination
.y() < 0) {
518 m_band
.destination
.setY(0);
520 if (m_band
.destination
.x() < 0) {
521 m_band
.destination
.setX(0);
523 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
524 setDirtyRegion(dirtyRegion
);
528 QRect
DolphinDetailsView::elasticBandRect() const
530 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
532 const QPoint topLeft
= m_band
.origin
- scrollPos
;
533 const QPoint bottomRight
= m_band
.destination
- scrollPos
;
534 return QRect(topLeft
, bottomRight
).normalized();
537 void DolphinDetailsView::setZoomLevel(int level
)
539 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
540 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
542 const bool showPreview
= m_controller
->dolphinView()->showPreview();
544 settings
->setPreviewSize(size
);
546 settings
->setIconSize(size
);
549 updateDecorationSize(showPreview
);
553 void DolphinDetailsView::slotShowPreviewChanged()
555 const DolphinView
* view
= m_controller
->dolphinView();
556 updateDecorationSize(view
->showPreview());
559 void DolphinDetailsView::configureSettings(const QPoint
& pos
)
562 popup
.addTitle(i18nc("@title:menu", "Columns"));
564 // add checkbox items for each column
565 QHeaderView
* headerView
= header();
566 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Version
; ++i
) {
567 const int logicalIndex
= headerView
->logicalIndex(i
);
568 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
569 QAction
* action
= popup
.addAction(text
);
570 action
->setCheckable(true);
571 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
574 popup
.addSeparator();
576 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
577 if (activatedAction
!= 0) {
578 const bool show
= activatedAction
->isChecked();
579 const int columnIndex
= activatedAction
->data().toInt();
581 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
582 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
584 Q_ASSERT(!list
.contains(info
));
587 Q_ASSERT(list
.contains(info
));
588 const int index
= list
.indexOf(info
);
589 list
.removeAt(index
);
592 m_controller
->indicateAdditionalInfoChange(list
);
593 setColumnHidden(columnIndex
, !show
);
598 void DolphinDetailsView::updateColumnVisibility()
600 const KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
601 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Version
; ++i
) {
602 const KFileItemDelegate::Information info
= infoForColumn(i
);
603 const bool hide
= !list
.contains(info
);
604 if (isColumnHidden(i
) != hide
) {
605 setColumnHidden(i
, hide
);
612 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex
, int oldSize
, int newSize
)
614 Q_UNUSED(logicalIndex
);
617 // If the user changes the size of the headers, the autoresize feature should be
618 // turned off. As there is no dedicated interface to find out whether the header
619 // section has been resized by the user or by a resize event, another approach is used.
620 // Attention: Take care when changing the if-condition to verify that there is no
621 // regression in combination with bug 178630 (see fix in comment #8).
622 if ((QApplication::mouseButtons() & Qt::LeftButton
) && header()->underMouse()) {
623 disableAutoResizing();
627 void DolphinDetailsView::slotActivationChanged(bool active
)
629 setAlternatingRowColors(active
);
632 void DolphinDetailsView::disableAutoResizing()
634 m_autoResize
= false;
637 void DolphinDetailsView::requestActivation()
639 m_controller
->requestActivation();
642 void DolphinDetailsView::slotGlobalSettingsChanged(int category
)
646 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
647 Q_ASSERT(settings
!= 0);
648 if (settings
->useSystemFont()) {
649 m_font
= KGlobalSettings::generalFont();
651 //Disconnect then reconnect, since the settings have been changed, the connection requirements may have also.
652 disconnect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
653 disconnect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
654 if (KGlobalSettings::singleClick()) {
655 connect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
657 connect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
661 void DolphinDetailsView::updateElasticBandSelection()
667 // Ensure the elastic band itself is up-to-date, in
668 // case we are being called due to e.g. a drag event.
671 // Clip horizontally to the name column, as some filenames will be
672 // longer than the column. We don't clip vertically as origin
673 // may be above or below the current viewport area.
674 const int nameColumnX
= header()->sectionPosition(DolphinModel::Name
);
675 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
676 QRect selRect
= elasticBandRect().normalized();
677 QRect
nameColumnArea(nameColumnX
, selRect
.y(), nameColumnWidth
, selRect
.height());
678 selRect
= nameColumnArea
.intersect(selRect
).normalized();
679 // Get the last elastic band rectangle, expressed in viewpoint coordinates.
680 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
681 QRect oldSelRect
= QRect(m_band
.lastSelectionOrigin
- scrollPos
, m_band
.lastSelectionDestination
- scrollPos
).normalized();
683 if (selRect
.isNull()) {
684 selectionModel()->select(m_band
.originalSelection
, QItemSelectionModel::ClearAndSelect
);
685 m_band
.ignoreOldInfo
= true;
689 if (!m_band
.ignoreOldInfo
) {
690 // Do some quick checks to see if we can rule out the need to
691 // update the selection.
692 Q_ASSERT(uniformRowHeights());
693 QModelIndex dummyIndex
= model()->index(0, 0);
694 if (!dummyIndex
.isValid()) {
695 // No items in the model presumably.
699 // If the elastic band does not cover the same rows as before, we'll
700 // need to re-check, and also invalidate the old item distances.
701 const int rowHeight
= QTreeView::rowHeight(dummyIndex
);
702 const bool coveringSameRows
=
703 (selRect
.top() / rowHeight
== oldSelRect
.top() / rowHeight
) &&
704 (selRect
.bottom() / rowHeight
== oldSelRect
.bottom() / rowHeight
);
705 if (coveringSameRows
) {
706 // Covering the same rows, but have we moved far enough horizontally
707 // that we might have (de)selected some other items?
708 const bool itemSelectionChanged
=
709 ((selRect
.left() > oldSelRect
.left()) &&
710 (selRect
.left() > m_band
.insideNearestLeftEdge
)) ||
711 ((selRect
.left() < oldSelRect
.left()) &&
712 (selRect
.left() <= m_band
.outsideNearestLeftEdge
)) ||
713 ((selRect
.right() < oldSelRect
.right()) &&
714 (selRect
.left() >= m_band
.insideNearestRightEdge
)) ||
715 ((selRect
.right() > oldSelRect
.right()) &&
716 (selRect
.right() >= m_band
.outsideNearestRightEdge
));
718 if (!itemSelectionChanged
) {
724 // This is the only piece of optimization data that needs to be explicitly
726 m_band
.lastSelectionOrigin
= QPoint();
727 m_band
.lastSelectionDestination
= QPoint();
728 oldSelRect
= selRect
;
731 // Do the selection from scratch. Force a update of the horizontal distances info.
732 m_band
.insideNearestLeftEdge
= nameColumnX
+ nameColumnWidth
+ 1;
733 m_band
.insideNearestRightEdge
= nameColumnX
- 1;
734 m_band
.outsideNearestLeftEdge
= nameColumnX
- 1;
735 m_band
.outsideNearestRightEdge
= nameColumnX
+ nameColumnWidth
+ 1;
737 // Include the old selection rect as well, so we can deselect
738 // items that were inside it but not in the new selRect.
739 const QRect boundingRect
= selRect
.united(oldSelRect
).normalized();
740 if (boundingRect
.isNull()) {
744 // Get the index of the item in this row in the name column.
745 // TODO - would this still work if the columns could be re-ordered?
746 QModelIndex startIndex
= QTreeView::indexAt(boundingRect
.topLeft());
747 if (startIndex
.parent().isValid()) {
748 startIndex
= startIndex
.parent().child(startIndex
.row(), KDirModel::Name
);
750 startIndex
= model()->index(startIndex
.row(), KDirModel::Name
);
752 if (!startIndex
.isValid()) {
753 selectionModel()->select(m_band
.originalSelection
, QItemSelectionModel::ClearAndSelect
);
754 m_band
.ignoreOldInfo
= true;
758 // Go through all indexes between the top and bottom of boundingRect, and
759 // update the selection.
760 const int verticalCutoff
= boundingRect
.bottom();
761 QModelIndex currIndex
= startIndex
;
762 QModelIndex lastIndex
;
763 bool allItemsInBoundDone
= false;
765 // Calling selectionModel()->select(...) for each item that needs to be
766 // toggled is slow as each call emits selectionChanged(...) so store them
767 // and do the selection toggle in one batch.
768 QItemSelection itemsToToggle
;
769 // QItemSelection's deal with continuous ranges of indexes better than
770 // single indexes, so try to portion items that need to be toggled into ranges.
771 bool formingToggleIndexRange
= false;
772 QModelIndex toggleIndexRangeBegin
= QModelIndex();
775 QRect currIndexRect
= nameColumnRect(currIndex
);
777 // Update some optimization info as we go.
778 const int cr
= currIndexRect
.right();
779 const int cl
= currIndexRect
.left();
780 const int sl
= selRect
.left();
781 const int sr
= selRect
.right();
782 // "The right edge of the name is outside of the rect but nearer than m_outsideNearestLeft", etc
783 if ((cr
< sl
&& cr
> m_band
.outsideNearestLeftEdge
)) {
784 m_band
.outsideNearestLeftEdge
= cr
;
786 if ((cl
> sr
&& cl
< m_band
.outsideNearestRightEdge
)) {
787 m_band
.outsideNearestRightEdge
= cl
;
789 if ((cl
>= sl
&& cl
<= sr
&& cl
> m_band
.insideNearestRightEdge
)) {
790 m_band
.insideNearestRightEdge
= cl
;
792 if ((cr
>= sl
&& cr
<= sr
&& cr
< m_band
.insideNearestLeftEdge
)) {
793 m_band
.insideNearestLeftEdge
= cr
;
796 bool currentlySelected
= selectionModel()->isSelected(currIndex
);
797 bool originallySelected
= m_band
.originalSelection
.contains(currIndex
);
798 bool intersectsSelectedRect
= currIndexRect
.intersects(selRect
);
799 bool shouldBeSelected
= (intersectsSelectedRect
&& !originallySelected
) || (!intersectsSelectedRect
&& originallySelected
);
800 bool needToToggleItem
= (currentlySelected
&& !shouldBeSelected
) || (!currentlySelected
&& shouldBeSelected
);
801 if (needToToggleItem
&& !formingToggleIndexRange
) {
802 toggleIndexRangeBegin
= currIndex
;
803 formingToggleIndexRange
= true;
806 // NOTE: indexBelow actually walks up and down expanded trees for us.
807 QModelIndex nextIndex
= indexBelow(currIndex
);
808 allItemsInBoundDone
= !nextIndex
.isValid() || currIndexRect
.top() > verticalCutoff
;
810 const bool commitToggleIndexRange
= formingToggleIndexRange
&&
811 (!needToToggleItem
||
812 allItemsInBoundDone
||
813 currIndex
.parent() != toggleIndexRangeBegin
.parent());
814 if (commitToggleIndexRange
) {
815 formingToggleIndexRange
= false;
816 // If this is the last item in the bounds and it is also the beginning of a range,
817 // don't toggle lastIndex - it will already have been dealt with.
818 if (!allItemsInBoundDone
|| toggleIndexRangeBegin
!= currIndex
) {
819 itemsToToggle
.select(toggleIndexRangeBegin
, lastIndex
);
821 // Need to start a new range immediately with currIndex?
822 if (needToToggleItem
) {
823 toggleIndexRangeBegin
= currIndex
;
824 formingToggleIndexRange
= true;
826 if (allItemsInBoundDone
&& needToToggleItem
) {
827 // Toggle the very last item in the bounds.
828 itemsToToggle
.select(currIndex
, currIndex
);
833 lastIndex
= currIndex
;
834 currIndex
= nextIndex
;
835 } while (!allItemsInBoundDone
);
837 selectionModel()->select(itemsToToggle
, QItemSelectionModel::Toggle
);
839 m_band
.lastSelectionOrigin
= m_band
.origin
;
840 m_band
.lastSelectionDestination
= m_band
.destination
;
841 m_band
.ignoreOldInfo
= false;
844 void DolphinDetailsView::setFoldersExpandable(bool expandable
)
847 // collapse all expanded folders, as QTreeView::setItemsExpandable(false)
848 // does not do this task
849 const int rowCount
= model()->rowCount();
850 for (int row
= 0; row
< rowCount
; ++row
) {
851 setExpanded(model()->index(row
, 0), false);
854 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
855 settings
->setExpandableFolders(expandable
);
856 setRootIsDecorated(expandable
);
857 setItemsExpandable(expandable
);
860 void DolphinDetailsView::updateDecorationSize(bool showPreview
)
862 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
863 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
864 setIconSize(QSize(iconSize
, iconSize
));
865 m_decorationSize
= QSize(iconSize
, iconSize
);
867 if (m_selectionManager
!= 0) {
868 m_selectionManager
->reset();
874 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
876 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
878 switch (columnIndex
) {
879 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
880 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
881 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
882 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
883 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
884 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
891 void DolphinDetailsView::resizeColumns()
893 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
894 // around 3 seconds for each (!) resize operation when having > 10000 items).
895 // This gets a problem especially when opening large directories, where several
896 // resize operations are received for showing the currently available items during
897 // loading (the application hangs around 20 seconds when loading > 10000 items).
899 QHeaderView
* headerView
= header();
900 QFontMetrics
fontMetrics(viewport()->font());
902 int columnWidth
[DolphinModel::Version
+ 1];
903 columnWidth
[DolphinModel::Size
] = fontMetrics
.width("00000 Items");
904 columnWidth
[DolphinModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
905 columnWidth
[DolphinModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
906 columnWidth
[DolphinModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
907 columnWidth
[DolphinModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
908 columnWidth
[DolphinModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
909 columnWidth
[DolphinModel::Version
] = fontMetrics
.width("xxxxxxxx");
911 int requiredWidth
= 0;
912 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
913 if (!isColumnHidden(i
)) {
914 columnWidth
[i
] += 20; // provide a default gap
915 requiredWidth
+= columnWidth
[i
];
916 headerView
->resizeSection(i
, columnWidth
[i
]);
920 // resize the name column in a way that the whole available width is used
921 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
923 const int minNameWidth
= 300;
924 if (columnWidth
[KDirModel::Name
] < minNameWidth
) {
925 columnWidth
[KDirModel::Name
] = minNameWidth
;
927 // It might be possible that the name column width can be
928 // decreased without clipping any text. For performance
929 // reasons the exact necessary width for full visible names is
930 // only checked for up to 200 items:
931 const int rowCount
= model()->rowCount();
932 if (rowCount
> 0 && rowCount
< 200) {
933 const int nameWidth
= sizeHintForColumn(DolphinModel::Name
);
934 if (nameWidth
+ requiredWidth
<= viewport()->width()) {
935 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
936 } else if (nameWidth
< minNameWidth
) {
937 columnWidth
[KDirModel::Name
] = nameWidth
;
942 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
945 QRect
DolphinDetailsView::nameColumnRect(const QModelIndex
& index
) const
947 QRect rect
= visualRect(index
);
948 const KFileItem item
= m_controller
->itemForIndex(index
);
949 if (!item
.isNull()) {
950 const int width
= DolphinFileItemDelegate::nameColumnWidth(item
.text(), viewOptions());
951 rect
.setWidth(width
);
957 bool DolphinDetailsView::isAboveExpandingToggle(const QPoint
& pos
) const
959 // QTreeView offers no public API to get the information whether an index has an
960 // expanding toggle and what boundaries the toggle has. The following approach
961 // also assumes a toggle for file items.
962 if (itemsExpandable()) {
963 const QModelIndex index
= QTreeView::indexAt(pos
);
964 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
965 QRect rect
= nameColumnRect(index
);
966 const int toggleSize
= rect
.height();
967 if (isRightToLeft()) {
968 rect
.moveRight(rect
.right());
970 rect
.moveLeft(rect
.x() - toggleSize
);
972 rect
.setWidth(toggleSize
);
977 rect
= style()->subElementRect(QStyle::SE_TreeViewDisclosureItem
, &opt
, this);
979 return rect
.contains(pos
);
985 DolphinDetailsView::ElasticBand::ElasticBand() :
989 lastSelectionOrigin(),
990 lastSelectionDestination(),
992 outsideNearestLeftEdge(0),
993 outsideNearestRightEdge(0),
994 insideNearestLeftEdge(0),
995 insideNearestRightEdge(0)
999 #include "dolphindetailsview.moc"