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 header()->setResizeMode(QHeaderView::Interactive
);
172 updateColumnVisibility();
175 return QTreeView::event(event
);
178 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
180 QStyleOptionViewItem viewOptions
= QTreeView::viewOptions();
181 viewOptions
.font
= m_font
;
182 viewOptions
.showDecorationSelected
= true;
183 viewOptions
.decorationSize
= m_decorationSize
;
187 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
189 QTreeView::contextMenuEvent(event
);
191 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
192 m_expandableFoldersAction
->setChecked(settings
->expandableFolders());
193 m_controller
->triggerContextMenuRequest(event
->pos(),
194 QList
<QAction
*>() << m_expandableFoldersAction
);
197 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
199 m_controller
->requestActivation();
201 const QModelIndex current
= currentIndex();
202 QTreeView::mousePressEvent(event
);
204 m_expandingTogglePressed
= isAboveExpandingToggle(event
->pos());
206 const QModelIndex index
= indexAt(event
->pos());
207 const bool updateState
= index
.isValid() &&
208 (index
.column() == DolphinModel::Name
) &&
209 (event
->button() == Qt::LeftButton
);
211 setState(QAbstractItemView::DraggingState
);
214 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
215 // the mouse press is done somewhere outside the filename column
216 if (QApplication::mouseButtons() & Qt::MidButton
) {
217 m_controller
->replaceUrlByClipboard();
220 const Qt::KeyboardModifiers mod
= QApplication::keyboardModifiers();
221 if (!m_expandingTogglePressed
&& !(mod
& Qt::ShiftModifier
) && !(mod
& Qt::ControlModifier
)) {
225 // restore the current index, other columns are handled as viewport area.
226 // setCurrentIndex(...) implicitly calls scrollTo(...), which we want to ignore.
227 m_ignoreScrollTo
= true;
228 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
229 m_ignoreScrollTo
= false;
231 if ((event
->button() == Qt::LeftButton
) && !m_expandingTogglePressed
) {
232 // Inform Qt about what we are doing - otherwise it starts dragging items around!
233 setState(DragSelectingState
);
235 // Incremental update data will not be useful - start from scratch.
236 m_band
.ignoreOldInfo
= true;
237 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
238 m_band
.origin
= event
->pos() + scrollPos
;
239 m_band
.destination
= m_band
.origin
;
240 m_band
.originalSelection
= selectionModel()->selection();
245 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
247 if (m_expandingTogglePressed
) {
248 // Per default QTreeView starts either a selection or a drag operation when dragging
249 // the expanding toggle button (Qt-issue - see TODO comment in DolphinIconsView::mousePressEvent()).
250 // Turn off this behavior in Dolphin to stay predictable:
251 setState(QAbstractItemView::NoState
);
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
);
278 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
280 if (!m_expandingTogglePressed
) {
281 const QModelIndex index
= indexAt(event
->pos());
282 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
283 QTreeView::mouseReleaseEvent(event
);
285 // don't change the current index if the cursor is released
286 // above any other column than the name column, as the other
287 // columns act as viewport
288 const QModelIndex current
= currentIndex();
289 QTreeView::mouseReleaseEvent(event
);
290 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
293 m_expandingTogglePressed
= false;
302 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions
)
304 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_controller
);
308 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
310 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
311 event
->acceptProposedAction();
320 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
322 QTreeView::dragLeaveEvent(event
);
323 setDirtyRegion(m_dropRect
);
326 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
328 QTreeView::dragMoveEvent(event
);
330 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
331 setDirtyRegion(m_dropRect
);
332 const QModelIndex index
= indexAt(event
->pos());
333 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
334 const KFileItem item
= m_controller
->itemForIndex(index
);
335 if (!item
.isNull() && item
.isDir()) {
336 m_dropRect
= visualRect(index
);
338 m_dropRect
.setSize(QSize()); // set as invalid
340 setDirtyRegion(m_dropRect
);
343 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
344 // accept url drops, independently from the destination item
345 event
->acceptProposedAction();
349 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
351 const QModelIndex index
= indexAt(event
->pos());
353 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
354 item
= m_controller
->itemForIndex(index
);
356 m_controller
->indicateDroppedUrls(item
, m_controller
->url(), event
);
357 QTreeView::dropEvent(event
);
360 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
362 QTreeView::paintEvent(event
);
364 // The following code has been taken from QListView
365 // and adapted to DolphinDetailsView.
366 // (C) 1992-2007 Trolltech ASA
367 QStyleOptionRubberBand opt
;
369 opt
.shape
= QRubberBand::Rectangle
;
371 opt
.rect
= elasticBandRect();
373 QPainter
painter(viewport());
375 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
380 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
382 // If the Control modifier is pressed, a multiple selection
383 // is done and DolphinDetailsView::currentChanged() may not
384 // not change the selection in a custom way.
385 m_keyPressed
= !(event
->modifiers() & Qt::ControlModifier
);
387 QTreeView::keyPressEvent(event
);
388 m_controller
->handleKeyPressEvent(event
);
391 void DolphinDetailsView::keyReleaseEvent(QKeyEvent
* event
)
393 QTreeView::keyReleaseEvent(event
);
394 m_keyPressed
= false;
397 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
399 QTreeView::resizeEvent(event
);
405 void DolphinDetailsView::wheelEvent(QWheelEvent
* event
)
407 if (m_selectionManager
!= 0) {
408 m_selectionManager
->reset();
411 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
412 if (event
->modifiers() & Qt::ControlModifier
) {
417 const int height
= m_decorationSize
.height();
418 const int step
= (height
>= KIconLoader::SizeHuge
) ? height
/ 10 : (KIconLoader::SizeHuge
- height
) / 2;
419 verticalScrollBar()->setSingleStep(step
);
420 QTreeView::wheelEvent(event
);
423 void DolphinDetailsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
425 QTreeView::currentChanged(current
, previous
);
426 m_autoScroller
->handleCurrentIndexChange(current
, previous
);
428 // Stay consistent with QListView: When changing the current index by key presses,
429 // also change the selection.
431 selectionModel()->select(current
, QItemSelectionModel::ClearAndSelect
);
435 bool DolphinDetailsView::eventFilter(QObject
* watched
, QEvent
* event
)
437 if ((watched
== viewport()) && (event
->type() == QEvent::Leave
)) {
438 // if the mouse is above an item and moved very fast outside the widget,
439 // no viewportEntered() signal might be emitted although the mouse has been moved
440 // above the viewport
441 m_controller
->emitViewportEntered();
444 return QTreeView::eventFilter(watched
, event
);
447 QModelIndex
DolphinDetailsView::indexAt(const QPoint
& point
) const
449 // the blank portion of the name column counts as empty space
450 const QModelIndex index
= QTreeView::indexAt(point
);
451 const bool isAboveEmptySpace
= !m_useDefaultIndexAt
&&
452 (index
.column() == KDirModel::Name
) && !nameColumnRect(index
).contains(point
);
453 return isAboveEmptySpace
? QModelIndex() : index
;
456 void DolphinDetailsView::setSelection(const QRect
& rect
, QItemSelectionModel::SelectionFlags command
)
458 // We must override setSelection() as Qt calls it internally and when this happens
459 // we must ensure that the default indexAt() is used.
461 m_useDefaultIndexAt
= true;
462 QTreeView::setSelection(rect
, command
);
463 m_useDefaultIndexAt
= false;
465 // Use our own elastic band selection algorithm
466 updateElasticBandSelection();
470 void DolphinDetailsView::scrollTo(const QModelIndex
& index
, ScrollHint hint
)
472 if (!m_ignoreScrollTo
) {
473 QTreeView::scrollTo(index
, hint
);
477 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
479 header()->setSortIndicator(sorting
, header()->sortIndicatorOrder());
482 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
484 header()->setSortIndicator(header()->sortIndicatorSection(), sortOrder
);
487 void DolphinDetailsView::synchronizeSortingState(int column
)
489 // The sorting has already been changed in QTreeView if this slot is
490 // invoked, but Dolphin is not informed about this.
491 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
492 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
493 m_controller
->indicateSortingChange(sorting
);
494 m_controller
->indicateSortOrderChange(sortOrder
);
497 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
499 if (index
.column() == DolphinModel::Name
) {
500 m_controller
->emitItemEntered(index
);
502 m_controller
->emitViewportEntered();
506 void DolphinDetailsView::updateElasticBand()
509 QRect
dirtyRegion(elasticBandRect());
510 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
511 m_band
.destination
= viewport()->mapFromGlobal(QCursor::pos()) + scrollPos
;
512 // Going above the (logical) top-left of the view causes complications during selection;
513 // we may as well prevent it.
514 if (m_band
.destination
.y() < 0) {
515 m_band
.destination
.setY(0);
517 if (m_band
.destination
.x() < 0) {
518 m_band
.destination
.setX(0);
520 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
521 setDirtyRegion(dirtyRegion
);
525 QRect
DolphinDetailsView::elasticBandRect() const
527 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
529 const QPoint topLeft
= m_band
.origin
- scrollPos
;
530 const QPoint bottomRight
= m_band
.destination
- scrollPos
;
531 return QRect(topLeft
, bottomRight
).normalized();
534 void DolphinDetailsView::setZoomLevel(int level
)
536 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
537 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
539 const bool showPreview
= m_controller
->dolphinView()->showPreview();
541 settings
->setPreviewSize(size
);
543 settings
->setIconSize(size
);
546 updateDecorationSize(showPreview
);
550 void DolphinDetailsView::slotShowPreviewChanged()
552 const DolphinView
* view
= m_controller
->dolphinView();
553 updateDecorationSize(view
->showPreview());
556 void DolphinDetailsView::configureSettings(const QPoint
& pos
)
559 popup
.addTitle(i18nc("@title:menu", "Columns"));
561 // add checkbox items for each column
562 QHeaderView
* headerView
= header();
563 const int columns
= model()->columnCount();
564 for (int i
= 0; i
< columns
; ++i
) {
565 const int logicalIndex
= headerView
->logicalIndex(i
);
566 const QString text
= model()->headerData(logicalIndex
, Qt::Horizontal
).toString();
567 QAction
* action
= popup
.addAction(text
);
568 action
->setCheckable(true);
569 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
570 action
->setData(logicalIndex
);
571 action
->setEnabled(logicalIndex
!= DolphinModel::Name
);
573 popup
.addSeparator();
575 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
576 if (activatedAction
!= 0) {
577 const bool show
= activatedAction
->isChecked();
578 const int columnIndex
= activatedAction
->data().toInt();
580 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
581 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
583 Q_ASSERT(!list
.contains(info
));
586 Q_ASSERT(list
.contains(info
));
587 const int index
= list
.indexOf(info
);
588 list
.removeAt(index
);
591 m_controller
->indicateAdditionalInfoChange(list
);
592 setColumnHidden(columnIndex
, !show
);
597 void DolphinDetailsView::updateColumnVisibility()
599 QHeaderView
* headerView
= header();
600 disconnect(headerView
, SIGNAL(sectionMoved(int, int, int)),
601 this, SLOT(saveColumnPositions()));
603 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
604 const QList
<int> columnPositions
= settings
->columnPositions();
606 const KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
607 for (int i
= DolphinModel::Name
; i
<= DolphinModel::Version
; ++i
) {
608 const KFileItemDelegate::Information info
= infoForColumn(i
);
609 const bool hide
= !list
.contains(info
) && (i
!= DolphinModel::Name
);
610 if (isColumnHidden(i
) != hide
) {
611 setColumnHidden(i
, hide
);
614 const int from
= headerView
->visualIndex(i
);
615 headerView
->moveSection(from
, columnPositions
[i
]);
620 connect(headerView
, SIGNAL(sectionMoved(int, int, int)),
621 this, SLOT(saveColumnPositions()));
625 void DolphinDetailsView::saveColumnPositions()
627 QList
<int> columnPositions
;
628 for (int i
= DolphinModel::Name
; i
<= DolphinModel::Version
; ++i
) {
629 columnPositions
.append(header()->visualIndex(i
));
632 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
633 settings
->setColumnPositions(columnPositions
);
636 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex
, int oldSize
, int newSize
)
638 Q_UNUSED(logicalIndex
);
641 // If the user changes the size of the headers, the autoresize feature should be
642 // turned off. As there is no dedicated interface to find out whether the header
643 // section has been resized by the user or by a resize event, another approach is used.
644 // Attention: Take care when changing the if-condition to verify that there is no
645 // regression in combination with bug 178630 (see fix in comment #8).
646 if ((QApplication::mouseButtons() & Qt::LeftButton
) && header()->underMouse()) {
647 disableAutoResizing();
651 void DolphinDetailsView::slotActivationChanged(bool active
)
653 setAlternatingRowColors(active
);
656 void DolphinDetailsView::disableAutoResizing()
658 m_autoResize
= false;
661 void DolphinDetailsView::requestActivation()
663 m_controller
->requestActivation();
666 void DolphinDetailsView::slotGlobalSettingsChanged(int category
)
670 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
671 Q_ASSERT(settings
!= 0);
672 if (settings
->useSystemFont()) {
673 m_font
= KGlobalSettings::generalFont();
675 //Disconnect then reconnect, since the settings have been changed, the connection requirements may have also.
676 disconnect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
677 disconnect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
678 if (KGlobalSettings::singleClick()) {
679 connect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
681 connect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
685 void DolphinDetailsView::updateElasticBandSelection()
691 // Ensure the elastic band itself is up-to-date, in
692 // case we are being called due to e.g. a drag event.
695 // Clip horizontally to the name column, as some filenames will be
696 // longer than the column. We don't clip vertically as origin
697 // may be above or below the current viewport area.
698 const int nameColumnX
= header()->sectionPosition(DolphinModel::Name
);
699 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
700 QRect selRect
= elasticBandRect().normalized();
701 QRect
nameColumnArea(nameColumnX
, selRect
.y(), nameColumnWidth
, selRect
.height());
702 selRect
= nameColumnArea
.intersect(selRect
).normalized();
703 // Get the last elastic band rectangle, expressed in viewpoint coordinates.
704 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
705 QRect oldSelRect
= QRect(m_band
.lastSelectionOrigin
- scrollPos
, m_band
.lastSelectionDestination
- scrollPos
).normalized();
707 if (selRect
.isNull()) {
708 selectionModel()->select(m_band
.originalSelection
, QItemSelectionModel::ClearAndSelect
);
709 m_band
.ignoreOldInfo
= true;
713 if (!m_band
.ignoreOldInfo
) {
714 // Do some quick checks to see if we can rule out the need to
715 // update the selection.
716 Q_ASSERT(uniformRowHeights());
717 QModelIndex dummyIndex
= model()->index(0, 0);
718 if (!dummyIndex
.isValid()) {
719 // No items in the model presumably.
723 // If the elastic band does not cover the same rows as before, we'll
724 // need to re-check, and also invalidate the old item distances.
725 const int rowHeight
= QTreeView::rowHeight(dummyIndex
);
726 const bool coveringSameRows
=
727 (selRect
.top() / rowHeight
== oldSelRect
.top() / rowHeight
) &&
728 (selRect
.bottom() / rowHeight
== oldSelRect
.bottom() / rowHeight
);
729 if (coveringSameRows
) {
730 // Covering the same rows, but have we moved far enough horizontally
731 // that we might have (de)selected some other items?
732 const bool itemSelectionChanged
=
733 ((selRect
.left() > oldSelRect
.left()) &&
734 (selRect
.left() > m_band
.insideNearestLeftEdge
)) ||
735 ((selRect
.left() < oldSelRect
.left()) &&
736 (selRect
.left() <= m_band
.outsideNearestLeftEdge
)) ||
737 ((selRect
.right() < oldSelRect
.right()) &&
738 (selRect
.left() >= m_band
.insideNearestRightEdge
)) ||
739 ((selRect
.right() > oldSelRect
.right()) &&
740 (selRect
.right() >= m_band
.outsideNearestRightEdge
));
742 if (!itemSelectionChanged
) {
748 // This is the only piece of optimization data that needs to be explicitly
750 m_band
.lastSelectionOrigin
= QPoint();
751 m_band
.lastSelectionDestination
= QPoint();
752 oldSelRect
= selRect
;
755 // Do the selection from scratch. Force a update of the horizontal distances info.
756 m_band
.insideNearestLeftEdge
= nameColumnX
+ nameColumnWidth
+ 1;
757 m_band
.insideNearestRightEdge
= nameColumnX
- 1;
758 m_band
.outsideNearestLeftEdge
= nameColumnX
- 1;
759 m_band
.outsideNearestRightEdge
= nameColumnX
+ nameColumnWidth
+ 1;
761 // Include the old selection rect as well, so we can deselect
762 // items that were inside it but not in the new selRect.
763 const QRect boundingRect
= selRect
.united(oldSelRect
).normalized();
764 if (boundingRect
.isNull()) {
768 // Get the index of the item in this row in the name column.
769 // TODO - would this still work if the columns could be re-ordered?
770 QModelIndex startIndex
= QTreeView::indexAt(boundingRect
.topLeft());
771 if (startIndex
.parent().isValid()) {
772 startIndex
= startIndex
.parent().child(startIndex
.row(), KDirModel::Name
);
774 startIndex
= model()->index(startIndex
.row(), KDirModel::Name
);
776 if (!startIndex
.isValid()) {
777 selectionModel()->select(m_band
.originalSelection
, QItemSelectionModel::ClearAndSelect
);
778 m_band
.ignoreOldInfo
= true;
782 // Go through all indexes between the top and bottom of boundingRect, and
783 // update the selection.
784 const int verticalCutoff
= boundingRect
.bottom();
785 QModelIndex currIndex
= startIndex
;
786 QModelIndex lastIndex
;
787 bool allItemsInBoundDone
= false;
789 // Calling selectionModel()->select(...) for each item that needs to be
790 // toggled is slow as each call emits selectionChanged(...) so store them
791 // and do the selection toggle in one batch.
792 QItemSelection itemsToToggle
;
793 // QItemSelection's deal with continuous ranges of indexes better than
794 // single indexes, so try to portion items that need to be toggled into ranges.
795 bool formingToggleIndexRange
= false;
796 QModelIndex toggleIndexRangeBegin
= QModelIndex();
799 QRect currIndexRect
= nameColumnRect(currIndex
);
801 // Update some optimization info as we go.
802 const int cr
= currIndexRect
.right();
803 const int cl
= currIndexRect
.left();
804 const int sl
= selRect
.left();
805 const int sr
= selRect
.right();
806 // "The right edge of the name is outside of the rect but nearer than m_outsideNearestLeft", etc
807 if ((cr
< sl
&& cr
> m_band
.outsideNearestLeftEdge
)) {
808 m_band
.outsideNearestLeftEdge
= cr
;
810 if ((cl
> sr
&& cl
< m_band
.outsideNearestRightEdge
)) {
811 m_band
.outsideNearestRightEdge
= cl
;
813 if ((cl
>= sl
&& cl
<= sr
&& cl
> m_band
.insideNearestRightEdge
)) {
814 m_band
.insideNearestRightEdge
= cl
;
816 if ((cr
>= sl
&& cr
<= sr
&& cr
< m_band
.insideNearestLeftEdge
)) {
817 m_band
.insideNearestLeftEdge
= cr
;
820 bool currentlySelected
= selectionModel()->isSelected(currIndex
);
821 bool originallySelected
= m_band
.originalSelection
.contains(currIndex
);
822 bool intersectsSelectedRect
= currIndexRect
.intersects(selRect
);
823 bool shouldBeSelected
= (intersectsSelectedRect
&& !originallySelected
) || (!intersectsSelectedRect
&& originallySelected
);
824 bool needToToggleItem
= (currentlySelected
&& !shouldBeSelected
) || (!currentlySelected
&& shouldBeSelected
);
825 if (needToToggleItem
&& !formingToggleIndexRange
) {
826 toggleIndexRangeBegin
= currIndex
;
827 formingToggleIndexRange
= true;
830 // NOTE: indexBelow actually walks up and down expanded trees for us.
831 QModelIndex nextIndex
= indexBelow(currIndex
);
832 allItemsInBoundDone
= !nextIndex
.isValid() || currIndexRect
.top() > verticalCutoff
;
834 const bool commitToggleIndexRange
= formingToggleIndexRange
&&
835 (!needToToggleItem
||
836 allItemsInBoundDone
||
837 currIndex
.parent() != toggleIndexRangeBegin
.parent());
838 if (commitToggleIndexRange
) {
839 formingToggleIndexRange
= false;
840 // If this is the last item in the bounds and it is also the beginning of a range,
841 // don't toggle lastIndex - it will already have been dealt with.
842 if (!allItemsInBoundDone
|| toggleIndexRangeBegin
!= currIndex
) {
843 itemsToToggle
.select(toggleIndexRangeBegin
, lastIndex
);
845 // Need to start a new range immediately with currIndex?
846 if (needToToggleItem
) {
847 toggleIndexRangeBegin
= currIndex
;
848 formingToggleIndexRange
= true;
850 if (allItemsInBoundDone
&& needToToggleItem
) {
851 // Toggle the very last item in the bounds.
852 itemsToToggle
.select(currIndex
, currIndex
);
857 lastIndex
= currIndex
;
858 currIndex
= nextIndex
;
859 } while (!allItemsInBoundDone
);
861 selectionModel()->select(itemsToToggle
, QItemSelectionModel::Toggle
);
863 m_band
.lastSelectionOrigin
= m_band
.origin
;
864 m_band
.lastSelectionDestination
= m_band
.destination
;
865 m_band
.ignoreOldInfo
= false;
868 void DolphinDetailsView::setFoldersExpandable(bool expandable
)
871 // collapse all expanded folders, as QTreeView::setItemsExpandable(false)
872 // does not do this task
873 const int rowCount
= model()->rowCount();
874 for (int row
= 0; row
< rowCount
; ++row
) {
875 setExpanded(model()->index(row
, 0), false);
878 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
879 settings
->setExpandableFolders(expandable
);
880 setRootIsDecorated(expandable
);
881 setItemsExpandable(expandable
);
884 void DolphinDetailsView::updateDecorationSize(bool showPreview
)
886 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
887 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
888 setIconSize(QSize(iconSize
, iconSize
));
889 m_decorationSize
= QSize(iconSize
, iconSize
);
891 if (m_selectionManager
!= 0) {
892 m_selectionManager
->reset();
898 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
900 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
902 switch (columnIndex
) {
903 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
904 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
905 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
906 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
907 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
908 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
915 void DolphinDetailsView::resizeColumns()
917 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
918 // around 3 seconds for each (!) resize operation when having > 10000 items).
919 // This gets a problem especially when opening large directories, where several
920 // resize operations are received for showing the currently available items during
921 // loading (the application hangs around 20 seconds when loading > 10000 items).
923 QHeaderView
* headerView
= header();
924 QFontMetrics
fontMetrics(viewport()->font());
926 int columnWidth
[DolphinModel::Version
+ 1];
927 columnWidth
[DolphinModel::Size
] = fontMetrics
.width("00000 Items");
928 columnWidth
[DolphinModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
929 columnWidth
[DolphinModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
930 columnWidth
[DolphinModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
931 columnWidth
[DolphinModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
932 columnWidth
[DolphinModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
933 columnWidth
[DolphinModel::Version
] = fontMetrics
.width("xxxxxxxx");
935 int requiredWidth
= 0;
936 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
937 if (!isColumnHidden(i
)) {
938 columnWidth
[i
] += 20; // provide a default gap
939 requiredWidth
+= columnWidth
[i
];
940 headerView
->resizeSection(i
, columnWidth
[i
]);
944 // resize the name column in a way that the whole available width is used
945 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
947 const int minNameWidth
= 300;
948 if (columnWidth
[KDirModel::Name
] < minNameWidth
) {
949 columnWidth
[KDirModel::Name
] = minNameWidth
;
951 // It might be possible that the name column width can be
952 // decreased without clipping any text. For performance
953 // reasons the exact necessary width for full visible names is
954 // only checked for up to 200 items:
955 const int rowCount
= model()->rowCount();
956 if (rowCount
> 0 && rowCount
< 200) {
957 const int nameWidth
= sizeHintForColumn(DolphinModel::Name
);
958 if (nameWidth
+ requiredWidth
<= viewport()->width()) {
959 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
960 } else if (nameWidth
< minNameWidth
) {
961 columnWidth
[KDirModel::Name
] = nameWidth
;
966 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
969 QRect
DolphinDetailsView::nameColumnRect(const QModelIndex
& index
) const
971 QRect rect
= visualRect(index
);
972 const KFileItem item
= m_controller
->itemForIndex(index
);
973 if (!item
.isNull()) {
974 const int width
= DolphinFileItemDelegate::nameColumnWidth(item
.text(), viewOptions());
975 rect
.setWidth(width
);
981 bool DolphinDetailsView::isAboveExpandingToggle(const QPoint
& pos
) const
983 // QTreeView offers no public API to get the information whether an index has an
984 // expanding toggle and what boundaries the toggle has. The following approach
985 // also assumes a toggle for file items.
986 if (itemsExpandable()) {
987 const QModelIndex index
= QTreeView::indexAt(pos
);
988 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
989 QRect rect
= nameColumnRect(index
);
990 const int toggleSize
= rect
.height();
991 if (isRightToLeft()) {
992 rect
.moveRight(rect
.right());
994 rect
.moveLeft(rect
.x() - toggleSize
);
996 rect
.setWidth(toggleSize
);
1001 rect
= style()->subElementRect(QStyle::SE_TreeViewDisclosureItem
, &opt
, this);
1003 return rect
.contains(pos
);
1009 DolphinDetailsView::ElasticBand::ElasticBand() :
1013 lastSelectionOrigin(),
1014 lastSelectionDestination(),
1015 ignoreOldInfo(true),
1016 outsideNearestLeftEdge(0),
1017 outsideNearestRightEdge(0),
1018 insideNearestLeftEdge(0),
1019 insideNearestRightEdge(0)
1023 #include "dolphindetailsview.moc"