1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
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 "dolphinsettings.h"
26 #include "dolphinsortfilterproxymodel.h"
27 #include "draganddrophelper.h"
28 #include "selectionmanager.h"
29 #include "viewproperties.h"
31 #include "dolphin_detailsmodesettings.h"
32 #include "dolphin_generalsettings.h"
34 #include <kdirmodel.h>
38 #include <QAbstractProxyModel>
40 #include <QApplication>
41 #include <QHeaderView>
42 #include <QRubberBand>
46 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
, DolphinController
* controller
) :
49 m_expandingTogglePressed(false),
51 m_controller(controller
),
52 m_selectionManager(0),
55 m_showElasticBand(false),
56 m_elasticBandOrigin(),
57 m_elasticBandDestination()
59 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
60 Q_ASSERT(settings
!= 0);
61 Q_ASSERT(controller
!= 0);
63 setLayoutDirection(Qt::LeftToRight
);
65 setSortingEnabled(true);
66 setUniformRowHeights(true);
67 setSelectionBehavior(SelectItems
);
68 setDragDropMode(QAbstractItemView::DragDrop
);
69 setDropIndicatorShown(false);
70 setAlternatingRowColors(true);
71 setRootIsDecorated(settings
->expandableFolders());
72 setItemsExpandable(settings
->expandableFolders());
73 setEditTriggers(QAbstractItemView::NoEditTriggers
);
75 setMouseTracking(true);
77 const ViewProperties
props(controller
->url());
78 setSortIndicatorSection(props
.sorting());
79 setSortIndicatorOrder(props
.sortOrder());
81 QHeaderView
* headerView
= header();
82 connect(headerView
, SIGNAL(sectionClicked(int)),
83 this, SLOT(synchronizeSortingState(int)));
84 headerView
->setContextMenuPolicy(Qt::CustomContextMenu
);
85 connect(headerView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
86 this, SLOT(configureColumns(const QPoint
&)));
87 connect(headerView
, SIGNAL(sectionResized(int, int, int)),
88 this, SLOT(slotHeaderSectionResized(int, int, int)));
89 connect(headerView
, SIGNAL(sectionHandleDoubleClicked(int)),
90 this, SLOT(disableAutoResizing()));
92 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
93 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
94 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
95 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
97 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
98 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
99 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
100 // RETURN-key in keyPressEvent().
101 if (KGlobalSettings::singleClick()) {
102 connect(this, SIGNAL(clicked(const QModelIndex
&)),
103 controller
, SLOT(triggerItem(const QModelIndex
&)));
105 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
106 controller
, SLOT(triggerItem(const QModelIndex
&)));
109 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
110 m_selectionManager
= new SelectionManager(this);
111 connect(m_selectionManager
, SIGNAL(selectionChanged()),
112 this, SLOT(requestActivation()));
113 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
114 m_selectionManager
, SLOT(reset()));
117 connect(this, SIGNAL(entered(const QModelIndex
&)),
118 this, SLOT(slotEntered(const QModelIndex
&)));
119 connect(this, SIGNAL(viewportEntered()),
120 controller
, SLOT(emitViewportEntered()));
121 connect(controller
, SIGNAL(zoomLevelChanged(int)),
122 this, SLOT(setZoomLevel(int)));
123 connect(controller
->dolphinView(), SIGNAL(additionalInfoChanged()),
124 this, SLOT(updateColumnVisibility()));
125 connect(controller
, SIGNAL(activationChanged(bool)),
126 this, SLOT(slotActivationChanged(bool)));
128 if (settings
->useSystemFont()) {
129 m_font
= KGlobalSettings::generalFont();
131 m_font
= QFont(settings
->fontFamily(),
132 settings
->fontSize(),
133 settings
->fontWeight(),
134 settings
->italicFont());
137 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
138 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
140 updateDecorationSize();
143 viewport()->installEventFilter(this);
145 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
146 this, SLOT(updateFont()));
149 DolphinDetailsView::~DolphinDetailsView()
153 bool DolphinDetailsView::event(QEvent
* event
)
155 if (event
->type() == QEvent::Polish
) {
156 QHeaderView
* headerView
= header();
157 headerView
->setResizeMode(QHeaderView::Interactive
);
158 headerView
->setMovable(false);
160 updateColumnVisibility();
162 hideColumn(DolphinModel::Rating
);
163 hideColumn(DolphinModel::Tags
);
164 } else if (event
->type() == QEvent::UpdateRequest
) {
165 // a wheel movement will scroll 4 items
166 if (model()->rowCount() > 0) {
167 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
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
);
186 m_controller
->triggerContextMenuRequest(event
->pos());
189 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
191 m_controller
->requestActivation();
193 const QModelIndex current
= currentIndex();
194 QTreeView::mousePressEvent(event
);
196 m_expandingTogglePressed
= false;
197 const QModelIndex index
= indexAt(event
->pos());
198 const bool updateState
= index
.isValid() &&
199 (index
.column() == DolphinModel::Name
) &&
200 (event
->button() == Qt::LeftButton
);
202 // TODO: See comment in DolphinIconsView::mousePressEvent(). Only update
203 // the state if no expanding/collapsing area has been hit:
204 const QRect rect
= visualRect(index
);
205 if (event
->pos().x() >= rect
.x() + indentation()) {
206 setState(QAbstractItemView::DraggingState
);
208 m_expandingTogglePressed
= true;
212 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
213 if (QApplication::mouseButtons() & Qt::MidButton
) {
214 m_controller
->replaceUrlByClipboard();
217 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
218 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
222 // restore the current index, other columns are handled as viewport area
223 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
226 if ((event
->button() == Qt::LeftButton
) && !m_expandingTogglePressed
) {
227 m_showElasticBand
= true;
229 const QPoint
pos(contentsPos());
230 m_elasticBandOrigin
= event
->pos();
231 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
232 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
233 m_elasticBandDestination
= event
->pos();
237 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
239 if (m_showElasticBand
) {
240 const QPoint mousePos
= event
->pos();
241 const QModelIndex index
= indexAt(mousePos
);
242 if (!index
.isValid()) {
243 // the destination of the selection rectangle is above the viewport. In this
244 // case QTreeView does no selection at all, which is not the wanted behavior
245 // in Dolphin -> select all items within the elastic band rectangle
248 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
249 QRect selRect
= QRect(m_elasticBandOrigin
, m_elasticBandDestination
).normalized();
250 const QRect
nameColumnsRect(0, 0, nameColumnWidth
, viewport()->height());
251 selRect
= nameColumnsRect
.intersected(selRect
);
253 setSelection(selRect
, QItemSelectionModel::Select
);
256 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
257 // as the Qt-issue #199631 has been fixed.
258 // QTreeView::mouseMoveEvent(event);
259 QAbstractItemView::mouseMoveEvent(event
);
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 if (m_expandingTogglePressed
) {
269 // Per default QTreeView starts either a selection or a drag operation when dragging
270 // the expanding toggle button (Qt-issue - see TODO comment in DolphinIconsView::mousePressEvent()).
271 // Turn off this behavior in Dolphin to stay predictable:
273 setState(QAbstractItemView::NoState
);
277 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
279 const QModelIndex index
= indexAt(event
->pos());
280 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
281 QTreeView::mouseReleaseEvent(event
);
283 // don't change the current index if the cursor is released
284 // above any other column than the name column, as the other
285 // columns act as viewport
286 const QModelIndex current
= currentIndex();
287 QTreeView::mouseReleaseEvent(event
);
288 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::Current
);
291 m_expandingTogglePressed
= false;
292 if (m_showElasticBand
) {
294 m_showElasticBand
= false;
298 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions
)
300 DragAndDropHelper::startDrag(this, supportedActions
);
301 m_showElasticBand
= false;
304 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
306 if (event
->mimeData()->hasUrls()) {
307 event
->acceptProposedAction();
310 if (m_showElasticBand
) {
312 m_showElasticBand
= false;
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 (event
->mimeData()->hasUrls()) {
340 // accept url drops, independently from the destination item
341 event
->acceptProposedAction();
345 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
347 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
348 if (!urls
.isEmpty()) {
349 event
->acceptProposedAction();
350 const QModelIndex index
= indexAt(event
->pos());
352 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
353 item
= m_controller
->itemForIndex(index
);
355 m_controller
->indicateDroppedUrls(urls
,
359 QTreeView::dropEvent(event
);
362 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
364 QTreeView::paintEvent(event
);
365 if (m_showElasticBand
) {
366 // The following code has been taken from QListView
367 // and adapted to DolphinDetailsView.
368 // (C) 1992-2007 Trolltech ASA
369 QStyleOptionRubberBand opt
;
371 opt
.shape
= QRubberBand::Rectangle
;
373 opt
.rect
= elasticBandRect();
375 QPainter
painter(viewport());
377 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
382 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
384 // If the Control modifier is pressed, a multiple selection
385 // is done and DolphinDetailsView::currentChanged() may not
386 // not change the selection in a custom way.
387 m_keyPressed
= !(event
->modifiers() & Qt::ControlModifier
);
389 QTreeView::keyPressEvent(event
);
390 m_controller
->handleKeyPressEvent(event
);
393 void DolphinDetailsView::keyReleaseEvent(QKeyEvent
* event
)
395 QTreeView::keyReleaseEvent(event
);
396 m_keyPressed
= false;
399 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
404 QTreeView::resizeEvent(event
);
407 void DolphinDetailsView::wheelEvent(QWheelEvent
* event
)
409 if (m_selectionManager
!= 0) {
410 m_selectionManager
->reset();
413 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
414 if (event
->modifiers() & Qt::ControlModifier
) {
419 QTreeView::wheelEvent(event
);
422 void DolphinDetailsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
424 QTreeView::currentChanged(current
, previous
);
426 // Stay consistent with QListView: When changing the current index by key presses,
427 // also change the selection.
429 selectionModel()->select(current
, QItemSelectionModel::ClearAndSelect
);
433 bool DolphinDetailsView::eventFilter(QObject
* watched
, QEvent
* event
)
435 if ((watched
== viewport()) && (event
->type() == QEvent::Leave
)) {
436 // if the mouse is above an item and moved very fast outside the widget,
437 // no viewportEntered() signal might be emitted although the mouse has been moved
438 // above the viewport
439 m_controller
->emitViewportEntered();
442 return QTreeView::eventFilter(watched
, event
);
445 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
447 QHeaderView
* headerView
= header();
448 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
451 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
453 QHeaderView
* headerView
= header();
454 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
457 void DolphinDetailsView::synchronizeSortingState(int column
)
459 // The sorting has already been changed in QTreeView if this slot is
460 // invoked, but Dolphin is not informed about this.
461 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
462 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
463 m_controller
->indicateSortingChange(sorting
);
464 m_controller
->indicateSortOrderChange(sortOrder
);
467 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
469 if (index
.column() == DolphinModel::Name
) {
470 m_controller
->emitItemEntered(index
);
472 m_controller
->emitViewportEntered();
476 void DolphinDetailsView::updateElasticBand()
478 if (m_showElasticBand
) {
479 QRect
dirtyRegion(elasticBandRect());
480 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
481 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
482 setDirtyRegion(dirtyRegion
);
486 QRect
DolphinDetailsView::elasticBandRect() const
488 const QPoint
pos(contentsPos());
489 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
490 return QRect(topLeft
, m_elasticBandDestination
).normalized();
493 void DolphinDetailsView::setZoomLevel(int level
)
495 const int size
= DolphinController::iconSizeForZoomLevel(level
);
496 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
497 settings
->setIconSize(size
);
499 updateDecorationSize();
502 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
505 popup
.addTitle(i18nc("@title:menu", "Columns"));
507 QHeaderView
* headerView
= header();
508 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
509 const int logicalIndex
= headerView
->logicalIndex(i
);
510 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
511 QAction
* action
= popup
.addAction(text
);
512 action
->setCheckable(true);
513 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
517 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
518 if (activatedAction
!= 0) {
519 const bool show
= activatedAction
->isChecked();
520 const int columnIndex
= activatedAction
->data().toInt();
522 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
523 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
525 Q_ASSERT(!list
.contains(info
));
528 Q_ASSERT(list
.contains(info
));
529 const int index
= list
.indexOf(info
);
530 list
.removeAt(index
);
533 m_controller
->indicateAdditionalInfoChange(list
);
534 setColumnHidden(columnIndex
, !show
);
538 void DolphinDetailsView::updateColumnVisibility()
540 const KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
541 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
542 const KFileItemDelegate::Information info
= infoForColumn(i
);
543 const bool hide
= !list
.contains(info
);
544 if (isColumnHidden(i
) != hide
) {
545 setColumnHidden(i
, hide
);
552 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex
, int oldSize
, int newSize
)
554 Q_UNUSED(logicalIndex
);
557 if (QApplication::mouseButtons() & Qt::LeftButton
) {
558 disableAutoResizing();
562 void DolphinDetailsView::slotActivationChanged(bool active
)
564 setAlternatingRowColors(active
);
567 void DolphinDetailsView::disableAutoResizing()
569 m_autoResize
= false;
572 void DolphinDetailsView::requestActivation()
574 m_controller
->requestActivation();
577 void DolphinDetailsView::updateFont()
579 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
580 Q_ASSERT(settings
!= 0);
582 if (settings
->useSystemFont()) {
583 m_font
= KGlobalSettings::generalFont();
587 void DolphinDetailsView::updateDecorationSize()
589 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
590 const int iconSize
= settings
->iconSize();
591 setIconSize(QSize(iconSize
, iconSize
));
592 m_decorationSize
= QSize(iconSize
, iconSize
);
594 if (m_selectionManager
!= 0) {
595 m_selectionManager
->reset();
601 QPoint
DolphinDetailsView::contentsPos() const
603 // implementation note: the horizonal position is ignored currently, as no
604 // horizontal scrolling is done anyway during a selection
605 const QScrollBar
* scrollbar
= verticalScrollBar();
606 Q_ASSERT(scrollbar
!= 0);
608 const int maxHeight
= maximumViewportSize().height();
609 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
610 const int visibleHeight
= model()->rowCount() + 1 - height
;
611 if (visibleHeight
<= 0) {
615 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
619 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
621 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
623 switch (columnIndex
) {
624 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
625 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
626 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
627 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
628 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
629 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
636 void DolphinDetailsView::resizeColumns()
638 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
639 // around 3 seconds for each (!) resize operation when having > 10000 items).
640 // This gets a problem especially when opening large directories, where several
641 // resize operations are received for showing the currently available items during
642 // loading (the application hangs around 20 seconds when loading > 10000 items).
644 QHeaderView
* headerView
= header();
645 QFontMetrics
fontMetrics(viewport()->font());
647 int columnWidth
[KDirModel::ColumnCount
];
648 columnWidth
[KDirModel::Size
] = fontMetrics
.width("00000 Items");
649 columnWidth
[KDirModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
650 columnWidth
[KDirModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
651 columnWidth
[KDirModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
652 columnWidth
[KDirModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
653 columnWidth
[KDirModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
655 int requiredWidth
= 0;
656 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
657 if (!isColumnHidden(i
)) {
658 columnWidth
[i
] += 20; // provide a default gap
659 requiredWidth
+= columnWidth
[i
];
660 headerView
->resizeSection(i
, columnWidth
[i
]);
664 // resize the name column in a way that the whole available width is used
665 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
666 if (columnWidth
[KDirModel::Name
] < 120) {
667 columnWidth
[KDirModel::Name
] = 120;
669 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
672 #include "dolphindetailsview.moc"