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 Q_ASSERT(controller
!= 0);
60 setRootIsDecorated(false);
61 setSortingEnabled(true);
62 setUniformRowHeights(true);
63 setSelectionBehavior(SelectItems
);
64 setDragDropMode(QAbstractItemView::DragDrop
);
65 setDropIndicatorShown(false);
66 setAlternatingRowColors(true);
67 setItemsExpandable(false);
69 setMouseTracking(true);
70 viewport()->setAttribute(Qt::WA_Hover
);
72 const ViewProperties
props(controller
->url());
73 setSortIndicatorSection(props
.sorting());
74 setSortIndicatorOrder(props
.sortOrder());
76 QHeaderView
* headerView
= header();
77 connect(headerView
, SIGNAL(sectionClicked(int)),
78 this, SLOT(synchronizeSortingState(int)));
79 headerView
->setContextMenuPolicy(Qt::CustomContextMenu
);
80 connect(headerView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
81 this, SLOT(configureColumns(const QPoint
&)));
82 connect(headerView
, SIGNAL(sectionResized(int, int, int)),
83 this, SLOT(slotHeaderSectionResized(int, int, int)));
84 connect(headerView
, SIGNAL(sectionHandleDoubleClicked(int)),
85 this, SLOT(disableAutoResizing()));
87 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
88 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
89 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
90 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
92 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
93 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
94 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
95 // RETURN-key in keyPressEvent().
96 if (KGlobalSettings::singleClick()) {
97 connect(this, SIGNAL(clicked(const QModelIndex
&)),
98 this, SLOT(triggerItem(const QModelIndex
&)));
99 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
100 SelectionManager
* selManager
= new SelectionManager(this);
101 connect(selManager
, SIGNAL(selectionChanged()),
102 this, SLOT(requestActivation()));
103 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
104 selManager
, SLOT(reset()));
107 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
108 this, SLOT(triggerItem(const QModelIndex
&)));
110 connect(this, SIGNAL(entered(const QModelIndex
&)),
111 this, SLOT(slotEntered(const QModelIndex
&)));
112 connect(this, SIGNAL(viewportEntered()),
113 controller
, SLOT(emitViewportEntered()));
114 connect(controller
, SIGNAL(zoomIn()),
115 this, SLOT(zoomIn()));
116 connect(controller
, SIGNAL(zoomOut()),
117 this, SLOT(zoomOut()));
118 connect(controller
->dolphinView(), SIGNAL(additionalInfoChanged()),
119 this, SLOT(updateColumnVisibility()));
121 // apply the details mode settings to the widget
122 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
123 Q_ASSERT(settings
!= 0);
125 m_font
= QFont(settings
->fontFamily(), settings
->fontSize());
127 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
128 // check avoids a division by zero happening on versions before 4.3.1.
129 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
131 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
132 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
133 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
136 updateDecorationSize();
141 DolphinDetailsView::~DolphinDetailsView()
145 bool DolphinDetailsView::event(QEvent
* event
)
147 if (event
->type() == QEvent::Polish
) {
148 QHeaderView
* headerView
= header();
149 headerView
->setResizeMode(QHeaderView::Interactive
);
150 headerView
->setMovable(false);
152 updateColumnVisibility();
154 hideColumn(DolphinModel::Rating
);
155 hideColumn(DolphinModel::Tags
);
157 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
158 // check avoids a division by zero happening on versions before 4.3.1.
159 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
161 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
162 else if (event
->type() == QEvent::UpdateRequest
) {
163 // a wheel movement will scroll 4 items
164 if (model()->rowCount() > 0) {
165 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
170 return QTreeView::event(event
);
173 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
175 QStyleOptionViewItem viewOptions
= QTreeView::viewOptions();
176 viewOptions
.font
= m_font
;
177 viewOptions
.showDecorationSelected
= true;
178 viewOptions
.decorationSize
= m_decorationSize
;
182 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
184 QTreeView::contextMenuEvent(event
);
185 m_controller
->triggerContextMenuRequest(event
->pos());
188 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
190 m_controller
->requestActivation();
192 QTreeView::mousePressEvent(event
);
194 const QModelIndex index
= indexAt(event
->pos());
195 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
196 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
197 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
202 if (event
->button() == Qt::LeftButton
) {
203 m_showElasticBand
= true;
205 const QPoint
pos(contentsPos());
206 m_elasticBandOrigin
= event
->pos();
207 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
208 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
209 m_elasticBandDestination
= event
->pos();
213 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
215 if (m_showElasticBand
) {
216 const QPoint mousePos
= event
->pos();
217 const QModelIndex index
= indexAt(mousePos
);
218 if (!index
.isValid()) {
219 // the destination of the selection rectangle is above the viewport. In this
220 // case QTreeView does no selection at all, which is not the wanted behavior
221 // in Dolphin -> select all items within the elastic band rectangle
224 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
225 QRect selRect
= QRect(m_elasticBandOrigin
, m_elasticBandDestination
).normalized();
226 const QRect
nameColumnsRect(0, 0, nameColumnWidth
, viewport()->height());
227 selRect
= nameColumnsRect
.intersected(selRect
);
229 setSelection(selRect
, QItemSelectionModel::Select
);
232 QTreeView::mouseMoveEvent(event
);
235 QTreeView::mouseMoveEvent(event
);
239 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
241 QTreeView::mouseReleaseEvent(event
);
242 if (m_showElasticBand
) {
244 m_showElasticBand
= false;
248 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions
)
250 DragAndDropHelper::startDrag(this, supportedActions
);
253 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
255 if (event
->mimeData()->hasUrls()) {
256 event
->acceptProposedAction();
259 if (m_showElasticBand
) {
261 m_showElasticBand
= false;
266 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
268 QTreeView::dragLeaveEvent(event
);
270 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
272 setDirtyRegion(m_dropRect
);
275 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
277 QTreeView::dragMoveEvent(event
);
279 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
280 setDirtyRegion(m_dropRect
);
281 const QModelIndex index
= indexAt(event
->pos());
282 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
286 const KFileItem item
= itemForIndex(index
);
287 if (!item
.isNull() && item
.isDir()) {
288 m_dropRect
= visualRect(index
);
290 m_dropRect
.setSize(QSize()); // set as invalid
292 setDirtyRegion(m_dropRect
);
295 if (event
->mimeData()->hasUrls()) {
296 // accept url drops, independently from the destination item
297 event
->acceptProposedAction();
301 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
303 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
304 if (!urls
.isEmpty()) {
305 event
->acceptProposedAction();
306 const QModelIndex index
= indexAt(event
->pos());
308 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
309 item
= itemForIndex(index
);
311 m_controller
->indicateDroppedUrls(urls
,
315 QTreeView::dropEvent(event
);
319 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
321 QTreeView::paintEvent(event
);
322 if (m_showElasticBand
) {
323 // The following code has been taken from QListView
324 // and adapted to DolphinDetailsView.
325 // (C) 1992-2007 Trolltech ASA
326 QStyleOptionRubberBand opt
;
328 opt
.shape
= QRubberBand::Rectangle
;
330 opt
.rect
= elasticBandRect();
332 QPainter
painter(viewport());
334 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
338 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
340 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
341 DragAndDropHelper::drawHoverIndication(this, m_dropRect
, brush
);
345 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
347 QTreeView::keyPressEvent(event
);
349 const QItemSelectionModel
* selModel
= selectionModel();
350 const QModelIndex currentIndex
= selModel
->currentIndex();
351 const bool trigger
= currentIndex
.isValid()
352 && (event
->key() == Qt::Key_Return
)
353 && (selModel
->selectedIndexes().count() <= 1);
355 triggerItem(currentIndex
);
359 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
364 QTreeView::resizeEvent(event
);
367 void DolphinDetailsView::wheelEvent(QWheelEvent
* event
)
369 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
370 if ((event
->modifiers() & Qt::ControlModifier
) == Qt::ControlModifier
) {
374 QTreeView::wheelEvent(event
);
377 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
379 QHeaderView
* headerView
= header();
380 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
383 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
385 QHeaderView
* headerView
= header();
386 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
389 void DolphinDetailsView::synchronizeSortingState(int column
)
391 // The sorting has already been changed in QTreeView if this slot is
392 // invoked, but Dolphin is not informed about this.
393 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
394 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
395 m_controller
->indicateSortingChange(sorting
);
396 m_controller
->indicateSortOrderChange(sortOrder
);
399 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
401 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
402 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
403 if (pos
.x() < nameColumnWidth
) {
404 m_controller
->emitItemEntered(itemForIndex(index
));
407 m_controller
->emitViewportEntered();
411 void DolphinDetailsView::updateElasticBand()
413 if (m_showElasticBand
) {
414 QRect
dirtyRegion(elasticBandRect());
415 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
416 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
417 setDirtyRegion(dirtyRegion
);
421 QRect
DolphinDetailsView::elasticBandRect() const
423 const QPoint
pos(contentsPos());
424 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
425 return QRect(topLeft
, m_elasticBandDestination
).normalized();
428 void DolphinDetailsView::zoomIn()
430 if (isZoomInPossible()) {
431 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
432 switch (settings
->iconSize()) {
433 case KIconLoader::SizeSmall
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
434 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeLarge
); break;
435 default: Q_ASSERT(false); break;
437 updateDecorationSize();
441 void DolphinDetailsView::zoomOut()
443 if (isZoomOutPossible()) {
444 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
445 switch (settings
->iconSize()) {
446 case KIconLoader::SizeLarge
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
447 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeSmall
); break;
448 default: Q_ASSERT(false); break;
450 updateDecorationSize();
454 void DolphinDetailsView::triggerItem(const QModelIndex
& index
)
456 const KFileItem item
= itemForIndex(index
);
457 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
458 m_controller
->triggerItem(item
);
461 m_controller
->emitItemEntered(item
);
465 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
468 popup
.addTitle(i18nc("@title:menu", "Columns"));
470 QHeaderView
* headerView
= header();
471 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
472 const int logicalIndex
= headerView
->logicalIndex(i
);
473 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
474 QAction
* action
= popup
.addAction(text
);
475 action
->setCheckable(true);
476 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
480 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
481 if (activatedAction
!= 0) {
482 const bool show
= activatedAction
->isChecked();
483 const int columnIndex
= activatedAction
->data().toInt();
485 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
486 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
488 Q_ASSERT(!list
.contains(info
));
491 Q_ASSERT(list
.contains(info
));
492 const int index
= list
.indexOf(info
);
493 list
.removeAt(index
);
496 m_controller
->indicateAdditionalInfoChange(list
);
497 setColumnHidden(columnIndex
, !show
);
501 void DolphinDetailsView::updateColumnVisibility()
503 const KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
504 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
505 const KFileItemDelegate::Information info
= infoForColumn(i
);
506 const bool hide
= !list
.contains(info
);
507 if (isColumnHidden(i
) != hide
) {
508 setColumnHidden(i
, hide
);
515 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex
, int oldSize
, int newSize
)
517 Q_UNUSED(logicalIndex
);
520 if (QApplication::mouseButtons() & Qt::LeftButton
) {
521 disableAutoResizing();
525 void DolphinDetailsView::disableAutoResizing()
527 m_autoResize
= false;
530 void DolphinDetailsView::requestActivation()
532 m_controller
->requestActivation();
535 bool DolphinDetailsView::isZoomInPossible() const
537 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
538 return settings
->iconSize() < KIconLoader::SizeLarge
;
541 bool DolphinDetailsView::isZoomOutPossible() const
543 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
544 return settings
->iconSize() > KIconLoader::SizeSmall
;
547 void DolphinDetailsView::updateDecorationSize()
549 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
550 const int iconSize
= settings
->iconSize();
551 setIconSize(QSize(iconSize
, iconSize
));
552 m_decorationSize
= QSize(iconSize
, iconSize
);
554 m_controller
->setZoomInPossible(isZoomInPossible());
555 m_controller
->setZoomOutPossible(isZoomOutPossible());
560 QPoint
DolphinDetailsView::contentsPos() const
562 // implementation note: the horizonal position is ignored currently, as no
563 // horizontal scrolling is done anyway during a selection
564 const QScrollBar
* scrollbar
= verticalScrollBar();
565 Q_ASSERT(scrollbar
!= 0);
567 const int maxHeight
= maximumViewportSize().height();
568 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
569 const int visibleHeight
= model()->rowCount() + 1 - height
;
570 if (visibleHeight
<= 0) {
574 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
578 KFileItem
DolphinDetailsView::itemForIndex(const QModelIndex
& index
) const
580 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
581 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
582 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
583 return dirModel
->itemForIndex(dirIndex
);
586 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
588 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
590 switch (columnIndex
) {
591 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
592 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
593 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
594 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
595 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
596 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
603 void DolphinDetailsView::resizeColumns()
605 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
606 // around 3 seconds for each (!) resize operation when having > 10000 items).
607 // This gets a problem especially when opening large directories, where several
608 // resize operations are received for showing the currently available items during
609 // loading (the application hangs around 20 seconds when loading > 10000 items).
611 QHeaderView
* headerView
= header();
612 QFontMetrics
fontMetrics(viewport()->font());
614 int columnWidth
[KDirModel::ColumnCount
];
615 columnWidth
[KDirModel::Size
] = fontMetrics
.width("00000 Items");
616 columnWidth
[KDirModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
617 columnWidth
[KDirModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
618 columnWidth
[KDirModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
619 columnWidth
[KDirModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
620 columnWidth
[KDirModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
622 int requiredWidth
= 0;
623 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
624 if (!isColumnHidden(i
)) {
625 columnWidth
[i
] += 20; // provide a default gap
626 requiredWidth
+= columnWidth
[i
];
627 headerView
->resizeSection(i
, columnWidth
[i
]);
631 // resize the name column in a way that the whole available width is used
632 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
633 if (columnWidth
[KDirModel::Name
] < 120) {
634 columnWidth
[KDirModel::Name
] = 120;
636 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
639 #include "dolphindetailsview.moc"