1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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 ***************************************************************************/
20 #include "treeviewsidebarpage.h"
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"
30 #include <kfileplacesmodel.h>
31 #include <kdirlister.h>
32 #include <kfileitem.h>
34 #include <QItemSelection>
37 #include <QModelIndex>
41 TreeViewSidebarPage::TreeViewSidebarPage(QWidget
* parent
) :
43 m_setLeafVisible(false),
53 TreeViewSidebarPage::~TreeViewSidebarPage()
55 FoldersPanelSettings::self()->writeConfig();
59 delete m_dolphinModel
;
61 m_dirLister
= 0; // deleted by m_dolphinModel
64 QSize
TreeViewSidebarPage::sizeHint() const
66 return QSize(200, 400);
69 void TreeViewSidebarPage::setShowHiddenFiles(bool show
)
71 FoldersPanelSettings::setShowHiddenFiles(show
);
72 if (m_dirLister
!= 0) {
73 m_dirLister
->setShowingDotFiles(show
);
74 m_dirLister
->openUrl(m_dirLister
->url(), KDirLister::Reload
);
78 bool TreeViewSidebarPage::showHiddenFiles() const
80 return FoldersPanelSettings::showHiddenFiles();
83 void TreeViewSidebarPage::setUrl(const KUrl
& url
)
85 if (!url
.isValid() || (url
== SidebarPage::url())) {
89 SidebarPage::setUrl(url
);
90 if (m_dirLister
!= 0) {
91 m_setLeafVisible
= true;
96 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
98 if (event
->spontaneous()) {
99 SidebarPage::showEvent(event
);
103 if (m_dirLister
== 0) {
104 // Postpone the creating of the dir lister to the first show event.
105 // This assures that no performance and memory overhead is given when the TreeView is not
106 // used at all (see TreeViewSidebarPage::setUrl()).
107 m_dirLister
= new KDirLister();
108 m_dirLister
->setDirOnlyMode(true);
109 m_dirLister
->setAutoUpdate(true);
110 m_dirLister
->setMainWindow(window());
111 m_dirLister
->setDelayedMimeTypes(true);
112 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
113 m_dirLister
->setShowingDotFiles(FoldersPanelSettings::showHiddenFiles());
115 connect(m_dirLister
, SIGNAL(completed()),
116 this, SLOT(triggerLoadSubTree()));
118 Q_ASSERT(m_dolphinModel
== 0);
119 m_dolphinModel
= new DolphinModel(this);
120 m_dolphinModel
->setDirLister(m_dirLister
);
121 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
122 connect(m_dolphinModel
, SIGNAL(expand(const QModelIndex
&)),
123 this, SLOT(triggerExpanding()));
125 Q_ASSERT(m_proxyModel
== 0);
126 m_proxyModel
= new DolphinSortFilterProxyModel(this);
127 m_proxyModel
->setSourceModel(m_dolphinModel
);
129 Q_ASSERT(m_treeView
== 0);
130 m_treeView
= new SidebarTreeView(this);
131 m_treeView
->setModel(m_proxyModel
);
132 m_proxyModel
->setSorting(DolphinView::SortByName
);
133 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
135 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
136 this, SLOT(updateActiveView(const QModelIndex
&)));
137 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
138 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
140 QVBoxLayout
* layout
= new QVBoxLayout(this);
141 layout
->setMargin(0);
142 layout
->addWidget(m_treeView
);
146 SidebarPage::showEvent(event
);
149 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
151 SidebarPage::contextMenuEvent(event
);
154 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
155 if (index
.isValid()) {
156 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
157 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
158 emit
changeSelection(KFileItemList());
161 TreeViewContextMenu
contextMenu(this, item
);
165 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
167 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
168 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
169 if (!item
.isNull()) {
170 emit
changeUrl(item
.url());
174 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
175 const QModelIndex
& index
)
177 if (index
.isValid()) {
178 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
179 KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
180 Q_ASSERT(!item
.isNull());
182 emit
urlsDropped(urls
, item
.url());
187 void TreeViewSidebarPage::triggerExpanding()
189 // the expanding of the folders may not be done in the context
191 QMetaObject::invokeMethod(this, "expandToLeafDir", Qt::QueuedConnection
);
194 void TreeViewSidebarPage::triggerLoadSubTree()
196 // the loading of the sub tree may not be done in the context
198 QMetaObject::invokeMethod(this, "loadSubTree", Qt::QueuedConnection
);
201 void TreeViewSidebarPage::expandToLeafDir()
203 // expand all directories until the parent directory of m_leafDir
204 const KUrl parentUrl
= m_leafDir
.upUrl();
205 QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(parentUrl
);
206 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
207 m_treeView
->setExpanded(proxyIndex
, true);
208 selectLeafDirectory();
212 void TreeViewSidebarPage::loadSubTree()
214 m_treeView
->selectionModel()->clearSelection();
216 if (m_leafDir
.isParentOf(m_dirLister
->url())) {
217 // The leaf directory is not a child of the base URL, hence
218 // no sub directory must be loaded or selected.
222 const QModelIndex index
= m_dolphinModel
->indexForUrl(m_leafDir
);
223 if (index
.isValid()) {
224 selectLeafDirectory();
226 // Load all sub directories that need to get expanded for making
227 // the leaf directory visible. The slot triggerExpanding() will
228 // get invoked if the expanding has been finished.
229 m_dolphinModel
->expandToUrl(m_leafDir
);
233 void TreeViewSidebarPage::scrollToLeaf()
235 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
236 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
237 if (proxyIndex
.isValid()) {
238 m_treeView
->scrollTo(proxyIndex
);
242 void TreeViewSidebarPage::loadTree(const KUrl
& url
)
244 Q_ASSERT(m_dirLister
!= 0);
247 m_horizontalPos
= m_treeView
->horizontalScrollBar()->value();
250 if (url
.isLocalFile()) {
251 // use the root directory as base for local URLs
252 baseUrl
= QDir::rootPath();
254 // clear the path for non-local URLs and use it as base
256 baseUrl
.setPath(QString());
259 if (m_dirLister
->url() != baseUrl
) {
261 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
267 void TreeViewSidebarPage::selectLeafDirectory()
269 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
270 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
271 if (!proxyIndex
.isValid()) {
275 if (m_setLeafVisible
) {
276 // Invoke m_treeView->scrollTo(proxyIndex) asynchronously by
277 // scrollToLeaf(). This assures that the scrolling is done after
278 // the horizontal scrollbar gets visible (otherwise the scrollbar
279 // might hide the leaf).
280 QTimer::singleShot(100, this, SLOT(scrollToLeaf()));
281 m_setLeafVisible
= false;
284 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
285 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
287 m_treeView
->horizontalScrollBar()->setValue(m_horizontalPos
);
290 #include "treeviewsidebarpage.moc"