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 if (selectionModel()->hasSelection()) {
171 m_controller
->triggerContextMenuRequest(event
->pos());
175 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
177 m_controller
->requestActivation();
179 QTreeView::mousePressEvent(event
);
181 const QModelIndex index
= indexAt(event
->pos());
182 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
183 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
184 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
189 if (event
->button() == Qt::LeftButton
) {
190 m_showElasticBand
= true;
192 const QPoint
pos(contentsPos());
193 m_elasticBandOrigin
= event
->pos();
194 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
195 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
196 m_elasticBandDestination
= event
->pos();
200 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
202 QTreeView::mouseMoveEvent(event
);
203 if (m_showElasticBand
) {
208 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
210 QTreeView::mouseReleaseEvent(event
);
211 if (m_showElasticBand
) {
213 m_showElasticBand
= false;
217 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
219 if (event
->mimeData()->hasUrls()) {
220 event
->acceptProposedAction();
223 if (m_showElasticBand
) {
225 m_showElasticBand
= false;
230 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
232 QTreeView::dragLeaveEvent(event
);
234 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
236 setDirtyRegion(m_dropRect
);
239 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
241 QTreeView::dragMoveEvent(event
);
243 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
244 setDirtyRegion(m_dropRect
);
245 const QModelIndex index
= indexAt(event
->pos());
246 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
250 if (itemForIndex(index
).isDir()) {
251 m_dropRect
= visualRect(index
);
253 m_dropRect
.setSize(QSize()); // set as invalid
255 setDirtyRegion(m_dropRect
);
259 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
261 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
262 if (!urls
.isEmpty()) {
263 event
->acceptProposedAction();
264 const QModelIndex index
= indexAt(event
->pos());
266 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
267 item
= itemForIndex(index
);
269 m_controller
->indicateDroppedUrls(urls
,
273 QTreeView::dropEvent(event
);
277 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
279 QTreeView::paintEvent(event
);
280 if (m_showElasticBand
) {
281 // The following code has been taken from QListView
282 // and adapted to DolphinDetailsView.
283 // (C) 1992-2007 Trolltech ASA
284 QStyleOptionRubberBand opt
;
286 opt
.shape
= QRubberBand::Rectangle
;
288 opt
.rect
= elasticBandRect();
290 QPainter
painter(viewport());
292 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
296 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
298 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
299 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
303 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
305 QTreeView::keyPressEvent(event
);
307 const QItemSelectionModel
* selModel
= selectionModel();
308 const QModelIndex currentIndex
= selModel
->currentIndex();
309 const bool trigger
= currentIndex
.isValid()
310 && (event
->key() == Qt::Key_Return
)
311 && (selModel
->selectedIndexes().count() <= 1);
313 triggerItem(currentIndex
);
317 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
319 QTreeView::resizeEvent(event
);
323 void DolphinDetailsView::closeEvent(QCloseEvent
* event
)
325 if (m_clearAdditionalInfo
) {
326 disconnect(m_controller
->dolphinView(), SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
327 this, SLOT(updateColumnVisibility()));
329 KFileItemDelegate::InformationList info
;
330 info
.append(KFileItemDelegate::NoInformation
);
331 m_controller
->indicateAdditionalInfoChange(info
);
332 m_clearAdditionalInfo
= false;
334 QTreeView::closeEvent(event
);
337 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
339 QHeaderView
* headerView
= header();
340 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
343 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
345 QHeaderView
* headerView
= header();
346 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
349 void DolphinDetailsView::synchronizeSortingState(int column
)
351 // The sorting has already been changed in QTreeView if this slot is
352 // invoked, but Dolphin is not informed about this.
353 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
354 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
355 m_controller
->indicateSortingChange(sorting
);
356 m_controller
->indicateSortOrderChange(sortOrder
);
359 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
361 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
362 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
363 if (pos
.x() < nameColumnWidth
) {
364 m_controller
->emitItemEntered(itemForIndex(index
));
367 m_controller
->emitViewportEntered();
371 void DolphinDetailsView::updateElasticBand()
373 Q_ASSERT(m_showElasticBand
);
374 QRect
dirtyRegion(elasticBandRect());
375 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
376 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
377 setDirtyRegion(dirtyRegion
);
380 QRect
DolphinDetailsView::elasticBandRect() const
382 const QPoint
pos(contentsPos());
383 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
384 return QRect(topLeft
, m_elasticBandDestination
).normalized();
387 void DolphinDetailsView::zoomIn()
389 if (isZoomInPossible()) {
390 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
391 switch (settings
->iconSize()) {
392 case KIconLoader::SizeSmall
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
393 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeLarge
); break;
394 default: Q_ASSERT(false); break;
396 updateDecorationSize();
400 void DolphinDetailsView::zoomOut()
402 if (isZoomOutPossible()) {
403 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
404 switch (settings
->iconSize()) {
405 case KIconLoader::SizeLarge
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
406 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeSmall
); break;
407 default: Q_ASSERT(false); break;
409 updateDecorationSize();
413 void DolphinDetailsView::triggerItem(const QModelIndex
& index
)
415 const KFileItem item
= itemForIndex(index
);
416 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
417 m_controller
->triggerItem(item
);
420 m_controller
->emitItemEntered(item
);
424 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
427 popup
.addTitle(i18nc("@title:menu", "Columns"));
429 QHeaderView
* headerView
= header();
430 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
431 const int logicalIndex
= headerView
->logicalIndex(i
);
432 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
433 QAction
* action
= popup
.addAction(text
);
434 action
->setCheckable(true);
435 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
439 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
440 if (activatedAction
!= 0) {
441 const bool show
= activatedAction
->isChecked();
442 const int columnIndex
= activatedAction
->data().toInt();
444 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
445 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
447 Q_ASSERT(!list
.contains(info
));
450 Q_ASSERT(list
.contains(info
));
451 const int index
= list
.indexOf(info
);
452 list
.removeAt(index
);
455 m_controller
->indicateAdditionalInfoChange(list
);
456 setColumnHidden(columnIndex
, !show
);
460 void DolphinDetailsView::updateColumnVisibility()
462 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
463 const bool useDefaultColumns
= !isVisible() &&
465 list
.contains(KFileItemDelegate::NoInformation
));
466 if (useDefaultColumns
) {
467 // Using the details view without any additional information (-> additional column)
468 // makes no sense and leads to a usability problem as no viewport area is available
469 // anymore. Hence as fallback provide at least a size and date column.
471 list
.append(KFileItemDelegate::Size
);
472 list
.append(KFileItemDelegate::ModificationTime
);
473 m_controller
->indicateAdditionalInfoChange(list
);
474 m_clearAdditionalInfo
= true;
477 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
478 const KFileItemDelegate::Information info
= infoForColumn(i
);
479 const bool hide
= !list
.contains(info
);
480 if (isColumnHidden(i
) != hide
) {
481 setColumnHidden(i
, hide
);
482 m_clearAdditionalInfo
= false;
489 bool DolphinDetailsView::isZoomInPossible() const
491 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
492 return settings
->iconSize() < KIconLoader::SizeLarge
;
495 bool DolphinDetailsView::isZoomOutPossible() const
497 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
498 return settings
->iconSize() > KIconLoader::SizeSmall
;
501 void DolphinDetailsView::updateDecorationSize()
503 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
504 const int iconSize
= settings
->iconSize();
505 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
507 m_controller
->setZoomInPossible(isZoomInPossible());
508 m_controller
->setZoomOutPossible(isZoomOutPossible());
513 QPoint
DolphinDetailsView::contentsPos() const
515 // implementation note: the horizonal position is ignored currently, as no
516 // horizontal scrolling is done anyway during a selection
517 const QScrollBar
* scrollbar
= verticalScrollBar();
518 Q_ASSERT(scrollbar
!= 0);
520 const int maxHeight
= maximumViewportSize().height();
521 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
522 const int visibleHeight
= model()->rowCount() + 1 - height
;
523 if (visibleHeight
<= 0) {
527 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
531 KFileItem
DolphinDetailsView::itemForIndex(const QModelIndex
& index
) const
533 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
534 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
535 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
536 return dirModel
->itemForIndex(dirIndex
);
539 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
541 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
543 switch (columnIndex
) {
544 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
545 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
546 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
547 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
548 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
549 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
556 void DolphinDetailsView::resizeColumns()
558 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
559 // around 3 seconds for each (!) resize operation when having > 10000 items).
560 // This gets a problem especially when opening large directories, where several
561 // resize operations are received for showing the currently available items during
562 // loading (the application hangs around 20 seconds when loading > 10000 items).
564 QHeaderView
* headerView
= header();
565 QFontMetrics
fontMetrics(viewport()->font());
567 int columnWidth
[KDirModel::ColumnCount
];
568 columnWidth
[KDirModel::Size
] = fontMetrics
.width("00000 Items");
569 columnWidth
[KDirModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
570 columnWidth
[KDirModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
571 columnWidth
[KDirModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
572 columnWidth
[KDirModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
573 columnWidth
[KDirModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
575 int requiredWidth
= 0;
576 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
577 if (!isColumnHidden(i
)) {
578 columnWidth
[i
] += 20; // provide a default gap
579 requiredWidth
+= columnWidth
[i
];
580 headerView
->resizeSection(i
, columnWidth
[i
]);
584 // resize the name column in a way that the whole available width is used
585 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
586 if (columnWidth
[KDirModel::Name
] < 120) {
587 columnWidth
[KDirModel::Name
] = 120;
589 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
592 #include "dolphindetailsview.moc"