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);
55 setAlternatingRowColors(true);
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 this, SLOT(slotItemActivated(const QModelIndex
&)));
76 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
77 this, SLOT(slotItemActivated(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 m_controller
->triggerActivation();
159 QTreeView::mousePressEvent(event
);
161 const QModelIndex index
= indexAt(event
->pos());
162 if (!index
.isValid() || (index
.column() != KDirModel::Name
)) {
163 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
164 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
169 if (event
->button() == Qt::LeftButton
) {
170 m_showElasticBand
= true;
172 const QPoint
pos(contentsPos());
173 m_elasticBandOrigin
= event
->pos();
174 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
175 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
176 m_elasticBandDestination
= event
->pos();
180 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
182 QTreeView::mouseMoveEvent(event
);
183 if (m_showElasticBand
) {
188 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
190 QTreeView::mouseReleaseEvent(event
);
191 if (m_showElasticBand
) {
193 m_showElasticBand
= false;
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::dragLeaveEvent(QDragLeaveEvent
* event
)
212 QTreeView::dragLeaveEvent(event
);
214 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
216 setDirtyRegion(m_dropRect
);
219 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
221 QTreeView::dragMoveEvent(event
);
223 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
224 setDirtyRegion(m_dropRect
);
225 const QModelIndex index
= indexAt(event
->pos());
226 if (!index
.isValid() || (index
.column() != KDirModel::Name
)) {
230 m_dropRect
= visualRect(index
);
231 setDirtyRegion(m_dropRect
);
235 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
237 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
238 if (!urls
.isEmpty()) {
239 event
->acceptProposedAction();
240 m_controller
->indicateDroppedUrls(urls
,
241 indexAt(event
->pos()),
244 QTreeView::dropEvent(event
);
248 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
250 QTreeView::paintEvent(event
);
251 if (m_showElasticBand
) {
252 // The following code has been taken from QListView
253 // and adapted to DolphinDetailsView.
254 // (C) 1992-2007 Trolltech ASA
255 QStyleOptionRubberBand opt
;
257 opt
.shape
= QRubberBand::Rectangle
;
259 opt
.rect
= elasticBandRect();
261 QPainter
painter(viewport());
263 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
267 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
269 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
270 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
274 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
276 QHeaderView
* headerView
= header();
277 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
280 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
282 QHeaderView
* headerView
= header();
283 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
286 void DolphinDetailsView::synchronizeSortingState(int column
)
288 // The sorting has already been changed in QTreeView if this slot is
289 // invoked, but Dolphin is not informed about this.
290 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
291 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
292 m_controller
->indicateSortingChange(sorting
);
293 m_controller
->indicateSortOrderChange(sortOrder
);
296 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
298 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
299 const int nameColumnWidth
= header()->sectionSize(KDirModel::Name
);
300 if (pos
.x() < nameColumnWidth
) {
301 m_controller
->emitItemEntered(index
);
304 m_controller
->emitViewportEntered();
308 void DolphinDetailsView::updateElasticBand()
310 Q_ASSERT(m_showElasticBand
);
311 QRect
dirtyRegion(elasticBandRect());
312 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
313 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
314 setDirtyRegion(dirtyRegion
);
317 void DolphinDetailsView::zoomIn()
319 if (isZoomInPossible()) {
320 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
321 // TODO: get rid of K3Icon sizes
322 switch (settings
->iconSize()) {
323 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
324 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
325 default: Q_ASSERT(false); break;
327 updateDecorationSize();
331 void DolphinDetailsView::zoomOut()
333 if (isZoomOutPossible()) {
334 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
335 // TODO: get rid of K3Icon sizes
336 switch (settings
->iconSize()) {
337 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
338 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
339 default: Q_ASSERT(false); break;
341 updateDecorationSize();
345 bool DolphinDetailsView::isZoomInPossible() const
347 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
348 return settings
->iconSize() < K3Icon::SizeLarge
;
351 bool DolphinDetailsView::isZoomOutPossible() const
353 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
354 return settings
->iconSize() > K3Icon::SizeSmall
;
357 void DolphinDetailsView::updateDecorationSize()
359 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
360 const int iconSize
= settings
->iconSize();
361 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
363 m_controller
->setZoomInPossible(isZoomInPossible());
364 m_controller
->setZoomOutPossible(isZoomOutPossible());
369 QPoint
DolphinDetailsView::contentsPos() const
371 // implementation note: the horizonal position is ignored currently, as no
372 // horizontal scrolling is done anyway during a selection
373 const QScrollBar
* scrollbar
= verticalScrollBar();
374 Q_ASSERT(scrollbar
!= 0);
376 const int maxHeight
= maximumViewportSize().height();
377 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
378 const int visibleHeight
= model()->rowCount() + 1 - height
;
379 if (visibleHeight
<= 0) {
383 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
387 QRect
DolphinDetailsView::elasticBandRect() const
389 const QPoint
pos(contentsPos());
390 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
391 return QRect(topLeft
, m_elasticBandDestination
).normalized();
394 static bool isValidNameIndex(const QModelIndex
& index
)
396 return index
.isValid() && (index
.column() == KDirModel::Name
);
399 void DolphinDetailsView::slotItemActivated(const QModelIndex
& index
)
401 if (!isValidNameIndex(index
)) {
403 m_controller
->emitItemEntered(index
);
405 m_controller
->triggerItem(index
);
409 #include "dolphindetailsview.moc"