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 if (itemForIndex(index
).isDir()) {
253 m_dropRect
= visualRect(index
);
255 m_dropRect
.setSize(QSize()); // set as invalid
257 setDirtyRegion(m_dropRect
);
261 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
263 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
264 if (!urls
.isEmpty()) {
265 event
->acceptProposedAction();
266 const QModelIndex index
= indexAt(event
->pos());
268 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
269 item
= itemForIndex(index
);
271 m_controller
->indicateDroppedUrls(urls
,
275 QTreeView::dropEvent(event
);
279 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
281 QTreeView::paintEvent(event
);
282 if (m_showElasticBand
) {
283 // The following code has been taken from QListView
284 // and adapted to DolphinDetailsView.
285 // (C) 1992-2007 Trolltech ASA
286 QStyleOptionRubberBand opt
;
288 opt
.shape
= QRubberBand::Rectangle
;
290 opt
.rect
= elasticBandRect();
292 QPainter
painter(viewport());
294 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
298 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
300 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
301 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
305 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
307 QTreeView::keyPressEvent(event
);
309 const QItemSelectionModel
* selModel
= selectionModel();
310 const QModelIndex currentIndex
= selModel
->currentIndex();
311 const bool trigger
= currentIndex
.isValid()
312 && (event
->key() == Qt::Key_Return
)
313 && (selModel
->selectedIndexes().count() <= 1);
315 triggerItem(currentIndex
);
319 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
321 QTreeView::resizeEvent(event
);
323 // assure that the width of the name-column does not get too small
324 const int minWidth
= 120;
325 QHeaderView
* headerView
= header();
326 bool useFixedWidth
= (headerView
->sectionSize(KDirModel::Name
) <= minWidth
)
327 && (headerView
->resizeMode(0) != QHeaderView::Fixed
);
329 // the current width of the name-column is too small, hence
331 headerView
->setResizeMode(QHeaderView::Fixed
);
332 headerView
->setResizeMode(0, QHeaderView::Fixed
);
333 headerView
->resizeSection(KDirModel::Name
, minWidth
);
334 } else if (headerView
->resizeMode(0) != QHeaderView::Stretch
) {
335 // check whether there is enough available viewport width
336 // to automatically resize the columns
337 const int availableWidth
= viewport()->width();
340 const int count
= headerView
->count();
341 for (int i
= 0; i
< count
; ++i
) {
342 headerWidth
+= headerView
->sectionSize(i
);
345 if (headerWidth
< availableWidth
) {
346 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
347 headerView
->setResizeMode(0, QHeaderView::Stretch
);
352 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
354 QHeaderView
* headerView
= header();
355 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
358 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
360 QHeaderView
* headerView
= header();
361 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
364 void DolphinDetailsView::synchronizeSortingState(int column
)
366 // The sorting has already been changed in QTreeView if this slot is
367 // invoked, but Dolphin is not informed about this.
368 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
369 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
370 m_controller
->indicateSortingChange(sorting
);
371 m_controller
->indicateSortOrderChange(sortOrder
);
374 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
376 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
377 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
378 if (pos
.x() < nameColumnWidth
) {
379 m_controller
->emitItemEntered(itemForIndex(index
));
382 m_controller
->emitViewportEntered();
386 void DolphinDetailsView::updateElasticBand()
388 Q_ASSERT(m_showElasticBand
);
389 QRect
dirtyRegion(elasticBandRect());
390 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
391 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
392 setDirtyRegion(dirtyRegion
);
395 QRect
DolphinDetailsView::elasticBandRect() const
397 const QPoint
pos(contentsPos());
398 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
399 return QRect(topLeft
, m_elasticBandDestination
).normalized();
402 void DolphinDetailsView::zoomIn()
404 if (isZoomInPossible()) {
405 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
406 switch (settings
->iconSize()) {
407 case KIconLoader::SizeSmall
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
408 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeLarge
); break;
409 default: Q_ASSERT(false); break;
411 updateDecorationSize();
415 void DolphinDetailsView::zoomOut()
417 if (isZoomOutPossible()) {
418 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
419 switch (settings
->iconSize()) {
420 case KIconLoader::SizeLarge
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
421 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeSmall
); break;
422 default: Q_ASSERT(false); break;
424 updateDecorationSize();
428 void DolphinDetailsView::triggerItem(const QModelIndex
& index
)
430 const KFileItem item
= itemForIndex(index
);
431 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
432 m_controller
->triggerItem(item
);
435 m_controller
->emitItemEntered(item
);
439 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
442 popup
.addTitle(i18nc("@title:menu", "Columns"));
444 QHeaderView
* headerView
= header();
445 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
446 const int logicalIndex
= headerView
->logicalIndex(i
);
447 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
448 QAction
* action
= popup
.addAction(text
);
449 action
->setCheckable(true);
450 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
454 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
455 if (activatedAction
!= 0) {
456 const bool show
= activatedAction
->isChecked();
457 const int columnIndex
= activatedAction
->data().toInt();
459 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
460 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
461 switch (columnIndex
) {
462 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
463 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
464 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
465 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
466 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
467 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
472 Q_ASSERT(!list
.contains(info
));
475 Q_ASSERT(list
.contains(info
));
476 const int index
= list
.indexOf(info
);
477 list
.removeAt(index
);
480 m_controller
->indicateAdditionalInfoChange(list
);
481 setColumnHidden(columnIndex
, !show
);
485 void DolphinDetailsView::updateColumnVisibility()
487 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
488 if (list
.isEmpty() || list
.contains(KFileItemDelegate::NoInformation
)) {
489 // Using the details view without any additional information (-> additional column)
490 // makes no sense and leads to a usability problem as no viewport area is available
491 // anymore. Hence as fallback provide at least a size and date column.
493 list
.append(KFileItemDelegate::Size
);
494 list
.append(KFileItemDelegate::ModificationTime
);
495 m_controller
->indicateAdditionalInfoChange(list
);
498 setColumnHidden(DolphinModel::Size
, !list
.contains(KFileItemDelegate::Size
));
499 setColumnHidden(DolphinModel::ModifiedTime
, !list
.contains(KFileItemDelegate::ModificationTime
));
500 setColumnHidden(DolphinModel::Permissions
, !list
.contains(KFileItemDelegate::Permissions
));
501 setColumnHidden(DolphinModel::Owner
, !list
.contains(KFileItemDelegate::Owner
));
502 setColumnHidden(DolphinModel::Group
, !list
.contains(KFileItemDelegate::OwnerAndGroup
));
503 setColumnHidden(DolphinModel::Type
, !list
.contains(KFileItemDelegate::FriendlyMimeType
));
506 bool DolphinDetailsView::isZoomInPossible() const
508 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
509 return settings
->iconSize() < KIconLoader::SizeLarge
;
512 bool DolphinDetailsView::isZoomOutPossible() const
514 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
515 return settings
->iconSize() > KIconLoader::SizeSmall
;
518 void DolphinDetailsView::updateDecorationSize()
520 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
521 const int iconSize
= settings
->iconSize();
522 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
524 m_controller
->setZoomInPossible(isZoomInPossible());
525 m_controller
->setZoomOutPossible(isZoomOutPossible());
530 QPoint
DolphinDetailsView::contentsPos() const
532 // implementation note: the horizonal position is ignored currently, as no
533 // horizontal scrolling is done anyway during a selection
534 const QScrollBar
* scrollbar
= verticalScrollBar();
535 Q_ASSERT(scrollbar
!= 0);
537 const int maxHeight
= maximumViewportSize().height();
538 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
539 const int visibleHeight
= model()->rowCount() + 1 - height
;
540 if (visibleHeight
<= 0) {
544 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
548 KFileItem
DolphinDetailsView::itemForIndex(const QModelIndex
& index
) const
550 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
551 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
552 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
553 return dirModel
->itemForIndex(dirIndex
);
556 #include "dolphindetailsview.moc"