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(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;
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
);
267 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
268 QPainter
painter(viewport());
270 QBrush
brush(m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
));
271 QColor color
= brush
.color();
273 brush
.setColor(color
);
274 painter
.fillRect(m_dropRect
, brush
);
279 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
281 QHeaderView
* headerView
= header();
282 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
285 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
287 QHeaderView
* headerView
= header();
288 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
291 void DolphinDetailsView::synchronizeSortingState(int column
)
293 // The sorting has already been changed in QTreeView if this slot is
294 // invoked, but Dolphin is not informed about this.
295 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
296 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
297 m_controller
->indicateSortingChange(sorting
);
298 m_controller
->indicateSortOrderChange(sortOrder
);
301 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
303 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
304 const int nameColumnWidth
= header()->sectionSize(KDirModel::Name
);
305 if (pos
.x() < nameColumnWidth
) {
306 m_controller
->emitItemEntered(index
);
309 m_controller
->emitViewportEntered();
313 void DolphinDetailsView::updateElasticBand()
315 Q_ASSERT(m_showElasticBand
);
316 QRect
dirtyRegion(elasticBandRect());
317 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
318 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
319 setDirtyRegion(dirtyRegion
);
322 void DolphinDetailsView::zoomIn()
324 if (isZoomInPossible()) {
325 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
326 // TODO: get rid of K3Icon sizes
327 switch (settings
->iconSize()) {
328 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
329 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
330 default: Q_ASSERT(false); break;
332 updateDecorationSize();
336 void DolphinDetailsView::zoomOut()
338 if (isZoomOutPossible()) {
339 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
340 // TODO: get rid of K3Icon sizes
341 switch (settings
->iconSize()) {
342 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
343 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
344 default: Q_ASSERT(false); break;
346 updateDecorationSize();
350 bool DolphinDetailsView::isZoomInPossible() const
352 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
353 return settings
->iconSize() < K3Icon::SizeLarge
;
356 bool DolphinDetailsView::isZoomOutPossible() const
358 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
359 return settings
->iconSize() > K3Icon::SizeSmall
;
362 void DolphinDetailsView::updateDecorationSize()
364 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
365 const int iconSize
= settings
->iconSize();
366 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
368 m_controller
->setZoomInPossible(isZoomInPossible());
369 m_controller
->setZoomOutPossible(isZoomOutPossible());
374 QPoint
DolphinDetailsView::contentsPos() const
376 // implementation note: the horizonal position is ignored currently, as no
377 // horizontal scrolling is done anyway during a selection
378 const QScrollBar
* scrollbar
= verticalScrollBar();
379 Q_ASSERT(scrollbar
!= 0);
381 const int maxHeight
= maximumViewportSize().height();
382 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
383 const int visibleHeight
= model()->rowCount() + 1 - height
;
384 if (visibleHeight
<= 0) {
388 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
392 QRect
DolphinDetailsView::elasticBandRect() const
394 const QPoint
pos(contentsPos());
395 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
396 return QRect(topLeft
, m_elasticBandDestination
).normalized();
399 #include "dolphindetailsview.moc"