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"
22 #include <QtCore/QTimer>
23 #include <QtGui/QAbstractItemView>
24 #include <QtGui/QTreeView>
25 #include <QtGui/QScrollBar>
27 #include <QtCore/QEvent>
28 #include <QtGui/QDragMoveEvent>
30 #include <QtGui/QSortFilterProxyModel>
32 #include <kdirmodel.h>
34 FolderExpander::FolderExpander(QAbstractItemView
*view
, QSortFilterProxyModel
*proxyModel
) :
38 m_proxyModel(proxyModel
),
39 m_autoExpandTriggerTimer(0),
45 if (m_proxyModel
== 0) {
48 KDirModel
*m_dirModel
= qobject_cast
<KDirModel
*>(m_proxyModel
->sourceModel());
49 if (m_dirModel
== 0) {
53 // Initialise auto-expand timer.
54 m_autoExpandTriggerTimer
= new QTimer(this);
55 m_autoExpandTriggerTimer
->setSingleShot(true);
56 connect(m_autoExpandTriggerTimer
, SIGNAL(timeout()),
57 this, SLOT(autoExpandTimeout()));
59 // The view scrolling complicates matters, so we want to
60 // be informed if they occur.
61 connect(m_view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
62 this, SLOT(viewScrolled()));
63 connect(m_view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
64 this, SLOT(viewScrolled()));
66 // "Dragging" events are sent to the QAbstractItemView's viewport.
67 m_view
->viewport()->installEventFilter(this);
70 void FolderExpander::setEnabled(bool enabled
)
75 bool FolderExpander::enabled() const
80 FolderExpander::~FolderExpander()
84 void FolderExpander::viewScrolled()
86 if (m_autoExpandTriggerTimer
->isActive()) {
87 m_autoExpandTriggerTimer
->start(AUTO_EXPAND_DELAY
);
91 void FolderExpander::autoExpandTimeout()
97 // We want to find whether the file currently being hovered over is a
98 // directory. TODO - is there a simpler way, preferably without
99 // needing to pass in m_proxyModel that has a KDirModel as its sourceModel() ... ?
100 QModelIndex proxyIndexToExpand
= m_view
->indexAt(m_autoExpandPos
);
101 QModelIndex indexToExpand
= m_proxyModel
->mapToSource(proxyIndexToExpand
);
102 KDirModel
* m_dirModel
= qobject_cast
< KDirModel
* >(m_proxyModel
->sourceModel());
103 Q_ASSERT(m_dirModel
!= 0);
104 KFileItem itemToExpand
= m_dirModel
->itemForIndex(indexToExpand
);
106 if (itemToExpand
.isNull() || itemToExpand
== m_dirModel
->itemForIndex(QModelIndex())) {
107 // The second clause occurs when we are expanding the folder represented
108 // by the view, which is a case we should ignore (#182618).
112 if (itemToExpand
.isDir()) {
113 QTreeView
* treeView
= qobject_cast
<QTreeView
*>(m_view
);
114 if ((treeView
!= 0) && treeView
->itemsExpandable()) {
115 // Toggle expanded state of this directory.
116 treeView
->setExpanded(proxyIndexToExpand
, !treeView
->isExpanded(proxyIndexToExpand
));
119 emit
enterDir(proxyIndexToExpand
);
124 bool FolderExpander::eventFilter(QObject
* watched
, QEvent
* event
)
127 // We're interested in reading Drag* events, but not filtering them,
128 // so always return false.
129 // We just store the position of the hover, here; actually working out
130 // what the hovered item is and whether it is expandable is done in
131 // autoExpandTimeout.
132 if (event
->type() == QEvent::DragMove
) {
133 QDragMoveEvent
*dragMoveEvent
= static_cast<QDragMoveEvent
*>(event
);
134 // (Re-)set the timer while we're still moving and dragging.
135 m_autoExpandTriggerTimer
->start(AUTO_EXPAND_DELAY
);
136 m_autoExpandPos
= dragMoveEvent
->pos();
137 } else if (event
->type() == QEvent::DragLeave
|| event
->type() == QEvent::Drop
) {
138 m_autoExpandTriggerTimer
->stop();