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 "selectionmanager.h"
29 #include "viewproperties.h"
31 #include "dolphin_detailsmodesettings.h"
32 #include "dolphin_generalsettings.h"
34 #include <kdirmodel.h>
38 #include <QAbstractProxyModel>
40 #include <QApplication>
41 #include <QHeaderView>
42 #include <QRubberBand>
46 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
, DolphinController
* controller
) :
49 m_controller(controller
),
53 m_showElasticBand(false),
54 m_elasticBandOrigin(),
55 m_elasticBandDestination()
57 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
58 Q_ASSERT(settings
!= 0);
59 Q_ASSERT(controller
!= 0);
62 setSortingEnabled(true);
63 setUniformRowHeights(true);
64 setSelectionBehavior(SelectItems
);
65 setDragDropMode(QAbstractItemView::DragDrop
);
66 setDropIndicatorShown(false);
67 setAlternatingRowColors(true);
68 setRootIsDecorated(settings
->expandableFolders());
69 setItemsExpandable(settings
->expandableFolders());
71 setMouseTracking(true);
72 viewport()->setAttribute(Qt::WA_Hover
);
74 const ViewProperties
props(controller
->url());
75 setSortIndicatorSection(props
.sorting());
76 setSortIndicatorOrder(props
.sortOrder());
78 QHeaderView
* headerView
= header();
79 connect(headerView
, SIGNAL(sectionClicked(int)),
80 this, SLOT(synchronizeSortingState(int)));
81 headerView
->setContextMenuPolicy(Qt::CustomContextMenu
);
82 connect(headerView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
83 this, SLOT(configureColumns(const QPoint
&)));
84 connect(headerView
, SIGNAL(sectionResized(int, int, int)),
85 this, SLOT(slotHeaderSectionResized(int, int, int)));
86 connect(headerView
, SIGNAL(sectionHandleDoubleClicked(int)),
87 this, SLOT(disableAutoResizing()));
89 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
90 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
91 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
92 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
94 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
95 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
96 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
97 // RETURN-key in keyPressEvent().
98 if (KGlobalSettings::singleClick()) {
99 connect(this, SIGNAL(clicked(const QModelIndex
&)),
100 controller
, SLOT(triggerItem(const QModelIndex
&)));
101 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
102 SelectionManager
* selManager
= new SelectionManager(this);
103 connect(selManager
, SIGNAL(selectionChanged()),
104 this, SLOT(requestActivation()));
105 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
106 selManager
, SLOT(reset()));
109 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
110 controller
, SLOT(triggerItem(const QModelIndex
&)));
112 connect(this, SIGNAL(entered(const QModelIndex
&)),
113 this, SLOT(slotEntered(const QModelIndex
&)));
114 connect(this, SIGNAL(viewportEntered()),
115 controller
, SLOT(emitViewportEntered()));
116 connect(controller
, SIGNAL(zoomIn()),
117 this, SLOT(zoomIn()));
118 connect(controller
, SIGNAL(zoomOut()),
119 this, SLOT(zoomOut()));
120 connect(controller
->dolphinView(), SIGNAL(additionalInfoChanged()),
121 this, SLOT(updateColumnVisibility()));
123 if (settings
->useSystemFont()) {
124 m_font
= KGlobalSettings::generalFont();
126 m_font
= QFont(settings
->fontFamily(),
127 settings
->fontSize(),
128 settings
->fontWeight(),
129 settings
->italicFont());
132 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
133 // check avoids a division by zero happening on versions before 4.3.1.
134 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
136 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
137 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
138 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
141 updateDecorationSize();
145 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
146 this, SLOT(updateFont()));
149 DolphinDetailsView::~DolphinDetailsView()
153 bool DolphinDetailsView::event(QEvent
* event
)
155 if (event
->type() == QEvent::Polish
) {
156 QHeaderView
* headerView
= header();
157 headerView
->setResizeMode(QHeaderView::Interactive
);
158 headerView
->setMovable(false);
160 updateColumnVisibility();
162 hideColumn(DolphinModel::Rating
);
163 hideColumn(DolphinModel::Tags
);
165 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
166 // check avoids a division by zero happening on versions before 4.3.1.
167 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
169 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
170 else if (event
->type() == QEvent::UpdateRequest
) {
171 // a wheel movement will scroll 4 items
172 if (model()->rowCount() > 0) {
173 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
178 return QTreeView::event(event
);
181 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
183 QStyleOptionViewItem viewOptions
= QTreeView::viewOptions();
184 viewOptions
.font
= m_font
;
185 viewOptions
.showDecorationSelected
= true;
186 viewOptions
.decorationSize
= m_decorationSize
;
190 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
192 QTreeView::contextMenuEvent(event
);
193 m_controller
->triggerContextMenuRequest(event
->pos());
196 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
198 m_controller
->requestActivation();
200 QTreeView::mousePressEvent(event
);
202 const QModelIndex index
= indexAt(event
->pos());
203 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
204 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
205 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
210 if (event
->button() == Qt::LeftButton
) {
211 m_showElasticBand
= true;
213 const QPoint
pos(contentsPos());
214 m_elasticBandOrigin
= event
->pos();
215 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
216 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
217 m_elasticBandDestination
= event
->pos();
221 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
223 if (m_showElasticBand
) {
224 const QPoint mousePos
= event
->pos();
225 const QModelIndex index
= indexAt(mousePos
);
226 if (!index
.isValid()) {
227 // the destination of the selection rectangle is above the viewport. In this
228 // case QTreeView does no selection at all, which is not the wanted behavior
229 // in Dolphin -> select all items within the elastic band rectangle
232 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
233 QRect selRect
= QRect(m_elasticBandOrigin
, m_elasticBandDestination
).normalized();
234 const QRect
nameColumnsRect(0, 0, nameColumnWidth
, viewport()->height());
235 selRect
= nameColumnsRect
.intersected(selRect
);
237 setSelection(selRect
, QItemSelectionModel::Select
);
240 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
241 // as the Qt-issue #199631 has been fixed.
242 // QTreeView::mouseMoveEvent(event);
243 QAbstractItemView::mouseMoveEvent(event
);
246 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
247 // as the Qt-issue #199631 has been fixed.
248 // QTreeView::mouseMoveEvent(event);
249 QAbstractItemView::mouseMoveEvent(event
);
253 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
255 QTreeView::mouseReleaseEvent(event
);
256 if (m_showElasticBand
) {
258 m_showElasticBand
= false;
262 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions
)
264 DragAndDropHelper::startDrag(this, supportedActions
);
267 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
269 if (event
->mimeData()->hasUrls()) {
270 event
->acceptProposedAction();
273 if (m_showElasticBand
) {
275 m_showElasticBand
= false;
280 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
282 QTreeView::dragLeaveEvent(event
);
284 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
286 setDirtyRegion(m_dropRect
);
289 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
291 QTreeView::dragMoveEvent(event
);
293 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
294 setDirtyRegion(m_dropRect
);
295 const QModelIndex index
= indexAt(event
->pos());
296 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
300 const KFileItem item
= m_controller
->itemForIndex(index
);
301 if (!item
.isNull() && item
.isDir()) {
302 m_dropRect
= visualRect(index
);
304 m_dropRect
.setSize(QSize()); // set as invalid
306 setDirtyRegion(m_dropRect
);
309 if (event
->mimeData()->hasUrls()) {
310 // accept url drops, independently from the destination item
311 event
->acceptProposedAction();
315 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
317 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
318 if (!urls
.isEmpty()) {
319 event
->acceptProposedAction();
320 const QModelIndex index
= indexAt(event
->pos());
322 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
323 item
= m_controller
->itemForIndex(index
);
325 m_controller
->indicateDroppedUrls(urls
,
329 QTreeView::dropEvent(event
);
333 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
335 QTreeView::paintEvent(event
);
336 if (m_showElasticBand
) {
337 // The following code has been taken from QListView
338 // and adapted to DolphinDetailsView.
339 // (C) 1992-2007 Trolltech ASA
340 QStyleOptionRubberBand opt
;
342 opt
.shape
= QRubberBand::Rectangle
;
344 opt
.rect
= elasticBandRect();
346 QPainter
painter(viewport());
348 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
352 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
354 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
355 DragAndDropHelper::drawHoverIndication(this, m_dropRect
, brush
);
359 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
361 QTreeView::keyPressEvent(event
);
362 m_controller
->handleKeyPressEvent(event
);
365 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
370 QTreeView::resizeEvent(event
);
373 void DolphinDetailsView::wheelEvent(QWheelEvent
* event
)
375 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
376 if (event
->modifiers() & Qt::ControlModifier
) {
380 QTreeView::wheelEvent(event
);
383 void DolphinDetailsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
385 QTreeView::currentChanged(current
, previous
);
386 selectionModel()->select(current
, QItemSelectionModel::ClearAndSelect
);
389 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
391 QHeaderView
* headerView
= header();
392 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
395 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
397 QHeaderView
* headerView
= header();
398 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
401 void DolphinDetailsView::synchronizeSortingState(int column
)
403 // The sorting has already been changed in QTreeView if this slot is
404 // invoked, but Dolphin is not informed about this.
405 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
406 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
407 m_controller
->indicateSortingChange(sorting
);
408 m_controller
->indicateSortOrderChange(sortOrder
);
411 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
413 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
414 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
415 if (pos
.x() < nameColumnWidth
) {
416 m_controller
->emitItemEntered(index
);
419 m_controller
->emitViewportEntered();
423 void DolphinDetailsView::updateElasticBand()
425 if (m_showElasticBand
) {
426 QRect
dirtyRegion(elasticBandRect());
427 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
428 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
429 setDirtyRegion(dirtyRegion
);
433 QRect
DolphinDetailsView::elasticBandRect() const
435 const QPoint
pos(contentsPos());
436 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
437 return QRect(topLeft
, m_elasticBandDestination
).normalized();
440 void DolphinDetailsView::zoomIn()
442 if (isZoomInPossible()) {
443 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
444 switch (settings
->iconSize()) {
445 case KIconLoader::SizeSmall
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
446 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeLarge
); break;
447 default: Q_ASSERT(false); break;
449 updateDecorationSize();
453 void DolphinDetailsView::zoomOut()
455 if (isZoomOutPossible()) {
456 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
457 switch (settings
->iconSize()) {
458 case KIconLoader::SizeLarge
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
459 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeSmall
); break;
460 default: Q_ASSERT(false); break;
462 updateDecorationSize();
466 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
469 popup
.addTitle(i18nc("@title:menu", "Columns"));
471 QHeaderView
* headerView
= header();
472 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
473 const int logicalIndex
= headerView
->logicalIndex(i
);
474 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
475 QAction
* action
= popup
.addAction(text
);
476 action
->setCheckable(true);
477 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
481 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
482 if (activatedAction
!= 0) {
483 const bool show
= activatedAction
->isChecked();
484 const int columnIndex
= activatedAction
->data().toInt();
486 KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
487 const KFileItemDelegate::Information info
= infoForColumn(columnIndex
);
489 Q_ASSERT(!list
.contains(info
));
492 Q_ASSERT(list
.contains(info
));
493 const int index
= list
.indexOf(info
);
494 list
.removeAt(index
);
497 m_controller
->indicateAdditionalInfoChange(list
);
498 setColumnHidden(columnIndex
, !show
);
502 void DolphinDetailsView::updateColumnVisibility()
504 const KFileItemDelegate::InformationList list
= m_controller
->dolphinView()->additionalInfo();
505 for (int i
= DolphinModel::Size
; i
<= DolphinModel::Type
; ++i
) {
506 const KFileItemDelegate::Information info
= infoForColumn(i
);
507 const bool hide
= !list
.contains(info
);
508 if (isColumnHidden(i
) != hide
) {
509 setColumnHidden(i
, hide
);
516 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex
, int oldSize
, int newSize
)
518 Q_UNUSED(logicalIndex
);
521 if (QApplication::mouseButtons() & Qt::LeftButton
) {
522 disableAutoResizing();
526 void DolphinDetailsView::disableAutoResizing()
528 m_autoResize
= false;
531 void DolphinDetailsView::requestActivation()
533 m_controller
->requestActivation();
536 void DolphinDetailsView::updateFont()
538 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
539 Q_ASSERT(settings
!= 0);
541 if (settings
->useSystemFont()) {
542 m_font
= KGlobalSettings::generalFont();
546 bool DolphinDetailsView::isZoomInPossible() const
548 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
549 return settings
->iconSize() < KIconLoader::SizeLarge
;
552 bool DolphinDetailsView::isZoomOutPossible() const
554 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
555 return settings
->iconSize() > KIconLoader::SizeSmall
;
558 void DolphinDetailsView::updateDecorationSize()
560 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
561 const int iconSize
= settings
->iconSize();
562 setIconSize(QSize(iconSize
, iconSize
));
563 m_decorationSize
= QSize(iconSize
, iconSize
);
565 m_controller
->setZoomInPossible(isZoomInPossible());
566 m_controller
->setZoomOutPossible(isZoomOutPossible());
571 QPoint
DolphinDetailsView::contentsPos() const
573 // implementation note: the horizonal position is ignored currently, as no
574 // horizontal scrolling is done anyway during a selection
575 const QScrollBar
* scrollbar
= verticalScrollBar();
576 Q_ASSERT(scrollbar
!= 0);
578 const int maxHeight
= maximumViewportSize().height();
579 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
580 const int visibleHeight
= model()->rowCount() + 1 - height
;
581 if (visibleHeight
<= 0) {
585 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
589 KFileItemDelegate::Information
DolphinDetailsView::infoForColumn(int columnIndex
) const
591 KFileItemDelegate::Information info
= KFileItemDelegate::NoInformation
;
593 switch (columnIndex
) {
594 case DolphinModel::Size
: info
= KFileItemDelegate::Size
; break;
595 case DolphinModel::ModifiedTime
: info
= KFileItemDelegate::ModificationTime
; break;
596 case DolphinModel::Permissions
: info
= KFileItemDelegate::Permissions
; break;
597 case DolphinModel::Owner
: info
= KFileItemDelegate::Owner
; break;
598 case DolphinModel::Group
: info
= KFileItemDelegate::OwnerAndGroup
; break;
599 case DolphinModel::Type
: info
= KFileItemDelegate::FriendlyMimeType
; break;
606 void DolphinDetailsView::resizeColumns()
608 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
609 // around 3 seconds for each (!) resize operation when having > 10000 items).
610 // This gets a problem especially when opening large directories, where several
611 // resize operations are received for showing the currently available items during
612 // loading (the application hangs around 20 seconds when loading > 10000 items).
614 QHeaderView
* headerView
= header();
615 QFontMetrics
fontMetrics(viewport()->font());
617 int columnWidth
[KDirModel::ColumnCount
];
618 columnWidth
[KDirModel::Size
] = fontMetrics
.width("00000 Items");
619 columnWidth
[KDirModel::ModifiedTime
] = fontMetrics
.width("0000-00-00 00:00");
620 columnWidth
[KDirModel::Permissions
] = fontMetrics
.width("xxxxxxxxxx");
621 columnWidth
[KDirModel::Owner
] = fontMetrics
.width("xxxxxxxxxx");
622 columnWidth
[KDirModel::Group
] = fontMetrics
.width("xxxxxxxxxx");
623 columnWidth
[KDirModel::Type
] = fontMetrics
.width("XXXX Xxxxxxx");
625 int requiredWidth
= 0;
626 for (int i
= KDirModel::Size
; i
<= KDirModel::Type
; ++i
) {
627 if (!isColumnHidden(i
)) {
628 columnWidth
[i
] += 20; // provide a default gap
629 requiredWidth
+= columnWidth
[i
];
630 headerView
->resizeSection(i
, columnWidth
[i
]);
634 // resize the name column in a way that the whole available width is used
635 columnWidth
[KDirModel::Name
] = viewport()->width() - requiredWidth
;
636 if (columnWidth
[KDirModel::Name
] < 120) {
637 columnWidth
[KDirModel::Name
] = 120;
639 headerView
->resizeSection(KDirModel::Name
, columnWidth
[KDirModel::Name
]);
642 #include "dolphindetailsview.moc"