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 if (itemForIndex(index
).isDir()) {
255 m_dropRect
= visualRect(index
);
257 m_dropRect
.setSize(QSize()); // set as invalid
259 setDirtyRegion(m_dropRect
);
263 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
265 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
266 if (!urls
.isEmpty()) {
267 event
->acceptProposedAction();
268 const QModelIndex index
= indexAt(event
->pos());
270 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
271 item
= itemForIndex(index
);
273 m_controller
->indicateDroppedUrls(urls
,
277 QTreeView::dropEvent(event
);
281 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
283 QTreeView::paintEvent(event
);
284 if (m_showElasticBand
) {
285 // The following code has been taken from QListView
286 // and adapted to DolphinDetailsView.
287 // (C) 1992-2007 Trolltech ASA
288 QStyleOptionRubberBand opt
;
290 opt
.shape
= QRubberBand::Rectangle
;
292 opt
.rect
= elasticBandRect();
294 QPainter
painter(viewport());
296 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
300 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
302 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
303 DragAndDropHelper::drawHoverIndication(viewport(), m_dropRect
, brush
);
307 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
309 QTreeView::keyPressEvent(event
);
311 const QItemSelectionModel
* selModel
= selectionModel();
312 const QModelIndex currentIndex
= selModel
->currentIndex();
313 const bool trigger
= currentIndex
.isValid()
314 && (event
->key() == Qt::Key_Return
)
315 && (selModel
->selectedIndexes().count() <= 1);
317 triggerItem(currentIndex
);
321 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
323 QTreeView::resizeEvent(event
);
327 void DolphinDetailsView::closeEvent(QCloseEvent
* event
)
329 if (m_clearAdditionalInfo
) {
330 disconnect(m_controller
->dolphinView(), SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
331 this, SLOT(updateColumnVisibility()));
333 KFileItemDelegate::InformationList info
;
334 info
.append(KFileItemDelegate::NoInformation
);
335 m_controller
->indicateAdditionalInfoChange(info
);
336 m_clearAdditionalInfo
= false;
338 QTreeView::closeEvent(event
);
341 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
343 QHeaderView
* headerView
= header();
344 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
347 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
349 QHeaderView
* headerView
= header();
350 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
353 void DolphinDetailsView::synchronizeSortingState(int column
)
355 // The sorting has already been changed in QTreeView if this slot is
356 // invoked, but Dolphin is not informed about this.
357 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
358 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
359 m_controller
->indicateSortingChange(sorting
);
360 m_controller
->indicateSortOrderChange(sortOrder
);
363 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
365 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
366 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
367 if (pos
.x() < nameColumnWidth
) {
368 m_controller
->emitItemEntered(itemForIndex(index
));
371 m_controller
->emitViewportEntered();
375 void DolphinDetailsView::updateElasticBand()
377 Q_ASSERT(m_showElasticBand
);
378 QRect
dirtyRegion(elasticBandRect());
379 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
380 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
381 setDirtyRegion(dirtyRegion
);
384 QRect
DolphinDetailsView::elasticBandRect() const
386 const QPoint
pos(contentsPos());
387 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
388 return QRect(topLeft
, m_elasticBandDestination
).normalized();
391 void DolphinDetailsView::zoomIn()
393 if (isZoomInPossible()) {
394 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
395 switch (settings
->iconSize()) {
396 case KIconLoader::SizeSmall
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
397 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeLarge
); break;
398 default: Q_ASSERT(false); break;
400 updateDecorationSize();
404 void DolphinDetailsView::zoomOut()
406 if (isZoomOutPossible()) {
407 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
408 switch (settings
->iconSize()) {
409 case KIconLoader::SizeLarge
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
410 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeSmall
); break;
411 default: Q_ASSERT(false); break;
413 updateDecorationSize();
417 void DolphinDetailsView::triggerItem(const QModelIndex
& index
)
419 const KFileItem item
= itemForIndex(index
);
420 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
421 m_controller
->triggerItem(item
);
424 m_controller
->emitItemEntered(item
);
428 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
431 popup
.addTitle(i18nc("@title:menu", "Columns"));
433 QHeaderView
* headerView
= header();
434 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
435 const int logicalIndex
= headerView
->logicalIndex(i
);
436 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
437 QAction
* action
= popup
.addAction(text
);
438 action
->setCheckable(true);
439 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
443 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
444 if (activatedAction
!= 0) {
445 const bool show
= activatedAction
->isChecked();
446 const int columnIndex
= activatedAction
->data().toInt();
448 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
449 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
451 Q_ASSERT(!list
.contains(info
));
454 Q_ASSERT(list
.contains(info
));
455 const int index
= list
.indexOf(info
);
456 list
.removeAt(index
);
459 m_controller
->indicateAdditionalInfoChange(list
);
460 setColumnHidden(columnIndex
, !show
);
464 void DolphinDetailsView::updateColumnVisibility()
466 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
467 const bool useDefaultColumns
= !isVisible() &&
469 list
.contains(KFileItemDelegate::NoInformation
));
470 if (useDefaultColumns
) {
471 // Using the details view without any additional information (-> additional column)
472 // makes no sense and leads to a usability problem as no viewport area is available
473 // anymore. Hence as fallback provide at least a size and date column.
475 list
.append(KFileItemDelegate::Size
);
476 list
.append(KFileItemDelegate::ModificationTime
);
477 m_controller
->indicateAdditionalInfoChange(list
);
478 m_clearAdditionalInfo
= true;
481 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
482 const KFileItemDelegate::Information info
= infoForColumn(i
);
483 const bool hide
= !list
.contains(info
);
484 if (isColumnHidden(i
) != hide
) {
485 setColumnHidden(i
, hide
);
486 m_clearAdditionalInfo
= false;
493 bool DolphinDetailsView::isZoomInPossible() const
495 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
496 return settings
->iconSize() < KIconLoader::SizeLarge
;
499 bool DolphinDetailsView::isZoomOutPossible() const
501 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
502 return settings
->iconSize() > KIconLoader::SizeSmall
;
505 void DolphinDetailsView::updateDecorationSize()
507 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
508 const int iconSize
= settings
->iconSize();
509 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
511 m_controller
->setZoomInPossible(isZoomInPossible());
512 m_controller
->setZoomOutPossible(isZoomOutPossible());
517 QPoint
DolphinDetailsView::contentsPos() const
519 // implementation note: the horizonal position is ignored currently, as no
520 // horizontal scrolling is done anyway during a selection
521 const QScrollBar
* scrollbar
= verticalScrollBar();
522 Q_ASSERT(scrollbar
!= 0);
524 const int maxHeight
= maximumViewportSize().height();
525 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
526 const int visibleHeight
= model()->rowCount() + 1 - height
;
527 if (visibleHeight
<= 0) {
531 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
535 KFileItem
DolphinDetailsView::itemForIndex(const QModelIndex
& index
) const
537 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
538 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
539 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
540 return dirModel
->itemForIndex(dirIndex
);
543 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
545 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
547 switch (columnIndex
) {
548 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
549 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
550 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
551 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
552 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
553 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
560 void DolphinDetailsView::resizeColumns()
562 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
563 // around 3 seconds for each (!) resize operation when having > 10000 items).
564 // This gets a problem especially when opening large directories, where several
565 // resize operations are received for showing the currently available items during
566 // loading (the application hangs around 20 seconds when loading > 10000 items).
568 QHeaderView
* headerView
= header();
569 QFontMetrics
fontMetrics(viewport()->font());
571 int columnWidth
[KDirModel::ColumnCount
];
572 columnWidth
[KDirModel::Size
] = fontMetrics
.width("00000 Items");
573 columnWidth
[KDirModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
574 columnWidth
[KDirModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
575 columnWidth
[KDirModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
576 columnWidth
[KDirModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
577 columnWidth
[KDirModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
579 int requiredWidth
= 0;
580 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
581 if (!isColumnHidden(i
)) {
582 columnWidth
[i
] += 20; // provide a default gap
583 requiredWidth
+= columnWidth
[i
];
584 headerView
->resizeSection(i
, columnWidth
[i
]);
588 // resize the name column in a way that the whole available width is used
589 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
590 if (columnWidth
[KDirModel::Name
] < 120) {
591 columnWidth
[KDirModel::Name
] = 120;
593 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
596 #include "dolphindetailsview.moc"