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
),
43 m_showElasticBand(false),
44 m_elasticBandOrigin(),
45 m_elasticBandDestination()
47 Q_ASSERT(controller
!= 0);
50 setRootIsDecorated(false);
51 setSortingEnabled(true);
52 setUniformRowHeights(true);
53 setSelectionBehavior(SelectItems
);
54 setDragDropMode(QAbstractItemView::DragDrop
);
55 setDropIndicatorShown(false);
57 setMouseTracking(true);
58 viewport()->setAttribute(Qt::WA_Hover
);
60 const ViewProperties
props(controller
->url());
61 setSortIndicatorSection(props
.sorting());
62 setSortIndicatorOrder(props
.sortOrder());
64 connect(header(), SIGNAL(sectionClicked(int)),
65 this, SLOT(synchronizeSortingState(int)));
67 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
68 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
69 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
70 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
72 if (KGlobalSettings::singleClick()) {
73 connect(this, SIGNAL(clicked(const QModelIndex
&)),
74 controller
, SLOT(triggerItem(const QModelIndex
&)));
76 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
77 controller
, SLOT(triggerItem(const QModelIndex
&)));
79 connect(this, SIGNAL(activated(const QModelIndex
&)),
80 controller
, SLOT(triggerItem(const QModelIndex
&)));
81 connect(this, SIGNAL(entered(const QModelIndex
&)),
82 this, SLOT(slotEntered(const QModelIndex
&)));
83 connect(this, SIGNAL(viewportEntered()),
84 controller
, SLOT(emitViewportEntered()));
85 connect(controller
, SIGNAL(zoomIn()),
86 this, SLOT(zoomIn()));
87 connect(controller
, SIGNAL(zoomOut()),
88 this, SLOT(zoomOut()));
90 // apply the details mode settings to the widget
91 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
92 Q_ASSERT(settings
!= 0);
94 m_viewOptions
= QTreeView::viewOptions();
96 QFont
font(settings
->fontFamily(), settings
->fontSize());
97 font
.setItalic(settings
->italicFont());
98 font
.setBold(settings
->boldFont());
99 m_viewOptions
.font
= font
;
101 updateDecorationSize();
104 DolphinDetailsView::~DolphinDetailsView()
108 bool DolphinDetailsView::event(QEvent
* event
)
110 if (event
->type() == QEvent::Polish
) {
111 // Assure that by respecting the available width that:
112 // - the 'Name' column is stretched as large as possible
113 // - the remaining columns are as small as possible
114 QHeaderView
* headerView
= header();
115 headerView
->setStretchLastSection(false);
116 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
117 headerView
->setResizeMode(0, QHeaderView::Stretch
);
119 // hide columns if this is indicated by the settings
120 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
121 Q_ASSERT(settings
!= 0);
122 if (!settings
->showDate()) {
123 hideColumn(KDirModel::ModifiedTime
);
126 if (!settings
->showPermissions()) {
127 hideColumn(KDirModel::Permissions
);
130 if (!settings
->showOwner()) {
131 hideColumn(KDirModel::Owner
);
134 if (!settings
->showGroup()) {
135 hideColumn(KDirModel::Group
);
138 if (!settings
->showType()) {
139 hideColumn(KDirModel::Type
);
143 return QTreeView::event(event
);
146 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
148 return m_viewOptions
;
151 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
153 QTreeView::contextMenuEvent(event
);
154 m_controller
->triggerContextMenuRequest(event
->pos());
157 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
159 if (!indexAt(event
->pos()).isValid()) {
160 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
161 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
166 QTreeView::mousePressEvent(event
);
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;
194 m_controller
->triggerActivation();
197 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
199 if (event
->mimeData()->hasUrls()) {
200 event
->acceptProposedAction();
203 if (m_showElasticBand
) {
205 m_showElasticBand
= false;
210 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
212 QTreeView::dragMoveEvent(event
);
214 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
215 const QPoint
pos(0, event
->pos().y());
216 const QModelIndex index
= indexAt(pos
);
217 setDirtyRegion(m_dropRect
);
218 m_dropRect
= visualRect(index
);
219 setDirtyRegion(m_dropRect
);
222 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
224 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
225 if (!urls
.isEmpty()) {
226 event
->acceptProposedAction();
227 m_controller
->indicateDroppedUrls(urls
,
228 indexAt(event
->pos()),
231 QTreeView::dropEvent(event
);
235 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
237 QTreeView::paintEvent(event
);
238 if (m_showElasticBand
) {
239 // The following code has been taken from QListView
240 // and adapted to DolphinDetailsView.
241 // (C) 1992-2007 Trolltech ASA
242 QStyleOptionRubberBand opt
;
244 opt
.shape
= QRubberBand::Rectangle
;
246 opt
.rect
= elasticBandRect();
248 QPainter
painter(viewport());
250 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
255 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
256 QPainter
painter(viewport());
258 QBrush
brush(m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
));
259 QColor color
= brush
.color();
261 brush
.setColor(color
);
262 painter
.fillRect(m_dropRect
, brush
);
267 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
269 QHeaderView
* headerView
= header();
270 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
273 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
275 QHeaderView
* headerView
= header();
276 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
279 void DolphinDetailsView::synchronizeSortingState(int column
)
281 // The sorting has already been changed in QTreeView if this slot is
282 // invoked, but Dolphin is not informed about this.
283 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
284 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
285 m_controller
->indicateSortingChange(sorting
);
286 m_controller
->indicateSortOrderChange(sortOrder
);
289 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
291 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
292 const int nameColumnWidth
= header()->sectionSize(KDirModel::Name
);
293 if (pos
.x() < nameColumnWidth
) {
294 m_controller
->emitItemEntered(index
);
297 m_controller
->emitViewportEntered();
301 void DolphinDetailsView::updateElasticBand()
303 Q_ASSERT(m_showElasticBand
);
304 QRect
dirtyRegion(elasticBandRect());
305 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
306 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
307 setDirtyRegion(dirtyRegion
);
310 void DolphinDetailsView::zoomIn()
312 if (isZoomInPossible()) {
313 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
314 // TODO: get rid of K3Icon sizes
315 switch (settings
->iconSize()) {
316 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
317 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
318 default: Q_ASSERT(false); break;
320 updateDecorationSize();
324 void DolphinDetailsView::zoomOut()
326 if (isZoomOutPossible()) {
327 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
328 // TODO: get rid of K3Icon sizes
329 switch (settings
->iconSize()) {
330 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
331 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
332 default: Q_ASSERT(false); break;
334 updateDecorationSize();
338 bool DolphinDetailsView::isZoomInPossible() const
340 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
341 return settings
->iconSize() < K3Icon::SizeLarge
;
344 bool DolphinDetailsView::isZoomOutPossible() const
346 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
347 return settings
->iconSize() > K3Icon::SizeSmall
;
350 void DolphinDetailsView::updateDecorationSize()
352 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
353 const int iconSize
= settings
->iconSize();
354 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
356 m_controller
->setZoomInPossible(isZoomInPossible());
357 m_controller
->setZoomOutPossible(isZoomOutPossible());
362 QPoint
DolphinDetailsView::contentsPos() const
364 // implementation note: the horizonal position is ignored currently, as no
365 // horizontal scrolling is done anyway during a selection
366 const QScrollBar
* scrollbar
= verticalScrollBar();
367 Q_ASSERT(scrollbar
!= 0);
369 const int maxHeight
= maximumViewportSize().height();
370 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
371 const int visibleHeight
= model()->rowCount() + 1 - height
;
372 if (visibleHeight
<= 0) {
376 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
380 QRect
DolphinDetailsView::elasticBandRect() const
382 const QPoint
pos(contentsPos());
383 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
384 return QRect(topLeft
, m_elasticBandDestination
).normalized();
387 #include "dolphindetailsview.moc"