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 "viewproperties.h"
29 #include "dolphin_detailsmodesettings.h"
31 #include <kdirmodel.h>
35 #include <QAbstractProxyModel>
37 #include <QApplication>
38 #include <QHeaderView>
39 #include <QRubberBand>
43 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
, DolphinController
* controller
) :
45 m_controller(controller
),
47 m_showElasticBand(false),
48 m_elasticBandOrigin(),
49 m_elasticBandDestination()
51 Q_ASSERT(controller
!= 0);
54 setRootIsDecorated(false);
55 setSortingEnabled(true);
56 setUniformRowHeights(true);
57 setSelectionBehavior(SelectItems
);
58 setDragDropMode(QAbstractItemView::DragDrop
);
59 setDropIndicatorShown(false);
60 setAlternatingRowColors(true);
62 setMouseTracking(true);
63 viewport()->setAttribute(Qt::WA_Hover
);
65 const ViewProperties
props(controller
->url());
66 setSortIndicatorSection(props
.sorting());
67 setSortIndicatorOrder(props
.sortOrder());
69 QHeaderView
* headerView
= header();
70 connect(headerView
, SIGNAL(sectionClicked(int)),
71 this, SLOT(synchronizeSortingState(int)));
72 headerView
->setContextMenuPolicy(Qt::CustomContextMenu
);
73 connect(headerView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
74 this, SLOT(configureColumns(const QPoint
&)));
76 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
77 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
78 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
79 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
81 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
82 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
83 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
84 // RETURN-key in keyPressEvent().
85 if (KGlobalSettings::singleClick()) {
86 connect(this, SIGNAL(clicked(const QModelIndex
&)),
87 this, SLOT(triggerItem(const QModelIndex
&)));
89 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
90 this, SLOT(triggerItem(const QModelIndex
&)));
92 connect(this, SIGNAL(entered(const QModelIndex
&)),
93 this, SLOT(slotEntered(const QModelIndex
&)));
94 connect(this, SIGNAL(viewportEntered()),
95 controller
, SLOT(emitViewportEntered()));
96 connect(controller
, SIGNAL(zoomIn()),
97 this, SLOT(zoomIn()));
98 connect(controller
, SIGNAL(zoomOut()),
99 this, SLOT(zoomOut()));
100 connect(controller
->dolphinView(), SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
101 this, SLOT(updateColumnVisibility()));
103 // apply the details mode settings to the widget
104 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
105 Q_ASSERT(settings
!= 0);
107 m_viewOptions
= QTreeView::viewOptions();
109 QFont
font(settings
->fontFamily(), settings
->fontSize());
110 font
.setItalic(settings
->italicFont());
111 font
.setBold(settings
->boldFont());
112 m_viewOptions
.font
= font
;
113 m_viewOptions
.showDecorationSelected
= true;
115 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
116 // check avoids a division by zero happening on versions before 4.3.1.
117 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
119 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
120 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
121 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
124 updateDecorationSize();
129 DolphinDetailsView::~DolphinDetailsView()
133 bool DolphinDetailsView::event(QEvent
* event
)
135 if (event
->type() == QEvent::Polish
) {
136 // Assure that by respecting the available width that:
137 // - the 'Name' column is stretched as large as possible
138 // - the remaining columns are as small as possible
139 QHeaderView
* headerView
= header();
140 headerView
->setStretchLastSection(false);
141 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
142 headerView
->setResizeMode(0, QHeaderView::Stretch
);
143 headerView
->setMovable(false);
145 updateColumnVisibility();
147 hideColumn(DolphinModel::Rating
);
148 hideColumn(DolphinModel::Tags
);
150 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
151 // check avoids a division by zero happening on versions before 4.3.1.
152 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
154 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
155 else if (event
->type() == QEvent::UpdateRequest
) {
156 // a wheel movement will scroll 4 items
157 if (model()->rowCount() > 0) {
158 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
163 return QTreeView::event(event
);
166 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
168 return m_viewOptions
;
171 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
173 QTreeView::contextMenuEvent(event
);
174 m_controller
->triggerContextMenuRequest(event
->pos());
177 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
179 m_controller
->requestActivation();
181 QTreeView::mousePressEvent(event
);
183 const QModelIndex index
= indexAt(event
->pos());
184 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
185 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
186 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
191 if (event
->button() == Qt::LeftButton
) {
192 m_showElasticBand
= true;
194 const QPoint
pos(contentsPos());
195 m_elasticBandOrigin
= event
->pos();
196 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
197 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
198 m_elasticBandDestination
= event
->pos();
202 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
204 QTreeView::mouseMoveEvent(event
);
205 if (m_showElasticBand
) {
210 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
212 QTreeView::mouseReleaseEvent(event
);
213 if (m_showElasticBand
) {
215 m_showElasticBand
= false;
219 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
221 if (event
->mimeData()->hasUrls()) {
222 event
->acceptProposedAction();
225 if (m_showElasticBand
) {
227 m_showElasticBand
= false;
232 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
234 QTreeView::dragLeaveEvent(event
);
236 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
238 setDirtyRegion(m_dropRect
);
241 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
243 QTreeView::dragMoveEvent(event
);
245 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
246 setDirtyRegion(m_dropRect
);
247 const QModelIndex index
= indexAt(event
->pos());
248 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
252 m_dropRect
= visualRect(index
);
253 setDirtyRegion(m_dropRect
);
257 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
259 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
260 if (!urls
.isEmpty()) {
261 event
->acceptProposedAction();
262 const QModelIndex index
= indexAt(event
->pos());
263 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
264 const KFileItem item
= itemForIndex(index
);
265 m_controller
->indicateDroppedUrls(urls
,
271 QTreeView::dropEvent(event
);
275 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
277 QTreeView::paintEvent(event
);
278 if (m_showElasticBand
) {
279 // The following code has been taken from QListView
280 // and adapted to DolphinDetailsView.
281 // (C) 1992-2007 Trolltech ASA
282 QStyleOptionRubberBand opt
;
284 opt
.shape
= QRubberBand::Rectangle
;
286 opt
.rect
= elasticBandRect();
288 QPainter
painter(viewport());
290 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
294 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
296 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
297 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
301 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
303 QTreeView::keyPressEvent(event
);
305 const QItemSelectionModel
* selModel
= selectionModel();
306 const QModelIndex currentIndex
= selModel
->currentIndex();
307 const bool trigger
= currentIndex
.isValid()
308 && (event
->key() == Qt::Key_Return
)
309 && (selModel
->selectedIndexes().count() <= 1);
311 triggerItem(currentIndex
);
315 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
317 QTreeView::resizeEvent(event
);
319 // assure that the width of the name-column does not get too small
320 const int minWidth
= 120;
321 QHeaderView
* headerView
= header();
322 bool useFixedWidth
= (headerView
->sectionSize(KDirModel::Name
) <= minWidth
)
323 && (headerView
->resizeMode(0) != QHeaderView::Fixed
);
325 // the current width of the name-column is too small, hence
327 headerView
->setResizeMode(QHeaderView::Fixed
);
328 headerView
->setResizeMode(0, QHeaderView::Fixed
);
329 headerView
->resizeSection(KDirModel::Name
, minWidth
);
330 } else if (headerView
->resizeMode(0) != QHeaderView::Stretch
) {
331 // check whether there is enough available viewport width
332 // to automatically resize the columns
333 const int availableWidth
= viewport()->width();
336 const int count
= headerView
->count();
337 for (int i
= 0; i
< count
; ++i
) {
338 headerWidth
+= headerView
->sectionSize(i
);
341 if (headerWidth
< availableWidth
) {
342 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
343 headerView
->setResizeMode(0, QHeaderView::Stretch
);
348 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
350 QHeaderView
* headerView
= header();
351 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
354 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
356 QHeaderView
* headerView
= header();
357 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
360 void DolphinDetailsView::synchronizeSortingState(int column
)
362 // The sorting has already been changed in QTreeView if this slot is
363 // invoked, but Dolphin is not informed about this.
364 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
365 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
366 m_controller
->indicateSortingChange(sorting
);
367 m_controller
->indicateSortOrderChange(sortOrder
);
370 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
372 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
373 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
374 if (pos
.x() < nameColumnWidth
) {
375 m_controller
->emitItemEntered(itemForIndex(index
));
378 m_controller
->emitViewportEntered();
382 void DolphinDetailsView::updateElasticBand()
384 Q_ASSERT(m_showElasticBand
);
385 QRect
dirtyRegion(elasticBandRect());
386 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
387 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
388 setDirtyRegion(dirtyRegion
);
391 QRect
DolphinDetailsView::elasticBandRect() const
393 const QPoint
pos(contentsPos());
394 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
395 return QRect(topLeft
, m_elasticBandDestination
).normalized();
398 void DolphinDetailsView::zoomIn()
400 if (isZoomInPossible()) {
401 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
402 switch (settings
->iconSize()) {
403 case KIconLoader::SizeSmall
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
404 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeLarge
); break;
405 default: Q_ASSERT(false); break;
407 updateDecorationSize();
411 void DolphinDetailsView::zoomOut()
413 if (isZoomOutPossible()) {
414 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
415 switch (settings
->iconSize()) {
416 case KIconLoader::SizeLarge
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
417 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeSmall
); break;
418 default: Q_ASSERT(false); break;
420 updateDecorationSize();
424 void DolphinDetailsView::triggerItem(const QModelIndex
& index
)
426 const KFileItem item
= itemForIndex(index
);
427 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
428 m_controller
->triggerItem(item
);
431 m_controller
->emitItemEntered(item
);
435 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
438 popup
.addTitle(i18nc("@title:menu", "Columns"));
440 QHeaderView
* headerView
= header();
441 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
442 const int logicalIndex
= headerView
->logicalIndex(i
);
443 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
444 QAction
* action
= popup
.addAction(text
);
445 action
->setCheckable(true);
446 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
450 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
451 if (activatedAction
!= 0) {
452 const bool show
= activatedAction
->isChecked();
453 const int columnIndex
= activatedAction
->data().toInt();
455 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
456 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
457 switch (columnIndex
) {
458 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
459 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
460 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
461 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
462 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
463 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
468 Q_ASSERT(!list
.contains(info
));
471 Q_ASSERT(list
.contains(info
));
472 const int index
= list
.indexOf(info
);
473 list
.removeAt(index
);
476 m_controller
->indicateAdditionalInfoChange(list
);
477 setColumnHidden(columnIndex
, !show
);
481 void DolphinDetailsView::updateColumnVisibility()
483 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
484 if (list
.isEmpty() || list
.contains(KFileItemDelegate::NoInformation
)) {
485 // Using the details view without any additional information (-> additional column)
486 // makes no sense and leads to a usability problem as no viewport area is available
487 // anymore. Hence as fallback provide at least a size and date column.
489 list
.append(KFileItemDelegate::Size
);
490 list
.append(KFileItemDelegate::ModificationTime
);
491 m_controller
->indicateAdditionalInfoChange(list
);
494 setColumnHidden(DolphinModel::Size
, !list
.contains(KFileItemDelegate::Size
));
495 setColumnHidden(DolphinModel::ModifiedTime
, !list
.contains(KFileItemDelegate::ModificationTime
));
496 setColumnHidden(DolphinModel::Permissions
, !list
.contains(KFileItemDelegate::Permissions
));
497 setColumnHidden(DolphinModel::Owner
, !list
.contains(KFileItemDelegate::Owner
));
498 setColumnHidden(DolphinModel::Group
, !list
.contains(KFileItemDelegate::OwnerAndGroup
));
499 setColumnHidden(DolphinModel::Type
, !list
.contains(KFileItemDelegate::FriendlyMimeType
));
502 bool DolphinDetailsView::isZoomInPossible() const
504 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
505 return settings
->iconSize() < KIconLoader::SizeLarge
;
508 bool DolphinDetailsView::isZoomOutPossible() const
510 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
511 return settings
->iconSize() > KIconLoader::SizeSmall
;
514 void DolphinDetailsView::updateDecorationSize()
516 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
517 const int iconSize
= settings
->iconSize();
518 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
520 m_controller
->setZoomInPossible(isZoomInPossible());
521 m_controller
->setZoomOutPossible(isZoomOutPossible());
526 QPoint
DolphinDetailsView::contentsPos() const
528 // implementation note: the horizonal position is ignored currently, as no
529 // horizontal scrolling is done anyway during a selection
530 const QScrollBar
* scrollbar
= verticalScrollBar();
531 Q_ASSERT(scrollbar
!= 0);
533 const int maxHeight
= maximumViewportSize().height();
534 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
535 const int visibleHeight
= model()->rowCount() + 1 - height
;
536 if (visibleHeight
<= 0) {
540 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
544 KFileItem
DolphinDetailsView::itemForIndex(const QModelIndex
& index
) const
546 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
547 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
548 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
549 return dirModel
->itemForIndex(dirIndex
);
552 #include "dolphindetailsview.moc"