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
),
47 m_showElasticBand(false),
48 m_elasticBandOrigin(),
49 m_elasticBandDestination()
51 Q_ASSERT(controller
!= 0);
54 setRootIsDecorated(false);
55 setSortingEnabled(true);
56 setUniformRowHeights(true);
57 setSelectionBehavior(SelectItems
);
58 setDragDropMode(QAbstractItemView::DragDrop
);
59 setDropIndicatorShown(false);
60 setAlternatingRowColors(true);
62 setMouseTracking(true);
63 viewport()->setAttribute(Qt::WA_Hover
);
65 const ViewProperties
props(controller
->url());
66 setSortIndicatorSection(props
.sorting());
67 setSortIndicatorOrder(props
.sortOrder());
69 QHeaderView
* headerView
= header();
70 connect(headerView
, SIGNAL(sectionClicked(int)),
71 this, SLOT(synchronizeSortingState(int)));
72 headerView
->setContextMenuPolicy(Qt::CustomContextMenu
);
73 connect(headerView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
74 this, SLOT(configureColumns(const QPoint
&)));
76 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
77 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
78 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
79 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
81 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
82 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
83 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
84 // RETURN-key in keyPressEvent().
85 if (KGlobalSettings::singleClick()) {
86 connect(this, SIGNAL(clicked(const QModelIndex
&)),
87 this, SLOT(triggerItem(const QModelIndex
&)));
89 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
90 this, SLOT(triggerItem(const QModelIndex
&)));
92 connect(this, SIGNAL(entered(const QModelIndex
&)),
93 this, SLOT(slotEntered(const QModelIndex
&)));
94 connect(this, SIGNAL(viewportEntered()),
95 controller
, SLOT(emitViewportEntered()));
96 connect(controller
, SIGNAL(zoomIn()),
97 this, SLOT(zoomIn()));
98 connect(controller
, SIGNAL(zoomOut()),
99 this, SLOT(zoomOut()));
101 // apply the details mode settings to the widget
102 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
103 Q_ASSERT(settings
!= 0);
105 m_viewOptions
= QTreeView::viewOptions();
107 QFont
font(settings
->fontFamily(), settings
->fontSize());
108 font
.setItalic(settings
->italicFont());
109 font
.setBold(settings
->boldFont());
110 m_viewOptions
.font
= font
;
111 m_viewOptions
.showDecorationSelected
= true;
113 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
114 // check avoids a division by zero happening on versions before 4.3.1.
115 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
117 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
118 setVerticalScrollMode(QTreeView::ScrollPerPixel
);
119 setHorizontalScrollMode(QTreeView::ScrollPerPixel
);
122 updateDecorationSize();
125 DolphinDetailsView::~DolphinDetailsView()
129 bool DolphinDetailsView::event(QEvent
* event
)
131 if (event
->type() == QEvent::Polish
) {
132 // Assure that by respecting the available width that:
133 // - the 'Name' column is stretched as large as possible
134 // - the remaining columns are as small as possible
135 QHeaderView
* headerView
= header();
136 headerView
->setStretchLastSection(false);
137 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
138 headerView
->setResizeMode(0, QHeaderView::Stretch
);
139 headerView
->setMovable(false);
141 // hide columns if this is indicated by the settings
142 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
143 Q_ASSERT(settings
!= 0);
144 if (!settings
->showDate()) {
145 hideColumn(DolphinModel::ModifiedTime
);
148 if (!settings
->showPermissions()) {
149 hideColumn(DolphinModel::Permissions
);
152 if (!settings
->showOwner()) {
153 hideColumn(DolphinModel::Owner
);
156 if (!settings
->showGroup()) {
157 hideColumn(DolphinModel::Group
);
160 if (!settings
->showType()) {
161 hideColumn(DolphinModel::Type
);
164 hideColumn(DolphinModel::Rating
);
165 hideColumn(DolphinModel::Tags
);
167 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
168 // check avoids a division by zero happening on versions before 4.3.1.
169 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
171 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
172 else if (event
->type() == QEvent::UpdateRequest
) {
173 // a wheel movement will scroll 4 items
174 if (model()->rowCount() > 0) {
175 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
180 return QTreeView::event(event
);
183 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
185 return m_viewOptions
;
188 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
190 QTreeView::contextMenuEvent(event
);
191 m_controller
->triggerContextMenuRequest(event
->pos());
194 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
196 m_controller
->requestActivation();
198 QTreeView::mousePressEvent(event
);
200 const QModelIndex index
= indexAt(event
->pos());
201 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
202 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
203 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
208 if (event
->button() == Qt::LeftButton
) {
209 m_showElasticBand
= true;
211 const QPoint
pos(contentsPos());
212 m_elasticBandOrigin
= event
->pos();
213 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
214 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
215 m_elasticBandDestination
= event
->pos();
219 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
221 QTreeView::mouseMoveEvent(event
);
222 if (m_showElasticBand
) {
227 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
229 QTreeView::mouseReleaseEvent(event
);
230 if (m_showElasticBand
) {
232 m_showElasticBand
= false;
236 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
238 if (event
->mimeData()->hasUrls()) {
239 event
->acceptProposedAction();
242 if (m_showElasticBand
) {
244 m_showElasticBand
= false;
249 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
251 QTreeView::dragLeaveEvent(event
);
253 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
255 setDirtyRegion(m_dropRect
);
258 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
260 QTreeView::dragMoveEvent(event
);
262 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
263 setDirtyRegion(m_dropRect
);
264 const QModelIndex index
= indexAt(event
->pos());
265 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
269 m_dropRect
= visualRect(index
);
270 setDirtyRegion(m_dropRect
);
274 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
276 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
277 if (!urls
.isEmpty()) {
278 event
->acceptProposedAction();
279 m_controller
->indicateDroppedUrls(urls
,
281 indexAt(event
->pos()),
284 QTreeView::dropEvent(event
);
288 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
290 QTreeView::paintEvent(event
);
291 if (m_showElasticBand
) {
292 // The following code has been taken from QListView
293 // and adapted to DolphinDetailsView.
294 // (C) 1992-2007 Trolltech ASA
295 QStyleOptionRubberBand opt
;
297 opt
.shape
= QRubberBand::Rectangle
;
299 opt
.rect
= elasticBandRect();
301 QPainter
painter(viewport());
303 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
307 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
309 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
310 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
314 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
316 QTreeView::keyPressEvent(event
);
318 const QItemSelectionModel
* selModel
= selectionModel();
319 const QModelIndex currentIndex
= selModel
->currentIndex();
320 const bool trigger
= currentIndex
.isValid()
321 && (event
->key() == Qt::Key_Return
)
322 && (selModel
->selectedIndexes().count() <= 1);
324 triggerItem(currentIndex
);
328 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
330 QTreeView::resizeEvent(event
);
332 // assure that the width of the name-column does not get too small
333 const int minWidth
= 120;
334 QHeaderView
* headerView
= header();
335 bool useFixedWidth
= (headerView
->sectionSize(KDirModel::Name
) <= minWidth
)
336 && (headerView
->resizeMode(0) != QHeaderView::Fixed
);
338 // the current width of the name-column is too small, hence
340 headerView
->setResizeMode(QHeaderView::Fixed
);
341 headerView
->setResizeMode(0, QHeaderView::Fixed
);
342 headerView
->resizeSection(KDirModel::Name
, minWidth
);
343 } else if (headerView
->resizeMode(0) != QHeaderView::Stretch
) {
344 // check whether there is enough available viewport width
345 // to automatically resize the columns
346 const int availableWidth
= viewport()->width();
349 const int count
= headerView
->count();
350 for (int i
= 0; i
< count
; ++i
) {
351 headerWidth
+= headerView
->sectionSize(i
);
354 if (headerWidth
< availableWidth
) {
355 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
356 headerView
->setResizeMode(0, QHeaderView::Stretch
);
361 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
363 QHeaderView
* headerView
= header();
364 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
367 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
369 QHeaderView
* headerView
= header();
370 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
373 void DolphinDetailsView::synchronizeSortingState(int column
)
375 // The sorting has already been changed in QTreeView if this slot is
376 // invoked, but Dolphin is not informed about this.
377 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
378 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
379 m_controller
->indicateSortingChange(sorting
);
380 m_controller
->indicateSortOrderChange(sortOrder
);
383 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
385 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
386 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
387 if (pos
.x() < nameColumnWidth
) {
388 m_controller
->emitItemEntered(itemForIndex(index
));
391 m_controller
->emitViewportEntered();
395 void DolphinDetailsView::updateElasticBand()
397 Q_ASSERT(m_showElasticBand
);
398 QRect
dirtyRegion(elasticBandRect());
399 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
400 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
401 setDirtyRegion(dirtyRegion
);
404 QRect
DolphinDetailsView::elasticBandRect() const
406 const QPoint
pos(contentsPos());
407 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
408 return QRect(topLeft
, m_elasticBandDestination
).normalized();
411 void DolphinDetailsView::zoomIn()
413 if (isZoomInPossible()) {
414 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
415 switch (settings
->iconSize()) {
416 case KIconLoader::SizeSmall
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
417 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeLarge
); break;
418 default: Q_ASSERT(false); break;
420 updateDecorationSize();
424 void DolphinDetailsView::zoomOut()
426 if (isZoomOutPossible()) {
427 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
428 switch (settings
->iconSize()) {
429 case KIconLoader::SizeLarge
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
430 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeSmall
); break;
431 default: Q_ASSERT(false); break;
433 updateDecorationSize();
437 void DolphinDetailsView::triggerItem(const QModelIndex
& index
)
439 const KFileItem item
= itemForIndex(index
);
440 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
441 m_controller
->triggerItem(item
);
444 m_controller
->emitItemEntered(item
);
448 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
451 popup
.addTitle(i18nc("@title:menu", "Columns"));
453 QHeaderView
* headerView
= header();
454 for (int i
= DolphinModel::ModifiedTime
; i
<= DolphinModel::Type
; ++i
) {
455 const int logicalIndex
= headerView
->logicalIndex(i
);
456 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
457 QAction
* action
= popup
.addAction(text
);
458 action
->setCheckable(true);
459 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
463 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
464 if (activatedAction
!= 0) {
465 const bool show
= activatedAction
->isChecked();
466 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
467 Q_ASSERT(settings
!= 0);
469 // remember the changed column visibility in the settings
470 const int columnIndex
= activatedAction
->data().toInt();
471 switch (columnIndex
) {
472 case DolphinModel::ModifiedTime
: settings
->setShowDate(show
); break;
473 case DolphinModel::Permissions
: settings
->setShowPermissions(show
); break;
474 case DolphinModel::Owner
: settings
->setShowOwner(show
); break;
475 case DolphinModel::Group
: settings
->setShowGroup(show
); break;
476 case DolphinModel::Type
: settings
->setShowType(show
); break;
480 // apply the changed column visibility
482 showColumn(columnIndex
);
484 hideColumn(columnIndex
);
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 #include "dolphindetailsview.moc"