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(), m_controller
->url());
156 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
158 m_controller
->triggerActivation();
160 QTreeView::mousePressEvent(event
);
162 const QModelIndex index
= indexAt(event
->pos());
163 if (!index
.isValid() || (index
.column() != KDirModel::Name
)) {
164 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
165 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
170 if (event
->button() == Qt::LeftButton
) {
171 m_showElasticBand
= true;
173 const QPoint
pos(contentsPos());
174 m_elasticBandOrigin
= event
->pos();
175 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
176 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
177 m_elasticBandDestination
= event
->pos();
181 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
183 QTreeView::mouseMoveEvent(event
);
184 if (m_showElasticBand
) {
189 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
191 QTreeView::mouseReleaseEvent(event
);
192 if (m_showElasticBand
) {
194 m_showElasticBand
= false;
198 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
200 if (event
->mimeData()->hasUrls()) {
201 event
->acceptProposedAction();
204 if (m_showElasticBand
) {
206 m_showElasticBand
= false;
211 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
213 QTreeView::dragLeaveEvent(event
);
215 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
217 setDirtyRegion(m_dropRect
);
220 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
222 QTreeView::dragMoveEvent(event
);
224 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
225 setDirtyRegion(m_dropRect
);
226 const QModelIndex index
= indexAt(event
->pos());
227 if (!index
.isValid() || (index
.column() != KDirModel::Name
)) {
231 m_dropRect
= visualRect(index
);
232 setDirtyRegion(m_dropRect
);
236 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
238 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
239 if (!urls
.isEmpty()) {
240 event
->acceptProposedAction();
241 m_controller
->indicateDroppedUrls(urls
,
242 indexAt(event
->pos()),
245 QTreeView::dropEvent(event
);
249 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
251 QTreeView::paintEvent(event
);
252 if (m_showElasticBand
) {
253 // The following code has been taken from QListView
254 // and adapted to DolphinDetailsView.
255 // (C) 1992-2007 Trolltech ASA
256 QStyleOptionRubberBand opt
;
258 opt
.shape
= QRubberBand::Rectangle
;
260 opt
.rect
= elasticBandRect();
262 QPainter
painter(viewport());
264 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
268 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
270 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
271 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
275 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
277 QHeaderView
* headerView
= header();
278 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
281 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
283 QHeaderView
* headerView
= header();
284 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
287 void DolphinDetailsView::synchronizeSortingState(int column
)
289 // The sorting has already been changed in QTreeView if this slot is
290 // invoked, but Dolphin is not informed about this.
291 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
292 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
293 m_controller
->indicateSortingChange(sorting
);
294 m_controller
->indicateSortOrderChange(sortOrder
);
297 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
299 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
300 const int nameColumnWidth
= header()->sectionSize(KDirModel::Name
);
301 if (pos
.x() < nameColumnWidth
) {
302 m_controller
->emitItemEntered(index
);
305 m_controller
->emitViewportEntered();
309 void DolphinDetailsView::updateElasticBand()
311 Q_ASSERT(m_showElasticBand
);
312 QRect
dirtyRegion(elasticBandRect());
313 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
314 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
315 setDirtyRegion(dirtyRegion
);
318 void DolphinDetailsView::zoomIn()
320 if (isZoomInPossible()) {
321 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
322 // TODO: get rid of K3Icon sizes
323 switch (settings
->iconSize()) {
324 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
325 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
326 default: Q_ASSERT(false); break;
328 updateDecorationSize();
332 void DolphinDetailsView::zoomOut()
334 if (isZoomOutPossible()) {
335 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
336 // TODO: get rid of K3Icon sizes
337 switch (settings
->iconSize()) {
338 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
339 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
340 default: Q_ASSERT(false); break;
342 updateDecorationSize();
346 bool DolphinDetailsView::isZoomInPossible() const
348 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
349 return settings
->iconSize() < K3Icon::SizeLarge
;
352 bool DolphinDetailsView::isZoomOutPossible() const
354 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
355 return settings
->iconSize() > K3Icon::SizeSmall
;
358 void DolphinDetailsView::updateDecorationSize()
360 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
361 const int iconSize
= settings
->iconSize();
362 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
364 m_controller
->setZoomInPossible(isZoomInPossible());
365 m_controller
->setZoomOutPossible(isZoomOutPossible());
370 QPoint
DolphinDetailsView::contentsPos() const
372 // implementation note: the horizonal position is ignored currently, as no
373 // horizontal scrolling is done anyway during a selection
374 const QScrollBar
* scrollbar
= verticalScrollBar();
375 Q_ASSERT(scrollbar
!= 0);
377 const int maxHeight
= maximumViewportSize().height();
378 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
379 const int visibleHeight
= model()->rowCount() + 1 - height
;
380 if (visibleHeight
<= 0) {
384 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
388 QRect
DolphinDetailsView::elasticBandRect() const
390 const QPoint
pos(contentsPos());
391 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
392 return QRect(topLeft
, m_elasticBandDestination
).normalized();
395 #include "dolphindetailsview.moc"