]> cloud.milkyroute.net Git - dolphin.git/blob - src/folderexpander.cpp
Only expand the tree view if it has an enabled 'itemsExpandable' property. If this...
[dolphin.git] / src / folderexpander.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by Simon St James <kdedevel@etotheipiplusone.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "folderexpander.h"
21 #include "dolphinview.h"
22
23 #include "dolphinsettings.h"
24 #include "dolphin_generalsettings.h"
25
26 #include <QtCore/QTimer>
27 #include <QtGui/QAbstractItemView>
28 #include <QtGui/QTreeView>
29 #include <QtGui/QScrollBar>
30
31 #include <QtCore/QEvent>
32 #include <QtGui/QDragMoveEvent>
33
34 #include <QtGui/QSortFilterProxyModel>
35
36 #include <kdirmodel.h>
37
38 FolderExpander::FolderExpander(QAbstractItemView *view, QSortFilterProxyModel *proxyModel) :
39 QObject(view),
40 m_enabled(true),
41 m_view(view),
42 m_proxyModel(proxyModel),
43 m_autoExpandTriggerTimer(0),
44 m_autoExpandPos()
45 {
46 // Validation. If these fail, the event filter is never
47 // installed on the view and the FolderExpander is inactive.
48 if (m_view == 0) {
49 kWarning() << "Need a view!";
50 return; // Not valid.
51 }
52 if (m_proxyModel == 0) {
53 kWarning() << "Need a proxyModel!";
54 return; // Not valid.
55 }
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!";
59 return; // Not valid.
60 }
61
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()));
67
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()));
74
75 // "Dragging" events are sent to the QAbstractItemView's viewport.
76 m_view->viewport()->installEventFilter(this);
77 }
78
79 void FolderExpander::setEnabled(bool enabled)
80 {
81 m_enabled = enabled;
82 }
83
84 bool FolderExpander::enabled() const
85 {
86 return m_enabled;
87 }
88
89 FolderExpander::~FolderExpander()
90 {
91 }
92
93 void FolderExpander::viewScrolled()
94 {
95 if (m_autoExpandTriggerTimer->isActive()) {
96 m_autoExpandTriggerTimer->start(AUTO_EXPAND_DELAY);
97 }
98 }
99
100 void FolderExpander::autoExpandTimeout()
101 {
102 if (!m_enabled) {
103 return;
104 }
105
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);
114
115 if (itemToExpand.isNull()) {
116 return;
117 }
118
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));
124 }
125 else {
126 emit enterDir(proxyIndexToExpand, m_view);
127 }
128 }
129 }
130
131 bool FolderExpander::eventFilter(QObject* watched, QEvent* event)
132 {
133 Q_UNUSED(watched);
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();
146 }
147 return false;
148 }