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(nameFilterChanged(const QString
&)),
118 this, SLOT(setNameFilter(const QString
&)));
119 connect(controller
, SIGNAL(zoomLevelChanged(int)),
120 this, SLOT(setZoomLevel(int)));
121 connect(controller
->dolphinView(), SIGNAL(additionalInfoChanged()),
122 this, SLOT(updateColumnVisibility()));
123 connect(controller
, SIGNAL(activationChanged(bool)),
124 this, SLOT(slotActivationChanged(bool)));
126 if (settings
->useSystemFont()) {
127 m_font
= KGlobalSettings::generalFont();
129 m_font
= QFont(settings
->fontFamily(),
130 settings
->fontSize(),
131 settings
->fontWeight(),
132 settings
->italicFont());
135 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
136 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
138 const DolphinView
* view
= controller
->dolphinView();
139 connect(view
, SIGNAL(showPreviewChanged()),
140 this, SLOT(slotShowPreviewChanged()));
142 updateDecorationSize(view
->showPreview());
145 viewport()->installEventFilter(this);
147 connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)),
148 this, SLOT(slotGlobalSettingsChanged(int)));
150 m_useDefaultIndexAt
= false;
152 m_expandableFoldersAction
= new QAction(i18nc("@option:check", "Expandable Folders"), this);
153 m_expandableFoldersAction
->setCheckable(true);
154 connect(m_expandableFoldersAction
, SIGNAL(toggled(bool)),
155 this, SLOT(setFoldersExpandable(bool)));
157 m_extensionsFactory
= new ViewExtensionsFactory(this, controller
);
158 m_extensionsFactory
->fileItemDelegate()->setMinimizedNameColumn(true);
161 DolphinDetailsView::~DolphinDetailsView()
165 bool DolphinDetailsView::event(QEvent
* event
)
167 if (event
->type() == QEvent::Polish
) {
168 header()->setResizeMode(QHeaderView::Interactive
);
169 updateColumnVisibility();
172 return QTreeView::event(event
);
175 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
177 QStyleOptionViewItem viewOptions
= QTreeView::viewOptions();
178 viewOptions
.font
= m_font
;
179 viewOptions
.showDecorationSelected
= true;
180 viewOptions
.decorationSize
= m_decorationSize
;
184 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
186 QTreeView::contextMenuEvent(event
);
188 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
189 m_expandableFoldersAction
->setChecked(settings
->expandableFolders());
190 m_controller
->triggerContextMenuRequest(event
->pos(),
191 QList
<QAction
*>() << m_expandableFoldersAction
);
194 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
196 m_controller
->requestActivation();
198 const QModelIndex current
= currentIndex();
199 QTreeView::mousePressEvent(event
);
201 m_expandingTogglePressed
= isAboveExpandingToggle(event
->pos());
203 const QModelIndex index
= indexAt(event
->pos());
204 const bool updateState
= index
.isValid() &&
205 (index
.column() == DolphinModel::Name
) &&
206 (event
->button() == Qt::LeftButton
);
208 setState(QAbstractItemView::DraggingState
);
211 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
212 // the mouse press is done somewhere outside the filename column
213 if (QApplication::mouseButtons() & Qt::MidButton
) {
214 m_controller
->replaceUrlByClipboard();
217 const Qt::KeyboardModifiers mod
= QApplication::keyboardModifiers();
218 if (!m_expandingTogglePressed
&& !(mod
& Qt::ShiftModifier
) && !(mod
& Qt::ControlModifier
)) {
222 // restore the current index, other columns are handled as viewport area.
223 // setCurrentIndex(...) implicitly calls scrollTo(...), which we want to ignore.
224 m_ignoreScrollTo
= true;
225 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
226 m_ignoreScrollTo
= false;
228 if ((event
->button() == Qt::LeftButton
) && !m_expandingTogglePressed
) {
229 // Inform Qt about what we are doing - otherwise it starts dragging items around!
230 setState(DragSelectingState
);
232 // Incremental update data will not be useful - start from scratch.
233 m_band
.ignoreOldInfo
= true;
234 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
235 m_band
.origin
= event
->pos() + scrollPos
;
236 m_band
.destination
= m_band
.origin
;
237 m_band
.originalSelection
= selectionModel()->selection();
242 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
244 if (m_expandingTogglePressed
) {
245 // Per default QTreeView starts either a selection or a drag operation when dragging
246 // the expanding toggle button (Qt-issue - see TODO comment in DolphinIconsView::mousePressEvent()).
247 // Turn off this behavior in Dolphin to stay predictable:
248 setState(QAbstractItemView::NoState
);
253 const QPoint mousePos
= event
->pos();
254 const QModelIndex index
= indexAt(mousePos
);
255 if (!index
.isValid()) {
256 // the destination of the selection rectangle is above the viewport. In this
257 // case QTreeView does no selection at all, which is not the wanted behavior
258 // in Dolphin -> select all items within the elastic band rectangle
259 updateElasticBandSelection();
262 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
263 // as the Qt-issue #199631 has been fixed.
264 // QTreeView::mouseMoveEvent(event);
265 QAbstractItemView::mouseMoveEvent(event
);
268 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
269 // as the Qt-issue #199631 has been fixed.
270 // QTreeView::mouseMoveEvent(event);
271 QAbstractItemView::mouseMoveEvent(event
);
275 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
277 if (!m_expandingTogglePressed
) {
278 const QModelIndex index
= indexAt(event
->pos());
279 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
280 QTreeView::mouseReleaseEvent(event
);
282 // don't change the current index if the cursor is released
283 // above any other column than the name column, as the other
284 // columns act as viewport
285 const QModelIndex current
= currentIndex();
286 QTreeView::mouseReleaseEvent(event
);
287 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
290 m_expandingTogglePressed
= false;
299 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions
)
301 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_controller
);
305 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
307 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
308 event
->acceptProposedAction();
317 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
319 QTreeView::dragLeaveEvent(event
);
320 setDirtyRegion(m_dropRect
);
323 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
325 QTreeView::dragMoveEvent(event
);
327 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
328 setDirtyRegion(m_dropRect
);
329 const QModelIndex index
= indexAt(event
->pos());
330 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
331 const KFileItem item
= m_controller
->itemForIndex(index
);
332 if (!item
.isNull() && item
.isDir()) {
333 m_dropRect
= visualRect(index
);
335 m_dropRect
.setSize(QSize()); // set as invalid
337 setDirtyRegion(m_dropRect
);
340 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
341 // accept url drops, independently from the destination item
342 event
->acceptProposedAction();
346 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
348 const QModelIndex index
= indexAt(event
->pos());
350 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
351 item
= m_controller
->itemForIndex(index
);
353 m_controller
->indicateDroppedUrls(item
, m_controller
->url(), event
);
354 QTreeView::dropEvent(event
);
357 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
359 QTreeView::paintEvent(event
);
361 // The following code has been taken from QListView
362 // and adapted to DolphinDetailsView.
363 // (C) 1992-2007 Trolltech ASA
364 QStyleOptionRubberBand opt
;
366 opt
.shape
= QRubberBand::Rectangle
;
368 opt
.rect
= elasticBandRect();
370 QPainter
painter(viewport());
372 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
377 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
379 // If the Control modifier is pressed, a multiple selection
380 // is done and DolphinDetailsView::currentChanged() may not
381 // not change the selection in a custom way.
382 m_keyPressed
= !(event
->modifiers() & Qt::ControlModifier
);
384 QTreeView::keyPressEvent(event
);
385 m_controller
->handleKeyPressEvent(event
);
388 void DolphinDetailsView::keyReleaseEvent(QKeyEvent
* event
)
390 QTreeView::keyReleaseEvent(event
);
391 m_keyPressed
= false;
394 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
396 QTreeView::resizeEvent(event
);
402 void DolphinDetailsView::wheelEvent(QWheelEvent
* event
)
404 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
405 if (event
->modifiers() & Qt::ControlModifier
) {
410 const int height
= m_decorationSize
.height();
411 const int step
= (height
>= KIconLoader::SizeHuge
) ? height
/ 10 : (KIconLoader::SizeHuge
- height
) / 2;
412 verticalScrollBar()->setSingleStep(step
);
413 QTreeView::wheelEvent(event
);
416 void DolphinDetailsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
418 QTreeView::currentChanged(current
, previous
);
419 m_extensionsFactory
->handleCurrentIndexChange(current
, previous
);
421 // Stay consistent with QListView: When changing the current index by key presses,
422 // also change the selection.
424 selectionModel()->select(current
, QItemSelectionModel::ClearAndSelect
);
428 bool DolphinDetailsView::eventFilter(QObject
* watched
, QEvent
* event
)
430 if ((watched
== viewport()) && (event
->type() == QEvent::Leave
)) {
431 // if the mouse is above an item and moved very fast outside the widget,
432 // no viewportEntered() signal might be emitted although the mouse has been moved
433 // above the viewport
434 m_controller
->emitViewportEntered();
437 return QTreeView::eventFilter(watched
, event
);
440 QModelIndex
DolphinDetailsView::indexAt(const QPoint
& point
) const
442 // the blank portion of the name column counts as empty space
443 const QModelIndex index
= QTreeView::indexAt(point
);
444 const bool isAboveEmptySpace
= !m_useDefaultIndexAt
&&
445 (index
.column() == KDirModel::Name
) && !nameColumnRect(index
).contains(point
);
446 return isAboveEmptySpace
? QModelIndex() : index
;
449 void DolphinDetailsView::setSelection(const QRect
& rect
, QItemSelectionModel::SelectionFlags command
)
451 // We must override setSelection() as Qt calls it internally and when this happens
452 // we must ensure that the default indexAt() is used.
454 m_useDefaultIndexAt
= true;
455 QTreeView::setSelection(rect
, command
);
456 m_useDefaultIndexAt
= false;
458 // Use our own elastic band selection algorithm
459 updateElasticBandSelection();
463 void DolphinDetailsView::scrollTo(const QModelIndex
& index
, ScrollHint hint
)
465 if (!m_ignoreScrollTo
) {
466 QTreeView::scrollTo(index
, hint
);
470 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
472 header()->setSortIndicator(sorting
, header()->sortIndicatorOrder());
475 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
477 header()->setSortIndicator(header()->sortIndicatorSection(), sortOrder
);
480 void DolphinDetailsView::synchronizeSortingState(int column
)
482 // The sorting has already been changed in QTreeView if this slot is
483 // invoked, but Dolphin is not informed about this.
484 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
485 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
486 m_controller
->indicateSortingChange(sorting
);
487 m_controller
->indicateSortOrderChange(sortOrder
);
490 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
492 if (index
.column() == DolphinModel::Name
) {
493 m_controller
->emitItemEntered(index
);
495 m_controller
->emitViewportEntered();
499 void DolphinDetailsView::updateElasticBand()
502 QRect
dirtyRegion(elasticBandRect());
503 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
504 m_band
.destination
= viewport()->mapFromGlobal(QCursor::pos()) + scrollPos
;
505 // Going above the (logical) top-left of the view causes complications during selection;
506 // we may as well prevent it.
507 if (m_band
.destination
.y() < 0) {
508 m_band
.destination
.setY(0);
510 if (m_band
.destination
.x() < 0) {
511 m_band
.destination
.setX(0);
513 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
514 setDirtyRegion(dirtyRegion
);
518 QRect
DolphinDetailsView::elasticBandRect() const
520 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
522 const QPoint topLeft
= m_band
.origin
- scrollPos
;
523 const QPoint bottomRight
= m_band
.destination
- scrollPos
;
524 return QRect(topLeft
, bottomRight
).normalized();
527 void DolphinDetailsView::setNameFilter(const QString
& nameFilter
)
529 DolphinSortFilterProxyModel
* proxyModel
= static_cast<DolphinSortFilterProxyModel
*>(model());
530 proxyModel
->setFilterRegExp(nameFilter
);
533 void DolphinDetailsView::setZoomLevel(int level
)
535 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
536 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
538 const bool showPreview
= m_controller
->dolphinView()->showPreview();
540 settings
->setPreviewSize(size
);
542 settings
->setIconSize(size
);
545 updateDecorationSize(showPreview
);
548 void DolphinDetailsView::slotShowPreviewChanged()
550 const DolphinView
* view
= m_controller
->dolphinView();
551 updateDecorationSize(view
->showPreview());
554 void DolphinDetailsView::configureSettings(const QPoint
& pos
)
557 popup
.addTitle(i18nc("@title:menu", "Columns"));
559 // add checkbox items for each column
560 QHeaderView
* headerView
= header();
561 const int columns
= model()->columnCount();
562 for (int i
= 0; i
< columns
; ++i
) {
563 const int logicalIndex
= headerView
->logicalIndex(i
);
564 const QString text
= model()->headerData(logicalIndex
, Qt::Horizontal
).toString();
565 QAction
* action
= popup
.addAction(text
);
566 action
->setCheckable(true);
567 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
568 action
->setData(logicalIndex
);
569 action
->setEnabled(logicalIndex
!= DolphinModel::Name
);
571 popup
.addSeparator();
573 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
574 if (activatedAction
!= 0) {
575 const bool show
= activatedAction
->isChecked();
576 const int columnIndex
= activatedAction
->data().toInt();
578 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
579 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
581 Q_ASSERT(!list
.contains(info
));
584 Q_ASSERT(list
.contains(info
));
585 const int index
= list
.indexOf(info
);
586 list
.removeAt(index
);
589 m_controller
->indicateAdditionalInfoChange(list
);
590 setColumnHidden(columnIndex
, !show
);
595 void DolphinDetailsView::updateColumnVisibility()
597 QHeaderView
* headerView
= header();
598 disconnect(headerView
, SIGNAL(sectionMoved(int, int, int)),
599 this, SLOT(saveColumnPositions()));
601 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
602 const QList
<int> columnPositions
= settings
->columnPositions();
604 const KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
605 for (int i
= DolphinModel::Name
; i
<= DolphinModel::Version
; ++i
) {
606 const KFileItemDelegate::Information info
= infoForColumn(i
);
607 const bool hide
= !list
.contains(info
) && (i
!= DolphinModel::Name
);
608 if (isColumnHidden(i
) != hide
) {
609 setColumnHidden(i
, hide
);
612 const int from
= headerView
->visualIndex(i
);
613 headerView
->moveSection(from
, columnPositions
[i
]);
618 connect(headerView
, SIGNAL(sectionMoved(int, int, int)),
619 this, SLOT(saveColumnPositions()));
623 void DolphinDetailsView::saveColumnPositions()
625 QList
<int> columnPositions
;
626 for (int i
= DolphinModel::Name
; i
<= DolphinModel::Version
; ++i
) {
627 columnPositions
.append(header()->visualIndex(i
));
630 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
631 settings
->setColumnPositions(columnPositions
);
634 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex
, int oldSize
, int newSize
)
636 Q_UNUSED(logicalIndex
);
639 // If the user changes the size of the headers, the autoresize feature should be
640 // turned off. As there is no dedicated interface to find out whether the header
641 // section has been resized by the user or by a resize event, another approach is used.
642 // Attention: Take care when changing the if-condition to verify that there is no
643 // regression in combination with bug 178630 (see fix in comment #8).
644 if ((QApplication::mouseButtons() & Qt::LeftButton
) && header()->underMouse()) {
645 disableAutoResizing();
649 void DolphinDetailsView::slotActivationChanged(bool active
)
651 setAlternatingRowColors(active
);
654 void DolphinDetailsView::disableAutoResizing()
656 m_autoResize
= false;
659 void DolphinDetailsView::requestActivation()
661 m_controller
->requestActivation();
664 void DolphinDetailsView::slotGlobalSettingsChanged(int category
)
668 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
669 Q_ASSERT(settings
!= 0);
670 if (settings
->useSystemFont()) {
671 m_font
= KGlobalSettings::generalFont();
673 //Disconnect then reconnect, since the settings have been changed, the connection requirements may have also.
674 disconnect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
675 disconnect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
676 if (KGlobalSettings::singleClick()) {
677 connect(this, SIGNAL(clicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
679 connect(this, SIGNAL(doubleClicked(QModelIndex
)), m_controller
, SLOT(triggerItem(QModelIndex
)));
683 void DolphinDetailsView::updateElasticBandSelection()
689 // Ensure the elastic band itself is up-to-date, in
690 // case we are being called due to e.g. a drag event.
693 // Clip horizontally to the name column, as some filenames will be
694 // longer than the column. We don't clip vertically as origin
695 // may be above or below the current viewport area.
696 const int nameColumnX
= header()->sectionPosition(DolphinModel::Name
);
697 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
698 QRect selRect
= elasticBandRect().normalized();
699 QRect
nameColumnArea(nameColumnX
, selRect
.y(), nameColumnWidth
, selRect
.height());
700 selRect
= nameColumnArea
.intersect(selRect
).normalized();
701 // Get the last elastic band rectangle, expressed in viewpoint coordinates.
702 const QPoint
scrollPos(horizontalScrollBar()->value(), verticalScrollBar()->value());
703 QRect oldSelRect
= QRect(m_band
.lastSelectionOrigin
- scrollPos
, m_band
.lastSelectionDestination
- scrollPos
).normalized();
705 if (selRect
.isNull()) {
706 selectionModel()->select(m_band
.originalSelection
, QItemSelectionModel::ClearAndSelect
);
707 m_band
.ignoreOldInfo
= true;
711 if (!m_band
.ignoreOldInfo
) {
712 // Do some quick checks to see if we can rule out the need to
713 // update the selection.
714 Q_ASSERT(uniformRowHeights());
715 QModelIndex dummyIndex
= model()->index(0, 0);
716 if (!dummyIndex
.isValid()) {
717 // No items in the model presumably.
721 // If the elastic band does not cover the same rows as before, we'll
722 // need to re-check, and also invalidate the old item distances.
723 const int rowHeight
= QTreeView::rowHeight(dummyIndex
);
724 const bool coveringSameRows
=
725 (selRect
.top() / rowHeight
== oldSelRect
.top() / rowHeight
) &&
726 (selRect
.bottom() / rowHeight
== oldSelRect
.bottom() / rowHeight
);
727 if (coveringSameRows
) {
728 // Covering the same rows, but have we moved far enough horizontally
729 // that we might have (de)selected some other items?
730 const bool itemSelectionChanged
=
731 ((selRect
.left() > oldSelRect
.left()) &&
732 (selRect
.left() > m_band
.insideNearestLeftEdge
)) ||
733 ((selRect
.left() < oldSelRect
.left()) &&
734 (selRect
.left() <= m_band
.outsideNearestLeftEdge
)) ||
735 ((selRect
.right() < oldSelRect
.right()) &&
736 (selRect
.left() >= m_band
.insideNearestRightEdge
)) ||
737 ((selRect
.right() > oldSelRect
.right()) &&
738 (selRect
.right() >= m_band
.outsideNearestRightEdge
));
740 if (!itemSelectionChanged
) {
746 // This is the only piece of optimization data that needs to be explicitly
748 m_band
.lastSelectionOrigin
= QPoint();
749 m_band
.lastSelectionDestination
= QPoint();
750 oldSelRect
= selRect
;
753 // Do the selection from scratch. Force a update of the horizontal distances info.
754 m_band
.insideNearestLeftEdge
= nameColumnX
+ nameColumnWidth
+ 1;
755 m_band
.insideNearestRightEdge
= nameColumnX
- 1;
756 m_band
.outsideNearestLeftEdge
= nameColumnX
- 1;
757 m_band
.outsideNearestRightEdge
= nameColumnX
+ nameColumnWidth
+ 1;
759 // Include the old selection rect as well, so we can deselect
760 // items that were inside it but not in the new selRect.
761 const QRect boundingRect
= selRect
.united(oldSelRect
).normalized();
762 if (boundingRect
.isNull()) {
766 // Get the index of the item in this row in the name column.
767 // TODO - would this still work if the columns could be re-ordered?
768 QModelIndex startIndex
= QTreeView::indexAt(boundingRect
.topLeft());
769 if (startIndex
.parent().isValid()) {
770 startIndex
= startIndex
.parent().child(startIndex
.row(), KDirModel::Name
);
772 startIndex
= model()->index(startIndex
.row(), KDirModel::Name
);
774 if (!startIndex
.isValid()) {
775 selectionModel()->select(m_band
.originalSelection
, QItemSelectionModel::ClearAndSelect
);
776 m_band
.ignoreOldInfo
= true;
780 // Go through all indexes between the top and bottom of boundingRect, and
781 // update the selection.
782 const int verticalCutoff
= boundingRect
.bottom();
783 QModelIndex currIndex
= startIndex
;
784 QModelIndex lastIndex
;
785 bool allItemsInBoundDone
= false;
787 // Calling selectionModel()->select(...) for each item that needs to be
788 // toggled is slow as each call emits selectionChanged(...) so store them
789 // and do the selection toggle in one batch.
790 QItemSelection itemsToToggle
;
791 // QItemSelection's deal with continuous ranges of indexes better than
792 // single indexes, so try to portion items that need to be toggled into ranges.
793 bool formingToggleIndexRange
= false;
794 QModelIndex toggleIndexRangeBegin
= QModelIndex();
797 QRect currIndexRect
= nameColumnRect(currIndex
);
799 // Update some optimization info as we go.
800 const int cr
= currIndexRect
.right();
801 const int cl
= currIndexRect
.left();
802 const int sl
= selRect
.left();
803 const int sr
= selRect
.right();
804 // "The right edge of the name is outside of the rect but nearer than m_outsideNearestLeft", etc
805 if ((cr
< sl
&& cr
> m_band
.outsideNearestLeftEdge
)) {
806 m_band
.outsideNearestLeftEdge
= cr
;
808 if ((cl
> sr
&& cl
< m_band
.outsideNearestRightEdge
)) {
809 m_band
.outsideNearestRightEdge
= cl
;
811 if ((cl
>= sl
&& cl
<= sr
&& cl
> m_band
.insideNearestRightEdge
)) {
812 m_band
.insideNearestRightEdge
= cl
;
814 if ((cr
>= sl
&& cr
<= sr
&& cr
< m_band
.insideNearestLeftEdge
)) {
815 m_band
.insideNearestLeftEdge
= cr
;
818 bool currentlySelected
= selectionModel()->isSelected(currIndex
);
819 bool originallySelected
= m_band
.originalSelection
.contains(currIndex
);
820 bool intersectsSelectedRect
= currIndexRect
.intersects(selRect
);
821 bool shouldBeSelected
= (intersectsSelectedRect
&& !originallySelected
) || (!intersectsSelectedRect
&& originallySelected
);
822 bool needToToggleItem
= (currentlySelected
&& !shouldBeSelected
) || (!currentlySelected
&& shouldBeSelected
);
823 if (needToToggleItem
&& !formingToggleIndexRange
) {
824 toggleIndexRangeBegin
= currIndex
;
825 formingToggleIndexRange
= true;
828 // NOTE: indexBelow actually walks up and down expanded trees for us.
829 QModelIndex nextIndex
= indexBelow(currIndex
);
830 allItemsInBoundDone
= !nextIndex
.isValid() || currIndexRect
.top() > verticalCutoff
;
832 const bool commitToggleIndexRange
= formingToggleIndexRange
&&
833 (!needToToggleItem
||
834 allItemsInBoundDone
||
835 currIndex
.parent() != toggleIndexRangeBegin
.parent());
836 if (commitToggleIndexRange
) {
837 formingToggleIndexRange
= false;
838 // If this is the last item in the bounds and it is also the beginning of a range,
839 // don't toggle lastIndex - it will already have been dealt with.
840 if (!allItemsInBoundDone
|| toggleIndexRangeBegin
!= currIndex
) {
841 itemsToToggle
.select(toggleIndexRangeBegin
, lastIndex
);
843 // Need to start a new range immediately with currIndex?
844 if (needToToggleItem
) {
845 toggleIndexRangeBegin
= currIndex
;
846 formingToggleIndexRange
= true;
848 if (allItemsInBoundDone
&& needToToggleItem
) {
849 // Toggle the very last item in the bounds.
850 itemsToToggle
.select(currIndex
, currIndex
);
855 lastIndex
= currIndex
;
856 currIndex
= nextIndex
;
857 } while (!allItemsInBoundDone
);
859 selectionModel()->select(itemsToToggle
, QItemSelectionModel::Toggle
);
861 m_band
.lastSelectionOrigin
= m_band
.origin
;
862 m_band
.lastSelectionDestination
= m_band
.destination
;
863 m_band
.ignoreOldInfo
= false;
866 void DolphinDetailsView::setFoldersExpandable(bool expandable
)
869 // collapse all expanded folders, as QTreeView::setItemsExpandable(false)
870 // does not do this task
871 const int rowCount
= model()->rowCount();
872 for (int row
= 0; row
< rowCount
; ++row
) {
873 setExpanded(model()->index(row
, 0), false);
876 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
877 settings
->setExpandableFolders(expandable
);
878 setRootIsDecorated(expandable
);
879 setItemsExpandable(expandable
);
882 void DolphinDetailsView::updateDecorationSize(bool showPreview
)
884 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
885 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
886 setIconSize(QSize(iconSize
, iconSize
));
887 m_decorationSize
= QSize(iconSize
, iconSize
);
892 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
894 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
896 switch (columnIndex
) {
897 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
898 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
899 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
900 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
901 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
902 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
909 void DolphinDetailsView::resizeColumns()
911 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
912 // around 3 seconds for each (!) resize operation when having > 10000 items).
913 // This gets a problem especially when opening large directories, where several
914 // resize operations are received for showing the currently available items during
915 // loading (the application hangs around 20 seconds when loading > 10000 items).
917 QHeaderView
* headerView
= header();
918 QFontMetrics
fontMetrics(viewport()->font());
920 int columnWidth
[DolphinModel::Version
+ 1];
921 columnWidth
[DolphinModel::Size
] = fontMetrics
.width("00000 Items");
922 columnWidth
[DolphinModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
923 columnWidth
[DolphinModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
924 columnWidth
[DolphinModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
925 columnWidth
[DolphinModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
926 columnWidth
[DolphinModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
927 columnWidth
[DolphinModel::Version
] = fontMetrics
.width("xxxxxxxx");
929 int requiredWidth
= 0;
930 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
931 if (!isColumnHidden(i
)) {
932 columnWidth
[i
] += 20; // provide a default gap
933 requiredWidth
+= columnWidth
[i
];
934 headerView
->resizeSection(i
, columnWidth
[i
]);
938 // resize the name column in a way that the whole available width is used
939 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
941 const int minNameWidth
= 300;
942 if (columnWidth
[KDirModel::Name
] < minNameWidth
) {
943 columnWidth
[KDirModel::Name
] = minNameWidth
;
945 // It might be possible that the name column width can be
946 // decreased without clipping any text. For performance
947 // reasons the exact necessary width for full visible names is
948 // only checked for up to 200 items:
949 const int rowCount
= model()->rowCount();
950 if (rowCount
> 0 && rowCount
< 200) {
951 const int nameWidth
= sizeHintForColumn(DolphinModel::Name
);
952 if (nameWidth
+ requiredWidth
<= viewport()->width()) {
953 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
954 } else if (nameWidth
< minNameWidth
) {
955 columnWidth
[KDirModel::Name
] = nameWidth
;
960 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
963 QRect
DolphinDetailsView::nameColumnRect(const QModelIndex
& index
) const
965 QRect rect
= visualRect(index
);
966 const KFileItem item
= m_controller
->itemForIndex(index
);
967 if (!item
.isNull()) {
968 const int width
= DolphinFileItemDelegate::nameColumnWidth(item
.text(), viewOptions());
969 rect
.setWidth(width
);
975 bool DolphinDetailsView::isAboveExpandingToggle(const QPoint
& pos
) const
977 // QTreeView offers no public API to get the information whether an index has an
978 // expanding toggle and what boundaries the toggle has. The following approach
979 // also assumes a toggle for file items.
980 if (itemsExpandable()) {
981 const QModelIndex index
= QTreeView::indexAt(pos
);
982 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
983 QRect rect
= nameColumnRect(index
);
984 const int toggleSize
= rect
.height();
985 if (isRightToLeft()) {
986 rect
.moveRight(rect
.right());
988 rect
.moveLeft(rect
.x() - toggleSize
);
990 rect
.setWidth(toggleSize
);
995 rect
= style()->subElementRect(QStyle::SE_TreeViewDisclosureItem
, &opt
, this);
997 return rect
.contains(pos
);
1003 DolphinDetailsView::ElasticBand::ElasticBand() :
1007 lastSelectionOrigin(),
1008 lastSelectionDestination(),
1009 ignoreOldInfo(true),
1010 outsideNearestLeftEdge(0),
1011 outsideNearestRightEdge(0),
1012 insideNearestLeftEdge(0),
1013 insideNearestRightEdge(0)
1017 #include "dolphindetailsview.moc"