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>
31 #include <kfileitemdelegate.h>
33 #include <QApplication>
34 #include <QHeaderView>
35 #include <QRubberBand>
39 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
, DolphinController
* controller
) :
41 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(activated(const QModelIndex
&)),
79 controller
, SLOT(triggerItem(const QModelIndex
&)));
80 connect(this, SIGNAL(entered(const QModelIndex
&)),
81 this, SLOT(slotEntered(const QModelIndex
&)));
82 connect(this, SIGNAL(viewportEntered()),
83 controller
, SLOT(emitViewportEntered()));
84 connect(controller
, SIGNAL(zoomIn()),
85 this, SLOT(zoomIn()));
86 connect(controller
, SIGNAL(zoomOut()),
87 this, SLOT(zoomOut()));
89 // apply the details mode settings to the widget
90 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
91 Q_ASSERT(settings
!= 0);
93 m_viewOptions
= QTreeView::viewOptions();
95 QFont
font(settings
->fontFamily(), settings
->fontSize());
96 font
.setItalic(settings
->italicFont());
97 font
.setBold(settings
->boldFont());
98 m_viewOptions
.font
= font
;
100 updateDecorationSize();
103 DolphinDetailsView::~DolphinDetailsView()
107 bool DolphinDetailsView::event(QEvent
* event
)
109 if (event
->type() == QEvent::Polish
) {
110 // Assure that by respecting the available width that:
111 // - the 'Name' column is stretched as large as possible
112 // - the remaining columns are as small as possible
113 QHeaderView
* headerView
= header();
114 headerView
->setStretchLastSection(false);
115 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
116 headerView
->setResizeMode(0, QHeaderView::Stretch
);
118 // hide columns if this is indicated by the settings
119 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
120 Q_ASSERT(settings
!= 0);
121 if (!settings
->showDate()) {
122 hideColumn(KDirModel::ModifiedTime
);
125 if (!settings
->showPermissions()) {
126 hideColumn(KDirModel::Permissions
);
129 if (!settings
->showOwner()) {
130 hideColumn(KDirModel::Owner
);
133 if (!settings
->showGroup()) {
134 hideColumn(KDirModel::Group
);
137 if (!settings
->showType()) {
138 hideColumn(KDirModel::Type
);
142 return QTreeView::event(event
);
145 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
147 return m_viewOptions
;
150 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
152 QTreeView::contextMenuEvent(event
);
153 m_controller
->triggerContextMenuRequest(event
->pos());
156 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
158 if (!indexAt(event
->pos()).isValid()) {
159 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
160 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
165 QTreeView::mousePressEvent(event
);
167 if (event
->button() == Qt::LeftButton
) {
168 m_showElasticBand
= true;
170 const QPoint
pos(contentsPos());
171 m_elasticBandOrigin
= event
->pos();
172 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
173 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
174 m_elasticBandDestination
= event
->pos();
178 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
180 QTreeView::mouseMoveEvent(event
);
181 if (m_showElasticBand
) {
186 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
188 QTreeView::mouseReleaseEvent(event
);
189 if (m_showElasticBand
) {
191 m_showElasticBand
= false;
193 m_controller
->triggerActivation();
196 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
198 if (event
->mimeData()->hasUrls()) {
199 event
->acceptProposedAction();
202 if (m_showElasticBand
) {
204 m_showElasticBand
= false;
208 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
210 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
211 if (!urls
.isEmpty()) {
212 event
->acceptProposedAction();
213 m_controller
->indicateDroppedUrls(urls
,
214 indexAt(event
->pos()),
217 QTreeView::dropEvent(event
);
220 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
222 QTreeView::paintEvent(event
);
223 if (m_showElasticBand
) {
224 // The following code has been taken from QListView
225 // and adapted to DolphinDetailsView.
226 // (C) 1992-2007 Trolltech ASA
227 QStyleOptionRubberBand opt
;
229 opt
.shape
= QRubberBand::Rectangle
;
231 opt
.rect
= elasticBandRect();
233 QPainter
painter(viewport());
235 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
240 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
242 QHeaderView
* headerView
= header();
243 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
246 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
248 QHeaderView
* headerView
= header();
249 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
252 void DolphinDetailsView::synchronizeSortingState(int column
)
254 // The sorting has already been changed in QTreeView if this slot is
255 // invoked, but Dolphin is not informed about this.
256 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
257 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
258 m_controller
->indicateSortingChange(sorting
);
259 m_controller
->indicateSortOrderChange(sortOrder
);
262 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
264 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
265 const int nameColumnWidth
= header()->sectionSize(KDirModel::Name
);
266 if (pos
.x() < nameColumnWidth
) {
267 m_controller
->emitItemEntered(index
);
270 m_controller
->emitViewportEntered();
274 void DolphinDetailsView::updateElasticBand()
276 Q_ASSERT(m_showElasticBand
);
277 QRect
dirtyRegion(elasticBandRect());
278 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
279 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
280 setDirtyRegion(dirtyRegion
);
283 void DolphinDetailsView::zoomIn()
285 if (isZoomInPossible()) {
286 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
287 // TODO: get rid of K3Icon sizes
288 switch (settings
->iconSize()) {
289 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
290 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
291 default: Q_ASSERT(false); break;
293 updateDecorationSize();
297 void DolphinDetailsView::zoomOut()
299 if (isZoomOutPossible()) {
300 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
301 // TODO: get rid of K3Icon sizes
302 switch (settings
->iconSize()) {
303 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
304 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
305 default: Q_ASSERT(false); break;
307 updateDecorationSize();
311 bool DolphinDetailsView::isZoomInPossible() const
313 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
314 return settings
->iconSize() < K3Icon::SizeLarge
;
317 bool DolphinDetailsView::isZoomOutPossible() const
319 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
320 return settings
->iconSize() > K3Icon::SizeSmall
;
323 void DolphinDetailsView::updateDecorationSize()
325 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
326 const int iconSize
= settings
->iconSize();
327 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
329 m_controller
->setZoomInPossible(isZoomInPossible());
330 m_controller
->setZoomOutPossible(isZoomOutPossible());
335 QPoint
DolphinDetailsView::contentsPos() const
337 // implementation note: the horizonal position is ignored currently, as no
338 // horizontal scrolling is done anyway during a selection
339 const QScrollBar
* scrollbar
= verticalScrollBar();
340 Q_ASSERT(scrollbar
!= 0);
342 const int maxHeight
= maximumViewportSize().height();
343 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
344 const int visibleHeight
= model()->rowCount() + 1 - height
;
345 if (visibleHeight
<= 0) {
349 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
353 QRect
DolphinDetailsView::elasticBandRect() const
355 const QPoint
pos(contentsPos());
356 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
357 return QRect(topLeft
, m_elasticBandDestination
).normalized();
360 #include "dolphindetailsview.moc"