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 "sidebartreeview.h"
27 #include "treeviewcontextmenu.h"
29 #include <kfileplacesmodel.h>
30 #include <kdirlister.h>
31 #include <kfileitem.h>
33 #include <QItemSelection>
36 #include <QModelIndex>
39 TreeViewSidebarPage::TreeViewSidebarPage(QWidget
* parent
) :
41 m_setLeafVisible(false),
51 TreeViewSidebarPage::~TreeViewSidebarPage()
55 delete m_dolphinModel
;
57 m_dirLister
= 0; // deleted by m_dolphinModel
60 QSize
TreeViewSidebarPage::sizeHint() const
62 return QSize(200, 400);
65 void TreeViewSidebarPage::setUrl(const KUrl
& url
)
67 if (!url
.isValid() || (url
== SidebarPage::url())) {
71 SidebarPage::setUrl(url
);
72 if (m_dirLister
!= 0) {
73 m_setLeafVisible
= true;
78 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
80 if (event
->spontaneous()) {
81 SidebarPage::showEvent(event
);
85 if (m_dirLister
== 0) {
86 // Postpone the creating of the dir lister to the first show event.
87 // This assures that no performance and memory overhead is given when the TreeView is not
88 // used at all (see TreeViewSidebarPage::setUrl()).
89 m_dirLister
= new KDirLister();
90 m_dirLister
->setDirOnlyMode(true);
91 m_dirLister
->setAutoUpdate(true);
92 m_dirLister
->setMainWindow(this);
93 m_dirLister
->setDelayedMimeTypes(true);
94 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
96 connect(m_dirLister
, SIGNAL(completed()),
97 this, SLOT(triggerLoadSubTree()));
99 Q_ASSERT(m_dolphinModel
== 0);
100 m_dolphinModel
= new DolphinModel(this);
101 m_dolphinModel
->setDirLister(m_dirLister
);
102 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
103 connect(m_dolphinModel
, SIGNAL(expand(const QModelIndex
&)),
104 this, SLOT(triggerExpanding()));
106 Q_ASSERT(m_proxyModel
== 0);
107 m_proxyModel
= new DolphinSortFilterProxyModel(this);
108 m_proxyModel
->setSourceModel(m_dolphinModel
);
110 Q_ASSERT(m_treeView
== 0);
111 m_treeView
= new SidebarTreeView(this);
112 m_treeView
->setModel(m_proxyModel
);
113 m_proxyModel
->setSorting(DolphinView::SortByName
);
114 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
116 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
117 this, SLOT(updateActiveView(const QModelIndex
&)));
118 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
119 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
121 QVBoxLayout
* layout
= new QVBoxLayout(this);
122 layout
->setMargin(0);
123 layout
->addWidget(m_treeView
);
127 SidebarPage::showEvent(event
);
130 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
132 SidebarPage::contextMenuEvent(event
);
134 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
135 if (!index
.isValid()) {
136 // only open a context menu above a directory item
140 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
141 KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
143 emit
changeSelection(KFileItemList());
144 TreeViewContextMenu
contextMenu(this, item
);
148 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
150 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
151 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
152 if (!item
.isNull()) {
153 emit
changeUrl(item
.url());
157 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
158 const QModelIndex
& index
)
160 if (index
.isValid()) {
161 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
162 KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
163 Q_ASSERT(!item
.isNull());
165 emit
urlsDropped(urls
, item
.url());
170 void TreeViewSidebarPage::triggerExpanding()
172 // the expanding of the folders may not be done in the context
174 QMetaObject::invokeMethod(this, "expandToLeafDir", Qt::QueuedConnection
);
177 void TreeViewSidebarPage::triggerLoadSubTree()
179 // the loading of the sub tree may not be done in the context
181 QMetaObject::invokeMethod(this, "loadSubTree", Qt::QueuedConnection
);
184 void TreeViewSidebarPage::expandToLeafDir()
186 // expand all directories until the parent directory of m_leafDir
187 const KUrl parentUrl
= m_leafDir
.upUrl();
188 QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(parentUrl
);
189 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
190 m_treeView
->setExpanded(proxyIndex
, true);
191 selectLeafDirectory();
195 void TreeViewSidebarPage::loadSubTree()
197 m_treeView
->selectionModel()->clearSelection();
199 if (m_leafDir
.isParentOf(m_dirLister
->url())) {
200 // The leaf directory is not a child of the base URL, hence
201 // no sub directory must be loaded or selected.
205 const QModelIndex index
= m_dolphinModel
->indexForUrl(m_leafDir
);
206 if (index
.isValid()) {
207 selectLeafDirectory();
209 // Load all sub directories that need to get expanded for making
210 // the leaf directory visible. The slot triggerExpanding() will
211 // get invoked if the expanding has been finished.
212 m_dolphinModel
->expandToUrl(m_leafDir
);
216 void TreeViewSidebarPage::loadTree(const KUrl
& url
)
218 Q_ASSERT(m_dirLister
!= 0);
221 m_horizontalPos
= m_treeView
->horizontalScrollBar()->value();
224 if (url
.isLocalFile()) {
225 // use the root directory as base for local URLs
226 baseUrl
= KUrl("file:///");
228 // clear the path for non-local URLs and use it as base
230 baseUrl
.setPath(QString());
233 if (m_dirLister
->url() != baseUrl
) {
235 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
241 void TreeViewSidebarPage::selectLeafDirectory()
243 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
244 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
245 if (!proxyIndex
.isValid()) {
249 if (m_setLeafVisible
) {
250 m_treeView
->scrollTo(proxyIndex
);
251 m_setLeafVisible
= false;
254 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
255 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
257 m_treeView
->horizontalScrollBar()->setValue(m_horizontalPos
);
260 #include "treeviewsidebarpage.moc"