1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "dolphincolumnview.h"
22 #include "dolphincontroller.h"
23 #include "dolphinsettings.h"
25 #include "dolphin_columnmodesettings.h"
27 #include <kcolorutils.h>
28 #include <kcolorscheme.h>
29 #include <kdirlister.h>
30 #include <kdirmodel.h>
32 #include <QAbstractProxyModel>
33 #include <QApplication>
37 * General implementation notes
38 * ----------------------------
40 * In Qt4.3 the QColumnView widget has a default behavior regarding the
41 * active column and the selection handling, which leads to some usability
42 * problems within Dolphin:
44 * - No matter which mouse button has been clicked: If the mouse is above
45 * a folder, the folder content will be loaded in the next column. The problem
46 * is that this column also will marked as 'active column' within QColumnView,
47 * hence it is not possible to select more than one folder within a column.
49 * - The currently opened folder is not always marked in the left column when
50 * doing drag & drop and selections inside other columns.
52 * - The currently active column is visually not recognizable.
54 * - It is not possible for derived classes to remove inactive columns.
56 * DolphinView tries to bypass those points, but this required some workarounds:
58 * - QColumnView internally maps the selection model from the ColumnView to the
59 * active column. As the active column from the Dolphin perspective is different
60 * as the active column from QColumnView, the selection model is adjusted on
61 * each interaction by the methods QColumnWidget::obtainSelectionModel(),
62 * QColumnWidget::releaseSelectionModel() and QColumnView::requestSelectionModel().
63 * QColumnView offers no hook to adjust this behavior, so those methods have to
64 * be invoked throughout the code...
66 * - Some copy/paste code from QColumnView is part of DolphinColumnView::createColumn(), but Qt 4.4
67 * will offer a solution for this.
69 * - The mousePressEvent() has been customized to prevent that folders are loaded on each
72 * We'll try to give some input for Trolltech if the Dolphin solution is stable enough, so hopefully
73 * some workarounds can be removed when switching to Qt 4.4 or later.
77 * Represents one column inside the DolphinColumnView and has been
78 * extended to respect view options and hovering information.
80 class ColumnWidget
: public QListView
83 ColumnWidget(QWidget
* parent
,
84 DolphinColumnView
* columnView
,
86 virtual ~ColumnWidget();
88 /** Sets the size of the icons. */
89 void setDecorationSize(const QSize
& size
);
92 * An active column is defined as column, which shows the same URL
93 * as indicated by the URL navigator. The active column is usually
94 * drawn in a lighter color. All operations are applied to this column.
96 void setActive(bool active
);
97 inline bool isActive() const;
99 inline const KUrl
& url() const;
102 * Obtains the selection model from the column view. This assures that
103 * selections of the column view will always applied to the active column.
105 void obtainSelectionModel();
108 * Releases the selection model from the column view and replaces it by
109 * a custom selection model.
111 void releaseSelectionModel();
114 virtual QStyleOptionViewItem
viewOptions() const;
115 virtual void dragEnterEvent(QDragEnterEvent
* event
);
116 virtual void dragLeaveEvent(QDragLeaveEvent
* event
);
117 virtual void dragMoveEvent(QDragMoveEvent
* event
);
118 virtual void dropEvent(QDropEvent
* event
);
119 virtual void mousePressEvent(QMouseEvent
* event
);
120 virtual void mouseMoveEvent(QMouseEvent
* event
);
121 virtual void mouseReleaseEvent(QMouseEvent
* event
);
122 virtual void paintEvent(QPaintEvent
* event
);
123 virtual void contextMenuEvent(QContextMenuEvent
* event
);
126 virtual void selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
);
129 /** Used by ColumnWidget::setActive(). */
132 /** Used by ColumnWidget::setActive(). */
137 bool m_swallowMouseMoveEvents
;
138 DolphinColumnView
* m_view
;
140 KUrl m_childUrl
; // URL of the next column that is shown
141 QStyleOptionViewItem m_viewOptions
;
143 bool m_dragging
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
144 QRect m_dropRect
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
147 ColumnWidget::ColumnWidget(QWidget
* parent
,
148 DolphinColumnView
* columnView
,
152 m_swallowMouseMoveEvents(false),
159 setMouseTracking(true);
160 viewport()->setAttribute(Qt::WA_Hover
);
162 // apply the column mode settings to the widget
163 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
164 Q_ASSERT(settings
!= 0);
166 m_viewOptions
= QListView::viewOptions();
168 QFont
font(settings
->fontFamily(), settings
->fontSize());
169 font
.setItalic(settings
->italicFont());
170 font
.setBold(settings
->boldFont());
171 m_viewOptions
.font
= font
;
173 const int iconSize
= settings
->iconSize();
174 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
179 ColumnWidget::~ColumnWidget()
183 void ColumnWidget::setDecorationSize(const QSize
& size
)
185 m_viewOptions
.decorationSize
= size
;
189 void ColumnWidget::setActive(bool active
)
192 obtainSelectionModel();
194 releaseSelectionModel();
197 if (m_active
== active
) {
210 inline bool ColumnWidget::isActive() const
215 const KUrl
& ColumnWidget::url() const
220 void ColumnWidget::obtainSelectionModel()
222 if (selectionModel() != m_view
->selectionModel()) {
223 selectionModel()->deleteLater();
224 setSelectionModel(m_view
->selectionModel());
229 void ColumnWidget::releaseSelectionModel()
231 if (selectionModel() == m_view
->selectionModel()) {
232 QItemSelectionModel
* replacementModel
= new QItemSelectionModel(model());
233 setSelectionModel(replacementModel
);
237 QStyleOptionViewItem
ColumnWidget::viewOptions() const
239 return m_viewOptions
;
242 void ColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
244 if (event
->mimeData()->hasUrls()) {
245 event
->acceptProposedAction();
251 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
253 QListView::dragLeaveEvent(event
);
255 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
257 setDirtyRegion(m_dropRect
);
260 void ColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
262 QListView::dragMoveEvent(event
);
264 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
265 const QModelIndex index
= indexAt(event
->pos());
266 setDirtyRegion(m_dropRect
);
267 m_dropRect
= visualRect(index
);
268 setDirtyRegion(m_dropRect
);
271 void ColumnWidget::dropEvent(QDropEvent
* event
)
273 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
274 if (!urls
.isEmpty()) {
275 event
->acceptProposedAction();
276 m_view
->m_controller
->indicateDroppedUrls(urls
,
277 indexAt(event
->pos()),
280 QListView::dropEvent(event
);
284 void ColumnWidget::mousePressEvent(QMouseEvent
* event
)
286 // On each mouse press event QColumnView triggers the loading of the
287 // current folder in the next column. This is not wanted for Dolphin when
288 // opening a context menu or when the CTRL modifier is pressed. Beside usability
289 // aspects the loading of the folder also implies losing the current selection,
290 // which makes it impossible to select folders from the current column. To bypass
291 // this behavior QListView::mousePressEvent() is not invoked in those cases, which
292 // is not a nice solution. Maybe another solution can be found in future versions
295 m_view
->requestSelectionModel(this);
297 bool swallowMousePressEvent
= false;
298 const QModelIndex index
= indexAt(event
->pos());
299 if (index
.isValid()) {
300 // a click on an item has been done
301 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(m_view
->model());
302 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
303 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
304 KFileItem
* item
= dirModel
->itemForIndex(dirIndex
);
306 QItemSelectionModel
* selModel
= selectionModel();
308 bool activate
= true;
309 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
310 if (modifier
& Qt::ControlModifier
) {
311 m_view
->requestActivation(this);
312 if (!selModel
->hasSelection()) {
313 // Assure to set the current index, so that a selection by the SHIFT key
314 // will work. TODO: If the index specifies a folder, the loading of the folder will
315 // be triggered by QColumnView although this is not wanted by Dolphin.
316 selModel
->setCurrentIndex(index
, QItemSelectionModel::Select
);
318 selModel
->select(index
, QItemSelectionModel::Toggle
);
319 swallowMousePressEvent
= true;
320 } else if (item
->isDir()) {
321 m_childUrl
= item
->url();
322 viewport()->update();
324 // Only request the activation if not the left button is pressed.
325 // The left button on a directory opens a new column, hence requesting
326 // an activation is useless as the new column will request the activation
328 if (event
->button() == Qt::LeftButton
) {
334 m_view
->requestActivation(this);
337 // TODO: is the assumption OK that Qt::RightButton always represents the context menu button?
338 if (event
->button() == Qt::RightButton
) {
339 swallowMousePressEvent
= true;
340 if (!selModel
->isSelected(index
)) {
343 selModel
->select(index
, QItemSelectionModel::Select
);
347 // a click on the viewport has been done
348 m_view
->requestActivation(this);
350 // Swallow mouse move events if a click is done on the viewport. Otherwise the QColumnView
351 // triggers an unwanted loading of directories on hovering folder items.
352 m_swallowMouseMoveEvents
= true;
356 if (!swallowMousePressEvent
) {
357 QListView::mousePressEvent(event
);
361 void ColumnWidget::mouseMoveEvent(QMouseEvent
* event
)
363 // see description in ColumnView::mousePressEvent()
364 if (!m_swallowMouseMoveEvents
) {
365 QListView::mouseMoveEvent(event
);
369 void ColumnWidget::mouseReleaseEvent(QMouseEvent
* event
)
371 QListView::mouseReleaseEvent(event
);
372 m_swallowMouseMoveEvents
= false;
376 void ColumnWidget::paintEvent(QPaintEvent
* event
)
378 if (!m_childUrl
.isEmpty()) {
379 // indicate the shown URL of the next column by highlighting the shown folder item
380 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(m_view
->model());
381 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
382 const QModelIndex dirIndex
= dirModel
->indexForUrl(m_childUrl
);
383 const QModelIndex proxyIndex
= proxyModel
->mapFromSource(dirIndex
);
384 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
385 const QRect itemRect
= visualRect(proxyIndex
);
386 QPainter
painter(viewport());
389 QColor color
= KColorScheme(KColorScheme::View
).foreground();
391 painter
.setPen(Qt::NoPen
);
392 painter
.setBrush(color
);
393 painter
.drawRect(itemRect
);
399 QListView::paintEvent(event
);
401 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
403 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
404 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
408 void ColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
411 m_view
->requestActivation(this);
414 QListView::contextMenuEvent(event
);
416 const QModelIndex index
= indexAt(event
->pos());
417 if (index
.isValid() || m_active
) {
418 // Only open a context menu above an item or if the mouse is above
419 // the active column.
420 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
421 m_view
->m_controller
->triggerContextMenuRequest(pos
);
425 void ColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
427 // inactive views should not have any selection
431 QListView::selectionChanged(selected
, deselected
);
434 void ColumnWidget::activate()
436 const QColor bgColor
= KColorScheme(KColorScheme::View
).background();
437 QPalette palette
= viewport()->palette();
438 palette
.setColor(viewport()->backgroundRole(), bgColor
);
439 viewport()->setPalette(palette
);
444 void ColumnWidget::deactivate()
446 QColor bgColor
= KColorScheme(KColorScheme::View
).background();
447 const QColor fgColor
= KColorScheme(KColorScheme::View
).foreground();
448 bgColor
= KColorUtils::mix(bgColor
, fgColor
, 0.04);
450 QPalette palette
= viewport()->palette();
451 palette
.setColor(viewport()->backgroundRole(), bgColor
);
452 viewport()->setPalette(palette
);
459 DolphinColumnView::DolphinColumnView(QWidget
* parent
, DolphinController
* controller
) :
461 m_controller(controller
)
463 Q_ASSERT(controller
!= 0);
465 setAcceptDrops(true);
466 setDragDropMode(QAbstractItemView::DragDrop
);
467 setDropIndicatorShown(false);
468 setSelectionMode(ExtendedSelection
);
470 if (KGlobalSettings::singleClick()) {
471 connect(this, SIGNAL(clicked(const QModelIndex
&)),
472 this, SLOT(triggerItem(const QModelIndex
&)));
474 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
475 this, SLOT(triggerItem(const QModelIndex
&)));
477 connect(this, SIGNAL(entered(const QModelIndex
&)),
478 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
479 connect(this, SIGNAL(viewportEntered()),
480 controller
, SLOT(emitViewportEntered()));
481 connect(controller
, SIGNAL(zoomIn()),
482 this, SLOT(zoomIn()));
483 connect(controller
, SIGNAL(zoomOut()),
484 this, SLOT(zoomOut()));
485 connect(controller
, SIGNAL(urlChanged(const KUrl
&)),
486 this, SLOT(updateColumnsState(const KUrl
&)));
488 updateDecorationSize();
491 DolphinColumnView::~DolphinColumnView()
495 void DolphinColumnView::invertSelection()
497 selectActiveColumn(QItemSelectionModel::Toggle
);
500 void DolphinColumnView::selectAll()
502 selectActiveColumn(QItemSelectionModel::Select
);
505 QAbstractItemView
* DolphinColumnView::createColumn(const QModelIndex
& index
)
507 // let the column widget be aware about its URL...
509 if (viewport()->children().count() == 0) {
510 // For the first column widget the directory lister has not been started
511 // yet, hence use the URL from the controller instead.
512 columnUrl
= m_controller
->url();
514 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
515 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
517 const QModelIndex dirModelIndex
= proxyModel
->mapToSource(index
);
518 KFileItem
* fileItem
= dirModel
->itemForIndex(dirModelIndex
);
520 columnUrl
= fileItem
->url();
524 ColumnWidget
* view
= new ColumnWidget(viewport(), this, columnUrl
);
526 // The following code has been copied 1:1 from QColumnView::createColumn().
527 // Copyright (C) 1992-2007 Trolltech ASA. In Qt 4.4 the new method
528 // QColumnView::initializeColumn() will be available for this.
530 view
->setFrameShape(QFrame::NoFrame
);
531 view
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
532 view
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
533 view
->setMinimumWidth(100);
534 view
->setAttribute(Qt::WA_MacShowFocusRect
, false);
536 // copy the 'view' behavior
537 view
->setDragDropMode(dragDropMode());
538 view
->setDragDropOverwriteMode(dragDropOverwriteMode());
539 view
->setDropIndicatorShown(showDropIndicator());
540 view
->setAlternatingRowColors(alternatingRowColors());
541 view
->setAutoScroll(hasAutoScroll());
542 view
->setEditTriggers(editTriggers());
543 view
->setHorizontalScrollMode(horizontalScrollMode());
544 view
->setIconSize(iconSize());
545 view
->setSelectionBehavior(selectionBehavior());
546 view
->setSelectionMode(selectionMode());
547 view
->setTabKeyNavigation(tabKeyNavigation());
548 view
->setTextElideMode(textElideMode());
549 view
->setVerticalScrollMode(verticalScrollMode());
551 view
->setModel(model());
553 // set the delegate to be the columnview delegate
554 QAbstractItemDelegate
* delegate
= view
->itemDelegate();
555 view
->setItemDelegate(itemDelegate());
558 view
->setRootIndex(index
);
560 if (model()->canFetchMore(index
)) {
561 model()->fetchMore(index
);
567 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
569 m_controller
->triggerActivation();
570 QColumnView::mousePressEvent(event
);
573 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
575 if (event
->mimeData()->hasUrls()) {
576 event
->acceptProposedAction();
580 void DolphinColumnView::dropEvent(QDropEvent
* event
)
582 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
583 if (!urls
.isEmpty()) {
584 m_controller
->indicateDroppedUrls(urls
,
585 indexAt(event
->pos()),
587 event
->acceptProposedAction();
589 QColumnView::dropEvent(event
);
592 void DolphinColumnView::zoomIn()
594 if (isZoomInPossible()) {
595 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
596 // TODO: get rid of K3Icon sizes
597 switch (settings
->iconSize()) {
598 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
599 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
600 default: Q_ASSERT(false); break;
602 updateDecorationSize();
606 void DolphinColumnView::zoomOut()
608 if (isZoomOutPossible()) {
609 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
610 // TODO: get rid of K3Icon sizes
611 switch (settings
->iconSize()) {
612 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
613 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
614 default: Q_ASSERT(false); break;
616 updateDecorationSize();
620 void DolphinColumnView::triggerItem(const QModelIndex
& index
)
622 m_controller
->triggerItem(index
);
623 updateColumnsState(m_controller
->url());
626 void DolphinColumnView::updateColumnsState(const KUrl
& url
)
628 foreach (QObject
* object
, viewport()->children()) {
629 if (object
->inherits("QListView")) {
630 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
631 widget
->setActive(widget
->url() == url
);
637 void DolphinColumnView::updateDecorationSize()
639 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
640 const int iconSize
= settings
->iconSize();
642 foreach (QObject
* object
, viewport()->children()) {
643 if (object
->inherits("QListView")) {
644 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
645 widget
->setDecorationSize(QSize(iconSize
, iconSize
));
649 m_controller
->setZoomInPossible(isZoomInPossible());
650 m_controller
->setZoomOutPossible(isZoomOutPossible());
655 bool DolphinColumnView::isZoomInPossible() const
657 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
658 return settings
->iconSize() < K3Icon::SizeLarge
;
661 bool DolphinColumnView::isZoomOutPossible() const
663 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
664 return settings
->iconSize() > K3Icon::SizeSmall
;
667 void DolphinColumnView::requestActivation(QWidget
* column
)
669 foreach (QObject
* object
, viewport()->children()) {
670 if (object
->inherits("QListView")) {
671 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
672 const bool isActive
= (widget
== column
);
673 widget
->setActive(isActive
);
675 m_controller
->setUrl(widget
->url());
681 void DolphinColumnView::requestSelectionModel(QAbstractItemView
* view
)
683 foreach (QObject
* object
, viewport()->children()) {
684 if (object
->inherits("QListView")) {
685 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
686 if (widget
== view
) {
687 widget
->obtainSelectionModel();
689 widget
->releaseSelectionModel();
695 void DolphinColumnView::selectActiveColumn(QItemSelectionModel::SelectionFlags flags
)
697 // TODO: this approach of selecting the active column is very slow. It should be
698 // possible to speedup the implementation by using QItemSelection, but all adempts
699 // have failed yet...
701 // assure that the selection model of the active column is set properly, otherwise
702 // no visual update of the selections is done
703 const KUrl
& activeUrl
= m_controller
->url();
704 foreach (QObject
* object
, viewport()->children()) {
705 if (object
->inherits("QListView")) {
706 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
707 if (widget
->url() == activeUrl
) {
708 widget
->obtainSelectionModel();
710 widget
->releaseSelectionModel();
715 QItemSelectionModel
* selModel
= selectionModel();
717 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
718 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
719 KDirLister
* dirLister
= dirModel
->dirLister();
721 const KFileItemList list
= dirLister
->itemsForDir(activeUrl
);
722 foreach (KFileItem
* item
, list
) {
723 const QModelIndex index
= dirModel
->indexForUrl(item
->url());
724 selModel
->select(proxyModel
->mapFromSource(index
), flags
);
728 #include "dolphincolumnview.moc"