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 "viewproperties.h"
30 #include "dolphin_detailsmodesettings.h"
32 #include <kdirmodel.h>
36 #include <QAbstractProxyModel>
38 #include <QApplication>
39 #include <QHeaderView>
40 #include <QRubberBand>
44 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
, DolphinController
* controller
) :
46 m_controller(controller
),
47 m_clearAdditionalInfo(false),
49 m_showElasticBand(false),
50 m_elasticBandOrigin(),
51 m_elasticBandDestination()
53 Q_ASSERT(controller
!= 0);
56 setRootIsDecorated(false);
57 setSortingEnabled(true);
58 setUniformRowHeights(true);
59 setSelectionBehavior(SelectItems
);
60 setDragDropMode(QAbstractItemView::DragDrop
);
61 setDropIndicatorShown(false);
62 setAlternatingRowColors(true);
64 setMouseTracking(true);
65 viewport()->setAttribute(Qt::WA_Hover
);
67 const ViewProperties
props(controller
->url());
68 setSortIndicatorSection(props
.sorting());
69 setSortIndicatorOrder(props
.sortOrder());
71 QHeaderView
* headerView
= header();
72 connect(headerView
, SIGNAL(sectionClicked(int)),
73 this, SLOT(synchronizeSortingState(int)));
74 headerView
->setContextMenuPolicy(Qt::CustomContextMenu
);
75 connect(headerView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
76 this, SLOT(configureColumns(const QPoint
&)));
78 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
79 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
80 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
81 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
83 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
84 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
85 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
86 // RETURN-key in keyPressEvent().
87 if (KGlobalSettings::singleClick()) {
88 connect(this, SIGNAL(clicked(const QModelIndex
&)),
89 this, SLOT(triggerItem(const QModelIndex
&)));
91 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
92 this, SLOT(triggerItem(const QModelIndex
&)));
94 connect(this, SIGNAL(entered(const QModelIndex
&)),
95 this, SLOT(slotEntered(const QModelIndex
&)));
96 connect(this, SIGNAL(viewportEntered()),
97 controller
, SLOT(emitViewportEntered()));
98 connect(controller
, SIGNAL(zoomIn()),
99 this, SLOT(zoomIn()));
100 connect(controller
, SIGNAL(zoomOut()),
101 this, SLOT(zoomOut()));
102 connect(controller
->dolphinView(), SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
103 this, SLOT(updateColumnVisibility()));
105 // apply the details mode settings to the widget
106 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
107 Q_ASSERT(settings
!= 0);
109 m_viewOptions
= QTreeView::viewOptions();
111 QFont
font(settings
->fontFamily(), settings
->fontSize());
112 font
.setItalic(settings
->italicFont());
113 font
.setBold(settings
->boldFont());
114 m_viewOptions
.font
= font
;
115 m_viewOptions
.showDecorationSelected
= true;
117 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
118 // check avoids a division by zero happening on versions before 4.3.1.
119 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
121 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
122 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
123 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
126 updateDecorationSize();
131 DolphinDetailsView::~DolphinDetailsView()
135 bool DolphinDetailsView::event(QEvent
* event
)
137 if (event
->type() == QEvent::Polish
) {
138 QHeaderView
* headerView
= header();
139 headerView
->setResizeMode(QHeaderView::Fixed
);
140 headerView
->setMovable(false);
142 updateColumnVisibility();
144 hideColumn(DolphinModel::Rating
);
145 hideColumn(DolphinModel::Tags
);
147 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
148 // check avoids a division by zero happening on versions before 4.3.1.
149 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
151 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
152 else if (event
->type() == QEvent::UpdateRequest
) {
153 // a wheel movement will scroll 4 items
154 if (model()->rowCount() > 0) {
155 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
160 return QTreeView::event(event
);
163 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
165 return m_viewOptions
;
168 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
170 QTreeView::contextMenuEvent(event
);
171 m_controller
->triggerContextMenuRequest(event
->pos());
174 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
176 m_controller
->requestActivation();
178 QTreeView::mousePressEvent(event
);
180 const QModelIndex index
= indexAt(event
->pos());
181 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
182 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
183 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
188 if (event
->button() == Qt::LeftButton
) {
189 m_showElasticBand
= true;
191 const QPoint
pos(contentsPos());
192 m_elasticBandOrigin
= event
->pos();
193 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
194 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
195 m_elasticBandDestination
= event
->pos();
199 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
201 QTreeView::mouseMoveEvent(event
);
202 if (m_showElasticBand
) {
207 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
209 QTreeView::mouseReleaseEvent(event
);
210 if (m_showElasticBand
) {
212 m_showElasticBand
= false;
216 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions
)
218 DragAndDropHelper::startDrag(this, supportedActions
);
221 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
223 if (event
->mimeData()->hasUrls()) {
224 event
->acceptProposedAction();
227 if (m_showElasticBand
) {
229 m_showElasticBand
= false;
234 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
236 QTreeView::dragLeaveEvent(event
);
238 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
240 setDirtyRegion(m_dropRect
);
243 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
245 QTreeView::dragMoveEvent(event
);
247 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
248 setDirtyRegion(m_dropRect
);
249 const QModelIndex index
= indexAt(event
->pos());
250 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
254 const KFileItem item
= itemForIndex(index
);
255 if (!item
.isNull() && item
.isDir()) {
256 m_dropRect
= visualRect(index
);
258 m_dropRect
.setSize(QSize()); // set as invalid
260 setDirtyRegion(m_dropRect
);
264 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
266 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
267 if (!urls
.isEmpty()) {
268 event
->acceptProposedAction();
269 const QModelIndex index
= indexAt(event
->pos());
271 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
272 item
= itemForIndex(index
);
274 m_controller
->indicateDroppedUrls(urls
,
278 QTreeView::dropEvent(event
);
282 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
284 QTreeView::paintEvent(event
);
285 if (m_showElasticBand
) {
286 // The following code has been taken from QListView
287 // and adapted to DolphinDetailsView.
288 // (C) 1992-2007 Trolltech ASA
289 QStyleOptionRubberBand opt
;
291 opt
.shape
= QRubberBand::Rectangle
;
293 opt
.rect
= elasticBandRect();
295 QPainter
painter(viewport());
297 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
301 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
303 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
304 DragAndDropHelper::drawHoverIndication(viewport(), m_dropRect
, brush
);
308 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
310 QTreeView::keyPressEvent(event
);
312 const QItemSelectionModel
* selModel
= selectionModel();
313 const QModelIndex currentIndex
= selModel
->currentIndex();
314 const bool trigger
= currentIndex
.isValid()
315 && (event
->key() == Qt::Key_Return
)
316 && (selModel
->selectedIndexes().count() <= 1);
318 triggerItem(currentIndex
);
322 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
324 QTreeView::resizeEvent(event
);
328 void DolphinDetailsView::closeEvent(QCloseEvent
* event
)
330 if (m_clearAdditionalInfo
) {
331 disconnect(m_controller
->dolphinView(), SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
332 this, SLOT(updateColumnVisibility()));
334 KFileItemDelegate::InformationList info
;
335 info
.append(KFileItemDelegate::NoInformation
);
336 m_controller
->indicateAdditionalInfoChange(info
);
337 m_clearAdditionalInfo
= false;
339 QTreeView::closeEvent(event
);
342 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
344 QHeaderView
* headerView
= header();
345 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
348 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
350 QHeaderView
* headerView
= header();
351 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
354 void DolphinDetailsView::synchronizeSortingState(int column
)
356 // The sorting has already been changed in QTreeView if this slot is
357 // invoked, but Dolphin is not informed about this.
358 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
359 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
360 m_controller
->indicateSortingChange(sorting
);
361 m_controller
->indicateSortOrderChange(sortOrder
);
364 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
366 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
367 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
368 if (pos
.x() < nameColumnWidth
) {
369 m_controller
->emitItemEntered(itemForIndex(index
));
372 m_controller
->emitViewportEntered();
376 void DolphinDetailsView::updateElasticBand()
378 Q_ASSERT(m_showElasticBand
);
379 QRect
dirtyRegion(elasticBandRect());
380 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
381 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
382 setDirtyRegion(dirtyRegion
);
385 QRect
DolphinDetailsView::elasticBandRect() const
387 const QPoint
pos(contentsPos());
388 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
389 return QRect(topLeft
, m_elasticBandDestination
).normalized();
392 void DolphinDetailsView::zoomIn()
394 if (isZoomInPossible()) {
395 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
396 switch (settings
->iconSize()) {
397 case KIconLoader::SizeSmall
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
398 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeLarge
); break;
399 default: Q_ASSERT(false); break;
401 updateDecorationSize();
405 void DolphinDetailsView::zoomOut()
407 if (isZoomOutPossible()) {
408 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
409 switch (settings
->iconSize()) {
410 case KIconLoader::SizeLarge
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
411 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeSmall
); break;
412 default: Q_ASSERT(false); break;
414 updateDecorationSize();
418 void DolphinDetailsView::triggerItem(const QModelIndex
& index
)
420 const KFileItem item
= itemForIndex(index
);
421 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
422 m_controller
->triggerItem(item
);
425 m_controller
->emitItemEntered(item
);
429 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
432 popup
.addTitle(i18nc("@title:menu", "Columns"));
434 QHeaderView
* headerView
= header();
435 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
436 const int logicalIndex
= headerView
->logicalIndex(i
);
437 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
438 QAction
* action
= popup
.addAction(text
);
439 action
->setCheckable(true);
440 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
444 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
445 if (activatedAction
!= 0) {
446 const bool show
= activatedAction
->isChecked();
447 const int columnIndex
= activatedAction
->data().toInt();
449 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
450 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
452 Q_ASSERT(!list
.contains(info
));
455 Q_ASSERT(list
.contains(info
));
456 const int index
= list
.indexOf(info
);
457 list
.removeAt(index
);
460 m_controller
->indicateAdditionalInfoChange(list
);
461 setColumnHidden(columnIndex
, !show
);
465 void DolphinDetailsView::updateColumnVisibility()
467 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
468 const bool useDefaultColumns
= !isVisible() &&
470 list
.contains(KFileItemDelegate::NoInformation
));
471 if (useDefaultColumns
) {
472 // Using the details view without any additional information (-> additional column)
473 // makes no sense and leads to a usability problem as no viewport area is available
474 // anymore. Hence as fallback provide at least a size and date column.
476 list
.append(KFileItemDelegate::Size
);
477 list
.append(KFileItemDelegate::ModificationTime
);
478 m_controller
->indicateAdditionalInfoChange(list
);
479 m_clearAdditionalInfo
= true;
482 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
483 const KFileItemDelegate::Information info
= infoForColumn(i
);
484 const bool hide
= !list
.contains(info
);
485 if (isColumnHidden(i
) != hide
) {
486 setColumnHidden(i
, hide
);
487 m_clearAdditionalInfo
= false;
494 bool DolphinDetailsView::isZoomInPossible() const
496 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
497 return settings
->iconSize() < KIconLoader::SizeLarge
;
500 bool DolphinDetailsView::isZoomOutPossible() const
502 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
503 return settings
->iconSize() > KIconLoader::SizeSmall
;
506 void DolphinDetailsView::updateDecorationSize()
508 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
509 const int iconSize
= settings
->iconSize();
510 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
512 m_controller
->setZoomInPossible(isZoomInPossible());
513 m_controller
->setZoomOutPossible(isZoomOutPossible());
518 QPoint
DolphinDetailsView::contentsPos() const
520 // implementation note: the horizonal position is ignored currently, as no
521 // horizontal scrolling is done anyway during a selection
522 const QScrollBar
* scrollbar
= verticalScrollBar();
523 Q_ASSERT(scrollbar
!= 0);
525 const int maxHeight
= maximumViewportSize().height();
526 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
527 const int visibleHeight
= model()->rowCount() + 1 - height
;
528 if (visibleHeight
<= 0) {
532 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
536 KFileItem
DolphinDetailsView::itemForIndex(const QModelIndex
& index
) const
538 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
539 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
540 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
541 return dirModel
->itemForIndex(dirIndex
);
544 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
546 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
548 switch (columnIndex
) {
549 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
550 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
551 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
552 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
553 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
554 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
561 void DolphinDetailsView::resizeColumns()
563 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
564 // around 3 seconds for each (!) resize operation when having > 10000 items).
565 // This gets a problem especially when opening large directories, where several
566 // resize operations are received for showing the currently available items during
567 // loading (the application hangs around 20 seconds when loading > 10000 items).
569 QHeaderView
* headerView
= header();
570 QFontMetrics
fontMetrics(viewport()->font());
572 int columnWidth
[KDirModel::ColumnCount
];
573 columnWidth
[KDirModel::Size
] = fontMetrics
.width("00000 Items");
574 columnWidth
[KDirModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
575 columnWidth
[KDirModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
576 columnWidth
[KDirModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
577 columnWidth
[KDirModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
578 columnWidth
[KDirModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
580 int requiredWidth
= 0;
581 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
582 if (!isColumnHidden(i
)) {
583 columnWidth
[i
] += 20; // provide a default gap
584 requiredWidth
+= columnWidth
[i
];
585 headerView
->resizeSection(i
, columnWidth
[i
]);
589 // resize the name column in a way that the whole available width is used
590 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
591 if (columnWidth
[KDirModel::Name
] < 120) {
592 columnWidth
[KDirModel::Name
] = 120;
594 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
597 #include "dolphindetailsview.moc"