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 QHeaderView
* headerView
= header();
138 headerView
->setResizeMode(QHeaderView::Fixed
);
139 headerView
->setMovable(false);
141 updateColumnVisibility();
143 hideColumn(DolphinModel::Rating
);
144 hideColumn(DolphinModel::Tags
);
146 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
147 // check avoids a division by zero happening on versions before 4.3.1.
148 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
150 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
151 else if (event
->type() == QEvent::UpdateRequest
) {
152 // a wheel movement will scroll 4 items
153 if (model()->rowCount() > 0) {
154 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
159 return QTreeView::event(event
);
162 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
164 return m_viewOptions
;
167 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
169 QTreeView::contextMenuEvent(event
);
170 m_controller
->triggerContextMenuRequest(event
->pos());
173 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
175 m_controller
->requestActivation();
177 QTreeView::mousePressEvent(event
);
179 const QModelIndex index
= indexAt(event
->pos());
180 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
181 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
182 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
187 if (event
->button() == Qt::LeftButton
) {
188 m_showElasticBand
= true;
190 const QPoint
pos(contentsPos());
191 m_elasticBandOrigin
= event
->pos();
192 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
193 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
194 m_elasticBandDestination
= event
->pos();
198 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
200 QTreeView::mouseMoveEvent(event
);
201 if (m_showElasticBand
) {
206 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
208 QTreeView::mouseReleaseEvent(event
);
209 if (m_showElasticBand
) {
211 m_showElasticBand
= false;
215 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
217 if (event
->mimeData()->hasUrls()) {
218 event
->acceptProposedAction();
221 if (m_showElasticBand
) {
223 m_showElasticBand
= false;
228 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
230 QTreeView::dragLeaveEvent(event
);
232 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
234 setDirtyRegion(m_dropRect
);
237 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
239 QTreeView::dragMoveEvent(event
);
241 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
242 setDirtyRegion(m_dropRect
);
243 const QModelIndex index
= indexAt(event
->pos());
244 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
248 if (itemForIndex(index
).isDir()) {
249 m_dropRect
= visualRect(index
);
251 m_dropRect
.setSize(QSize()); // set as invalid
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());
264 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
265 item
= itemForIndex(index
);
267 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
);
321 void DolphinDetailsView::closeEvent(QCloseEvent
* event
)
323 if (m_clearAdditionalInfo
) {
324 disconnect(m_controller
->dolphinView(), SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
325 this, SLOT(updateColumnVisibility()));
327 KFileItemDelegate::InformationList info
;
328 info
.append(KFileItemDelegate::NoInformation
);
329 m_controller
->indicateAdditionalInfoChange(info
);
330 m_clearAdditionalInfo
= false;
332 QTreeView::closeEvent(event
);
335 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
337 QHeaderView
* headerView
= header();
338 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
341 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
343 QHeaderView
* headerView
= header();
344 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
347 void DolphinDetailsView::synchronizeSortingState(int column
)
349 // The sorting has already been changed in QTreeView if this slot is
350 // invoked, but Dolphin is not informed about this.
351 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
352 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
353 m_controller
->indicateSortingChange(sorting
);
354 m_controller
->indicateSortOrderChange(sortOrder
);
357 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
359 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
360 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
361 if (pos
.x() < nameColumnWidth
) {
362 m_controller
->emitItemEntered(itemForIndex(index
));
365 m_controller
->emitViewportEntered();
369 void DolphinDetailsView::updateElasticBand()
371 Q_ASSERT(m_showElasticBand
);
372 QRect
dirtyRegion(elasticBandRect());
373 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
374 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
375 setDirtyRegion(dirtyRegion
);
378 QRect
DolphinDetailsView::elasticBandRect() const
380 const QPoint
pos(contentsPos());
381 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
382 return QRect(topLeft
, m_elasticBandDestination
).normalized();
385 void DolphinDetailsView::zoomIn()
387 if (isZoomInPossible()) {
388 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
389 switch (settings
->iconSize()) {
390 case KIconLoader::SizeSmall
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
391 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeLarge
); break;
392 default: Q_ASSERT(false); break;
394 updateDecorationSize();
398 void DolphinDetailsView::zoomOut()
400 if (isZoomOutPossible()) {
401 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
402 switch (settings
->iconSize()) {
403 case KIconLoader::SizeLarge
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
404 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeSmall
); break;
405 default: Q_ASSERT(false); break;
407 updateDecorationSize();
411 void DolphinDetailsView::triggerItem(const QModelIndex
& index
)
413 const KFileItem item
= itemForIndex(index
);
414 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
415 m_controller
->triggerItem(item
);
418 m_controller
->emitItemEntered(item
);
422 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
425 popup
.addTitle(i18nc("@title:menu", "Columns"));
427 QHeaderView
* headerView
= header();
428 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
429 const int logicalIndex
= headerView
->logicalIndex(i
);
430 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
431 QAction
* action
= popup
.addAction(text
);
432 action
->setCheckable(true);
433 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
437 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
438 if (activatedAction
!= 0) {
439 const bool show
= activatedAction
->isChecked();
440 const int columnIndex
= activatedAction
->data().toInt();
442 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
443 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
445 Q_ASSERT(!list
.contains(info
));
448 Q_ASSERT(list
.contains(info
));
449 const int index
= list
.indexOf(info
);
450 list
.removeAt(index
);
453 m_controller
->indicateAdditionalInfoChange(list
);
454 setColumnHidden(columnIndex
, !show
);
458 void DolphinDetailsView::updateColumnVisibility()
460 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
461 const bool useDefaultColumns
= !isVisible() &&
463 list
.contains(KFileItemDelegate::NoInformation
));
464 if (useDefaultColumns
) {
465 // Using the details view without any additional information (-> additional column)
466 // makes no sense and leads to a usability problem as no viewport area is available
467 // anymore. Hence as fallback provide at least a size and date column.
469 list
.append(KFileItemDelegate::Size
);
470 list
.append(KFileItemDelegate::ModificationTime
);
471 m_controller
->indicateAdditionalInfoChange(list
);
472 m_clearAdditionalInfo
= true;
475 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
476 const KFileItemDelegate::Information info
= infoForColumn(i
);
477 const bool hide
= !list
.contains(info
);
478 if (isColumnHidden(i
) != hide
) {
479 setColumnHidden(i
, hide
);
480 m_clearAdditionalInfo
= false;
487 bool DolphinDetailsView::isZoomInPossible() const
489 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
490 return settings
->iconSize() < KIconLoader::SizeLarge
;
493 bool DolphinDetailsView::isZoomOutPossible() const
495 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
496 return settings
->iconSize() > KIconLoader::SizeSmall
;
499 void DolphinDetailsView::updateDecorationSize()
501 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
502 const int iconSize
= settings
->iconSize();
503 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
505 m_controller
->setZoomInPossible(isZoomInPossible());
506 m_controller
->setZoomOutPossible(isZoomOutPossible());
511 QPoint
DolphinDetailsView::contentsPos() const
513 // implementation note: the horizonal position is ignored currently, as no
514 // horizontal scrolling is done anyway during a selection
515 const QScrollBar
* scrollbar
= verticalScrollBar();
516 Q_ASSERT(scrollbar
!= 0);
518 const int maxHeight
= maximumViewportSize().height();
519 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
520 const int visibleHeight
= model()->rowCount() + 1 - height
;
521 if (visibleHeight
<= 0) {
525 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
529 KFileItem
DolphinDetailsView::itemForIndex(const QModelIndex
& index
) const
531 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
532 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
533 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
534 return dirModel
->itemForIndex(dirIndex
);
537 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
539 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
541 switch (columnIndex
) {
542 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
543 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
544 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
545 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
546 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
547 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
554 void DolphinDetailsView::resizeColumns()
556 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
557 // around 3 seconds for each (!) resize operation when having > 10000 items).
558 // This gets a problem especially when opening large directories, where several
559 // resize operations are received for showing the currently available items during
560 // loading (the application hangs around 20 seconds when loading > 10000 items).
562 QHeaderView
* headerView
= header();
563 QFontMetrics
fontMetrics(viewport()->font());
565 int columnWidth
[KDirModel::ColumnCount
];
566 columnWidth
[KDirModel::Size
] = fontMetrics
.width("00000 Items");
567 columnWidth
[KDirModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
568 columnWidth
[KDirModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
569 columnWidth
[KDirModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
570 columnWidth
[KDirModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
571 columnWidth
[KDirModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
573 int requiredWidth
= 0;
574 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
575 if (!isColumnHidden(i
)) {
576 columnWidth
[i
] += 20; // provide a default gap
577 requiredWidth
+= columnWidth
[i
];
578 headerView
->resizeSection(i
, columnWidth
[i
]);
582 // resize the name column in a way that the whole available width is used
583 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
584 if (columnWidth
[KDirModel::Name
] < 120) {
585 columnWidth
[KDirModel::Name
] = 120;
587 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
590 #include "dolphindetailsview.moc"