]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/folderexpander.cpp
a2dfb137b29a2bfdc4c4ab45f7c2f5b807408e94
[dolphin.git] / src / views / 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
22 #include <QtCore/QTimer>
23 #include <QtGui/QAbstractItemView>
24 #include <QtGui/QTreeView>
25 #include <QtGui/QScrollBar>
26
27 #include <QtCore/QEvent>
28 #include <QtGui/QDragMoveEvent>
29
30 #include <QtGui/QSortFilterProxyModel>
31
32 #include <kdirmodel.h>
33
34 FolderExpander::FolderExpander(QAbstractItemView *view, QSortFilterProxyModel *proxyModel) :
35 QObject(view),
36 m_enabled(true),
37 m_view(view),
38 m_proxyModel(proxyModel),
39 m_autoExpandTriggerTimer(0),
40 m_autoExpandPos()
41 {
42 if (m_view == 0) {
43 return;
44 }
45 if (m_proxyModel == 0) {
46 return;
47 }
48 KDirModel *m_dirModel = qobject_cast<KDirModel*>(m_proxyModel->sourceModel());
49 if (m_dirModel == 0) {
50 return;
51 }
52
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()));
58
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()));
65
66 // "Dragging" events are sent to the QAbstractItemView's viewport.
67 m_view->viewport()->installEventFilter(this);
68 }
69
70 void FolderExpander::setEnabled(bool enabled)
71 {
72 m_enabled = enabled;
73 }
74
75 bool FolderExpander::enabled() const
76 {
77 return m_enabled;
78 }
79
80 FolderExpander::~FolderExpander()
81 {
82 }
83
84 void FolderExpander::viewScrolled()
85 {
86 if (m_autoExpandTriggerTimer->isActive()) {
87 m_autoExpandTriggerTimer->start(AUTO_EXPAND_DELAY);
88 }
89 }
90
91 void FolderExpander::autoExpandTimeout()
92 {
93 if (!m_enabled) {
94 return;
95 }
96
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);
105
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).
109 return;
110 }
111
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));
117 }
118 else {
119 emit enterDir(proxyIndexToExpand);
120 }
121 }
122 }
123
124 bool FolderExpander::eventFilter(QObject* watched, QEvent* event)
125 {
126 Q_UNUSED(watched);
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();
139 }
140 return false;
141 }