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 <QApplication>
32 #include <QHeaderView>
33 #include <QRubberBand>
37 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
, DolphinController
* controller
) :
39 m_controller(controller
),
41 m_showElasticBand(false),
42 m_elasticBandOrigin(),
43 m_elasticBandDestination()
45 Q_ASSERT(controller
!= 0);
48 setRootIsDecorated(false);
49 setSortingEnabled(true);
50 setUniformRowHeights(true);
51 setSelectionBehavior(SelectItems
);
52 setDragDropMode(QAbstractItemView::DragDrop
);
53 setDropIndicatorShown(false);
54 setAlternatingRowColors(true);
55 // TODO: enable ScrollPerPixel again as soon as a Qt-patch
56 // is supplied which fixes a possible crash
57 // (see http://lists.kde.org/?l=kde-core-devel&m=119077433611662&w=2)
58 //setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
59 //setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
61 setMouseTracking(true);
62 viewport()->setAttribute(Qt::WA_Hover
);
64 const ViewProperties
props(controller
->url());
65 setSortIndicatorSection(props
.sorting());
66 setSortIndicatorOrder(props
.sortOrder());
68 connect(header(), SIGNAL(sectionClicked(int)),
69 this, SLOT(synchronizeSortingState(int)));
71 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
72 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
73 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
74 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
76 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
77 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
78 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
79 // RETURN-key in keyPressEvent().
80 if (KGlobalSettings::singleClick()) {
81 connect(this, SIGNAL(clicked(const QModelIndex
&)),
82 this, SLOT(slotItemActivated(const QModelIndex
&)));
84 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
85 this, SLOT(slotItemActivated(const QModelIndex
&)));
87 connect(this, SIGNAL(entered(const QModelIndex
&)),
88 this, SLOT(slotEntered(const QModelIndex
&)));
89 connect(this, SIGNAL(viewportEntered()),
90 controller
, SLOT(emitViewportEntered()));
91 connect(controller
, SIGNAL(zoomIn()),
92 this, SLOT(zoomIn()));
93 connect(controller
, SIGNAL(zoomOut()),
94 this, SLOT(zoomOut()));
96 // apply the details mode settings to the widget
97 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
98 Q_ASSERT(settings
!= 0);
100 m_viewOptions
= QTreeView::viewOptions();
102 QFont
font(settings
->fontFamily(), settings
->fontSize());
103 font
.setItalic(settings
->italicFont());
104 font
.setBold(settings
->boldFont());
105 m_viewOptions
.font
= font
;
107 updateDecorationSize();
110 DolphinDetailsView::~DolphinDetailsView()
114 bool DolphinDetailsView::event(QEvent
* event
)
116 if (event
->type() == QEvent::Polish
) {
117 // Assure that by respecting the available width that:
118 // - the 'Name' column is stretched as large as possible
119 // - the remaining columns are as small as possible
120 QHeaderView
* headerView
= header();
121 headerView
->setStretchLastSection(false);
122 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
123 headerView
->setResizeMode(0, QHeaderView::Stretch
);
125 // hide columns if this is indicated by the settings
126 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
127 Q_ASSERT(settings
!= 0);
128 if (!settings
->showDate()) {
129 hideColumn(DolphinModel::ModifiedTime
);
132 if (!settings
->showPermissions()) {
133 hideColumn(DolphinModel::Permissions
);
136 if (!settings
->showOwner()) {
137 hideColumn(DolphinModel::Owner
);
140 if (!settings
->showGroup()) {
141 hideColumn(DolphinModel::Group
);
144 if (!settings
->showType()) {
145 hideColumn(DolphinModel::Type
);
148 hideColumn(DolphinModel::Rating
);
149 hideColumn(DolphinModel::Tags
);
152 return QTreeView::event(event
);
155 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
157 return m_viewOptions
;
160 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
162 QTreeView::contextMenuEvent(event
);
163 m_controller
->triggerContextMenuRequest(event
->pos());
166 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
168 m_controller
->triggerActivation();
170 QTreeView::mousePressEvent(event
);
172 const QModelIndex index
= indexAt(event
->pos());
173 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
174 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
175 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
180 if (event
->button() == Qt::LeftButton
) {
181 m_showElasticBand
= true;
183 const QPoint
pos(contentsPos());
184 m_elasticBandOrigin
= event
->pos();
185 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
186 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
187 m_elasticBandDestination
= event
->pos();
191 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
193 QTreeView::mouseMoveEvent(event
);
194 if (m_showElasticBand
) {
199 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
201 QTreeView::mouseReleaseEvent(event
);
202 if (m_showElasticBand
) {
204 m_showElasticBand
= false;
208 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
210 if (event
->mimeData()->hasUrls()) {
211 event
->acceptProposedAction();
214 if (m_showElasticBand
) {
216 m_showElasticBand
= false;
221 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
223 QTreeView::dragLeaveEvent(event
);
225 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
227 setDirtyRegion(m_dropRect
);
230 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
232 QTreeView::dragMoveEvent(event
);
234 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
235 setDirtyRegion(m_dropRect
);
236 const QModelIndex index
= indexAt(event
->pos());
237 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
241 m_dropRect
= visualRect(index
);
242 setDirtyRegion(m_dropRect
);
246 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
248 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
249 if (!urls
.isEmpty()) {
250 event
->acceptProposedAction();
251 m_controller
->indicateDroppedUrls(urls
,
253 indexAt(event
->pos()),
256 QTreeView::dropEvent(event
);
260 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
262 QTreeView::paintEvent(event
);
263 if (m_showElasticBand
) {
264 // The following code has been taken from QListView
265 // and adapted to DolphinDetailsView.
266 // (C) 1992-2007 Trolltech ASA
267 QStyleOptionRubberBand opt
;
269 opt
.shape
= QRubberBand::Rectangle
;
271 opt
.rect
= elasticBandRect();
273 QPainter
painter(viewport());
275 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
279 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
281 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
282 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
286 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
288 QTreeView::keyPressEvent(event
);
290 const QItemSelectionModel
* selModel
= selectionModel();
291 const QModelIndex currentIndex
= selModel
->currentIndex();
292 const bool triggerItem
= currentIndex
.isValid()
293 && (event
->key() == Qt::Key_Return
)
294 && (selModel
->selectedIndexes().count() <= 1);
296 m_controller
->triggerItem(currentIndex
);
300 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
302 QHeaderView
* headerView
= header();
303 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
306 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
308 QHeaderView
* headerView
= header();
309 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
312 void DolphinDetailsView::synchronizeSortingState(int column
)
314 // The sorting has already been changed in QTreeView if this slot is
315 // invoked, but Dolphin is not informed about this.
316 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
317 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
318 m_controller
->indicateSortingChange(sorting
);
319 m_controller
->indicateSortOrderChange(sortOrder
);
322 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
324 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
325 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
326 if (pos
.x() < nameColumnWidth
) {
327 m_controller
->emitItemEntered(index
);
330 m_controller
->emitViewportEntered();
334 void DolphinDetailsView::updateElasticBand()
336 Q_ASSERT(m_showElasticBand
);
337 QRect
dirtyRegion(elasticBandRect());
338 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
339 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
340 setDirtyRegion(dirtyRegion
);
343 void DolphinDetailsView::zoomIn()
345 if (isZoomInPossible()) {
346 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
347 // TODO: get rid of K3Icon sizes
348 switch (settings
->iconSize()) {
349 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
350 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
351 default: Q_ASSERT(false); break;
353 updateDecorationSize();
357 void DolphinDetailsView::zoomOut()
359 if (isZoomOutPossible()) {
360 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
361 // TODO: get rid of K3Icon sizes
362 switch (settings
->iconSize()) {
363 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
364 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
365 default: Q_ASSERT(false); break;
367 updateDecorationSize();
371 bool DolphinDetailsView::isZoomInPossible() const
373 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
374 return settings
->iconSize() < K3Icon::SizeLarge
;
377 bool DolphinDetailsView::isZoomOutPossible() const
379 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
380 return settings
->iconSize() > K3Icon::SizeSmall
;
383 void DolphinDetailsView::updateDecorationSize()
385 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
386 const int iconSize
= settings
->iconSize();
387 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
389 m_controller
->setZoomInPossible(isZoomInPossible());
390 m_controller
->setZoomOutPossible(isZoomOutPossible());
395 QPoint
DolphinDetailsView::contentsPos() const
397 // implementation note: the horizonal position is ignored currently, as no
398 // horizontal scrolling is done anyway during a selection
399 const QScrollBar
* scrollbar
= verticalScrollBar();
400 Q_ASSERT(scrollbar
!= 0);
402 const int maxHeight
= maximumViewportSize().height();
403 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
404 const int visibleHeight
= model()->rowCount() + 1 - height
;
405 if (visibleHeight
<= 0) {
409 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
413 QRect
DolphinDetailsView::elasticBandRect() const
415 const QPoint
pos(contentsPos());
416 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
417 return QRect(topLeft
, m_elasticBandDestination
).normalized();
420 static bool isValidNameIndex(const QModelIndex
& index
)
422 return index
.isValid() && (index
.column() == KDirModel::Name
);
425 void DolphinDetailsView::slotItemActivated(const QModelIndex
& index
)
427 if (!isValidNameIndex(index
)) {
429 m_controller
->emitItemEntered(index
);
431 m_controller
->triggerItem(index
);
435 #include "dolphindetailsview.moc"