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
),
46 m_clearAdditionalInfo(false),
48 m_showElasticBand(false),
49 m_elasticBandOrigin(),
50 m_elasticBandDestination()
52 Q_ASSERT(controller
!= 0);
55 setRootIsDecorated(false);
56 setSortingEnabled(true);
57 setUniformRowHeights(true);
58 setSelectionBehavior(SelectItems
);
59 setDragDropMode(QAbstractItemView::DragDrop
);
60 setDropIndicatorShown(false);
61 setAlternatingRowColors(true);
63 setMouseTracking(true);
64 viewport()->setAttribute(Qt::WA_Hover
);
66 const ViewProperties
props(controller
->url());
67 setSortIndicatorSection(props
.sorting());
68 setSortIndicatorOrder(props
.sortOrder());
70 QHeaderView
* headerView
= header();
71 connect(headerView
, SIGNAL(sectionClicked(int)),
72 this, SLOT(synchronizeSortingState(int)));
73 headerView
->setContextMenuPolicy(Qt::CustomContextMenu
);
74 connect(headerView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
75 this, SLOT(configureColumns(const QPoint
&)));
77 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
78 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
79 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
80 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
82 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
83 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
84 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
85 // RETURN-key in keyPressEvent().
86 if (KGlobalSettings::singleClick()) {
87 connect(this, SIGNAL(clicked(const QModelIndex
&)),
88 this, SLOT(triggerItem(const QModelIndex
&)));
90 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
91 this, SLOT(triggerItem(const QModelIndex
&)));
93 connect(this, SIGNAL(entered(const QModelIndex
&)),
94 this, SLOT(slotEntered(const QModelIndex
&)));
95 connect(this, SIGNAL(viewportEntered()),
96 controller
, SLOT(emitViewportEntered()));
97 connect(controller
, SIGNAL(zoomIn()),
98 this, SLOT(zoomIn()));
99 connect(controller
, SIGNAL(zoomOut()),
100 this, SLOT(zoomOut()));
101 connect(controller
->dolphinView(), SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
102 this, SLOT(updateColumnVisibility()));
104 // apply the details mode settings to the widget
105 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
106 Q_ASSERT(settings
!= 0);
108 m_viewOptions
= QTreeView::viewOptions();
110 QFont
font(settings
->fontFamily(), settings
->fontSize());
111 font
.setItalic(settings
->italicFont());
112 font
.setBold(settings
->boldFont());
113 m_viewOptions
.font
= font
;
114 m_viewOptions
.showDecorationSelected
= true;
116 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
117 // check avoids a division by zero happening on versions before 4.3.1.
118 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
120 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
121 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
122 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
125 updateDecorationSize();
130 DolphinDetailsView::~DolphinDetailsView()
134 bool DolphinDetailsView::event(QEvent
* event
)
136 if (event
->type() == QEvent::Polish
) {
137 // Assure that by respecting the available width that:
138 // - the 'Name' column is stretched as large as possible
139 // - the remaining columns are as small as possible
140 QHeaderView
* headerView
= header();
141 headerView
->setStretchLastSection(false);
142 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
143 headerView
->setResizeMode(0, QHeaderView::Stretch
);
144 headerView
->setMovable(false);
146 updateColumnVisibility();
148 hideColumn(DolphinModel::Rating
);
149 hideColumn(DolphinModel::Tags
);
151 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
152 // check avoids a division by zero happening on versions before 4.3.1.
153 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
155 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
156 else if (event
->type() == QEvent::UpdateRequest
) {
157 // a wheel movement will scroll 4 items
158 if (model()->rowCount() > 0) {
159 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
164 return QTreeView::event(event
);
167 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
169 return m_viewOptions
;
172 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
174 QTreeView::contextMenuEvent(event
);
175 m_controller
->triggerContextMenuRequest(event
->pos());
178 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
180 m_controller
->requestActivation();
182 QTreeView::mousePressEvent(event
);
184 const QModelIndex index
= indexAt(event
->pos());
185 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
186 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
187 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
192 if (event
->button() == Qt::LeftButton
) {
193 m_showElasticBand
= true;
195 const QPoint
pos(contentsPos());
196 m_elasticBandOrigin
= event
->pos();
197 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
198 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
199 m_elasticBandDestination
= event
->pos();
203 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
205 QTreeView::mouseMoveEvent(event
);
206 if (m_showElasticBand
) {
211 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
213 QTreeView::mouseReleaseEvent(event
);
214 if (m_showElasticBand
) {
216 m_showElasticBand
= false;
220 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
222 if (event
->mimeData()->hasUrls()) {
223 event
->acceptProposedAction();
226 if (m_showElasticBand
) {
228 m_showElasticBand
= false;
233 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
235 QTreeView::dragLeaveEvent(event
);
237 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
239 setDirtyRegion(m_dropRect
);
242 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
244 QTreeView::dragMoveEvent(event
);
246 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
247 setDirtyRegion(m_dropRect
);
248 const QModelIndex index
= indexAt(event
->pos());
249 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
253 if (itemForIndex(index
).isDir()) {
254 m_dropRect
= visualRect(index
);
256 m_dropRect
.setSize(QSize()); // set as invalid
258 setDirtyRegion(m_dropRect
);
262 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
264 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
265 if (!urls
.isEmpty()) {
266 event
->acceptProposedAction();
267 const QModelIndex index
= indexAt(event
->pos());
269 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
270 item
= itemForIndex(index
);
272 m_controller
->indicateDroppedUrls(urls
,
276 QTreeView::dropEvent(event
);
280 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
282 QTreeView::paintEvent(event
);
283 if (m_showElasticBand
) {
284 // The following code has been taken from QListView
285 // and adapted to DolphinDetailsView.
286 // (C) 1992-2007 Trolltech ASA
287 QStyleOptionRubberBand opt
;
289 opt
.shape
= QRubberBand::Rectangle
;
291 opt
.rect
= elasticBandRect();
293 QPainter
painter(viewport());
295 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
299 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
301 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
302 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
306 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
308 QTreeView::keyPressEvent(event
);
310 const QItemSelectionModel
* selModel
= selectionModel();
311 const QModelIndex currentIndex
= selModel
->currentIndex();
312 const bool trigger
= currentIndex
.isValid()
313 && (event
->key() == Qt::Key_Return
)
314 && (selModel
->selectedIndexes().count() <= 1);
316 triggerItem(currentIndex
);
320 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
322 QTreeView::resizeEvent(event
);
324 // assure that the width of the name-column does not get too small
325 const int minWidth
= 120;
326 QHeaderView
* headerView
= header();
327 bool useFixedWidth
= (headerView
->sectionSize(KDirModel::Name
) <= minWidth
)
328 && (headerView
->resizeMode(0) != QHeaderView::Fixed
);
330 // the current width of the name-column is too small, hence
332 headerView
->setResizeMode(QHeaderView::Fixed
);
333 headerView
->setResizeMode(0, QHeaderView::Fixed
);
334 headerView
->resizeSection(KDirModel::Name
, minWidth
);
335 } else if (headerView
->resizeMode(0) != QHeaderView::Stretch
) {
336 // check whether there is enough available viewport width
337 // to automatically resize the columns
338 const int availableWidth
= viewport()->width();
341 const int count
= headerView
->count();
342 for (int i
= 0; i
< count
; ++i
) {
343 headerWidth
+= headerView
->sectionSize(i
);
346 if (headerWidth
< availableWidth
) {
347 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
348 headerView
->setResizeMode(0, QHeaderView::Stretch
);
353 void DolphinDetailsView::closeEvent(QCloseEvent
* event
)
355 if (m_clearAdditionalInfo
) {
356 disconnect(m_controller
->dolphinView(), SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
357 this, SLOT(updateColumnVisibility()));
359 KFileItemDelegate::InformationList info
;
360 info
.append(KFileItemDelegate::NoInformation
);
361 m_controller
->indicateAdditionalInfoChange(info
);
362 m_clearAdditionalInfo
= false;
364 QTreeView::closeEvent(event
);
367 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
369 QHeaderView
* headerView
= header();
370 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
373 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
375 QHeaderView
* headerView
= header();
376 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
379 void DolphinDetailsView::synchronizeSortingState(int column
)
381 // The sorting has already been changed in QTreeView if this slot is
382 // invoked, but Dolphin is not informed about this.
383 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
384 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
385 m_controller
->indicateSortingChange(sorting
);
386 m_controller
->indicateSortOrderChange(sortOrder
);
389 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
391 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
392 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
393 if (pos
.x() < nameColumnWidth
) {
394 m_controller
->emitItemEntered(itemForIndex(index
));
397 m_controller
->emitViewportEntered();
401 void DolphinDetailsView::updateElasticBand()
403 Q_ASSERT(m_showElasticBand
);
404 QRect
dirtyRegion(elasticBandRect());
405 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
406 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
407 setDirtyRegion(dirtyRegion
);
410 QRect
DolphinDetailsView::elasticBandRect() const
412 const QPoint
pos(contentsPos());
413 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
414 return QRect(topLeft
, m_elasticBandDestination
).normalized();
417 void DolphinDetailsView::zoomIn()
419 if (isZoomInPossible()) {
420 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
421 switch (settings
->iconSize()) {
422 case KIconLoader::SizeSmall
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
423 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeLarge
); break;
424 default: Q_ASSERT(false); break;
426 updateDecorationSize();
430 void DolphinDetailsView::zoomOut()
432 if (isZoomOutPossible()) {
433 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
434 switch (settings
->iconSize()) {
435 case KIconLoader::SizeLarge
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
436 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeSmall
); break;
437 default: Q_ASSERT(false); break;
439 updateDecorationSize();
443 void DolphinDetailsView::triggerItem(const QModelIndex
& index
)
445 const KFileItem item
= itemForIndex(index
);
446 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
447 m_controller
->triggerItem(item
);
450 m_controller
->emitItemEntered(item
);
454 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
457 popup
.addTitle(i18nc("@title:menu", "Columns"));
459 QHeaderView
* headerView
= header();
460 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
461 const int logicalIndex
= headerView
->logicalIndex(i
);
462 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
463 QAction
* action
= popup
.addAction(text
);
464 action
->setCheckable(true);
465 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
469 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
470 if (activatedAction
!= 0) {
471 const bool show
= activatedAction
->isChecked();
472 const int columnIndex
= activatedAction
->data().toInt();
474 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
475 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
477 Q_ASSERT(!list
.contains(info
));
480 Q_ASSERT(list
.contains(info
));
481 const int index
= list
.indexOf(info
);
482 list
.removeAt(index
);
485 m_controller
->indicateAdditionalInfoChange(list
);
486 setColumnHidden(columnIndex
, !show
);
490 void DolphinDetailsView::updateColumnVisibility()
492 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
493 const bool useDefaultColumns
= !isVisible() &&
495 list
.contains(KFileItemDelegate::NoInformation
));
496 if (useDefaultColumns
) {
497 // Using the details view without any additional information (-> additional column)
498 // makes no sense and leads to a usability problem as no viewport area is available
499 // anymore. Hence as fallback provide at least a size and date column.
501 list
.append(KFileItemDelegate::Size
);
502 list
.append(KFileItemDelegate::ModificationTime
);
503 m_controller
->indicateAdditionalInfoChange(list
);
504 m_clearAdditionalInfo
= true;
507 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
508 const KFileItemDelegate::Information info
= infoForColumn(i
);
509 const bool hide
= !list
.contains(info
);
510 if (isColumnHidden(i
) != hide
) {
511 setColumnHidden(i
, hide
);
512 m_clearAdditionalInfo
= false;
517 bool DolphinDetailsView::isZoomInPossible() const
519 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
520 return settings
->iconSize() < KIconLoader::SizeLarge
;
523 bool DolphinDetailsView::isZoomOutPossible() const
525 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
526 return settings
->iconSize() > KIconLoader::SizeSmall
;
529 void DolphinDetailsView::updateDecorationSize()
531 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
532 const int iconSize
= settings
->iconSize();
533 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
535 m_controller
->setZoomInPossible(isZoomInPossible());
536 m_controller
->setZoomOutPossible(isZoomOutPossible());
541 QPoint
DolphinDetailsView::contentsPos() const
543 // implementation note: the horizonal position is ignored currently, as no
544 // horizontal scrolling is done anyway during a selection
545 const QScrollBar
* scrollbar
= verticalScrollBar();
546 Q_ASSERT(scrollbar
!= 0);
548 const int maxHeight
= maximumViewportSize().height();
549 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
550 const int visibleHeight
= model()->rowCount() + 1 - height
;
551 if (visibleHeight
<= 0) {
555 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
559 KFileItem
DolphinDetailsView::itemForIndex(const QModelIndex
& index
) const
561 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
562 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
563 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
564 return dirModel
->itemForIndex(dirIndex
);
567 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
569 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
571 switch (columnIndex
) {
572 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
573 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
574 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
575 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
576 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
577 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
584 #include "dolphindetailsview.moc"