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 <QHeaderView>
34 #include <QRubberBand>
38 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
, DolphinController
* controller
) :
40 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);
55 setMouseTracking(true);
56 viewport()->setAttribute(Qt::WA_Hover
);
58 const ViewProperties
props(controller
->url());
59 setSortIndicatorSection(props
.sorting());
60 setSortIndicatorOrder(props
.sortOrder());
62 connect(header(), SIGNAL(sectionClicked(int)),
63 this, SLOT(synchronizeSortingState(int)));
65 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
66 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
67 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
68 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
70 if (KGlobalSettings::singleClick()) {
71 connect(this, SIGNAL(clicked(const QModelIndex
&)),
72 controller
, SLOT(triggerItem(const QModelIndex
&)));
74 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
75 controller
, SLOT(triggerItem(const QModelIndex
&)));
77 connect(this, SIGNAL(activated(const QModelIndex
&)),
78 controller
, SLOT(triggerItem(const QModelIndex
&)));
79 connect(this, SIGNAL(entered(const QModelIndex
&)),
80 this, SLOT(slotEntered(const QModelIndex
&)));
81 connect(this, SIGNAL(viewportEntered()),
82 controller
, SLOT(emitViewportEntered()));
83 connect(controller
, SIGNAL(zoomIn()),
84 this, SLOT(zoomIn()));
85 connect(controller
, SIGNAL(zoomOut()),
86 this, SLOT(zoomOut()));
88 // apply the details mode settings to the widget
89 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
90 Q_ASSERT(settings
!= 0);
92 m_viewOptions
= QTreeView::viewOptions();
94 QFont
font(settings
->fontFamily(), settings
->fontSize());
95 font
.setItalic(settings
->italicFont());
96 font
.setBold(settings
->boldFont());
97 m_viewOptions
.font
= font
;
99 updateDecorationSize();
102 DolphinDetailsView::~DolphinDetailsView()
106 bool DolphinDetailsView::event(QEvent
* event
)
108 if (event
->type() == QEvent::Polish
) {
109 // Assure that by respecting the available width that:
110 // - the 'Name' column is stretched as large as possible
111 // - the remaining columns are as small as possible
112 QHeaderView
* headerView
= header();
113 headerView
->setStretchLastSection(false);
114 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
115 headerView
->setResizeMode(0, QHeaderView::Stretch
);
117 // hide columns if this is indicated by the settings
118 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
119 Q_ASSERT(settings
!= 0);
120 if (!settings
->showDate()) {
121 hideColumn(KDirModel::ModifiedTime
);
124 if (!settings
->showPermissions()) {
125 hideColumn(KDirModel::Permissions
);
128 if (!settings
->showOwner()) {
129 hideColumn(KDirModel::Owner
);
132 if (!settings
->showGroup()) {
133 hideColumn(KDirModel::Group
);
136 if (!settings
->showType()) {
137 hideColumn(KDirModel::Type
);
141 return QTreeView::event(event
);
144 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
146 return m_viewOptions
;
149 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
151 QTreeView::contextMenuEvent(event
);
152 m_controller
->triggerContextMenuRequest(event
->pos());
155 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
157 QTreeView::mousePressEvent(event
);
159 if (event
->button() == Qt::LeftButton
) {
160 m_showElasticBand
= true;
162 const QPoint
pos(contentsPos());
163 m_elasticBandOrigin
= event
->pos();
164 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
165 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
166 m_elasticBandDestination
= event
->pos();
170 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
172 QTreeView::mouseMoveEvent(event
);
173 if (m_showElasticBand
) {
178 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
180 QTreeView::mouseReleaseEvent(event
);
182 m_showElasticBand
= false;
183 m_controller
->triggerActivation();
186 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
188 if (event
->mimeData()->hasUrls()) {
189 event
->acceptProposedAction();
192 m_showElasticBand
= false;
195 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
197 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
198 if (!urls
.isEmpty()) {
199 event
->acceptProposedAction();
200 m_controller
->indicateDroppedUrls(urls
,
201 indexAt(event
->pos()),
204 QTreeView::dropEvent(event
);
207 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
209 QTreeView::paintEvent(event
);
210 if (m_showElasticBand
) {
211 // The following code has been taken from QListView
212 // and adapted to DolphinDetailsView.
213 // (C) 1992-2007 Trolltech ASA
214 QStyleOptionRubberBand opt
;
216 opt
.shape
= QRubberBand::Rectangle
;
218 opt
.rect
= elasticBandRect();
220 QPainter
painter(viewport());
222 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
227 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
229 QHeaderView
* headerView
= header();
230 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
233 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
235 QHeaderView
* headerView
= header();
236 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
239 void DolphinDetailsView::synchronizeSortingState(int column
)
241 // The sorting has already been changed in QTreeView if this slot is
242 // invoked, but Dolphin is not informed about this.
243 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
244 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
245 m_controller
->indicateSortingChange(sorting
);
246 m_controller
->indicateSortOrderChange(sortOrder
);
249 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
251 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
252 const int nameColumnWidth
= header()->sectionSize(KDirModel::Name
);
253 if (pos
.x() < nameColumnWidth
) {
254 m_controller
->emitItemEntered(index
);
257 m_controller
->emitViewportEntered();
261 void DolphinDetailsView::updateElasticBand()
263 Q_ASSERT(m_showElasticBand
);
264 QRect
dirtyRegion(elasticBandRect());
265 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
266 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
267 setDirtyRegion(dirtyRegion
);
270 void DolphinDetailsView::zoomIn()
272 if (isZoomInPossible()) {
273 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
274 // TODO: get rid of K3Icon sizes
275 switch (settings
->iconSize()) {
276 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
277 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
278 default: Q_ASSERT(false); break;
280 updateDecorationSize();
284 void DolphinDetailsView::zoomOut()
286 if (isZoomOutPossible()) {
287 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
288 // TODO: get rid of K3Icon sizes
289 switch (settings
->iconSize()) {
290 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
291 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
292 default: Q_ASSERT(false); break;
294 updateDecorationSize();
298 bool DolphinDetailsView::isZoomInPossible() const
300 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
301 return settings
->iconSize() < K3Icon::SizeLarge
;
304 bool DolphinDetailsView::isZoomOutPossible() const
306 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
307 return settings
->iconSize() > K3Icon::SizeSmall
;
310 void DolphinDetailsView::updateDecorationSize()
312 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
313 const int iconSize
= settings
->iconSize();
314 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
316 m_controller
->setZoomInPossible(isZoomInPossible());
317 m_controller
->setZoomOutPossible(isZoomOutPossible());
322 QPoint
DolphinDetailsView::contentsPos() const
324 // implementation note: the horizonal position is ignored currently, as no
325 // horizontal scrolling is done anyway during a selection
326 const QScrollBar
* scrollbar
= verticalScrollBar();
327 Q_ASSERT(scrollbar
!= 0);
329 const int maxHeight
= maximumViewportSize().height();
330 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
331 const int visibleHeight
= model()->rowCount() + 1 - height
;
332 if (visibleHeight
<= 0) {
336 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
340 QRect
DolphinDetailsView::elasticBandRect() const
342 const QPoint
pos(contentsPos());
343 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
344 return QRect(topLeft
, m_elasticBandDestination
).normalized();
347 #include "dolphindetailsview.moc"