1 /***************************************************************************
2 * Copyright (C) 2008 by Simon St James <kdedevel@etotheipiplusone.com> *
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 "folderexpander.h"
21 #include "dolphinview.h"
23 #include "dolphinsettings.h"
24 #include "dolphin_generalsettings.h"
26 #include <QtCore/QTimer>
27 #include <QtGui/QAbstractItemView>
28 #include <QtGui/QTreeView>
29 #include <QtGui/QScrollBar>
31 #include <QtCore/QEvent>
32 #include <QtGui/QDragMoveEvent>
34 #include <QtGui/QSortFilterProxyModel>
36 #include <kdirmodel.h>
39 FolderExpander::FolderExpander(QAbstractItemView
*view
, QSortFilterProxyModel
*proxyModel
) :
43 m_proxyModel(proxyModel
),
44 m_autoExpandTriggerTimer(0),
47 // Validation. If these fail, the event filter is never
48 // installed on the view and the FolderExpander is inactive.
50 kWarning() << "Need a view!";
53 if (m_proxyModel
== 0) {
54 kWarning() << "Need a proxyModel!";
57 KDirModel
*m_dirModel
= qobject_cast
< KDirModel
* >( m_proxyModel
->sourceModel() );
58 if (m_dirModel
== 0) {
59 kWarning() << "Expected m_proxyModel's sourceModel() to be a KDirModel!";
63 // Initialise auto-expand timer.
64 m_autoExpandTriggerTimer
= new QTimer(this);
65 m_autoExpandTriggerTimer
->setSingleShot(true);
66 connect(m_autoExpandTriggerTimer
, SIGNAL(timeout()),
67 this, SLOT(autoExpandTimeout()));
69 // The view scrolling complicates matters, so we want to
70 // be informed if they occur.
71 connect(m_view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
72 this, SLOT(viewScrolled()));
73 connect(m_view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
74 this, SLOT(viewScrolled()));
76 // "Dragging" events are sent to the QAbstractItemView's viewport.
77 m_view
->viewport()->installEventFilter(this);
80 void FolderExpander::setEnabled(bool enabled
)
85 bool FolderExpander::enabled() const
90 FolderExpander::~FolderExpander()
94 void FolderExpander::viewScrolled()
96 if (m_autoExpandTriggerTimer
->isActive()) {
97 kDebug() << "Resetting time due to scrolling!";
98 // (Re-)set the timer while we're scrolling the view
99 // (or it's being scrolled by some external mechanism).
100 // TODO - experiment with this. Cancelling the timer,
101 // or adding a "penalty" on top of AUTO_EXPAND_DELAY
102 // might work more nicely when drilling down through the sidebar
104 m_autoExpandTriggerTimer
->start(AUTO_EXPAND_DELAY
);
108 void FolderExpander::autoExpandTimeout()
114 // We want to find whether the file currently being hovered over is a
115 // directory. TODO - is there a simpler way, preferably without
116 // needing to pass in m_proxyModel that has a KDirModel as its sourceModel() ... ?
117 QModelIndex proxyIndexToExpand
= m_view
->indexAt(m_autoExpandPos
);
118 QModelIndex indexToExpand
= m_proxyModel
->mapToSource(proxyIndexToExpand
);
119 KDirModel
* m_dirModel
= qobject_cast
< KDirModel
* >(m_proxyModel
->sourceModel());
120 Q_ASSERT(m_dirModel
!= 0);
121 KFileItem itemToExpand
= m_dirModel
->itemForIndex(indexToExpand
);
123 if (itemToExpand
.isNull()) {
127 kDebug() << "Need to expand: " << itemToExpand
.targetUrl() << " isDir? = " << itemToExpand
.isDir();
129 if (itemToExpand
.isDir()) {
130 QTreeView
*viewAsTreeView
= qobject_cast
<QTreeView
*>(m_view
);
131 if (viewAsTreeView
!= 0) {
132 // Toggle expanded state of this directory.
133 viewAsTreeView
->setExpanded(proxyIndexToExpand
, !viewAsTreeView
->isExpanded(proxyIndexToExpand
));
136 // Enter this directory.
137 emit
enterDir(proxyIndexToExpand
);
142 bool FolderExpander::eventFilter(QObject
* watched
, QEvent
* event
)
145 // We're interested in reading Drag* events, but not filtering them,
146 // so always return false.
147 // We just store the position of the hover, here; actually working out
148 // what the hovered item is and whether it is expandable is done in
149 // autoExpandTimeout.
150 if (event
->type() == QEvent::DragMove
) {
151 QDragMoveEvent
*dragMoveEvent
= static_cast<QDragMoveEvent
*>(event
);
152 // (Re-)set the timer while we're still moving and dragging.
153 m_autoExpandTriggerTimer
->start(AUTO_EXPAND_DELAY
);
154 m_autoExpandPos
= dragMoveEvent
->pos();
155 } else if (event
->type() == QEvent::DragLeave
|| event
->type() == QEvent::Drop
) {
156 m_autoExpandTriggerTimer
->stop();