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 updateDecorationSize();
114 DolphinDetailsView::~DolphinDetailsView()
118 bool DolphinDetailsView::event(QEvent
* event
)
120 if (event
->type() == QEvent::Polish
) {
121 // Assure that by respecting the available width that:
122 // - the 'Name' column is stretched as large as possible
123 // - the remaining columns are as small as possible
124 QHeaderView
* headerView
= header();
125 headerView
->setStretchLastSection(false);
126 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
127 headerView
->setResizeMode(0, QHeaderView::Stretch
);
128 headerView
->setMovable(false);
130 // hide columns if this is indicated by the settings
131 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
132 Q_ASSERT(settings
!= 0);
133 if (!settings
->showDate()) {
134 hideColumn(DolphinModel::ModifiedTime
);
137 if (!settings
->showPermissions()) {
138 hideColumn(DolphinModel::Permissions
);
141 if (!settings
->showOwner()) {
142 hideColumn(DolphinModel::Owner
);
145 if (!settings
->showGroup()) {
146 hideColumn(DolphinModel::Group
);
149 if (!settings
->showType()) {
150 hideColumn(DolphinModel::Type
);
153 hideColumn(DolphinModel::Rating
);
154 hideColumn(DolphinModel::Tags
);
157 return QTreeView::event(event
);
160 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
162 return m_viewOptions
;
165 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
167 QTreeView::contextMenuEvent(event
);
168 m_controller
->triggerContextMenuRequest(event
->pos());
171 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
173 m_controller
->triggerActivation();
175 QTreeView::mousePressEvent(event
);
177 const QModelIndex index
= indexAt(event
->pos());
178 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
179 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
180 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
185 if (event
->button() == Qt::LeftButton
) {
186 m_showElasticBand
= true;
188 const QPoint
pos(contentsPos());
189 m_elasticBandOrigin
= event
->pos();
190 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
191 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
192 m_elasticBandDestination
= event
->pos();
196 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
198 QTreeView::mouseMoveEvent(event
);
199 if (m_showElasticBand
) {
204 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
206 QTreeView::mouseReleaseEvent(event
);
207 if (m_showElasticBand
) {
209 m_showElasticBand
= false;
213 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
215 if (event
->mimeData()->hasUrls()) {
216 event
->acceptProposedAction();
219 if (m_showElasticBand
) {
221 m_showElasticBand
= false;
226 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
228 QTreeView::dragLeaveEvent(event
);
230 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
232 setDirtyRegion(m_dropRect
);
235 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
237 QTreeView::dragMoveEvent(event
);
239 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
240 setDirtyRegion(m_dropRect
);
241 const QModelIndex index
= indexAt(event
->pos());
242 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
246 m_dropRect
= visualRect(index
);
247 setDirtyRegion(m_dropRect
);
251 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
253 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
254 if (!urls
.isEmpty()) {
255 event
->acceptProposedAction();
256 m_controller
->indicateDroppedUrls(urls
,
258 indexAt(event
->pos()),
261 QTreeView::dropEvent(event
);
265 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
267 QTreeView::paintEvent(event
);
268 if (m_showElasticBand
) {
269 // The following code has been taken from QListView
270 // and adapted to DolphinDetailsView.
271 // (C) 1992-2007 Trolltech ASA
272 QStyleOptionRubberBand opt
;
274 opt
.shape
= QRubberBand::Rectangle
;
276 opt
.rect
= elasticBandRect();
278 QPainter
painter(viewport());
280 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
284 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
286 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
287 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
291 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
293 QTreeView::keyPressEvent(event
);
295 const QItemSelectionModel
* selModel
= selectionModel();
296 const QModelIndex currentIndex
= selModel
->currentIndex();
297 const bool triggerItem
= currentIndex
.isValid()
298 && (event
->key() == Qt::Key_Return
)
299 && (selModel
->selectedIndexes().count() <= 1);
301 m_controller
->triggerItem(currentIndex
);
305 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
307 QTreeView::resizeEvent(event
);
309 // assure that the width of the name-column does not get too small
310 const int minWidth
= 120;
311 QHeaderView
* headerView
= header();
312 bool useFixedWidth
= (headerView
->sectionSize(KDirModel::Name
) <= minWidth
)
313 && (headerView
->resizeMode(0) != QHeaderView::Fixed
);
315 // the current width of the name-column is too small, hence
317 headerView
->setResizeMode(QHeaderView::Fixed
);
318 headerView
->setResizeMode(0, QHeaderView::Fixed
);
319 headerView
->resizeSection(KDirModel::Name
, minWidth
);
320 } else if (headerView
->resizeMode(0) != QHeaderView::Stretch
) {
321 // check whether there is enough available viewport width
322 // to automatically resize the columns
323 const int availableWidth
= viewport()->width();
326 const int count
= headerView
->count();
327 for (int i
= 0; i
< count
; ++i
) {
328 headerWidth
+= headerView
->sectionSize(i
);
331 if (headerWidth
< availableWidth
) {
332 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
333 headerView
->setResizeMode(0, QHeaderView::Stretch
);
338 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
340 QHeaderView
* headerView
= header();
341 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
344 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
346 QHeaderView
* headerView
= header();
347 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
350 void DolphinDetailsView::synchronizeSortingState(int column
)
352 // The sorting has already been changed in QTreeView if this slot is
353 // invoked, but Dolphin is not informed about this.
354 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
355 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
356 m_controller
->indicateSortingChange(sorting
);
357 m_controller
->indicateSortOrderChange(sortOrder
);
360 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
362 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
363 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
364 if (pos
.x() < nameColumnWidth
) {
365 m_controller
->emitItemEntered(index
);
368 m_controller
->emitViewportEntered();
372 void DolphinDetailsView::updateElasticBand()
374 Q_ASSERT(m_showElasticBand
);
375 QRect
dirtyRegion(elasticBandRect());
376 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
377 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
378 setDirtyRegion(dirtyRegion
);
381 void DolphinDetailsView::zoomIn()
383 if (isZoomInPossible()) {
384 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
385 switch (settings
->iconSize()) {
386 case KIconLoader::SizeSmall
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
387 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeLarge
); break;
388 default: Q_ASSERT(false); break;
390 updateDecorationSize();
394 void DolphinDetailsView::zoomOut()
396 if (isZoomOutPossible()) {
397 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
398 switch (settings
->iconSize()) {
399 case KIconLoader::SizeLarge
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
400 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeSmall
); break;
401 default: Q_ASSERT(false); break;
403 updateDecorationSize();
407 void DolphinDetailsView::slotItemActivated(const QModelIndex
& index
)
409 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
410 m_controller
->triggerItem(index
);
413 m_controller
->emitItemEntered(index
);
417 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
420 popup
.addTitle(i18nc("@title:menu", "Columns"));
422 QHeaderView
* headerView
= header();
423 for (int i
= DolphinModel::ModifiedTime
; i
<= DolphinModel::Type
; ++i
) {
424 const int logicalIndex
= headerView
->logicalIndex(i
);
425 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
426 QAction
* action
= popup
.addAction(text
);
427 action
->setCheckable(true);
428 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
432 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
433 if (activatedAction
!= 0) {
434 const bool show
= activatedAction
->isChecked();
435 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
436 Q_ASSERT(settings
!= 0);
438 // remember the changed column visibility in the settings
439 const int columnIndex
= activatedAction
->data().toInt();
440 switch (columnIndex
) {
441 case DolphinModel::ModifiedTime
: settings
->setShowDate(show
); break;
442 case DolphinModel::Permissions
: settings
->setShowPermissions(show
); break;
443 case DolphinModel::Owner
: settings
->setShowOwner(show
); break;
444 case DolphinModel::Group
: settings
->setShowGroup(show
); break;
445 case DolphinModel::Type
: settings
->setShowType(show
); break;
449 // apply the changed column visibility
451 showColumn(columnIndex
);
453 hideColumn(columnIndex
);
458 bool DolphinDetailsView::isZoomInPossible() const
460 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
461 return settings
->iconSize() < KIconLoader::SizeLarge
;
464 bool DolphinDetailsView::isZoomOutPossible() const
466 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
467 return settings
->iconSize() > KIconLoader::SizeSmall
;
470 void DolphinDetailsView::updateDecorationSize()
472 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
473 const int iconSize
= settings
->iconSize();
474 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
476 m_controller
->setZoomInPossible(isZoomInPossible());
477 m_controller
->setZoomOutPossible(isZoomOutPossible());
482 QPoint
DolphinDetailsView::contentsPos() const
484 // implementation note: the horizonal position is ignored currently, as no
485 // horizontal scrolling is done anyway during a selection
486 const QScrollBar
* scrollbar
= verticalScrollBar();
487 Q_ASSERT(scrollbar
!= 0);
489 const int maxHeight
= maximumViewportSize().height();
490 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
491 const int visibleHeight
= model()->rowCount() + 1 - height
;
492 if (visibleHeight
<= 0) {
496 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
500 QRect
DolphinDetailsView::elasticBandRect() const
502 const QPoint
pos(contentsPos());
503 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
504 return QRect(topLeft
, m_elasticBandDestination
).normalized();
507 static bool isValidNameIndex(const QModelIndex
& index
)
509 return index
.isValid() && (index
.column() == KDirModel::Name
);
512 #include "dolphindetailsview.moc"