]> cloud.milkyroute.net Git - dolphin.git/blob - src/folderexpander.cpp
- Fix crash found while investigating https://bugs.kde.org/show_bug.cgi?id=170927
[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 #include <kdebug.h>
38
39 FolderExpander::FolderExpander(QAbstractItemView *view, QSortFilterProxyModel *proxyModel) :
40 QObject(view),
41 m_enabled(true),
42 m_view(view),
43 m_proxyModel(proxyModel),
44 m_autoExpandTriggerTimer(0),
45 m_autoExpandPos()
46 {
47 // Validation. If these fail, the event filter is never
48 // installed on the view and the FolderExpander is inactive.
49 if (m_view == 0) {
50 kWarning() << "Need a view!";
51 return; // Not valid.
52 }
53 if (m_proxyModel == 0) {
54 kWarning() << "Need a proxyModel!";
55 return; // Not valid.
56 }
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!";
60 return; // Not valid.
61 }
62
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()));
68
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()));
75
76 // "Dragging" events are sent to the QAbstractItemView's viewport.
77 m_view->viewport()->installEventFilter(this);
78 }
79
80 void FolderExpander::setEnabled(bool enabled)
81 {
82 m_enabled = enabled;
83 }
84
85 bool FolderExpander::enabled() const
86 {
87 return m_enabled;
88 }
89
90 FolderExpander::~FolderExpander()
91 {
92 }
93
94 void FolderExpander::viewScrolled()
95 {
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
103 // tree.
104 m_autoExpandTriggerTimer->start(AUTO_EXPAND_DELAY);
105 }
106 }
107
108 void FolderExpander::autoExpandTimeout()
109 {
110 if (!m_enabled) {
111 return;
112 }
113
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 );
122
123 if (itemToExpand.isNull())
124 return;
125
126 kDebug() << "Need to expand: " << itemToExpand.targetUrl() << " isDir? = " << itemToExpand.isDir();
127
128 if (itemToExpand.isDir()) {
129 QTreeView *viewAsTreeView = qobject_cast<QTreeView*>(m_view);
130 if (viewAsTreeView != 0) {
131 // Toggle expanded state of this directory.
132 viewAsTreeView->setExpanded(proxyIndexToExpand, !viewAsTreeView->isExpanded(proxyIndexToExpand));
133 }
134 else {
135 // Enter this directory.
136 emit enterDir(proxyIndexToExpand);
137 }
138 }
139 }
140
141 bool FolderExpander::eventFilter(QObject* watched, QEvent* event)
142 {
143 Q_UNUSED(watched);
144 // We're interested in reading Drag* events, but not filtering them,
145 // so always return false.
146 // We just store the position of the hover, here; actually working out
147 // what the hovered item is and whether it is expandable is done in
148 // autoExpandTimeout.
149 if (event->type() == QEvent::DragMove) {
150 QDragMoveEvent *dragMoveEvent = static_cast<QDragMoveEvent*>(event);
151 // (Re-)set the timer while we're still moving and dragging.
152 m_autoExpandTriggerTimer->start(AUTO_EXPAND_DELAY);
153 m_autoExpandPos = dragMoveEvent->pos();
154 } else if (event->type() == QEvent::DragLeave || event->type() == QEvent::Drop) {
155 m_autoExpandTriggerTimer->stop();
156 }
157 return false;
158 }