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>
40 TreeViewSidebarPage::TreeViewSidebarPage(QWidget
* parent
) :
42 m_setLeafVisible(false),
52 TreeViewSidebarPage::~TreeViewSidebarPage()
56 delete m_dolphinModel
;
58 m_dirLister
= 0; // deleted by m_dolphinModel
61 QSize
TreeViewSidebarPage::sizeHint() const
63 return QSize(200, 400);
66 void TreeViewSidebarPage::setUrl(const KUrl
& url
)
68 if (!url
.isValid() || (url
== SidebarPage::url())) {
72 SidebarPage::setUrl(url
);
73 if (m_dirLister
!= 0) {
74 m_setLeafVisible
= true;
79 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
81 if (event
->spontaneous()) {
82 SidebarPage::showEvent(event
);
86 if (m_dirLister
== 0) {
87 // Postpone the creating of the dir lister to the first show event.
88 // This assures that no performance and memory overhead is given when the TreeView is not
89 // used at all (see TreeViewSidebarPage::setUrl()).
90 m_dirLister
= new KDirLister();
91 m_dirLister
->setDirOnlyMode(true);
92 m_dirLister
->setAutoUpdate(true);
93 m_dirLister
->setMainWindow(this);
94 m_dirLister
->setDelayedMimeTypes(true);
95 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
97 connect(m_dirLister
, SIGNAL(completed()),
98 this, SLOT(triggerLoadSubTree()));
100 Q_ASSERT(m_dolphinModel
== 0);
101 m_dolphinModel
= new DolphinModel(this);
102 m_dolphinModel
->setDirLister(m_dirLister
);
103 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
104 connect(m_dolphinModel
, SIGNAL(expand(const QModelIndex
&)),
105 this, SLOT(triggerExpanding()));
107 Q_ASSERT(m_proxyModel
== 0);
108 m_proxyModel
= new DolphinSortFilterProxyModel(this);
109 m_proxyModel
->setSourceModel(m_dolphinModel
);
111 Q_ASSERT(m_treeView
== 0);
112 m_treeView
= new SidebarTreeView(this);
113 m_treeView
->setModel(m_proxyModel
);
114 m_proxyModel
->setSorting(DolphinView::SortByName
);
115 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
117 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
118 this, SLOT(updateActiveView(const QModelIndex
&)));
119 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
120 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
122 QVBoxLayout
* layout
= new QVBoxLayout(this);
123 layout
->setMargin(0);
124 layout
->addWidget(m_treeView
);
128 SidebarPage::showEvent(event
);
131 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
133 SidebarPage::contextMenuEvent(event
);
135 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
136 if (!index
.isValid()) {
137 // only open a context menu above a directory item
141 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
142 KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
144 emit
changeSelection(KFileItemList());
145 TreeViewContextMenu
contextMenu(this, item
);
149 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
151 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
152 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
153 if (!item
.isNull()) {
154 emit
changeUrl(item
.url());
158 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
159 const QModelIndex
& index
)
161 if (index
.isValid()) {
162 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
163 KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
164 Q_ASSERT(!item
.isNull());
166 emit
urlsDropped(urls
, item
.url());
171 void TreeViewSidebarPage::triggerExpanding()
173 // the expanding of the folders may not be done in the context
175 QMetaObject::invokeMethod(this, "expandToLeafDir", Qt::QueuedConnection
);
178 void TreeViewSidebarPage::triggerLoadSubTree()
180 // the loading of the sub tree may not be done in the context
182 QMetaObject::invokeMethod(this, "loadSubTree", Qt::QueuedConnection
);
185 void TreeViewSidebarPage::expandToLeafDir()
187 // expand all directories until the parent directory of m_leafDir
188 const KUrl parentUrl
= m_leafDir
.upUrl();
189 QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(parentUrl
);
190 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
191 m_treeView
->setExpanded(proxyIndex
, true);
192 selectLeafDirectory();
196 void TreeViewSidebarPage::loadSubTree()
198 m_treeView
->selectionModel()->clearSelection();
200 if (m_leafDir
.isParentOf(m_dirLister
->url())) {
201 // The leaf directory is not a child of the base URL, hence
202 // no sub directory must be loaded or selected.
206 const QModelIndex index
= m_dolphinModel
->indexForUrl(m_leafDir
);
207 if (index
.isValid()) {
208 selectLeafDirectory();
210 // Load all sub directories that need to get expanded for making
211 // the leaf directory visible. The slot triggerExpanding() will
212 // get invoked if the expanding has been finished.
213 m_dolphinModel
->expandToUrl(m_leafDir
);
217 void TreeViewSidebarPage::scrollToLeaf()
219 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
220 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
221 if (proxyIndex
.isValid()) {
222 m_treeView
->scrollTo(proxyIndex
);
226 void TreeViewSidebarPage::loadTree(const KUrl
& url
)
228 Q_ASSERT(m_dirLister
!= 0);
231 m_horizontalPos
= m_treeView
->horizontalScrollBar()->value();
234 if (url
.isLocalFile()) {
235 // use the root directory as base for local URLs
236 baseUrl
= KUrl("file:///");
238 // clear the path for non-local URLs and use it as base
240 baseUrl
.setPath(QString());
243 if (m_dirLister
->url() != baseUrl
) {
245 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
251 void TreeViewSidebarPage::selectLeafDirectory()
253 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
254 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
255 if (!proxyIndex
.isValid()) {
259 if (m_setLeafVisible
) {
260 // Invoke m_treeView->scrollTo(proxyIndex) asynchronously by
261 // scrollToLeaf(). This assures that the scrolling is done after
262 // the horizontal scrollbar gets visible (otherwise the scrollbar
263 // might hide the leaf).
264 QTimer::singleShot(100, this, SLOT(scrollToLeaf()));
265 m_setLeafVisible
= false;
268 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
269 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
271 m_treeView
->horizontalScrollBar()->setValue(m_horizontalPos
);
274 #include "treeviewsidebarpage.moc"