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 "dolphinmodel.h"
24 #include "dolphincontroller.h"
25 #include "dolphinsettings.h"
26 #include "dolphinsortfilterproxymodel.h"
27 #include "viewproperties.h"
29 #include "dolphin_detailsmodesettings.h"
31 #include <QApplication>
32 #include <QHeaderView>
33 #include <QRubberBand>
37 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
, DolphinController
* controller
) :
39 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);
54 setAlternatingRowColors(true);
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 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
72 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
73 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
74 // RETURN-key in keyPressEvent().
75 if (KGlobalSettings::singleClick()) {
76 connect(this, SIGNAL(clicked(const QModelIndex
&)),
77 this, SLOT(slotItemActivated(const QModelIndex
&)));
79 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
80 this, SLOT(slotItemActivated(const QModelIndex
&)));
82 connect(this, SIGNAL(entered(const QModelIndex
&)),
83 this, SLOT(slotEntered(const QModelIndex
&)));
84 connect(this, SIGNAL(viewportEntered()),
85 controller
, SLOT(emitViewportEntered()));
86 connect(controller
, SIGNAL(zoomIn()),
87 this, SLOT(zoomIn()));
88 connect(controller
, SIGNAL(zoomOut()),
89 this, SLOT(zoomOut()));
91 // apply the details mode settings to the widget
92 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
93 Q_ASSERT(settings
!= 0);
95 m_viewOptions
= QTreeView::viewOptions();
97 QFont
font(settings
->fontFamily(), settings
->fontSize());
98 font
.setItalic(settings
->italicFont());
99 font
.setBold(settings
->boldFont());
100 m_viewOptions
.font
= font
;
102 updateDecorationSize();
105 DolphinDetailsView::~DolphinDetailsView()
109 bool DolphinDetailsView::event(QEvent
* event
)
111 if (event
->type() == QEvent::Polish
) {
112 // Assure that by respecting the available width that:
113 // - the 'Name' column is stretched as large as possible
114 // - the remaining columns are as small as possible
115 QHeaderView
* headerView
= header();
116 headerView
->setStretchLastSection(false);
117 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
118 headerView
->setResizeMode(0, QHeaderView::Stretch
);
120 // hide columns if this is indicated by the settings
121 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
122 Q_ASSERT(settings
!= 0);
123 if (!settings
->showDate()) {
124 hideColumn(DolphinModel::ModifiedTime
);
127 if (!settings
->showPermissions()) {
128 hideColumn(DolphinModel::Permissions
);
131 if (!settings
->showOwner()) {
132 hideColumn(DolphinModel::Owner
);
135 if (!settings
->showGroup()) {
136 hideColumn(DolphinModel::Group
);
139 if (!settings
->showType()) {
140 hideColumn(DolphinModel::Type
);
144 return QTreeView::event(event
);
147 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
149 return m_viewOptions
;
152 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
154 QTreeView::contextMenuEvent(event
);
155 m_controller
->triggerContextMenuRequest(event
->pos());
158 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
160 m_controller
->triggerActivation();
162 QTreeView::mousePressEvent(event
);
164 const QModelIndex index
= indexAt(event
->pos());
165 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
166 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
167 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
172 if (event
->button() == Qt::LeftButton
) {
173 m_showElasticBand
= true;
175 const QPoint
pos(contentsPos());
176 m_elasticBandOrigin
= event
->pos();
177 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
178 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
179 m_elasticBandDestination
= event
->pos();
183 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
185 QTreeView::mouseMoveEvent(event
);
186 if (m_showElasticBand
) {
191 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
193 QTreeView::mouseReleaseEvent(event
);
194 if (m_showElasticBand
) {
196 m_showElasticBand
= false;
200 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
202 if (event
->mimeData()->hasUrls()) {
203 event
->acceptProposedAction();
206 if (m_showElasticBand
) {
208 m_showElasticBand
= false;
213 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
215 QTreeView::dragLeaveEvent(event
);
217 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
219 setDirtyRegion(m_dropRect
);
222 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
224 QTreeView::dragMoveEvent(event
);
226 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
227 setDirtyRegion(m_dropRect
);
228 const QModelIndex index
= indexAt(event
->pos());
229 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
233 m_dropRect
= visualRect(index
);
234 setDirtyRegion(m_dropRect
);
238 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
240 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
241 if (!urls
.isEmpty()) {
242 event
->acceptProposedAction();
243 m_controller
->indicateDroppedUrls(urls
,
245 indexAt(event
->pos()),
248 QTreeView::dropEvent(event
);
252 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
254 QTreeView::paintEvent(event
);
255 if (m_showElasticBand
) {
256 // The following code has been taken from QListView
257 // and adapted to DolphinDetailsView.
258 // (C) 1992-2007 Trolltech ASA
259 QStyleOptionRubberBand opt
;
261 opt
.shape
= QRubberBand::Rectangle
;
263 opt
.rect
= elasticBandRect();
265 QPainter
painter(viewport());
267 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
271 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
273 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
274 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
278 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
280 QTreeView::keyPressEvent(event
);
282 const QItemSelectionModel
* selModel
= selectionModel();
283 const QModelIndex currentIndex
= selModel
->currentIndex();
284 const bool triggerItem
= currentIndex
.isValid()
285 && (event
->key() == Qt::Key_Return
)
286 && (selModel
->selectedIndexes().count() <= 1);
288 m_controller
->triggerItem(currentIndex
);
292 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
294 QHeaderView
* headerView
= header();
295 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
298 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
300 QHeaderView
* headerView
= header();
301 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
304 void DolphinDetailsView::synchronizeSortingState(int column
)
306 // The sorting has already been changed in QTreeView if this slot is
307 // invoked, but Dolphin is not informed about this.
308 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
309 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
310 m_controller
->indicateSortingChange(sorting
);
311 m_controller
->indicateSortOrderChange(sortOrder
);
314 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
316 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
317 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
318 if (pos
.x() < nameColumnWidth
) {
319 m_controller
->emitItemEntered(index
);
322 m_controller
->emitViewportEntered();
326 void DolphinDetailsView::updateElasticBand()
328 Q_ASSERT(m_showElasticBand
);
329 QRect
dirtyRegion(elasticBandRect());
330 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
331 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
332 setDirtyRegion(dirtyRegion
);
335 void DolphinDetailsView::zoomIn()
337 if (isZoomInPossible()) {
338 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
339 // TODO: get rid of K3Icon sizes
340 switch (settings
->iconSize()) {
341 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
342 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
343 default: Q_ASSERT(false); break;
345 updateDecorationSize();
349 void DolphinDetailsView::zoomOut()
351 if (isZoomOutPossible()) {
352 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
353 // TODO: get rid of K3Icon sizes
354 switch (settings
->iconSize()) {
355 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
356 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
357 default: Q_ASSERT(false); break;
359 updateDecorationSize();
363 bool DolphinDetailsView::isZoomInPossible() const
365 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
366 return settings
->iconSize() < K3Icon::SizeLarge
;
369 bool DolphinDetailsView::isZoomOutPossible() const
371 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
372 return settings
->iconSize() > K3Icon::SizeSmall
;
375 void DolphinDetailsView::updateDecorationSize()
377 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
378 const int iconSize
= settings
->iconSize();
379 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
381 m_controller
->setZoomInPossible(isZoomInPossible());
382 m_controller
->setZoomOutPossible(isZoomOutPossible());
387 QPoint
DolphinDetailsView::contentsPos() const
389 // implementation note: the horizonal position is ignored currently, as no
390 // horizontal scrolling is done anyway during a selection
391 const QScrollBar
* scrollbar
= verticalScrollBar();
392 Q_ASSERT(scrollbar
!= 0);
394 const int maxHeight
= maximumViewportSize().height();
395 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
396 const int visibleHeight
= model()->rowCount() + 1 - height
;
397 if (visibleHeight
<= 0) {
401 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
405 QRect
DolphinDetailsView::elasticBandRect() const
407 const QPoint
pos(contentsPos());
408 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
409 return QRect(topLeft
, m_elasticBandDestination
).normalized();
412 static bool isValidNameIndex(const QModelIndex
& index
)
414 return index
.isValid() && (index
.column() == KDirModel::Name
);
417 void DolphinDetailsView::slotItemActivated(const QModelIndex
& index
)
419 if (!isValidNameIndex(index
)) {
421 m_controller
->emitItemEntered(index
);
423 m_controller
->triggerItem(index
);
427 #include "dolphindetailsview.moc"