]> cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewsidebarpage.cpp
* If one item is selected and the item is (at least partly) visible, assure that...
[dolphin.git] / src / treeviewsidebarpage.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
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 "treeviewsidebarpage.h"
21
22 #include "dolphinmodel.h"
23 #include "dolphinsortfilterproxymodel.h"
24 #include "dolphinview.h"
25 #include "dolphinsettings.h"
26 #include "dolphin_folderspanelsettings.h"
27 #include "sidebartreeview.h"
28 #include "treeviewcontextmenu.h"
29
30 #include <kfileplacesmodel.h>
31 #include <kdirlister.h>
32 #include <kfileitem.h>
33
34 #include <QItemSelection>
35 #include <QTreeView>
36 #include <QBoxLayout>
37 #include <QModelIndex>
38 #include <QScrollBar>
39 #include <QTimer>
40
41 TreeViewSidebarPage::TreeViewSidebarPage(QWidget* parent) :
42 SidebarPage(parent),
43 m_setLeafVisible(false),
44 m_dirLister(0),
45 m_dolphinModel(0),
46 m_proxyModel(0),
47 m_treeView(0),
48 m_leafDir()
49 {
50 }
51
52 TreeViewSidebarPage::~TreeViewSidebarPage()
53 {
54 FoldersPanelSettings::self()->writeConfig();
55
56 delete m_proxyModel;
57 m_proxyModel = 0;
58 delete m_dolphinModel;
59 m_dolphinModel = 0;
60 m_dirLister = 0; // deleted by m_dolphinModel
61 }
62
63 QSize TreeViewSidebarPage::sizeHint() const
64 {
65 return QSize(200, 400);
66 }
67
68 void TreeViewSidebarPage::setShowHiddenFiles(bool show)
69 {
70 FoldersPanelSettings::setShowHiddenFiles(show);
71 if (m_dirLister != 0) {
72 m_dirLister->setShowingDotFiles(show);
73 m_dirLister->openUrl(m_dirLister->url(), KDirLister::Reload);
74 }
75 }
76
77 bool TreeViewSidebarPage::showHiddenFiles() const
78 {
79 return FoldersPanelSettings::showHiddenFiles();
80 }
81
82 void TreeViewSidebarPage::setUrl(const KUrl& url)
83 {
84 if (!url.isValid() || (url == SidebarPage::url())) {
85 return;
86 }
87
88 SidebarPage::setUrl(url);
89 if (m_dirLister != 0) {
90 m_setLeafVisible = true;
91 loadTree(url);
92 }
93 }
94
95 void TreeViewSidebarPage::showEvent(QShowEvent* event)
96 {
97 if (event->spontaneous()) {
98 SidebarPage::showEvent(event);
99 return;
100 }
101
102 if (m_dirLister == 0) {
103 // Postpone the creating of the dir lister to the first show event.
104 // This assures that no performance and memory overhead is given when the TreeView is not
105 // used at all (see TreeViewSidebarPage::setUrl()).
106 m_dirLister = new KDirLister();
107 m_dirLister->setDirOnlyMode(true);
108 m_dirLister->setAutoUpdate(true);
109 m_dirLister->setMainWindow(window());
110 m_dirLister->setDelayedMimeTypes(true);
111 m_dirLister->setAutoErrorHandlingEnabled(false, this);
112 m_dirLister->setShowingDotFiles(FoldersPanelSettings::showHiddenFiles());
113
114 connect(m_dirLister, SIGNAL(completed()),
115 this, SLOT(triggerLoadSubTree()));
116
117 Q_ASSERT(m_dolphinModel == 0);
118 m_dolphinModel = new DolphinModel(this);
119 m_dolphinModel->setDirLister(m_dirLister);
120 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
121 connect(m_dolphinModel, SIGNAL(expand(const QModelIndex&)),
122 this, SLOT(triggerExpanding()));
123
124 Q_ASSERT(m_proxyModel == 0);
125 m_proxyModel = new DolphinSortFilterProxyModel(this);
126 m_proxyModel->setSourceModel(m_dolphinModel);
127
128 Q_ASSERT(m_treeView == 0);
129 m_treeView = new SidebarTreeView(this);
130 m_treeView->setModel(m_proxyModel);
131 m_proxyModel->setSorting(DolphinView::SortByName);
132 m_proxyModel->setSortOrder(Qt::AscendingOrder);
133
134 connect(m_treeView, SIGNAL(clicked(const QModelIndex&)),
135 this, SLOT(updateActiveView(const QModelIndex&)));
136 connect(m_treeView, SIGNAL(urlsDropped(const KUrl::List&, const QModelIndex&)),
137 this, SLOT(dropUrls(const KUrl::List&, const QModelIndex&)));
138
139 QVBoxLayout* layout = new QVBoxLayout(this);
140 layout->setMargin(0);
141 layout->addWidget(m_treeView);
142 }
143
144 loadTree(url());
145 SidebarPage::showEvent(event);
146 }
147
148 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent* event)
149 {
150 SidebarPage::contextMenuEvent(event);
151
152 KFileItem item;
153 const QModelIndex index = m_treeView->indexAt(event->pos());
154 if (index.isValid()) {
155 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
156 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
157 emit changeSelection(KFileItemList());
158 }
159
160 TreeViewContextMenu contextMenu(this, item);
161 contextMenu.open();
162 }
163
164 void TreeViewSidebarPage::updateActiveView(const QModelIndex& index)
165 {
166 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
167 const KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
168 if (!item.isNull()) {
169 emit changeUrl(item.url());
170 }
171 }
172
173 void TreeViewSidebarPage::dropUrls(const KUrl::List& urls,
174 const QModelIndex& index)
175 {
176 if (index.isValid()) {
177 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
178 KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
179 Q_ASSERT(!item.isNull());
180 if (item.isDir()) {
181 emit urlsDropped(urls, item.url());
182 }
183 }
184 }
185
186 void TreeViewSidebarPage::triggerExpanding()
187 {
188 // the expanding of the folders may not be done in the context
189 // of this slot
190 QMetaObject::invokeMethod(this, "expandToLeafDir", Qt::QueuedConnection);
191 }
192
193 void TreeViewSidebarPage::triggerLoadSubTree()
194 {
195 // the loading of the sub tree may not be done in the context
196 // of this slot
197 QMetaObject::invokeMethod(this, "loadSubTree", Qt::QueuedConnection);
198 }
199
200 void TreeViewSidebarPage::expandToLeafDir()
201 {
202 // expand all directories until the parent directory of m_leafDir
203 const KUrl parentUrl = m_leafDir.upUrl();
204 QModelIndex dirIndex = m_dolphinModel->indexForUrl(parentUrl);
205 QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
206 m_treeView->setExpanded(proxyIndex, true);
207 selectLeafDirectory();
208 }
209
210
211 void TreeViewSidebarPage::loadSubTree()
212 {
213 if (m_leafDir.isParentOf(m_dirLister->url())) {
214 // The leaf directory is not a child of the base URL, hence
215 // no sub directory must be loaded or selected.
216 return;
217 }
218
219 const QModelIndex index = m_dolphinModel->indexForUrl(m_leafDir);
220 if (index.isValid()) {
221 selectLeafDirectory();
222 } else {
223 // Load all sub directories that need to get expanded for making
224 // the leaf directory visible. The slot triggerExpanding() will
225 // get invoked if the expanding has been finished.
226 m_dolphinModel->expandToUrl(m_leafDir);
227 }
228 }
229
230 void TreeViewSidebarPage::scrollToLeaf()
231 {
232 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
233 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
234 if (proxyIndex.isValid()) {
235 m_treeView->scrollTo(proxyIndex);
236 }
237 }
238
239 void TreeViewSidebarPage::loadTree(const KUrl& url)
240 {
241 Q_ASSERT(m_dirLister != 0);
242 m_leafDir = url;
243
244 KUrl baseUrl = url;
245 if (url.isLocalFile()) {
246 // use the root directory as base for local URLs
247 baseUrl = QDir::rootPath();
248 } else {
249 // clear the path for non-local URLs and use it as base
250 baseUrl = url;
251 baseUrl.setPath(QString());
252 }
253
254 if (m_dirLister->url() != baseUrl) {
255 m_dirLister->stop();
256 m_dirLister->openUrl(baseUrl, KDirLister::Reload);
257 } else {
258 loadSubTree();
259 }
260 }
261
262 void TreeViewSidebarPage::selectLeafDirectory()
263 {
264 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
265 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
266 if (!proxyIndex.isValid()) {
267 return;
268 }
269
270 if (m_setLeafVisible) {
271 // Invoke m_treeView->scrollTo(proxyIndex) asynchronously by
272 // scrollToLeaf(). This assures that the scrolling is done after
273 // the horizontal scrollbar gets visible (otherwise the scrollbar
274 // might hide the leaf).
275 QTimer::singleShot(100, this, SLOT(scrollToLeaf()));
276 m_setLeafVisible = false;
277 }
278
279 QItemSelectionModel* selModel = m_treeView->selectionModel();
280 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
281 }
282
283 #include "treeviewsidebarpage.moc"