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