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 "dolphincontroller.h"
24 #include "dolphinsettings.h"
25 #include "dolphinsortfilterproxymodel.h"
26 #include "viewproperties.h"
28 #include "dolphin_detailsmodesettings.h"
30 #include <kdirmodel.h>
32 #include <QApplication>
33 #include <QHeaderView>
34 #include <QRubberBand>
38 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
, DolphinController
* controller
) :
40 m_controller(controller
),
42 m_showElasticBand(false),
43 m_elasticBandOrigin(),
44 m_elasticBandDestination()
46 Q_ASSERT(controller
!= 0);
49 setRootIsDecorated(false);
50 setSortingEnabled(true);
51 setUniformRowHeights(true);
52 setSelectionBehavior(SelectItems
);
53 setDragDropMode(QAbstractItemView::DragDrop
);
54 setDropIndicatorShown(false);
55 setAlternatingRowColors(true);
57 setMouseTracking(true);
58 viewport()->setAttribute(Qt::WA_Hover
);
60 const ViewProperties
props(controller
->url());
61 setSortIndicatorSection(props
.sorting());
62 setSortIndicatorOrder(props
.sortOrder());
64 connect(header(), SIGNAL(sectionClicked(int)),
65 this, SLOT(synchronizeSortingState(int)));
67 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
68 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
69 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
70 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
72 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
73 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
74 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
75 // RETURN-key in keyPressEvent().
76 if (KGlobalSettings::singleClick()) {
77 connect(this, SIGNAL(clicked(const QModelIndex
&)),
78 this, SLOT(slotItemActivated(const QModelIndex
&)));
80 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
81 this, SLOT(slotItemActivated(const QModelIndex
&)));
83 connect(this, SIGNAL(entered(const QModelIndex
&)),
84 this, SLOT(slotEntered(const QModelIndex
&)));
85 connect(this, SIGNAL(viewportEntered()),
86 controller
, SLOT(emitViewportEntered()));
87 connect(controller
, SIGNAL(zoomIn()),
88 this, SLOT(zoomIn()));
89 connect(controller
, SIGNAL(zoomOut()),
90 this, SLOT(zoomOut()));
92 // apply the details mode settings to the widget
93 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
94 Q_ASSERT(settings
!= 0);
96 m_viewOptions
= QTreeView::viewOptions();
98 QFont
font(settings
->fontFamily(), settings
->fontSize());
99 font
.setItalic(settings
->italicFont());
100 font
.setBold(settings
->boldFont());
101 m_viewOptions
.font
= font
;
103 updateDecorationSize();
106 DolphinDetailsView::~DolphinDetailsView()
110 bool DolphinDetailsView::event(QEvent
* event
)
112 if (event
->type() == QEvent::Polish
) {
113 // Assure that by respecting the available width that:
114 // - the 'Name' column is stretched as large as possible
115 // - the remaining columns are as small as possible
116 QHeaderView
* headerView
= header();
117 headerView
->setStretchLastSection(false);
118 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
119 headerView
->setResizeMode(0, QHeaderView::Stretch
);
121 // hide columns if this is indicated by the settings
122 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
123 Q_ASSERT(settings
!= 0);
124 if (!settings
->showDate()) {
125 hideColumn(KDirModel::ModifiedTime
);
128 if (!settings
->showPermissions()) {
129 hideColumn(KDirModel::Permissions
);
132 if (!settings
->showOwner()) {
133 hideColumn(KDirModel::Owner
);
136 if (!settings
->showGroup()) {
137 hideColumn(KDirModel::Group
);
140 if (!settings
->showType()) {
141 hideColumn(KDirModel::Type
);
145 return QTreeView::event(event
);
148 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
150 return m_viewOptions
;
153 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
155 QTreeView::contextMenuEvent(event
);
156 m_controller
->triggerContextMenuRequest(event
->pos());
159 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
161 m_controller
->triggerActivation();
163 QTreeView::mousePressEvent(event
);
165 const QModelIndex index
= indexAt(event
->pos());
166 if (!index
.isValid() || (index
.column() != KDirModel::Name
)) {
167 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
168 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
173 if (event
->button() == Qt::LeftButton
) {
174 m_showElasticBand
= true;
176 const QPoint
pos(contentsPos());
177 m_elasticBandOrigin
= event
->pos();
178 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
179 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
180 m_elasticBandDestination
= event
->pos();
184 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
186 QTreeView::mouseMoveEvent(event
);
187 if (m_showElasticBand
) {
192 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
194 QTreeView::mouseReleaseEvent(event
);
195 if (m_showElasticBand
) {
197 m_showElasticBand
= false;
201 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
203 if (event
->mimeData()->hasUrls()) {
204 event
->acceptProposedAction();
207 if (m_showElasticBand
) {
209 m_showElasticBand
= false;
214 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
216 QTreeView::dragLeaveEvent(event
);
218 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
220 setDirtyRegion(m_dropRect
);
223 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
225 QTreeView::dragMoveEvent(event
);
227 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
228 setDirtyRegion(m_dropRect
);
229 const QModelIndex index
= indexAt(event
->pos());
230 if (!index
.isValid() || (index
.column() != KDirModel::Name
)) {
234 m_dropRect
= visualRect(index
);
235 setDirtyRegion(m_dropRect
);
239 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
241 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
242 if (!urls
.isEmpty()) {
243 event
->acceptProposedAction();
244 m_controller
->indicateDroppedUrls(urls
,
245 indexAt(event
->pos()),
248 QTreeView::dropEvent(event
);
252 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
254 QTreeView::paintEvent(event
);
255 if (m_showElasticBand
) {
256 // The following code has been taken from QListView
257 // and adapted to DolphinDetailsView.
258 // (C) 1992-2007 Trolltech ASA
259 QStyleOptionRubberBand opt
;
261 opt
.shape
= QRubberBand::Rectangle
;
263 opt
.rect
= elasticBandRect();
265 QPainter
painter(viewport());
267 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
271 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
273 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
274 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
278 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
280 QTreeView::keyPressEvent(event
);
281 if (event
->key() == Qt::Key_Return
) {
282 m_controller
->triggerItem(selectionModel()->currentIndex());
286 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
288 QHeaderView
* headerView
= header();
289 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
292 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
294 QHeaderView
* headerView
= header();
295 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
298 void DolphinDetailsView::synchronizeSortingState(int column
)
300 // The sorting has already been changed in QTreeView if this slot is
301 // invoked, but Dolphin is not informed about this.
302 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
303 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
304 m_controller
->indicateSortingChange(sorting
);
305 m_controller
->indicateSortOrderChange(sortOrder
);
308 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
310 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
311 const int nameColumnWidth
= header()->sectionSize(KDirModel::Name
);
312 if (pos
.x() < nameColumnWidth
) {
313 m_controller
->emitItemEntered(index
);
316 m_controller
->emitViewportEntered();
320 void DolphinDetailsView::updateElasticBand()
322 Q_ASSERT(m_showElasticBand
);
323 QRect
dirtyRegion(elasticBandRect());
324 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
325 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
326 setDirtyRegion(dirtyRegion
);
329 void DolphinDetailsView::zoomIn()
331 if (isZoomInPossible()) {
332 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
333 // TODO: get rid of K3Icon sizes
334 switch (settings
->iconSize()) {
335 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
336 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
337 default: Q_ASSERT(false); break;
339 updateDecorationSize();
343 void DolphinDetailsView::zoomOut()
345 if (isZoomOutPossible()) {
346 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
347 // TODO: get rid of K3Icon sizes
348 switch (settings
->iconSize()) {
349 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
350 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
351 default: Q_ASSERT(false); break;
353 updateDecorationSize();
357 bool DolphinDetailsView::isZoomInPossible() const
359 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
360 return settings
->iconSize() < K3Icon::SizeLarge
;
363 bool DolphinDetailsView::isZoomOutPossible() const
365 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
366 return settings
->iconSize() > K3Icon::SizeSmall
;
369 void DolphinDetailsView::updateDecorationSize()
371 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
372 const int iconSize
= settings
->iconSize();
373 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
375 m_controller
->setZoomInPossible(isZoomInPossible());
376 m_controller
->setZoomOutPossible(isZoomOutPossible());
381 QPoint
DolphinDetailsView::contentsPos() const
383 // implementation note: the horizonal position is ignored currently, as no
384 // horizontal scrolling is done anyway during a selection
385 const QScrollBar
* scrollbar
= verticalScrollBar();
386 Q_ASSERT(scrollbar
!= 0);
388 const int maxHeight
= maximumViewportSize().height();
389 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
390 const int visibleHeight
= model()->rowCount() + 1 - height
;
391 if (visibleHeight
<= 0) {
395 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
399 QRect
DolphinDetailsView::elasticBandRect() const
401 const QPoint
pos(contentsPos());
402 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
403 return QRect(topLeft
, m_elasticBandDestination
).normalized();
406 static bool isValidNameIndex(const QModelIndex
& index
)
408 return index
.isValid() && (index
.column() == KDirModel::Name
);
411 void DolphinDetailsView::slotItemActivated(const QModelIndex
& index
)
413 if (!isValidNameIndex(index
)) {
415 m_controller
->emitItemEntered(index
);
417 m_controller
->triggerItem(index
);
421 #include "dolphindetailsview.moc"