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"
35 #include <QApplication>
36 #include <QHeaderView>
37 #include <QRubberBand>
41 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
, DolphinController
* controller
) :
43 m_controller(controller
),
45 m_showElasticBand(false),
46 m_elasticBandOrigin(),
47 m_elasticBandDestination()
49 Q_ASSERT(controller
!= 0);
52 setRootIsDecorated(false);
53 setSortingEnabled(true);
54 setUniformRowHeights(true);
55 setSelectionBehavior(SelectItems
);
56 setDragDropMode(QAbstractItemView::DragDrop
);
57 setDropIndicatorShown(false);
58 setAlternatingRowColors(true);
60 setMouseTracking(true);
61 viewport()->setAttribute(Qt::WA_Hover
);
63 const ViewProperties
props(controller
->url());
64 setSortIndicatorSection(props
.sorting());
65 setSortIndicatorOrder(props
.sortOrder());
67 QHeaderView
* headerView
= header();
68 connect(headerView
, SIGNAL(sectionClicked(int)),
69 this, SLOT(synchronizeSortingState(int)));
70 headerView
->setContextMenuPolicy(Qt::CustomContextMenu
);
71 connect(headerView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
72 this, SLOT(configureColumns(const QPoint
&)));
74 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
75 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
76 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
77 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
79 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
80 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
81 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
82 // RETURN-key in keyPressEvent().
83 if (KGlobalSettings::singleClick()) {
84 connect(this, SIGNAL(clicked(const QModelIndex
&)),
85 this, SLOT(slotItemActivated(const QModelIndex
&)));
87 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
88 this, SLOT(slotItemActivated(const QModelIndex
&)));
90 connect(this, SIGNAL(entered(const QModelIndex
&)),
91 this, SLOT(slotEntered(const QModelIndex
&)));
92 connect(this, SIGNAL(viewportEntered()),
93 controller
, SLOT(emitViewportEntered()));
94 connect(controller
, SIGNAL(zoomIn()),
95 this, SLOT(zoomIn()));
96 connect(controller
, SIGNAL(zoomOut()),
97 this, SLOT(zoomOut()));
99 // apply the details mode settings to the widget
100 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
101 Q_ASSERT(settings
!= 0);
103 m_viewOptions
= QTreeView::viewOptions();
105 QFont
font(settings
->fontFamily(), settings
->fontSize());
106 font
.setItalic(settings
->italicFont());
107 font
.setBold(settings
->boldFont());
108 m_viewOptions
.font
= font
;
109 m_viewOptions
.showDecorationSelected
= true;
111 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
112 // check avoids a division by zero happening on versions before 4.3.1.
113 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
115 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
116 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
117 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
120 updateDecorationSize();
123 DolphinDetailsView::~DolphinDetailsView()
127 bool DolphinDetailsView::event(QEvent
* event
)
129 if (event
->type() == QEvent::Polish
) {
130 // Assure that by respecting the available width that:
131 // - the 'Name' column is stretched as large as possible
132 // - the remaining columns are as small as possible
133 QHeaderView
* headerView
= header();
134 headerView
->setStretchLastSection(false);
135 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
136 headerView
->setResizeMode(0, QHeaderView::Stretch
);
137 headerView
->setMovable(false);
139 // hide columns if this is indicated by the settings
140 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
141 Q_ASSERT(settings
!= 0);
142 if (!settings
->showDate()) {
143 hideColumn(DolphinModel::ModifiedTime
);
146 if (!settings
->showPermissions()) {
147 hideColumn(DolphinModel::Permissions
);
150 if (!settings
->showOwner()) {
151 hideColumn(DolphinModel::Owner
);
154 if (!settings
->showGroup()) {
155 hideColumn(DolphinModel::Group
);
158 if (!settings
->showType()) {
159 hideColumn(DolphinModel::Type
);
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 return m_viewOptions
;
186 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
188 QTreeView::contextMenuEvent(event
);
189 m_controller
->triggerContextMenuRequest(event
->pos());
192 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
194 m_controller
->triggerActivation();
196 QTreeView::mousePressEvent(event
);
198 const QModelIndex index
= indexAt(event
->pos());
199 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
200 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
201 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
206 if (event
->button() == Qt::LeftButton
) {
207 m_showElasticBand
= true;
209 const QPoint
pos(contentsPos());
210 m_elasticBandOrigin
= event
->pos();
211 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
212 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
213 m_elasticBandDestination
= event
->pos();
217 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
219 QTreeView::mouseMoveEvent(event
);
220 if (m_showElasticBand
) {
225 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
227 QTreeView::mouseReleaseEvent(event
);
228 if (m_showElasticBand
) {
230 m_showElasticBand
= false;
234 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
236 if (event
->mimeData()->hasUrls()) {
237 event
->acceptProposedAction();
240 if (m_showElasticBand
) {
242 m_showElasticBand
= false;
247 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
249 QTreeView::dragLeaveEvent(event
);
251 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
253 setDirtyRegion(m_dropRect
);
256 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
258 QTreeView::dragMoveEvent(event
);
260 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
261 setDirtyRegion(m_dropRect
);
262 const QModelIndex index
= indexAt(event
->pos());
263 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
267 m_dropRect
= visualRect(index
);
268 setDirtyRegion(m_dropRect
);
272 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
274 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
275 if (!urls
.isEmpty()) {
276 event
->acceptProposedAction();
277 m_controller
->indicateDroppedUrls(urls
,
279 indexAt(event
->pos()),
282 QTreeView::dropEvent(event
);
286 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
288 QTreeView::paintEvent(event
);
289 if (m_showElasticBand
) {
290 // The following code has been taken from QListView
291 // and adapted to DolphinDetailsView.
292 // (C) 1992-2007 Trolltech ASA
293 QStyleOptionRubberBand opt
;
295 opt
.shape
= QRubberBand::Rectangle
;
297 opt
.rect
= elasticBandRect();
299 QPainter
painter(viewport());
301 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
305 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
307 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
308 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
312 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
314 QTreeView::keyPressEvent(event
);
316 const QItemSelectionModel
* selModel
= selectionModel();
317 const QModelIndex currentIndex
= selModel
->currentIndex();
318 const bool triggerItem
= currentIndex
.isValid()
319 && (event
->key() == Qt::Key_Return
)
320 && (selModel
->selectedIndexes().count() <= 1);
322 m_controller
->triggerItem(currentIndex
);
326 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
328 QTreeView::resizeEvent(event
);
330 // assure that the width of the name-column does not get too small
331 const int minWidth
= 120;
332 QHeaderView
* headerView
= header();
333 bool useFixedWidth
= (headerView
->sectionSize(KDirModel::Name
) <= minWidth
)
334 && (headerView
->resizeMode(0) != QHeaderView::Fixed
);
336 // the current width of the name-column is too small, hence
338 headerView
->setResizeMode(QHeaderView::Fixed
);
339 headerView
->setResizeMode(0, QHeaderView::Fixed
);
340 headerView
->resizeSection(KDirModel::Name
, minWidth
);
341 } else if (headerView
->resizeMode(0) != QHeaderView::Stretch
) {
342 // check whether there is enough available viewport width
343 // to automatically resize the columns
344 const int availableWidth
= viewport()->width();
347 const int count
= headerView
->count();
348 for (int i
= 0; i
< count
; ++i
) {
349 headerWidth
+= headerView
->sectionSize(i
);
352 if (headerWidth
< availableWidth
) {
353 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
354 headerView
->setResizeMode(0, QHeaderView::Stretch
);
359 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
361 QHeaderView
* headerView
= header();
362 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
365 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
367 QHeaderView
* headerView
= header();
368 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
371 void DolphinDetailsView::synchronizeSortingState(int column
)
373 // The sorting has already been changed in QTreeView if this slot is
374 // invoked, but Dolphin is not informed about this.
375 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
376 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
377 m_controller
->indicateSortingChange(sorting
);
378 m_controller
->indicateSortOrderChange(sortOrder
);
381 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
383 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
384 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
385 if (pos
.x() < nameColumnWidth
) {
386 m_controller
->emitItemEntered(index
);
389 m_controller
->emitViewportEntered();
393 void DolphinDetailsView::updateElasticBand()
395 Q_ASSERT(m_showElasticBand
);
396 QRect
dirtyRegion(elasticBandRect());
397 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
398 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
399 setDirtyRegion(dirtyRegion
);
402 void DolphinDetailsView::zoomIn()
404 if (isZoomInPossible()) {
405 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
406 switch (settings
->iconSize()) {
407 case KIconLoader::SizeSmall
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
408 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeLarge
); break;
409 default: Q_ASSERT(false); break;
411 updateDecorationSize();
415 void DolphinDetailsView::zoomOut()
417 if (isZoomOutPossible()) {
418 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
419 switch (settings
->iconSize()) {
420 case KIconLoader::SizeLarge
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
421 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeSmall
); break;
422 default: Q_ASSERT(false); break;
424 updateDecorationSize();
428 void DolphinDetailsView::slotItemActivated(const QModelIndex
& index
)
430 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
431 m_controller
->triggerItem(index
);
434 m_controller
->emitItemEntered(index
);
438 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
441 popup
.addTitle(i18nc("@title:menu", "Columns"));
443 QHeaderView
* headerView
= header();
444 for (int i
= DolphinModel::ModifiedTime
; i
<= DolphinModel::Type
; ++i
) {
445 const int logicalIndex
= headerView
->logicalIndex(i
);
446 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
447 QAction
* action
= popup
.addAction(text
);
448 action
->setCheckable(true);
449 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
453 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
454 if (activatedAction
!= 0) {
455 const bool show
= activatedAction
->isChecked();
456 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
457 Q_ASSERT(settings
!= 0);
459 // remember the changed column visibility in the settings
460 const int columnIndex
= activatedAction
->data().toInt();
461 switch (columnIndex
) {
462 case DolphinModel::ModifiedTime
: settings
->setShowDate(show
); break;
463 case DolphinModel::Permissions
: settings
->setShowPermissions(show
); break;
464 case DolphinModel::Owner
: settings
->setShowOwner(show
); break;
465 case DolphinModel::Group
: settings
->setShowGroup(show
); break;
466 case DolphinModel::Type
: settings
->setShowType(show
); break;
470 // apply the changed column visibility
472 showColumn(columnIndex
);
474 hideColumn(columnIndex
);
479 bool DolphinDetailsView::isZoomInPossible() const
481 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
482 return settings
->iconSize() < KIconLoader::SizeLarge
;
485 bool DolphinDetailsView::isZoomOutPossible() const
487 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
488 return settings
->iconSize() > KIconLoader::SizeSmall
;
491 void DolphinDetailsView::updateDecorationSize()
493 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
494 const int iconSize
= settings
->iconSize();
495 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
497 m_controller
->setZoomInPossible(isZoomInPossible());
498 m_controller
->setZoomOutPossible(isZoomOutPossible());
503 QPoint
DolphinDetailsView::contentsPos() const
505 // implementation note: the horizonal position is ignored currently, as no
506 // horizontal scrolling is done anyway during a selection
507 const QScrollBar
* scrollbar
= verticalScrollBar();
508 Q_ASSERT(scrollbar
!= 0);
510 const int maxHeight
= maximumViewportSize().height();
511 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
512 const int visibleHeight
= model()->rowCount() + 1 - height
;
513 if (visibleHeight
<= 0) {
517 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
521 QRect
DolphinDetailsView::elasticBandRect() const
523 const QPoint
pos(contentsPos());
524 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
525 return QRect(topLeft
, m_elasticBandDestination
).normalized();
528 static bool isValidNameIndex(const QModelIndex
& index
)
530 return index
.isValid() && (index
.column() == KDirModel::Name
);
533 #include "dolphindetailsview.moc"