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 "viewextensionsfactory.h"
31 #include "viewproperties.h"
32 #include "zoomlevelinfo.h"
34 #include "dolphin_detailsmodesettings.h"
35 #include "dolphin_generalsettings.h"
37 #include <kdirmodel.h>
42 #include <QApplication>
43 #include <QHeaderView>
44 #include <QRubberBand>
48 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
,
49 DolphinController
* controller
,
50 DolphinSortFilterProxyModel
* proxyModel
) :
53 m_expandingTogglePressed(false),
55 m_useDefaultIndexAt(true),
56 m_ignoreScrollTo(false),
57 m_controller(controller
),
58 m_extensionsFactory(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
);
81 setMouseTracking(true);
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 connect(this, SIGNAL(entered(const QModelIndex
&)),
114 this, SLOT(slotEntered(const QModelIndex
&)));
115 connect(this, SIGNAL(viewportEntered()),
116 controller
, SLOT(emitViewportEntered()));
117 connect(controller
, SIGNAL(zoomLevelChanged(int)),
118 this, SLOT(setZoomLevel(int)));
119 connect(controller
->dolphinView(), SIGNAL(additionalInfoChanged()),
120 this, SLOT(updateColumnVisibility()));
121 connect(controller
, SIGNAL(activationChanged(bool)),
122 this, SLOT(slotActivationChanged(bool)));
124 if (settings
->useSystemFont()) {
125 m_font
= KGlobalSettings::generalFont();
127 m_font
= QFont(settings
->fontFamily(),
128 settings
->fontSize(),
129 settings
->fontWeight(),
130 settings
->italicFont());
133 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
134 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
136 const DolphinView
* view
= controller
->dolphinView();
137 connect(view
, SIGNAL(showPreviewChanged()),
138 this, SLOT(slotShowPreviewChanged()));
142 viewport()->installEventFilter(this);
144 connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)),
145 this, SLOT(slotGlobalSettingsChanged(int)));
147 m_useDefaultIndexAt
= false;
149 m_expandableFoldersAction
= new QAction(i18nc("@option:check", "Expandable Folders"), this);
150 m_expandableFoldersAction
->setCheckable(true);
151 connect(m_expandableFoldersAction
, SIGNAL(toggled(bool)),
152 this, SLOT(setFoldersExpandable(bool)));
154 m_extensionsFactory
= new ViewExtensionsFactory(this, controller
);
155 m_extensionsFactory
->fileItemDelegate()->setMinimizedNameColumn(true);
157 updateDecorationSize(view
->showPreview());
160 DolphinDetailsView::~DolphinDetailsView()
164 bool DolphinDetailsView::event(QEvent
* event
)
166 if (event
->type() == QEvent::Polish
) {
167 header()->setResizeMode(QHeaderView::Interactive
);
168 updateColumnVisibility();
171 return QTreeView::event(event
);
174 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
176 QStyleOptionViewItem viewOptions
= QTreeView::viewOptions();
177 viewOptions
.font
= m_font
;
178 viewOptions
.showDecorationSelected
= true;
179 viewOptions
.decorationSize
= m_decorationSize
;
183 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
185 QTreeView::contextMenuEvent(event
);
187 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
188 m_expandableFoldersAction
->setChecked(settings
->expandableFolders());
189 m_controller
->triggerContextMenuRequest(event
->pos(),
190 QList
<QAction
*>() << m_expandableFoldersAction
);
193 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
195 m_controller
->requestActivation();
197 const QModelIndex current
= currentIndex();
198 QTreeView::mousePressEvent(event
);
200 m_expandingTogglePressed
= isAboveExpandingToggle(event
->pos());
202 const QModelIndex index
= indexAt(event
->pos());
203 const bool updateState
= index
.isValid() &&
204 (index
.column() == DolphinModel::Name
) &&
205 (event
->button() == Qt::LeftButton
);
207 setState(QAbstractItemView::DraggingState
);
210 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
211 // the mouse press is done somewhere outside the filename column
212 if (QApplication::mouseButtons() & Qt::MidButton
) {
213 m_controller
->replaceUrlByClipboard();
216 const Qt::KeyboardModifiers mod
= QApplication::keyboardModifiers();
217 if (!m_expandingTogglePressed
&& !(mod
& Qt::ShiftModifier
) && !(mod
& Qt::ControlModifier
)) {
221 // restore the current index, other columns are handled as viewport area.
222 // setCurrentIndex(...) implicitly calls scrollTo(...), which we want to ignore.
223 m_ignoreScrollTo
= true;
224 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
225 m_ignoreScrollTo
= false;
227 if ((event
->button() == Qt::LeftButton
) && !m_expandingTogglePressed
) {
228 // Inform Qt about what we are doing - otherwise it starts dragging items around!
229 setState(DragSelectingState
);
231 // Incremental update data will not be useful - start from scratch.
232 m_band
.ignoreOldInfo
= true;
233 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
234 m_band
.origin
= event
->pos() + scrollPos
;
235 m_band
.destination
= m_band
.origin
;
236 m_band
.originalSelection
= selectionModel()->selection();
241 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
243 if (m_expandingTogglePressed
) {
244 // Per default QTreeView starts either a selection or a drag operation when dragging
245 // the expanding toggle button (Qt-issue - see TODO comment in DolphinIconsView::mousePressEvent()).
246 // Turn off this behavior in Dolphin to stay predictable:
247 setState(QAbstractItemView::NoState
);
252 const QPoint mousePos
= event
->pos();
253 const QModelIndex index
= indexAt(mousePos
);
254 if (!index
.isValid()) {
255 // the destination of the selection rectangle is above the viewport. In this
256 // case QTreeView does no selection at all, which is not the wanted behavior
257 // in Dolphin -> select all items within the elastic band rectangle
258 updateElasticBandSelection();
261 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
262 // as the Qt-issue #199631 has been fixed.
263 // QTreeView::mouseMoveEvent(event);
264 QAbstractItemView::mouseMoveEvent(event
);
267 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
268 // as the Qt-issue #199631 has been fixed.
269 // QTreeView::mouseMoveEvent(event);
270 QAbstractItemView::mouseMoveEvent(event
);
274 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
276 if (!m_expandingTogglePressed
) {
277 const QModelIndex index
= indexAt(event
->pos());
278 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
279 QTreeView::mouseReleaseEvent(event
);
281 // don't change the current index if the cursor is released
282 // above any other column than the name column, as the other
283 // columns act as viewport
284 const QModelIndex current
= currentIndex();
285 QTreeView::mouseReleaseEvent(event
);
286 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
289 m_expandingTogglePressed
= false;
298 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions
)
300 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_controller
);
304 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
306 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
307 event
->acceptProposedAction();
316 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
318 QTreeView::dragLeaveEvent(event
);
319 setDirtyRegion(m_dropRect
);
322 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
324 QTreeView::dragMoveEvent(event
);
326 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
327 setDirtyRegion(m_dropRect
);
328 const QModelIndex index
= indexAt(event
->pos());
329 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
330 const KFileItem item
= m_controller
->itemForIndex(index
);
331 if (!item
.isNull() && item
.isDir()) {
332 m_dropRect
= visualRect(index
);
334 m_dropRect
.setSize(QSize()); // set as invalid
336 setDirtyRegion(m_dropRect
);
339 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
340 // accept url drops, independently from the destination item
341 event
->acceptProposedAction();
345 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
347 const QModelIndex index
= indexAt(event
->pos());
349 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
350 item
= m_controller
->itemForIndex(index
);
352 m_controller
->indicateDroppedUrls(item
, m_controller
->url(), event
);
353 QTreeView::dropEvent(event
);
356 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
358 QTreeView::paintEvent(event
);
360 // The following code has been taken from QListView
361 // and adapted to DolphinDetailsView.
362 // (C) 1992-2007 Trolltech ASA
363 QStyleOptionRubberBand opt
;
365 opt
.shape
= QRubberBand::Rectangle
;
367 opt
.rect
= elasticBandRect();
369 QPainter
painter(viewport());
371 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
376 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
378 // If the Control modifier is pressed, a multiple selection
379 // is done and DolphinDetailsView::currentChanged() may not
380 // not change the selection in a custom way.
381 m_keyPressed
= !(event
->modifiers() & Qt::ControlModifier
);
383 QTreeView::keyPressEvent(event
);
384 m_controller
->handleKeyPressEvent(event
);
387 void DolphinDetailsView::keyReleaseEvent(QKeyEvent
* event
)
389 QTreeView::keyReleaseEvent(event
);
390 m_keyPressed
= false;
393 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
395 QTreeView::resizeEvent(event
);
401 void DolphinDetailsView::wheelEvent(QWheelEvent
* event
)
403 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
404 if (event
->modifiers() & Qt::ControlModifier
) {
409 const int height
= m_decorationSize
.height();
410 const int step
= (height
>= KIconLoader::SizeHuge
) ? height
/ 10 : (KIconLoader::SizeHuge
- height
) / 2;
411 verticalScrollBar()->setSingleStep(step
);
412 QTreeView::wheelEvent(event
);
415 void DolphinDetailsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
417 QTreeView::currentChanged(current
, previous
);
418 m_extensionsFactory
->handleCurrentIndexChange(current
, previous
);
420 // Stay consistent with QListView: When changing the current index by key presses,
421 // also change the selection.
423 selectionModel()->select(current
, QItemSelectionModel::ClearAndSelect
);
427 bool DolphinDetailsView::eventFilter(QObject
* watched
, QEvent
* event
)
429 if ((watched
== viewport()) && (event
->type() == QEvent::Leave
)) {
430 // if the mouse is above an item and moved very fast outside the widget,
431 // no viewportEntered() signal might be emitted although the mouse has been moved
432 // above the viewport
433 m_controller
->emitViewportEntered();
436 return QTreeView::eventFilter(watched
, event
);
439 QModelIndex
DolphinDetailsView::indexAt(const QPoint
& point
) const
441 // the blank portion of the name column counts as empty space
442 const QModelIndex index
= QTreeView::indexAt(point
);
443 const bool isAboveEmptySpace
= !m_useDefaultIndexAt
&&
444 (index
.column() == KDirModel::Name
) && !nameColumnRect(index
).contains(point
);
445 return isAboveEmptySpace
? QModelIndex() : index
;
448 void DolphinDetailsView::setSelection(const QRect
& rect
, QItemSelectionModel::SelectionFlags command
)
450 // We must override setSelection() as Qt calls it internally and when this happens
451 // we must ensure that the default indexAt() is used.
453 m_useDefaultIndexAt
= true;
454 QTreeView::setSelection(rect
, command
);
455 m_useDefaultIndexAt
= false;
457 // Use our own elastic band selection algorithm
458 updateElasticBandSelection();
462 void DolphinDetailsView::scrollTo(const QModelIndex
& index
, ScrollHint hint
)
464 if (!m_ignoreScrollTo
) {
465 QTreeView::scrollTo(index
, hint
);
469 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
471 header()->setSortIndicator(sorting
, header()->sortIndicatorOrder());
474 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
476 header()->setSortIndicator(header()->sortIndicatorSection(), sortOrder
);
479 void DolphinDetailsView::synchronizeSortingState(int column
)
481 // The sorting has already been changed in QTreeView if this slot is
482 // invoked, but Dolphin is not informed about this.
483 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
484 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
485 m_controller
->indicateSortingChange(sorting
);
486 m_controller
->indicateSortOrderChange(sortOrder
);
489 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
491 if (index
.column() == DolphinModel::Name
) {
492 m_controller
->emitItemEntered(index
);
494 m_controller
->emitViewportEntered();
498 void DolphinDetailsView::updateElasticBand()
501 QRect
dirtyRegion(elasticBandRect());
502 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
503 m_band
.destination
= viewport()->mapFromGlobal(QCursor::pos()) + scrollPos
;
504 // Going above the (logical) top-left of the view causes complications during selection;
505 // we may as well prevent it.
506 if (m_band
.destination
.y() < 0) {
507 m_band
.destination
.setY(0);
509 if (m_band
.destination
.x() < 0) {
510 m_band
.destination
.setX(0);
512 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
513 setDirtyRegion(dirtyRegion
);
517 QRect
DolphinDetailsView::elasticBandRect() const
519 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
521 const QPoint topLeft
= m_band
.origin
- scrollPos
;
522 const QPoint bottomRight
= m_band
.destination
- scrollPos
;
523 return QRect(topLeft
, bottomRight
).normalized();
526 void DolphinDetailsView::setZoomLevel(int level
)
528 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
529 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
531 const bool showPreview
= m_controller
->dolphinView()->showPreview();
533 settings
->setPreviewSize(size
);
535 settings
->setIconSize(size
);
538 updateDecorationSize(showPreview
);
541 void DolphinDetailsView::slotShowPreviewChanged()
543 const DolphinView
* view
= m_controller
->dolphinView();
544 updateDecorationSize(view
->showPreview());
547 void DolphinDetailsView::configureSettings(const QPoint
& pos
)
550 popup
.addTitle(i18nc("@title:menu", "Columns"));
552 // add checkbox items for each column
553 QHeaderView
* headerView
= header();
554 const int columns
= model()->columnCount();
555 for (int i
= 0; i
< columns
; ++i
) {
556 const int logicalIndex
= headerView
->logicalIndex(i
);
557 const QString text
= model()->headerData(logicalIndex
, Qt::Horizontal
).toString();
558 QAction
* action
= popup
.addAction(text
);
559 action
->setCheckable(true);
560 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
561 action
->setData(logicalIndex
);
562 action
->setEnabled(logicalIndex
!= DolphinModel::Name
);
564 popup
.addSeparator();
566 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
567 if (activatedAction
!= 0) {
568 const bool show
= activatedAction
->isChecked();
569 const int columnIndex
= activatedAction
->data().toInt();
571 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
572 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
574 Q_ASSERT(!list
.contains(info
));
577 Q_ASSERT(list
.contains(info
));
578 const int index
= list
.indexOf(info
);
579 list
.removeAt(index
);
582 m_controller
->indicateAdditionalInfoChange(list
);
583 setColumnHidden(columnIndex
, !show
);
588 void DolphinDetailsView::updateColumnVisibility()
590 QHeaderView
* headerView
= header();
591 disconnect(headerView
, SIGNAL(sectionMoved(int, int, int)),
592 this, SLOT(saveColumnPositions()));
594 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
595 const QList
<int> columnPositions
= settings
->columnPositions();
597 const KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
598 for (int i
= DolphinModel::Name
; i
<= DolphinModel::Version
; ++i
) {
599 const KFileItemDelegate::Information info
= infoForColumn(i
);
600 const bool hide
= !list
.contains(info
) && (i
!= DolphinModel::Name
);
601 if (isColumnHidden(i
) != hide
) {
602 setColumnHidden(i
, hide
);
605 const int from
= headerView
->visualIndex(i
);
606 headerView
->moveSection(from
, columnPositions
[i
]);
611 connect(headerView
, SIGNAL(sectionMoved(int, int, int)),
612 this, SLOT(saveColumnPositions()));
616 void DolphinDetailsView::saveColumnPositions()
618 QList
<int> columnPositions
;
619 for (int i
= DolphinModel::Name
; i
<= DolphinModel::Version
; ++i
) {
620 columnPositions
.append(header()->visualIndex(i
));
623 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
624 settings
->setColumnPositions(columnPositions
);
627 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex
, int oldSize
, int newSize
)
629 Q_UNUSED(logicalIndex
);
632 // If the user changes the size of the headers, the autoresize feature should be
633 // turned off. As there is no dedicated interface to find out whether the header
634 // section has been resized by the user or by a resize event, another approach is used.
635 // Attention: Take care when changing the if-condition to verify that there is no
636 // regression in combination with bug 178630 (see fix in comment #8).
637 if ((QApplication::mouseButtons() & Qt::LeftButton
) && header()->underMouse()) {
638 disableAutoResizing();
642 void DolphinDetailsView::slotActivationChanged(bool active
)
644 setAlternatingRowColors(active
);
647 void DolphinDetailsView::disableAutoResizing()
649 m_autoResize
= false;
652 void DolphinDetailsView::requestActivation()
654 m_controller
->requestActivation();
657 void DolphinDetailsView::slotGlobalSettingsChanged(int category
)
661 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
662 Q_ASSERT(settings
!= 0);
663 if (settings
->useSystemFont()) {
664 m_font
= KGlobalSettings::generalFont();
666 //Disconnect then reconnect, since the settings have been changed, the connection requirements may have also.
667 disconnect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
668 disconnect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
669 if (KGlobalSettings::singleClick()) {
670 connect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
672 connect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
676 void DolphinDetailsView::updateElasticBandSelection()
682 // Ensure the elastic band itself is up-to-date, in
683 // case we are being called due to e.g. a drag event.
686 // Clip horizontally to the name column, as some filenames will be
687 // longer than the column. We don't clip vertically as origin
688 // may be above or below the current viewport area.
689 const int nameColumnX
= header()->sectionPosition(DolphinModel::Name
);
690 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
691 QRect selRect
= elasticBandRect().normalized();
692 QRect
nameColumnArea(nameColumnX
, selRect
.y(), nameColumnWidth
, selRect
.height());
693 selRect
= nameColumnArea
.intersect(selRect
).normalized();
694 // Get the last elastic band rectangle, expressed in viewpoint coordinates.
695 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
696 QRect oldSelRect
= QRect(m_band
.lastSelectionOrigin
- scrollPos
, m_band
.lastSelectionDestination
- scrollPos
).normalized();
698 if (selRect
.isNull()) {
699 selectionModel()->select(m_band
.originalSelection
, QItemSelectionModel::ClearAndSelect
);
700 m_band
.ignoreOldInfo
= true;
704 if (!m_band
.ignoreOldInfo
) {
705 // Do some quick checks to see if we can rule out the need to
706 // update the selection.
707 Q_ASSERT(uniformRowHeights());
708 QModelIndex dummyIndex
= model()->index(0, 0);
709 if (!dummyIndex
.isValid()) {
710 // No items in the model presumably.
714 // If the elastic band does not cover the same rows as before, we'll
715 // need to re-check, and also invalidate the old item distances.
716 const int rowHeight
= QTreeView::rowHeight(dummyIndex
);
717 const bool coveringSameRows
=
718 (selRect
.top() / rowHeight
== oldSelRect
.top() / rowHeight
) &&
719 (selRect
.bottom() / rowHeight
== oldSelRect
.bottom() / rowHeight
);
720 if (coveringSameRows
) {
721 // Covering the same rows, but have we moved far enough horizontally
722 // that we might have (de)selected some other items?
723 const bool itemSelectionChanged
=
724 ((selRect
.left() > oldSelRect
.left()) &&
725 (selRect
.left() > m_band
.insideNearestLeftEdge
)) ||
726 ((selRect
.left() < oldSelRect
.left()) &&
727 (selRect
.left() <= m_band
.outsideNearestLeftEdge
)) ||
728 ((selRect
.right() < oldSelRect
.right()) &&
729 (selRect
.left() >= m_band
.insideNearestRightEdge
)) ||
730 ((selRect
.right() > oldSelRect
.right()) &&
731 (selRect
.right() >= m_band
.outsideNearestRightEdge
));
733 if (!itemSelectionChanged
) {
739 // This is the only piece of optimization data that needs to be explicitly
741 m_band
.lastSelectionOrigin
= QPoint();
742 m_band
.lastSelectionDestination
= QPoint();
743 oldSelRect
= selRect
;
746 // Do the selection from scratch. Force a update of the horizontal distances info.
747 m_band
.insideNearestLeftEdge
= nameColumnX
+ nameColumnWidth
+ 1;
748 m_band
.insideNearestRightEdge
= nameColumnX
- 1;
749 m_band
.outsideNearestLeftEdge
= nameColumnX
- 1;
750 m_band
.outsideNearestRightEdge
= nameColumnX
+ nameColumnWidth
+ 1;
752 // Include the old selection rect as well, so we can deselect
753 // items that were inside it but not in the new selRect.
754 const QRect boundingRect
= selRect
.united(oldSelRect
).normalized();
755 if (boundingRect
.isNull()) {
759 // Get the index of the item in this row in the name column.
760 // TODO - would this still work if the columns could be re-ordered?
761 QModelIndex startIndex
= QTreeView::indexAt(boundingRect
.topLeft());
762 if (startIndex
.parent().isValid()) {
763 startIndex
= startIndex
.parent().child(startIndex
.row(), KDirModel::Name
);
765 startIndex
= model()->index(startIndex
.row(), KDirModel::Name
);
767 if (!startIndex
.isValid()) {
768 selectionModel()->select(m_band
.originalSelection
, QItemSelectionModel::ClearAndSelect
);
769 m_band
.ignoreOldInfo
= true;
773 // Go through all indexes between the top and bottom of boundingRect, and
774 // update the selection.
775 const int verticalCutoff
= boundingRect
.bottom();
776 QModelIndex currIndex
= startIndex
;
777 QModelIndex lastIndex
;
778 bool allItemsInBoundDone
= false;
780 // Calling selectionModel()->select(...) for each item that needs to be
781 // toggled is slow as each call emits selectionChanged(...) so store them
782 // and do the selection toggle in one batch.
783 QItemSelection itemsToToggle
;
784 // QItemSelection's deal with continuous ranges of indexes better than
785 // single indexes, so try to portion items that need to be toggled into ranges.
786 bool formingToggleIndexRange
= false;
787 QModelIndex toggleIndexRangeBegin
= QModelIndex();
790 QRect currIndexRect
= nameColumnRect(currIndex
);
792 // Update some optimization info as we go.
793 const int cr
= currIndexRect
.right();
794 const int cl
= currIndexRect
.left();
795 const int sl
= selRect
.left();
796 const int sr
= selRect
.right();
797 // "The right edge of the name is outside of the rect but nearer than m_outsideNearestLeft", etc
798 if ((cr
< sl
&& cr
> m_band
.outsideNearestLeftEdge
)) {
799 m_band
.outsideNearestLeftEdge
= cr
;
801 if ((cl
> sr
&& cl
< m_band
.outsideNearestRightEdge
)) {
802 m_band
.outsideNearestRightEdge
= cl
;
804 if ((cl
>= sl
&& cl
<= sr
&& cl
> m_band
.insideNearestRightEdge
)) {
805 m_band
.insideNearestRightEdge
= cl
;
807 if ((cr
>= sl
&& cr
<= sr
&& cr
< m_band
.insideNearestLeftEdge
)) {
808 m_band
.insideNearestLeftEdge
= cr
;
811 bool currentlySelected
= selectionModel()->isSelected(currIndex
);
812 bool originallySelected
= m_band
.originalSelection
.contains(currIndex
);
813 bool intersectsSelectedRect
= currIndexRect
.intersects(selRect
);
814 bool shouldBeSelected
= (intersectsSelectedRect
&& !originallySelected
) || (!intersectsSelectedRect
&& originallySelected
);
815 bool needToToggleItem
= (currentlySelected
&& !shouldBeSelected
) || (!currentlySelected
&& shouldBeSelected
);
816 if (needToToggleItem
&& !formingToggleIndexRange
) {
817 toggleIndexRangeBegin
= currIndex
;
818 formingToggleIndexRange
= true;
821 // NOTE: indexBelow actually walks up and down expanded trees for us.
822 QModelIndex nextIndex
= indexBelow(currIndex
);
823 allItemsInBoundDone
= !nextIndex
.isValid() || currIndexRect
.top() > verticalCutoff
;
825 const bool commitToggleIndexRange
= formingToggleIndexRange
&&
826 (!needToToggleItem
||
827 allItemsInBoundDone
||
828 currIndex
.parent() != toggleIndexRangeBegin
.parent());
829 if (commitToggleIndexRange
) {
830 formingToggleIndexRange
= false;
831 // If this is the last item in the bounds and it is also the beginning of a range,
832 // don't toggle lastIndex - it will already have been dealt with.
833 if (!allItemsInBoundDone
|| toggleIndexRangeBegin
!= currIndex
) {
834 itemsToToggle
.select(toggleIndexRangeBegin
, lastIndex
);
836 // Need to start a new range immediately with currIndex?
837 if (needToToggleItem
) {
838 toggleIndexRangeBegin
= currIndex
;
839 formingToggleIndexRange
= true;
841 if (allItemsInBoundDone
&& needToToggleItem
) {
842 // Toggle the very last item in the bounds.
843 itemsToToggle
.select(currIndex
, currIndex
);
848 lastIndex
= currIndex
;
849 currIndex
= nextIndex
;
850 } while (!allItemsInBoundDone
);
852 selectionModel()->select(itemsToToggle
, QItemSelectionModel::Toggle
);
854 m_band
.lastSelectionOrigin
= m_band
.origin
;
855 m_band
.lastSelectionDestination
= m_band
.destination
;
856 m_band
.ignoreOldInfo
= false;
859 void DolphinDetailsView::setFoldersExpandable(bool expandable
)
862 // collapse all expanded folders, as QTreeView::setItemsExpandable(false)
863 // does not do this task
864 const int rowCount
= model()->rowCount();
865 for (int row
= 0; row
< rowCount
; ++row
) {
866 setExpanded(model()->index(row
, 0), false);
869 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
870 settings
->setExpandableFolders(expandable
);
871 setRootIsDecorated(expandable
);
872 setItemsExpandable(expandable
);
875 void DolphinDetailsView::updateDecorationSize(bool showPreview
)
877 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
878 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
879 setIconSize(QSize(iconSize
, iconSize
));
880 m_decorationSize
= QSize(iconSize
, iconSize
);
885 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
887 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
889 switch (columnIndex
) {
890 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
891 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
892 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
893 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
894 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
895 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
902 void DolphinDetailsView::resizeColumns()
904 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
905 // around 3 seconds for each (!) resize operation when having > 10000 items).
906 // This gets a problem especially when opening large directories, where several
907 // resize operations are received for showing the currently available items during
908 // loading (the application hangs around 20 seconds when loading > 10000 items).
910 QHeaderView
* headerView
= header();
911 QFontMetrics
fontMetrics(viewport()->font());
913 int columnWidth
[DolphinModel::Version
+ 1];
914 columnWidth
[DolphinModel::Size
] = fontMetrics
.width("00000 Items");
915 columnWidth
[DolphinModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
916 columnWidth
[DolphinModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
917 columnWidth
[DolphinModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
918 columnWidth
[DolphinModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
919 columnWidth
[DolphinModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
920 columnWidth
[DolphinModel::Version
] = fontMetrics
.width("xxxxxxxx");
922 int requiredWidth
= 0;
923 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
924 if (!isColumnHidden(i
)) {
925 columnWidth
[i
] += 20; // provide a default gap
926 requiredWidth
+= columnWidth
[i
];
927 headerView
->resizeSection(i
, columnWidth
[i
]);
931 // resize the name column in a way that the whole available width is used
932 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
934 const int minNameWidth
= 300;
935 if (columnWidth
[KDirModel::Name
] < minNameWidth
) {
936 columnWidth
[KDirModel::Name
] = minNameWidth
;
938 // It might be possible that the name column width can be
939 // decreased without clipping any text. For performance
940 // reasons the exact necessary width for full visible names is
941 // only checked for up to 200 items:
942 const int rowCount
= model()->rowCount();
943 if (rowCount
> 0 && rowCount
< 200) {
944 const int nameWidth
= sizeHintForColumn(DolphinModel::Name
);
945 if (nameWidth
+ requiredWidth
<= viewport()->width()) {
946 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
947 } else if (nameWidth
< minNameWidth
) {
948 columnWidth
[KDirModel::Name
] = nameWidth
;
953 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
956 QRect
DolphinDetailsView::nameColumnRect(const QModelIndex
& index
) const
958 QRect rect
= visualRect(index
);
959 const KFileItem item
= m_controller
->itemForIndex(index
);
960 if (!item
.isNull()) {
961 const int width
= DolphinFileItemDelegate::nameColumnWidth(item
.text(), viewOptions());
962 rect
.setWidth(width
);
968 bool DolphinDetailsView::isAboveExpandingToggle(const QPoint
& pos
) const
970 // QTreeView offers no public API to get the information whether an index has an
971 // expanding toggle and what boundaries the toggle has. The following approach
972 // also assumes a toggle for file items.
973 if (itemsExpandable()) {
974 const QModelIndex index
= QTreeView::indexAt(pos
);
975 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
976 QRect rect
= nameColumnRect(index
);
977 const int toggleSize
= rect
.height();
978 if (isRightToLeft()) {
979 rect
.moveRight(rect
.right());
981 rect
.moveLeft(rect
.x() - toggleSize
);
983 rect
.setWidth(toggleSize
);
988 rect
= style()->subElementRect(QStyle::SE_TreeViewDisclosureItem
, &opt
, this);
990 return rect
.contains(pos
);
996 DolphinDetailsView::ElasticBand::ElasticBand() :
1000 lastSelectionOrigin(),
1001 lastSelectionDestination(),
1002 ignoreOldInfo(true),
1003 outsideNearestLeftEdge(0),
1004 outsideNearestRightEdge(0),
1005 insideNearestLeftEdge(0),
1006 insideNearestRightEdge(0)
1010 #include "dolphindetailsview.moc"