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 "dolphinsettings.h"
27 #include "dolphinsortfilterproxymodel.h"
28 #include "draganddrophelper.h"
29 #include "selectionmanager.h"
30 #include "viewproperties.h"
31 #include "zoomlevelinfo.h"
33 #include "dolphin_detailsmodesettings.h"
34 #include "dolphin_generalsettings.h"
36 #include <kdirmodel.h>
40 #include <QAbstractProxyModel>
42 #include <QApplication>
43 #include <QHeaderView>
44 #include <QRubberBand>
48 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
, DolphinController
* controller
) :
51 m_expandingTogglePressed(false),
53 m_useDefaultIndexAt(true),
54 m_controller(controller
),
55 m_selectionManager(0),
59 m_ignoreScrollTo(false)
61 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
62 Q_ASSERT(settings
!= 0);
63 Q_ASSERT(controller
!= 0);
65 setLayoutDirection(Qt::LeftToRight
);
67 setSortingEnabled(true);
68 setUniformRowHeights(true);
69 setSelectionBehavior(SelectItems
);
70 setDragDropMode(QAbstractItemView::DragDrop
);
71 setDropIndicatorShown(false);
72 setAlternatingRowColors(true);
73 setRootIsDecorated(settings
->expandableFolders());
74 setItemsExpandable(settings
->expandableFolders());
75 setEditTriggers(QAbstractItemView::NoEditTriggers
);
77 setMouseTracking(true);
79 const ViewProperties
props(controller
->url());
80 setSortIndicatorSection(props
.sorting());
81 setSortIndicatorOrder(props
.sortOrder());
83 QHeaderView
* headerView
= header();
84 connect(headerView
, SIGNAL(sectionClicked(int)),
85 this, SLOT(synchronizeSortingState(int)));
86 headerView
->setContextMenuPolicy(Qt::CustomContextMenu
);
87 connect(headerView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
88 this, SLOT(configureColumns(const QPoint
&)));
89 connect(headerView
, SIGNAL(sectionResized(int, int, int)),
90 this, SLOT(slotHeaderSectionResized(int, int, int)));
91 connect(headerView
, SIGNAL(sectionHandleDoubleClicked(int)),
92 this, SLOT(disableAutoResizing()));
94 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
95 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
96 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
97 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
99 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
100 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
101 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
102 // RETURN-key in keyPressEvent().
103 if (KGlobalSettings::singleClick()) {
104 connect(this, SIGNAL(clicked(const QModelIndex
&)),
105 controller
, SLOT(triggerItem(const QModelIndex
&)));
107 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
108 controller
, SLOT(triggerItem(const QModelIndex
&)));
111 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
112 m_selectionManager
= new SelectionManager(this);
113 connect(m_selectionManager
, SIGNAL(selectionChanged()),
114 this, SLOT(requestActivation()));
115 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
116 m_selectionManager
, SLOT(reset()));
119 connect(this, SIGNAL(entered(const QModelIndex
&)),
120 this, SLOT(slotEntered(const QModelIndex
&)));
121 connect(this, SIGNAL(viewportEntered()),
122 controller
, SLOT(emitViewportEntered()));
123 connect(controller
, SIGNAL(zoomLevelChanged(int)),
124 this, SLOT(setZoomLevel(int)));
125 connect(controller
->dolphinView(), SIGNAL(additionalInfoChanged()),
126 this, SLOT(updateColumnVisibility()));
127 connect(controller
, SIGNAL(activationChanged(bool)),
128 this, SLOT(slotActivationChanged(bool)));
130 if (settings
->useSystemFont()) {
131 m_font
= KGlobalSettings::generalFont();
133 m_font
= QFont(settings
->fontFamily(),
134 settings
->fontSize(),
135 settings
->fontWeight(),
136 settings
->italicFont());
139 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
140 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
142 const DolphinView
* view
= controller
->dolphinView();
143 connect(view
, SIGNAL(showPreviewChanged()),
144 this, SLOT(slotShowPreviewChanged()));
146 updateDecorationSize(view
->showPreview());
149 viewport()->installEventFilter(this);
151 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
152 this, SLOT(updateFont()));
154 m_useDefaultIndexAt
= false;
157 DolphinDetailsView::~DolphinDetailsView()
161 bool DolphinDetailsView::event(QEvent
* event
)
163 if (event
->type() == QEvent::Polish
) {
164 QHeaderView
* headerView
= header();
165 headerView
->setResizeMode(QHeaderView::Interactive
);
166 headerView
->setMovable(false);
168 updateColumnVisibility();
170 hideColumn(DolphinModel::Rating
);
171 hideColumn(DolphinModel::Tags
);
172 } else if (event
->type() == QEvent::UpdateRequest
) {
173 // a wheel movement will scroll 4 items
174 if (model()->rowCount() > 0) {
175 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
179 return QTreeView::event(event
);
182 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
184 QStyleOptionViewItem viewOptions
= QTreeView::viewOptions();
185 viewOptions
.font
= m_font
;
186 viewOptions
.showDecorationSelected
= true;
187 viewOptions
.decorationSize
= m_decorationSize
;
191 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
193 QTreeView::contextMenuEvent(event
);
194 m_controller
->triggerContextMenuRequest(event
->pos());
197 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
199 m_controller
->requestActivation();
201 const QModelIndex current
= currentIndex();
202 QTreeView::mousePressEvent(event
);
204 m_expandingTogglePressed
= false;
205 const QModelIndex index
= indexAt(event
->pos());
206 const bool updateState
= index
.isValid() &&
207 (index
.column() == DolphinModel::Name
) &&
208 (event
->button() == Qt::LeftButton
);
210 // TODO: See comment in DolphinIconsView::mousePressEvent(). Only update
211 // the state if no expanding/collapsing area has been hit:
212 const QRect rect
= visualRect(index
);
213 if (event
->pos().x() >= rect
.x() + indentation()) {
214 setState(QAbstractItemView::DraggingState
);
216 m_expandingTogglePressed
= true;
217 kDebug() << "m_expandingTogglePressed " << m_expandingTogglePressed
;
221 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
222 // the mouse press is done somewhere outside the filename column
223 if (QApplication::mouseButtons() & Qt::MidButton
) {
224 m_controller
->replaceUrlByClipboard();
227 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
228 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
232 // restore the current index, other columns are handled as viewport area.
233 // setCurrentIndex(...) implicitly calls scrollTo(...), which we want to ignore.
234 m_ignoreScrollTo
= true;
235 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
236 m_ignoreScrollTo
= false;
238 if ((event
->button() == Qt::LeftButton
) && !m_expandingTogglePressed
) {
239 // Inform Qt about what we are doing - otherwise it starts dragging items around!
240 setState(DragSelectingState
);
242 // Incremental update data will not be useful - start from scratch.
243 m_band
.ignoreOldInfo
= true;
244 const QPoint pos
= contentsPos();
245 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
246 m_band
.origin
= event
->pos() + pos
+ scrollPos
;
247 m_band
.destination
= m_band
.origin
;
248 m_band
.originalSelection
= selectionModel()->selection();
253 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
256 const QPoint mousePos
= event
->pos();
257 const QModelIndex index
= indexAt(mousePos
);
258 if (!index
.isValid()) {
259 // the destination of the selection rectangle is above the viewport. In this
260 // case QTreeView does no selection at all, which is not the wanted behavior
261 // in Dolphin -> select all items within the elastic band rectangle
262 updateElasticBandSelection();
265 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
266 // as the Qt-issue #199631 has been fixed.
267 // QTreeView::mouseMoveEvent(event);
268 QAbstractItemView::mouseMoveEvent(event
);
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 if (m_expandingTogglePressed
) {
278 // Per default QTreeView starts either a selection or a drag operation when dragging
279 // the expanding toggle button (Qt-issue - see TODO comment in DolphinIconsView::mousePressEvent()).
280 // Turn off this behavior in Dolphin to stay predictable:
282 setState(QAbstractItemView::NoState
);
286 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
288 const QModelIndex index
= indexAt(event
->pos());
289 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
290 QTreeView::mouseReleaseEvent(event
);
292 // don't change the current index if the cursor is released
293 // above any other column than the name column, as the other
294 // columns act as viewport
295 const QModelIndex current
= currentIndex();
296 QTreeView::mouseReleaseEvent(event
);
297 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
300 m_expandingTogglePressed
= false;
308 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions
)
310 DragAndDropHelper::startDrag(this, supportedActions
, m_controller
);
314 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
316 if (DragAndDropHelper::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::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
)
408 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 QTreeView::wheelEvent(event
);
426 void DolphinDetailsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
428 QTreeView::currentChanged(current
, previous
);
430 // Stay consistent with QListView: When changing the current index by key presses,
431 // also change the selection.
433 selectionModel()->select(current
, QItemSelectionModel::ClearAndSelect
);
437 bool DolphinDetailsView::eventFilter(QObject
* watched
, QEvent
* event
)
439 if ((watched
== viewport()) && (event
->type() == QEvent::Leave
)) {
440 // if the mouse is above an item and moved very fast outside the widget,
441 // no viewportEntered() signal might be emitted although the mouse has been moved
442 // above the viewport
443 m_controller
->emitViewportEntered();
446 return QTreeView::eventFilter(watched
, event
);
449 QModelIndex
DolphinDetailsView::indexAt(const QPoint
& point
) const
451 // the blank portion of the name column counts as empty space
452 const QModelIndex index
= QTreeView::indexAt(point
);
453 const bool isAboveEmptySpace
= !m_useDefaultIndexAt
&&
454 (index
.column() == KDirModel::Name
) && !nameColumnRect(index
).contains(point
);
455 return isAboveEmptySpace
? QModelIndex() : index
;
458 void DolphinDetailsView::setSelection(const QRect
&rect
, QItemSelectionModel::SelectionFlags command
)
460 // We must override setSelection() as Qt calls it internally and when this happens
461 // we must ensure that the default indexAt() is used.
463 m_useDefaultIndexAt
= true;
464 QTreeView::setSelection(rect
, command
);
465 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
)
477 QTreeView::scrollTo(index
, hint
);
480 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
482 QHeaderView
* headerView
= header();
483 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
486 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
488 QHeaderView
* headerView
= header();
489 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
492 void DolphinDetailsView::synchronizeSortingState(int column
)
494 // The sorting has already been changed in QTreeView if this slot is
495 // invoked, but Dolphin is not informed about this.
496 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
497 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
498 m_controller
->indicateSortingChange(sorting
);
499 m_controller
->indicateSortOrderChange(sortOrder
);
502 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
504 if (index
.column() == DolphinModel::Name
) {
505 m_controller
->emitItemEntered(index
);
507 m_controller
->emitViewportEntered();
511 void DolphinDetailsView::updateElasticBand()
514 QRect
dirtyRegion(elasticBandRect());
515 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
516 m_band
.destination
= viewport()->mapFromGlobal(QCursor::pos()) + scrollPos
;
517 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
518 setDirtyRegion(dirtyRegion
);
522 QRect
DolphinDetailsView::elasticBandRect() const
524 const QPoint
pos(contentsPos());
525 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
527 const QPoint topLeft
= m_band
.origin
- pos
- scrollPos
;
528 const QPoint bottomRight
= m_band
.destination
- pos
- scrollPos
;
529 return QRect(topLeft
, bottomRight
).normalized();
532 void DolphinDetailsView::setZoomLevel(int level
)
534 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
535 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
537 const bool showPreview
= m_controller
->dolphinView()->showPreview();
539 settings
->setPreviewSize(size
);
541 settings
->setIconSize(size
);
544 updateDecorationSize(showPreview
);
548 void DolphinDetailsView::slotShowPreviewChanged()
550 const DolphinView
* view
= m_controller
->dolphinView();
551 updateDecorationSize(view
->showPreview());
554 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
557 popup
.addTitle(i18nc("@title:menu", "Columns"));
559 QHeaderView
* headerView
= header();
560 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
561 const int logicalIndex
= headerView
->logicalIndex(i
);
562 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
563 QAction
* action
= popup
.addAction(text
);
564 action
->setCheckable(true);
565 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
569 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
570 if (activatedAction
!= 0) {
571 const bool show
= activatedAction
->isChecked();
572 const int columnIndex
= activatedAction
->data().toInt();
574 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
575 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
577 Q_ASSERT(!list
.contains(info
));
580 Q_ASSERT(list
.contains(info
));
581 const int index
= list
.indexOf(info
);
582 list
.removeAt(index
);
585 m_controller
->indicateAdditionalInfoChange(list
);
586 setColumnHidden(columnIndex
, !show
);
590 void DolphinDetailsView::updateColumnVisibility()
592 const KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
593 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
594 const KFileItemDelegate::Information info
= infoForColumn(i
);
595 const bool hide
= !list
.contains(info
);
596 if (isColumnHidden(i
) != hide
) {
597 setColumnHidden(i
, hide
);
604 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex
, int oldSize
, int newSize
)
606 Q_UNUSED(logicalIndex
);
609 if (QApplication::mouseButtons() & Qt::LeftButton
) {
610 disableAutoResizing();
614 void DolphinDetailsView::slotActivationChanged(bool active
)
616 setAlternatingRowColors(active
);
619 void DolphinDetailsView::disableAutoResizing()
621 m_autoResize
= false;
624 void DolphinDetailsView::requestActivation()
626 m_controller
->requestActivation();
629 void DolphinDetailsView::updateFont()
631 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
632 Q_ASSERT(settings
!= 0);
634 if (settings
->useSystemFont()) {
635 m_font
= KGlobalSettings::generalFont();
639 void DolphinDetailsView::updateElasticBandSelection()
645 // Ensure the elastic band itself is up-to-date, in
646 // case we are being called due to e.g. a drag event.
649 // Clip horizontally to the name column, as some filenames will be
650 // longer than the column. We don't clip vertically as origin
651 // may be above or below the current viewport area.
652 const int nameColumnX
= header()->sectionPosition(DolphinModel::Name
);
653 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
654 QRect selRect
= elasticBandRect().normalized();
655 QRect
nameColumnRect(nameColumnX
, selRect
.y(), nameColumnWidth
, selRect
.height());
656 selRect
= nameColumnRect
.intersect(selRect
).normalized();
658 if (selRect
.isNull()) {
659 selectionModel()->select(m_band
.originalSelection
, QItemSelectionModel::ClearAndSelect
);
660 m_band
.ignoreOldInfo
= true;
664 if (!m_band
.ignoreOldInfo
) {
665 // Do some quick checks to see if we can rule out the need to
666 // update the selection.
667 Q_ASSERT(uniformRowHeights());
668 QModelIndex dummyIndex
= model()->index(0, 0);
669 if (!dummyIndex
.isValid()) {
670 // No items in the model presumably.
674 // If the elastic band does not cover the same rows as before, we'll
675 // need to re-check, and also invalidate the old item distances.
676 const int rowHeight
= QTreeView::rowHeight(dummyIndex
);
677 const bool coveringSameRows
=
678 (selRect
.top() / rowHeight
== m_band
.oldSelectionRect
.top() / rowHeight
) &&
679 (selRect
.bottom() / rowHeight
== m_band
.oldSelectionRect
.bottom() / rowHeight
);
680 if (coveringSameRows
) {
681 // Covering the same rows, but have we moved far enough horizontally
682 // that we might have (de)selected some other items?
683 const QRect oldSelRect
= m_band
.oldSelectionRect
;
684 const bool itemSelectionChanged
=
685 ((selRect
.left() > oldSelRect
.left()) &&
686 (selRect
.left() > m_band
.insideNearestLeftEdge
)) ||
687 ((selRect
.left() < oldSelRect
.left()) &&
688 (selRect
.left() <= m_band
.outsideNearestLeftEdge
)) ||
689 ((selRect
.right() < oldSelRect
.right()) &&
690 (selRect
.left() >= m_band
.insideNearestRightEdge
)) ||
691 ((selRect
.right() > oldSelRect
.right()) &&
692 (selRect
.right() >= m_band
.outsideNearestRightEdge
));
694 if (!itemSelectionChanged
) {
700 // Do the selection from scratch. Force a update of the horizontal distances info.
701 m_band
.insideNearestLeftEdge
= nameColumnX
+ nameColumnWidth
+ 1;
702 m_band
.insideNearestRightEdge
= nameColumnX
- 1;
703 m_band
.outsideNearestLeftEdge
= nameColumnX
- 1;
704 m_band
.outsideNearestRightEdge
= nameColumnX
+ nameColumnWidth
+ 1;
706 // Include the old selection rect as well, so we can deselect
707 // items that were inside it but not in the new selRect.
708 const QRect boundingRect
= selRect
.united(m_band
.oldSelectionRect
).normalized();
709 if (boundingRect
.isNull()) {
713 // Get the index of the item in this row in the name column.
714 // TODO - would this still work if the columns could be re-ordered?
715 QModelIndex startIndex
= QTreeView::indexAt(boundingRect
.topLeft());
716 if (startIndex
.parent().isValid()) {
717 startIndex
= startIndex
.parent().child(startIndex
.row(), KDirModel::Name
);
719 startIndex
= model()->index(startIndex
.row(), KDirModel::Name
);
721 if (!startIndex
.isValid()) {
722 selectionModel()->select(m_band
.originalSelection
, QItemSelectionModel::ClearAndSelect
);
723 m_band
.ignoreOldInfo
= true;
727 // Go through all indexes between the top and bottom of boundingRect, and
728 // update the selection.
729 const int verticalCutoff
= boundingRect
.bottom();
730 QModelIndex currIndex
= startIndex
;
731 QModelIndex lastIndex
;
732 bool allItemsInBoundDone
= false;
734 // Calling selectionModel()->select(...) for each item that needs to be
735 // toggled is slow as each call emits selectionChanged(...) so store them
736 // and do the selection toggle in one batch.
737 QItemSelection itemsToToggle
;
738 // QItemSelection's deal with continuous ranges of indexes better than
739 // single indexes, so try to portion items that need to be toggled into ranges.
740 bool formingToggleIndexRange
= false;
741 QModelIndex toggleIndexRangeBegin
= QModelIndex();
744 QRect currIndexRect
= visualRect(currIndex
);
745 const QString name
= m_controller
->itemForIndex(currIndex
).name();
746 currIndexRect
.setWidth(DolphinFileItemDelegate::nameColumnWidth(name
, viewOptions()));
748 // Update some optimisation info as we go.
749 const int cr
= currIndexRect
.right();
750 const int cl
= currIndexRect
.left();
751 const int sl
= selRect
.left();
752 const int sr
= selRect
.right();
753 // "The right edge of the name is outside of the rect but nearer than m_outsideNearestLeft", etc
754 if ((cr
< sl
&& cr
> m_band
.outsideNearestLeftEdge
)) {
755 m_band
.outsideNearestLeftEdge
= cr
;
757 if ((cl
> sr
&& cl
< m_band
.outsideNearestRightEdge
)) {
758 m_band
.outsideNearestRightEdge
= cl
;
760 if ((cl
>= sl
&& cl
<= sr
&& cl
> m_band
.insideNearestRightEdge
)) {
761 m_band
.insideNearestRightEdge
= cl
;
763 if ((cr
>= sl
&& cr
<= sr
&& cr
< m_band
.insideNearestLeftEdge
)) {
764 m_band
.insideNearestLeftEdge
= cr
;
767 bool currentlySelected
= selectionModel()->isSelected(currIndex
);
768 bool originallySelected
= m_band
.originalSelection
.contains(currIndex
);
769 bool intersectsSelectedRect
= currIndexRect
.intersects(selRect
);
770 bool shouldBeSelected
= (intersectsSelectedRect
&& !originallySelected
) || (!intersectsSelectedRect
&& originallySelected
);
771 bool needToToggleItem
= (currentlySelected
&& !shouldBeSelected
) || (!currentlySelected
&& shouldBeSelected
);
772 if (needToToggleItem
&& !formingToggleIndexRange
) {
773 toggleIndexRangeBegin
= currIndex
;
774 formingToggleIndexRange
= true;
777 // NOTE: indexBelow actually walks up and down expanded trees for us.
778 QModelIndex nextIndex
= indexBelow(currIndex
);
779 allItemsInBoundDone
= !nextIndex
.isValid() || currIndexRect
.top() > verticalCutoff
;
781 const bool commitToggleIndexRange
= formingToggleIndexRange
&&
782 (!needToToggleItem
||
783 allItemsInBoundDone
||
784 currIndex
.parent() != toggleIndexRangeBegin
.parent());
785 if (commitToggleIndexRange
) {
786 itemsToToggle
.select(toggleIndexRangeBegin
, lastIndex
);
787 // Immediately start a new range with currIndex?
788 if (needToToggleItem
) {
789 toggleIndexRangeBegin
= currIndex
;
791 formingToggleIndexRange
= needToToggleItem
;
795 lastIndex
= currIndex
;
796 currIndex
= nextIndex
;
797 } while (!allItemsInBoundDone
);
799 selectionModel()->select(itemsToToggle
, QItemSelectionModel::Toggle
);
801 m_band
.oldSelectionRect
= selRect
;
802 m_band
.ignoreOldInfo
= false;
805 void DolphinDetailsView::updateDecorationSize(bool showPreview
)
807 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
808 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
809 setIconSize(QSize(iconSize
, iconSize
));
810 m_decorationSize
= QSize(iconSize
, iconSize
);
812 if (m_selectionManager
!= 0) {
813 m_selectionManager
->reset();
819 QPoint
DolphinDetailsView::contentsPos() const
821 // implementation note: the horizonal position is ignored currently, as no
822 // horizontal scrolling is done anyway during a selection
823 const QScrollBar
* scrollbar
= verticalScrollBar();
824 Q_ASSERT(scrollbar
!= 0);
826 const int maxHeight
= maximumViewportSize().height();
827 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
828 const int visibleHeight
= model()->rowCount() + 1 - height
;
829 if (visibleHeight
<= 0) {
833 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
837 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
839 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
841 switch (columnIndex
) {
842 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
843 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
844 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
845 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
846 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
847 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
854 void DolphinDetailsView::resizeColumns()
856 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
857 // around 3 seconds for each (!) resize operation when having > 10000 items).
858 // This gets a problem especially when opening large directories, where several
859 // resize operations are received for showing the currently available items during
860 // loading (the application hangs around 20 seconds when loading > 10000 items).
862 QHeaderView
* headerView
= header();
863 QFontMetrics
fontMetrics(viewport()->font());
865 int columnWidth
[KDirModel::ColumnCount
];
866 columnWidth
[KDirModel::Size
] = fontMetrics
.width("00000 Items");
867 columnWidth
[KDirModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
868 columnWidth
[KDirModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
869 columnWidth
[KDirModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
870 columnWidth
[KDirModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
871 columnWidth
[KDirModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
873 int requiredWidth
= 0;
874 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
875 if (!isColumnHidden(i
)) {
876 columnWidth
[i
] += 20; // provide a default gap
877 requiredWidth
+= columnWidth
[i
];
878 headerView
->resizeSection(i
, columnWidth
[i
]);
882 // resize the name column in a way that the whole available width is used
883 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
884 if (columnWidth
[KDirModel::Name
] < 120) {
885 columnWidth
[KDirModel::Name
] = 120;
887 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
890 QRect
DolphinDetailsView::nameColumnRect(const QModelIndex
& index
) const
892 QRect rect
= visualRect(index
);
893 const KFileItem item
= m_controller
->itemForIndex(index
);
894 if (!item
.isNull()) {
895 const int width
= DolphinFileItemDelegate::nameColumnWidth(item
.name(), viewOptions());
896 rect
.setWidth(width
);
902 DolphinDetailsView::ElasticBand::ElasticBand() :
908 outsideNearestLeftEdge(0),
909 outsideNearestRightEdge(0),
910 insideNearestLeftEdge(0),
911 insideNearestRightEdge(0)
915 #include "dolphindetailsview.moc"