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_controller(controller
),
53 m_showElasticBand(false),
54 m_elasticBandOrigin(),
55 m_elasticBandDestination()
57 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
58 Q_ASSERT(settings
!= 0);
59 Q_ASSERT(controller
!= 0);
62 setSortingEnabled(true);
63 setUniformRowHeights(true);
64 setSelectionBehavior(SelectItems
);
65 setDragDropMode(QAbstractItemView::DragDrop
);
66 setDropIndicatorShown(false);
67 setAlternatingRowColors(true);
68 setRootIsDecorated(settings
->expandableFolders());
69 setItemsExpandable(settings
->expandableFolders());
71 setMouseTracking(true);
72 viewport()->setAttribute(Qt::WA_Hover
);
74 const ViewProperties
props(controller
->url());
75 setSortIndicatorSection(props
.sorting());
76 setSortIndicatorOrder(props
.sortOrder());
78 QHeaderView
* headerView
= header();
79 connect(headerView
, SIGNAL(sectionClicked(int)),
80 this, SLOT(synchronizeSortingState(int)));
81 headerView
->setContextMenuPolicy(Qt::CustomContextMenu
);
82 connect(headerView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
83 this, SLOT(configureColumns(const QPoint
&)));
84 connect(headerView
, SIGNAL(sectionResized(int, int, int)),
85 this, SLOT(slotHeaderSectionResized(int, int, int)));
86 connect(headerView
, SIGNAL(sectionHandleDoubleClicked(int)),
87 this, SLOT(disableAutoResizing()));
89 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
90 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
91 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
92 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
94 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
95 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
96 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
97 // RETURN-key in keyPressEvent().
98 if (KGlobalSettings::singleClick()) {
99 connect(this, SIGNAL(clicked(const QModelIndex
&)),
100 this, SLOT(triggerItem(const QModelIndex
&)));
101 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
102 SelectionManager
* selManager
= new SelectionManager(this);
103 connect(selManager
, SIGNAL(selectionChanged()),
104 this, SLOT(requestActivation()));
105 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
106 selManager
, SLOT(reset()));
109 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
110 this, SLOT(triggerItem(const QModelIndex
&)));
112 connect(this, SIGNAL(entered(const QModelIndex
&)),
113 this, SLOT(slotEntered(const QModelIndex
&)));
114 connect(this, SIGNAL(viewportEntered()),
115 controller
, SLOT(emitViewportEntered()));
116 connect(controller
, SIGNAL(zoomIn()),
117 this, SLOT(zoomIn()));
118 connect(controller
, SIGNAL(zoomOut()),
119 this, SLOT(zoomOut()));
120 connect(controller
->dolphinView(), SIGNAL(additionalInfoChanged()),
121 this, SLOT(updateColumnVisibility()));
123 m_font
= QFont(settings
->fontFamily(), settings
->fontSize());
125 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
126 // check avoids a division by zero happening on versions before 4.3.1.
127 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
129 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
130 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
131 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
134 updateDecorationSize();
139 DolphinDetailsView::~DolphinDetailsView()
143 bool DolphinDetailsView::event(QEvent
* event
)
145 if (event
->type() == QEvent::Polish
) {
146 QHeaderView
* headerView
= header();
147 headerView
->setResizeMode(QHeaderView::Interactive
);
148 headerView
->setMovable(false);
150 updateColumnVisibility();
152 hideColumn(DolphinModel::Rating
);
153 hideColumn(DolphinModel::Tags
);
155 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
156 // check avoids a division by zero happening on versions before 4.3.1.
157 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
159 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
160 else if (event
->type() == QEvent::UpdateRequest
) {
161 // a wheel movement will scroll 4 items
162 if (model()->rowCount() > 0) {
163 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
168 return QTreeView::event(event
);
171 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
173 QStyleOptionViewItem viewOptions
= QTreeView::viewOptions();
174 viewOptions
.font
= m_font
;
175 viewOptions
.showDecorationSelected
= true;
176 viewOptions
.decorationSize
= m_decorationSize
;
180 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
182 QTreeView::contextMenuEvent(event
);
183 m_controller
->triggerContextMenuRequest(event
->pos());
186 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
188 m_controller
->requestActivation();
190 QTreeView::mousePressEvent(event
);
192 const QModelIndex index
= indexAt(event
->pos());
193 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
194 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
195 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
200 if (event
->button() == Qt::LeftButton
) {
201 m_showElasticBand
= true;
203 const QPoint
pos(contentsPos());
204 m_elasticBandOrigin
= event
->pos();
205 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
206 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
207 m_elasticBandDestination
= event
->pos();
211 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
213 if (m_showElasticBand
) {
214 const QPoint mousePos
= event
->pos();
215 const QModelIndex index
= indexAt(mousePos
);
216 if (!index
.isValid()) {
217 // the destination of the selection rectangle is above the viewport. In this
218 // case QTreeView does no selection at all, which is not the wanted behavior
219 // in Dolphin -> select all items within the elastic band rectangle
222 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
223 QRect selRect
= QRect(m_elasticBandOrigin
, m_elasticBandDestination
).normalized();
224 const QRect
nameColumnsRect(0, 0, nameColumnWidth
, viewport()->height());
225 selRect
= nameColumnsRect
.intersected(selRect
);
227 setSelection(selRect
, QItemSelectionModel::Select
);
230 QTreeView::mouseMoveEvent(event
);
233 QTreeView::mouseMoveEvent(event
);
237 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
239 QTreeView::mouseReleaseEvent(event
);
240 if (m_showElasticBand
) {
242 m_showElasticBand
= false;
246 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions
)
248 DragAndDropHelper::startDrag(this, supportedActions
);
251 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
253 if (event
->mimeData()->hasUrls()) {
254 event
->acceptProposedAction();
257 if (m_showElasticBand
) {
259 m_showElasticBand
= false;
264 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
266 QTreeView::dragLeaveEvent(event
);
268 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
270 setDirtyRegion(m_dropRect
);
273 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
275 QTreeView::dragMoveEvent(event
);
277 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
278 setDirtyRegion(m_dropRect
);
279 const QModelIndex index
= indexAt(event
->pos());
280 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
284 const KFileItem item
= itemForIndex(index
);
285 if (!item
.isNull() && item
.isDir()) {
286 m_dropRect
= visualRect(index
);
288 m_dropRect
.setSize(QSize()); // set as invalid
290 setDirtyRegion(m_dropRect
);
293 if (event
->mimeData()->hasUrls()) {
294 // accept url drops, independently from the destination item
295 event
->acceptProposedAction();
299 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
301 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
302 if (!urls
.isEmpty()) {
303 event
->acceptProposedAction();
304 const QModelIndex index
= indexAt(event
->pos());
306 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
307 item
= itemForIndex(index
);
309 m_controller
->indicateDroppedUrls(urls
,
313 QTreeView::dropEvent(event
);
317 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
319 QTreeView::paintEvent(event
);
320 if (m_showElasticBand
) {
321 // The following code has been taken from QListView
322 // and adapted to DolphinDetailsView.
323 // (C) 1992-2007 Trolltech ASA
324 QStyleOptionRubberBand opt
;
326 opt
.shape
= QRubberBand::Rectangle
;
328 opt
.rect
= elasticBandRect();
330 QPainter
painter(viewport());
332 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
336 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
338 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
339 DragAndDropHelper::drawHoverIndication(this, m_dropRect
, brush
);
343 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
345 QTreeView::keyPressEvent(event
);
347 const QItemSelectionModel
* selModel
= selectionModel();
348 const QModelIndex currentIndex
= selModel
->currentIndex();
349 const bool trigger
= currentIndex
.isValid()
350 && (event
->key() == Qt::Key_Return
)
351 && (selModel
->selectedIndexes().count() <= 1);
353 triggerItem(currentIndex
);
357 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
362 QTreeView::resizeEvent(event
);
365 void DolphinDetailsView::wheelEvent(QWheelEvent
* event
)
367 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
368 if ((event
->modifiers() & Qt::ControlModifier
) == Qt::ControlModifier
) {
372 QTreeView::wheelEvent(event
);
375 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
377 QHeaderView
* headerView
= header();
378 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
381 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
383 QHeaderView
* headerView
= header();
384 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
387 void DolphinDetailsView::synchronizeSortingState(int column
)
389 // The sorting has already been changed in QTreeView if this slot is
390 // invoked, but Dolphin is not informed about this.
391 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
392 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
393 m_controller
->indicateSortingChange(sorting
);
394 m_controller
->indicateSortOrderChange(sortOrder
);
397 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
399 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
400 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
401 if (pos
.x() < nameColumnWidth
) {
402 m_controller
->emitItemEntered(itemForIndex(index
));
405 m_controller
->emitViewportEntered();
409 void DolphinDetailsView::updateElasticBand()
411 if (m_showElasticBand
) {
412 QRect
dirtyRegion(elasticBandRect());
413 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
414 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
415 setDirtyRegion(dirtyRegion
);
419 QRect
DolphinDetailsView::elasticBandRect() const
421 const QPoint
pos(contentsPos());
422 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
423 return QRect(topLeft
, m_elasticBandDestination
).normalized();
426 void DolphinDetailsView::zoomIn()
428 if (isZoomInPossible()) {
429 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
430 switch (settings
->iconSize()) {
431 case KIconLoader::SizeSmall
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
432 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeLarge
); break;
433 default: Q_ASSERT(false); break;
435 updateDecorationSize();
439 void DolphinDetailsView::zoomOut()
441 if (isZoomOutPossible()) {
442 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
443 switch (settings
->iconSize()) {
444 case KIconLoader::SizeLarge
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
445 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeSmall
); break;
446 default: Q_ASSERT(false); break;
448 updateDecorationSize();
452 void DolphinDetailsView::triggerItem(const QModelIndex
& index
)
454 const KFileItem item
= itemForIndex(index
);
455 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
456 m_controller
->triggerItem(item
);
459 m_controller
->emitItemEntered(item
);
463 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
466 popup
.addTitle(i18nc("@title:menu", "Columns"));
468 QHeaderView
* headerView
= header();
469 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
470 const int logicalIndex
= headerView
->logicalIndex(i
);
471 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
472 QAction
* action
= popup
.addAction(text
);
473 action
->setCheckable(true);
474 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
478 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
479 if (activatedAction
!= 0) {
480 const bool show
= activatedAction
->isChecked();
481 const int columnIndex
= activatedAction
->data().toInt();
483 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
484 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
486 Q_ASSERT(!list
.contains(info
));
489 Q_ASSERT(list
.contains(info
));
490 const int index
= list
.indexOf(info
);
491 list
.removeAt(index
);
494 m_controller
->indicateAdditionalInfoChange(list
);
495 setColumnHidden(columnIndex
, !show
);
499 void DolphinDetailsView::updateColumnVisibility()
501 const KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
502 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
503 const KFileItemDelegate::Information info
= infoForColumn(i
);
504 const bool hide
= !list
.contains(info
);
505 if (isColumnHidden(i
) != hide
) {
506 setColumnHidden(i
, hide
);
513 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex
, int oldSize
, int newSize
)
515 Q_UNUSED(logicalIndex
);
518 if (QApplication::mouseButtons() & Qt::LeftButton
) {
519 disableAutoResizing();
523 void DolphinDetailsView::disableAutoResizing()
525 m_autoResize
= false;
528 void DolphinDetailsView::requestActivation()
530 m_controller
->requestActivation();
533 bool DolphinDetailsView::isZoomInPossible() const
535 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
536 return settings
->iconSize() < KIconLoader::SizeLarge
;
539 bool DolphinDetailsView::isZoomOutPossible() const
541 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
542 return settings
->iconSize() > KIconLoader::SizeSmall
;
545 void DolphinDetailsView::updateDecorationSize()
547 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
548 const int iconSize
= settings
->iconSize();
549 setIconSize(QSize(iconSize
, iconSize
));
550 m_decorationSize
= QSize(iconSize
, iconSize
);
552 m_controller
->setZoomInPossible(isZoomInPossible());
553 m_controller
->setZoomOutPossible(isZoomOutPossible());
558 QPoint
DolphinDetailsView::contentsPos() const
560 // implementation note: the horizonal position is ignored currently, as no
561 // horizontal scrolling is done anyway during a selection
562 const QScrollBar
* scrollbar
= verticalScrollBar();
563 Q_ASSERT(scrollbar
!= 0);
565 const int maxHeight
= maximumViewportSize().height();
566 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
567 const int visibleHeight
= model()->rowCount() + 1 - height
;
568 if (visibleHeight
<= 0) {
572 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
576 KFileItem
DolphinDetailsView::itemForIndex(const QModelIndex
& index
) const
578 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
579 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
580 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
581 return dirModel
->itemForIndex(dirIndex
);
584 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
586 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
588 switch (columnIndex
) {
589 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
590 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
591 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
592 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
593 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
594 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
601 void DolphinDetailsView::resizeColumns()
603 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
604 // around 3 seconds for each (!) resize operation when having > 10000 items).
605 // This gets a problem especially when opening large directories, where several
606 // resize operations are received for showing the currently available items during
607 // loading (the application hangs around 20 seconds when loading > 10000 items).
609 QHeaderView
* headerView
= header();
610 QFontMetrics
fontMetrics(viewport()->font());
612 int columnWidth
[KDirModel::ColumnCount
];
613 columnWidth
[KDirModel::Size
] = fontMetrics
.width("00000 Items");
614 columnWidth
[KDirModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
615 columnWidth
[KDirModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
616 columnWidth
[KDirModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
617 columnWidth
[KDirModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
618 columnWidth
[KDirModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
620 int requiredWidth
= 0;
621 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
622 if (!isColumnHidden(i
)) {
623 columnWidth
[i
] += 20; // provide a default gap
624 requiredWidth
+= columnWidth
[i
];
625 headerView
->resizeSection(i
, columnWidth
[i
]);
629 // resize the name column in a way that the whole available width is used
630 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
631 if (columnWidth
[KDirModel::Name
] < 120) {
632 columnWidth
[KDirModel::Name
] = 120;
634 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
637 #include "dolphindetailsview.moc"