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);
56 setMouseTracking(true);
57 viewport()->setAttribute(Qt::WA_Hover
);
59 const ViewProperties
props(controller
->url());
60 setSortIndicatorSection(props
.sorting());
61 setSortIndicatorOrder(props
.sortOrder());
63 connect(header(), SIGNAL(sectionClicked(int)),
64 this, SLOT(synchronizeSortingState(int)));
66 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
67 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
68 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
69 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
71 if (KGlobalSettings::singleClick()) {
72 connect(this, SIGNAL(clicked(const QModelIndex
&)),
73 controller
, SLOT(triggerItem(const QModelIndex
&)));
75 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
76 controller
, SLOT(triggerItem(const QModelIndex
&)));
78 connect(this, SIGNAL(entered(const QModelIndex
&)),
79 this, SLOT(slotEntered(const QModelIndex
&)));
80 connect(this, SIGNAL(viewportEntered()),
81 controller
, SLOT(emitViewportEntered()));
82 connect(controller
, SIGNAL(zoomIn()),
83 this, SLOT(zoomIn()));
84 connect(controller
, SIGNAL(zoomOut()),
85 this, SLOT(zoomOut()));
87 // apply the details mode settings to the widget
88 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
89 Q_ASSERT(settings
!= 0);
91 m_viewOptions
= QTreeView::viewOptions();
93 QFont
font(settings
->fontFamily(), settings
->fontSize());
94 font
.setItalic(settings
->italicFont());
95 font
.setBold(settings
->boldFont());
96 m_viewOptions
.font
= font
;
98 updateDecorationSize();
101 DolphinDetailsView::~DolphinDetailsView()
105 bool DolphinDetailsView::event(QEvent
* event
)
107 if (event
->type() == QEvent::Polish
) {
108 // Assure that by respecting the available width that:
109 // - the 'Name' column is stretched as large as possible
110 // - the remaining columns are as small as possible
111 QHeaderView
* headerView
= header();
112 headerView
->setStretchLastSection(false);
113 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
114 headerView
->setResizeMode(0, QHeaderView::Stretch
);
116 // hide columns if this is indicated by the settings
117 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
118 Q_ASSERT(settings
!= 0);
119 if (!settings
->showDate()) {
120 hideColumn(KDirModel::ModifiedTime
);
123 if (!settings
->showPermissions()) {
124 hideColumn(KDirModel::Permissions
);
127 if (!settings
->showOwner()) {
128 hideColumn(KDirModel::Owner
);
131 if (!settings
->showGroup()) {
132 hideColumn(KDirModel::Group
);
135 if (!settings
->showType()) {
136 hideColumn(KDirModel::Type
);
140 return QTreeView::event(event
);
143 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
145 return m_viewOptions
;
148 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
150 QTreeView::contextMenuEvent(event
);
151 m_controller
->triggerContextMenuRequest(event
->pos());
154 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
156 m_controller
->triggerActivation();
158 QTreeView::mousePressEvent(event
);
160 const QModelIndex index
= indexAt(event
->pos());
161 if (!index
.isValid() || (index
.column() != KDirModel::Name
)) {
162 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
163 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
168 if (event
->button() == Qt::LeftButton
) {
169 m_showElasticBand
= true;
171 const QPoint
pos(contentsPos());
172 m_elasticBandOrigin
= event
->pos();
173 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
174 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
175 m_elasticBandDestination
= event
->pos();
179 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
181 QTreeView::mouseMoveEvent(event
);
182 if (m_showElasticBand
) {
187 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
189 QTreeView::mouseReleaseEvent(event
);
190 if (m_showElasticBand
) {
192 m_showElasticBand
= false;
196 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
198 if (event
->mimeData()->hasUrls()) {
199 event
->acceptProposedAction();
202 if (m_showElasticBand
) {
204 m_showElasticBand
= false;
209 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
211 QTreeView::dragLeaveEvent(event
);
213 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
215 setDirtyRegion(m_dropRect
);
218 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
220 QTreeView::dragMoveEvent(event
);
222 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
223 setDirtyRegion(m_dropRect
);
224 const QModelIndex index
= indexAt(event
->pos());
225 if (!index
.isValid() || (index
.column() != KDirModel::Name
)) {
229 m_dropRect
= visualRect(index
);
230 setDirtyRegion(m_dropRect
);
234 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
236 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
237 if (!urls
.isEmpty()) {
238 event
->acceptProposedAction();
239 m_controller
->indicateDroppedUrls(urls
,
240 indexAt(event
->pos()),
243 QTreeView::dropEvent(event
);
247 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
249 QTreeView::paintEvent(event
);
250 if (m_showElasticBand
) {
251 // The following code has been taken from QListView
252 // and adapted to DolphinDetailsView.
253 // (C) 1992-2007 Trolltech ASA
254 QStyleOptionRubberBand opt
;
256 opt
.shape
= QRubberBand::Rectangle
;
258 opt
.rect
= elasticBandRect();
260 QPainter
painter(viewport());
262 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
266 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
268 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
269 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
273 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
275 QHeaderView
* headerView
= header();
276 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
279 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
281 QHeaderView
* headerView
= header();
282 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
285 void DolphinDetailsView::synchronizeSortingState(int column
)
287 // The sorting has already been changed in QTreeView if this slot is
288 // invoked, but Dolphin is not informed about this.
289 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
290 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
291 m_controller
->indicateSortingChange(sorting
);
292 m_controller
->indicateSortOrderChange(sortOrder
);
295 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
297 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
298 const int nameColumnWidth
= header()->sectionSize(KDirModel::Name
);
299 if (pos
.x() < nameColumnWidth
) {
300 m_controller
->emitItemEntered(index
);
303 m_controller
->emitViewportEntered();
307 void DolphinDetailsView::updateElasticBand()
309 Q_ASSERT(m_showElasticBand
);
310 QRect
dirtyRegion(elasticBandRect());
311 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
312 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
313 setDirtyRegion(dirtyRegion
);
316 void DolphinDetailsView::zoomIn()
318 if (isZoomInPossible()) {
319 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
320 // TODO: get rid of K3Icon sizes
321 switch (settings
->iconSize()) {
322 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
323 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
324 default: Q_ASSERT(false); break;
326 updateDecorationSize();
330 void DolphinDetailsView::zoomOut()
332 if (isZoomOutPossible()) {
333 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
334 // TODO: get rid of K3Icon sizes
335 switch (settings
->iconSize()) {
336 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
337 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
338 default: Q_ASSERT(false); break;
340 updateDecorationSize();
344 bool DolphinDetailsView::isZoomInPossible() const
346 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
347 return settings
->iconSize() < K3Icon::SizeLarge
;
350 bool DolphinDetailsView::isZoomOutPossible() const
352 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
353 return settings
->iconSize() > K3Icon::SizeSmall
;
356 void DolphinDetailsView::updateDecorationSize()
358 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
359 const int iconSize
= settings
->iconSize();
360 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
362 m_controller
->setZoomInPossible(isZoomInPossible());
363 m_controller
->setZoomOutPossible(isZoomOutPossible());
368 QPoint
DolphinDetailsView::contentsPos() const
370 // implementation note: the horizonal position is ignored currently, as no
371 // horizontal scrolling is done anyway during a selection
372 const QScrollBar
* scrollbar
= verticalScrollBar();
373 Q_ASSERT(scrollbar
!= 0);
375 const int maxHeight
= maximumViewportSize().height();
376 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
377 const int visibleHeight
= model()->rowCount() + 1 - height
;
378 if (visibleHeight
<= 0) {
382 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
386 QRect
DolphinDetailsView::elasticBandRect() const
388 const QPoint
pos(contentsPos());
389 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
390 return QRect(topLeft
, m_elasticBandDestination
).normalized();
393 #include "dolphindetailsview.moc"