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"
35 #include <QApplication>
36 #include <QHeaderView>
37 #include <QRubberBand>
41 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
, DolphinController
* controller
) :
43 m_controller(controller
),
45 m_showElasticBand(false),
46 m_elasticBandOrigin(),
47 m_elasticBandDestination()
49 Q_ASSERT(controller
!= 0);
52 setRootIsDecorated(false);
53 setSortingEnabled(true);
54 setUniformRowHeights(true);
55 setSelectionBehavior(SelectItems
);
56 setDragDropMode(QAbstractItemView::DragDrop
);
57 setDropIndicatorShown(false);
58 setAlternatingRowColors(true);
60 setMouseTracking(true);
61 viewport()->setAttribute(Qt::WA_Hover
);
63 const ViewProperties
props(controller
->url());
64 setSortIndicatorSection(props
.sorting());
65 setSortIndicatorOrder(props
.sortOrder());
67 QHeaderView
* headerView
= header();
68 connect(headerView
, SIGNAL(sectionClicked(int)),
69 this, SLOT(synchronizeSortingState(int)));
70 headerView
->setContextMenuPolicy(Qt::CustomContextMenu
);
71 connect(headerView
, SIGNAL(customContextMenuRequested(const QPoint
&)),
72 this, SLOT(configureColumns(const QPoint
&)));
74 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
75 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
76 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
77 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
79 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
80 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
81 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
82 // RETURN-key in keyPressEvent().
83 if (KGlobalSettings::singleClick()) {
84 connect(this, SIGNAL(clicked(const QModelIndex
&)),
85 this, SLOT(slotItemActivated(const QModelIndex
&)));
87 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
88 this, SLOT(slotItemActivated(const QModelIndex
&)));
90 connect(this, SIGNAL(entered(const QModelIndex
&)),
91 this, SLOT(slotEntered(const QModelIndex
&)));
92 connect(this, SIGNAL(viewportEntered()),
93 controller
, SLOT(emitViewportEntered()));
94 connect(controller
, SIGNAL(zoomIn()),
95 this, SLOT(zoomIn()));
96 connect(controller
, SIGNAL(zoomOut()),
97 this, SLOT(zoomOut()));
99 // apply the details mode settings to the widget
100 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
101 Q_ASSERT(settings
!= 0);
103 m_viewOptions
= QTreeView::viewOptions();
105 QFont
font(settings
->fontFamily(), settings
->fontSize());
106 font
.setItalic(settings
->italicFont());
107 font
.setBold(settings
->boldFont());
108 m_viewOptions
.font
= font
;
110 updateDecorationSize();
113 DolphinDetailsView::~DolphinDetailsView()
117 bool DolphinDetailsView::event(QEvent
* event
)
119 if (event
->type() == QEvent::Polish
) {
120 // Assure that by respecting the available width that:
121 // - the 'Name' column is stretched as large as possible
122 // - the remaining columns are as small as possible
123 QHeaderView
* headerView
= header();
124 headerView
->setStretchLastSection(false);
125 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
126 headerView
->setResizeMode(0, QHeaderView::Stretch
);
128 // hide columns if this is indicated by the settings
129 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
130 Q_ASSERT(settings
!= 0);
131 if (!settings
->showDate()) {
132 hideColumn(DolphinModel::ModifiedTime
);
135 if (!settings
->showPermissions()) {
136 hideColumn(DolphinModel::Permissions
);
139 if (!settings
->showOwner()) {
140 hideColumn(DolphinModel::Owner
);
143 if (!settings
->showGroup()) {
144 hideColumn(DolphinModel::Group
);
147 if (!settings
->showType()) {
148 hideColumn(DolphinModel::Type
);
151 hideColumn(DolphinModel::Rating
);
152 hideColumn(DolphinModel::Tags
);
155 return QTreeView::event(event
);
158 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
160 return m_viewOptions
;
163 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
165 QTreeView::contextMenuEvent(event
);
166 m_controller
->triggerContextMenuRequest(event
->pos());
169 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
171 m_controller
->triggerActivation();
173 QTreeView::mousePressEvent(event
);
175 const QModelIndex index
= indexAt(event
->pos());
176 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
177 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
178 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
183 if (event
->button() == Qt::LeftButton
) {
184 m_showElasticBand
= true;
186 const QPoint
pos(contentsPos());
187 m_elasticBandOrigin
= event
->pos();
188 m_elasticBandOrigin
.setX(m_elasticBandOrigin
.x() + pos
.x());
189 m_elasticBandOrigin
.setY(m_elasticBandOrigin
.y() + pos
.y());
190 m_elasticBandDestination
= event
->pos();
194 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
196 QTreeView::mouseMoveEvent(event
);
197 if (m_showElasticBand
) {
202 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
204 QTreeView::mouseReleaseEvent(event
);
205 if (m_showElasticBand
) {
207 m_showElasticBand
= false;
211 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
213 if (event
->mimeData()->hasUrls()) {
214 event
->acceptProposedAction();
217 if (m_showElasticBand
) {
219 m_showElasticBand
= false;
224 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent
* event
)
226 QTreeView::dragLeaveEvent(event
);
228 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
230 setDirtyRegion(m_dropRect
);
233 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent
* event
)
235 QTreeView::dragMoveEvent(event
);
237 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
238 setDirtyRegion(m_dropRect
);
239 const QModelIndex index
= indexAt(event
->pos());
240 if (!index
.isValid() || (index
.column() != DolphinModel::Name
)) {
244 m_dropRect
= visualRect(index
);
245 setDirtyRegion(m_dropRect
);
249 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
251 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
252 if (!urls
.isEmpty()) {
253 event
->acceptProposedAction();
254 m_controller
->indicateDroppedUrls(urls
,
256 indexAt(event
->pos()),
259 QTreeView::dropEvent(event
);
263 void DolphinDetailsView::paintEvent(QPaintEvent
* event
)
265 QTreeView::paintEvent(event
);
266 if (m_showElasticBand
) {
267 // The following code has been taken from QListView
268 // and adapted to DolphinDetailsView.
269 // (C) 1992-2007 Trolltech ASA
270 QStyleOptionRubberBand opt
;
272 opt
.shape
= QRubberBand::Rectangle
;
274 opt
.rect
= elasticBandRect();
276 QPainter
painter(viewport());
278 style()->drawControl(QStyle::CE_RubberBand
, &opt
, &painter
);
282 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
284 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
285 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
289 void DolphinDetailsView::keyPressEvent(QKeyEvent
* event
)
291 QTreeView::keyPressEvent(event
);
293 const QItemSelectionModel
* selModel
= selectionModel();
294 const QModelIndex currentIndex
= selModel
->currentIndex();
295 const bool triggerItem
= currentIndex
.isValid()
296 && (event
->key() == Qt::Key_Return
)
297 && (selModel
->selectedIndexes().count() <= 1);
299 m_controller
->triggerItem(currentIndex
);
303 void DolphinDetailsView::resizeEvent(QResizeEvent
* event
)
305 QTreeView::resizeEvent(event
);
307 // assure that the width of the name-column does not get too small
308 const int minWidth
= 120;
309 QHeaderView
* headerView
= header();
310 bool useFixedWidth
= (headerView
->sectionSize(KDirModel::Name
) <= minWidth
)
311 && (headerView
->resizeMode(0) != QHeaderView::Fixed
);
313 // the current width of the name-column is too small, hence
315 headerView
->setResizeMode(QHeaderView::Fixed
);
316 headerView
->setResizeMode(0, QHeaderView::Fixed
);
317 headerView
->resizeSection(KDirModel::Name
, minWidth
);
318 } else if (headerView
->resizeMode(0) != QHeaderView::Stretch
) {
319 // check whether there is enough available viewport width
320 // to automatically resize the columns
321 const int availableWidth
= viewport()->width();
324 const int count
= headerView
->count();
325 for (int i
= 0; i
< count
; ++i
) {
326 headerWidth
+= headerView
->sectionSize(i
);
329 if (headerWidth
< availableWidth
) {
330 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
331 headerView
->setResizeMode(0, QHeaderView::Stretch
);
336 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
338 QHeaderView
* headerView
= header();
339 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
342 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
344 QHeaderView
* headerView
= header();
345 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
348 void DolphinDetailsView::synchronizeSortingState(int column
)
350 // The sorting has already been changed in QTreeView if this slot is
351 // invoked, but Dolphin is not informed about this.
352 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
353 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
354 m_controller
->indicateSortingChange(sorting
);
355 m_controller
->indicateSortOrderChange(sortOrder
);
358 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
360 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
361 const int nameColumnWidth
= header()->sectionSize(DolphinModel::Name
);
362 if (pos
.x() < nameColumnWidth
) {
363 m_controller
->emitItemEntered(index
);
366 m_controller
->emitViewportEntered();
370 void DolphinDetailsView::updateElasticBand()
372 Q_ASSERT(m_showElasticBand
);
373 QRect
dirtyRegion(elasticBandRect());
374 m_elasticBandDestination
= viewport()->mapFromGlobal(QCursor::pos());
375 dirtyRegion
= dirtyRegion
.united(elasticBandRect());
376 setDirtyRegion(dirtyRegion
);
379 void DolphinDetailsView::zoomIn()
381 if (isZoomInPossible()) {
382 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
383 // TODO: get rid of K3Icon sizes
384 switch (settings
->iconSize()) {
385 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
386 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
387 default: Q_ASSERT(false); break;
389 updateDecorationSize();
393 void DolphinDetailsView::zoomOut()
395 if (isZoomOutPossible()) {
396 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
397 // TODO: get rid of K3Icon sizes
398 switch (settings
->iconSize()) {
399 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
400 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
401 default: Q_ASSERT(false); break;
403 updateDecorationSize();
407 void DolphinDetailsView::slotItemActivated(const QModelIndex
& index
)
409 if (index
.isValid() && (index
.column() == KDirModel::Name
)) {
410 m_controller
->triggerItem(index
);
413 m_controller
->emitItemEntered(index
);
417 void DolphinDetailsView::configureColumns(const QPoint
& pos
)
420 popup
.addTitle(i18nc("@title:menu", "Columns"));
422 QHeaderView
* headerView
= header();
423 for (int i
= DolphinModel::ModifiedTime
; i
<= DolphinModel::Type
; ++i
) {
424 const int logicalIndex
= headerView
->logicalIndex(i
);
425 const QString text
= model()->headerData(i
, Qt::Horizontal
).toString();
426 QAction
* action
= popup
.addAction(text
);
427 action
->setCheckable(true);
428 action
->setChecked(!headerView
->isSectionHidden(logicalIndex
));
432 QAction
* activatedAction
= popup
.exec(header()->mapToGlobal(pos
));
433 if (activatedAction
!= 0) {
434 const bool show
= activatedAction
->isChecked();
435 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
436 Q_ASSERT(settings
!= 0);
438 // remember the changed column visibility in the settings
439 const int columnIndex
= activatedAction
->data().toInt();
440 switch (columnIndex
) {
441 case DolphinModel::ModifiedTime
: settings
->setShowDate(show
); break;
442 case DolphinModel::Permissions
: settings
->setShowPermissions(show
); break;
443 case DolphinModel::Owner
: settings
->setShowOwner(show
); break;
444 case DolphinModel::Group
: settings
->setShowGroup(show
); break;
445 case DolphinModel::Type
: settings
->setShowType(show
); break;
449 // apply the changed column visibility
451 showColumn(columnIndex
);
453 hideColumn(columnIndex
);
458 bool DolphinDetailsView::isZoomInPossible() const
460 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
461 return settings
->iconSize() < K3Icon::SizeLarge
;
464 bool DolphinDetailsView::isZoomOutPossible() const
466 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
467 return settings
->iconSize() > K3Icon::SizeSmall
;
470 void DolphinDetailsView::updateDecorationSize()
472 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
473 const int iconSize
= settings
->iconSize();
474 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
476 m_controller
->setZoomInPossible(isZoomInPossible());
477 m_controller
->setZoomOutPossible(isZoomOutPossible());
482 QPoint
DolphinDetailsView::contentsPos() const
484 // implementation note: the horizonal position is ignored currently, as no
485 // horizontal scrolling is done anyway during a selection
486 const QScrollBar
* scrollbar
= verticalScrollBar();
487 Q_ASSERT(scrollbar
!= 0);
489 const int maxHeight
= maximumViewportSize().height();
490 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
491 const int visibleHeight
= model()->rowCount() + 1 - height
;
492 if (visibleHeight
<= 0) {
496 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
500 QRect
DolphinDetailsView::elasticBandRect() const
502 const QPoint
pos(contentsPos());
503 const QPoint
topLeft(m_elasticBandOrigin
.x() - pos
.x(), m_elasticBandOrigin
.y() - pos
.y());
504 return QRect(topLeft
, m_elasticBandDestination
).normalized();
507 static bool isValidNameIndex(const QModelIndex
& index
)
509 return index
.isValid() && (index
.column() == KDirModel::Name
);
512 #include "dolphindetailsview.moc"