]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/folderexpander.cpp
Disallow renaming to names containing '/', names being equal to "." or "..".
[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 <QTimer>
23 #include <QAbstractItemView>
24 #include <QTreeView>
25 #include <QScrollBar>
26
27 #include <QEvent>
28 #include <QDragMoveEvent>
29
30 #include <QSortFilterProxyModel>
31
32 #include <KDirModel>
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 || !m_proxyModel) {
43 return;
44 }
45 KDirModel *m_dirModel = qobject_cast<KDirModel*>(m_proxyModel->sourceModel());
46 if (!m_dirModel) {
47 return;
48 }
49
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()));
55
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()));
62
63 // "Dragging" events are sent to the QAbstractItemView's viewport.
64 m_view->viewport()->installEventFilter(this);
65 }
66
67 void FolderExpander::setEnabled(bool enabled)
68 {
69 m_enabled = enabled;
70 }
71
72 bool FolderExpander::enabled() const
73 {
74 return m_enabled;
75 }
76
77 FolderExpander::~FolderExpander()
78 {
79 }
80
81 void FolderExpander::viewScrolled()
82 {
83 if (m_autoExpandTriggerTimer->isActive()) {
84 m_autoExpandTriggerTimer->start(AUTO_EXPAND_DELAY);
85 }
86 }
87
88 void FolderExpander::autoExpandTimeout()
89 {
90 if (!m_enabled) {
91 return;
92 }
93
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);
102
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).
106 return;
107 }
108
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));
114 }
115 else {
116 emit enterDir(proxyIndexToExpand);
117 }
118 }
119 }
120
121 bool FolderExpander::eventFilter(QObject* watched, QEvent* event)
122 {
123 Q_UNUSED(watched);
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();
136 }
137 return false;
138 }