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 "settings/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>
38 FolderExpander::FolderExpander(QAbstractItemView
*view
, QSortFilterProxyModel
*proxyModel
) :
42 m_proxyModel(proxyModel
),
43 m_autoExpandTriggerTimer(0),
46 // Validation. If these fail, the event filter is never
47 // installed on the view and the FolderExpander is inactive.
49 kWarning() << "Need a view!";
52 if (m_proxyModel
== 0) {
53 kWarning() << "Need a proxyModel!";
56 KDirModel
*m_dirModel
= qobject_cast
< KDirModel
* >( m_proxyModel
->sourceModel() );
57 if (m_dirModel
== 0) {
58 kWarning() << "Expected m_proxyModel's sourceModel() to be a KDirModel!";
62 // Initialise auto-expand timer.
63 m_autoExpandTriggerTimer
= new QTimer(this);
64 m_autoExpandTriggerTimer
->setSingleShot(true);
65 connect(m_autoExpandTriggerTimer
, SIGNAL(timeout()),
66 this, SLOT(autoExpandTimeout()));
68 // The view scrolling complicates matters, so we want to
69 // be informed if they occur.
70 connect(m_view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
71 this, SLOT(viewScrolled()));
72 connect(m_view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
73 this, SLOT(viewScrolled()));
75 // "Dragging" events are sent to the QAbstractItemView's viewport.
76 m_view
->viewport()->installEventFilter(this);
79 void FolderExpander::setEnabled(bool enabled
)
84 bool FolderExpander::enabled() const
89 FolderExpander::~FolderExpander()
93 void FolderExpander::viewScrolled()
95 if (m_autoExpandTriggerTimer
->isActive()) {
96 m_autoExpandTriggerTimer
->start(AUTO_EXPAND_DELAY
);
100 void FolderExpander::autoExpandTimeout()
106 // We want to find whether the file currently being hovered over is a
107 // directory. TODO - is there a simpler way, preferably without
108 // needing to pass in m_proxyModel that has a KDirModel as its sourceModel() ... ?
109 QModelIndex proxyIndexToExpand
= m_view
->indexAt(m_autoExpandPos
);
110 QModelIndex indexToExpand
= m_proxyModel
->mapToSource(proxyIndexToExpand
);
111 KDirModel
* m_dirModel
= qobject_cast
< KDirModel
* >(m_proxyModel
->sourceModel());
112 Q_ASSERT(m_dirModel
!= 0);
113 KFileItem itemToExpand
= m_dirModel
->itemForIndex(indexToExpand
);
115 if (itemToExpand
.isNull()) {
119 if (itemToExpand
.isDir()) {
120 QTreeView
* treeView
= qobject_cast
<QTreeView
*>(m_view
);
121 if ((treeView
!= 0) && treeView
->itemsExpandable()) {
122 // Toggle expanded state of this directory.
123 treeView
->setExpanded(proxyIndexToExpand
, !treeView
->isExpanded(proxyIndexToExpand
));
126 emit
enterDir(proxyIndexToExpand
, m_view
);
131 bool FolderExpander::eventFilter(QObject
* watched
, QEvent
* event
)
134 // We're interested in reading Drag* events, but not filtering them,
135 // so always return false.
136 // We just store the position of the hover, here; actually working out
137 // what the hovered item is and whether it is expandable is done in
138 // autoExpandTimeout.
139 if (event
->type() == QEvent::DragMove
) {
140 QDragMoveEvent
*dragMoveEvent
= static_cast<QDragMoveEvent
*>(event
);
141 // (Re-)set the timer while we're still moving and dragging.
142 m_autoExpandTriggerTimer
->start(AUTO_EXPAND_DELAY
);
143 m_autoExpandPos
= dragMoveEvent
->pos();
144 } else if (event
->type() == QEvent::DragLeave
|| event
->type() == QEvent::Drop
) {
145 m_autoExpandTriggerTimer
->stop();