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 "dolphinmainwindow.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 <kdirmodel.h>
32 #include <kfileitem.h>
34 #include <QHeaderView>
35 #include <QItemSelectionModel>
37 #include <QVBoxLayout>
39 TreeViewSidebarPage::TreeViewSidebarPage(QWidget
* parent
) :
46 m_dirLister
= new KDirLister();
47 m_dirLister
->setDirOnlyMode(true);
48 m_dirLister
->setAutoUpdate(true);
49 m_dirLister
->setMainWindow(this);
50 m_dirLister
->setDelayedMimeTypes(true);
51 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
53 m_dirModel
= new KDirModel();
54 m_dirModel
->setDirLister(m_dirLister
);
55 m_dirModel
->setDropsAllowed(KDirModel::DropOnDirectory
);
57 m_proxyModel
= new DolphinSortFilterProxyModel(this);
58 m_proxyModel
->setSourceModel(m_dirModel
);
60 m_treeView
= new SidebarTreeView(this);
61 m_treeView
->setModel(m_proxyModel
);
63 m_proxyModel
->setSorting(DolphinView::SortByName
);
64 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
66 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
67 this, SLOT(updateActiveView(const QModelIndex
&)));
68 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
69 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
71 QVBoxLayout
* layout
= new QVBoxLayout(this);
73 layout
->addWidget(m_treeView
);
76 TreeViewSidebarPage::~TreeViewSidebarPage()
82 void TreeViewSidebarPage::setUrl(const KUrl
& url
)
84 if (!url
.isValid() || (url
== SidebarPage::url())) {
88 SidebarPage::setUrl(url
);
89 // TODO: it makes only sense to load the tree if the TreeViewSidebarPage
90 // is visible, but currently an assertion is triggered in KDirModel when hiding
91 // the tree view, changing to a complete different hierarchy and showing it again.
97 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
99 SidebarPage::showEvent(event
);
103 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
105 SidebarPage::contextMenuEvent(event
);
107 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
108 if (!index
.isValid()) {
109 // only open a context menu above a directory item
113 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
114 KFileItem
* item
= m_dirModel
->itemForIndex(dirModelIndex
);
116 emit
changeSelection(KFileItemList());
117 TreeViewContextMenu
contextMenu(this, item
);
121 void TreeViewSidebarPage::expandSelectionParent()
123 disconnect(m_dirLister
, SIGNAL(completed()),
124 this, SLOT(expandSelectionParent()));
126 // expand the parent folder of the selected item
127 KUrl parentUrl
= url().upUrl();
128 if (!m_dirLister
->url().isParentOf(parentUrl
)) {
132 QModelIndex index
= m_dirModel
->indexForUrl(parentUrl
);
133 if (index
.isValid()) {
134 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
135 m_treeView
->setExpanded(proxyIndex
, true);
137 // select the item and assure that the item is visible
138 index
= m_dirModel
->indexForUrl(url());
139 if (index
.isValid()) {
140 proxyIndex
= m_proxyModel
->mapFromSource(index
);
141 m_treeView
->scrollTo(proxyIndex
);
143 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
144 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
149 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
151 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
152 const KFileItem
* item
= m_dirModel
->itemForIndex(dirIndex
);
154 const KUrl
& url
= item
->url();
159 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
160 const QModelIndex
& index
)
162 if (index
.isValid()) {
163 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
164 KFileItem
* item
= m_dirModel
->itemForIndex(dirIndex
);
167 emit
urlsDropped(urls
, item
->url());
172 void TreeViewSidebarPage::loadTree(const KUrl
& url
)
174 // adjust the root of the tree to the base bookmark
175 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
176 KUrl baseUrl
= placesModel
->url(placesModel
->closestItem(url
));
177 if (!baseUrl
.isValid()) {
178 // it's possible that no closest item is available and hence an
179 // empty URL is returned
183 if (m_dirLister
->url() != baseUrl
) {
185 m_dirLister
->openUrl(baseUrl
);
188 // select the folder which contains the given URL
189 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
190 selModel
->clearSelection();
192 const QModelIndex index
= m_dirModel
->indexForUrl(url
);
193 if (index
.isValid()) {
194 // the item with the given URL is already part of the model
195 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
196 m_treeView
->scrollTo(proxyIndex
);
197 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
199 // The item with the given URL is not loaded by the model yet. Iterate
200 // backward to the base URL and trigger the loading of the items for
201 // each hierarchy level.
202 connect(m_dirLister
, SIGNAL(completed()),
203 this, SLOT(expandSelectionParent()));
205 // Implementation note: It is important to remove the trailing slash from
206 // the parent URL, as the directories from the dir lister (KDirLister::directories())
207 // don't have a trailing slash and hence KUrl::List::contains() would fail...
208 KUrl parentUrl
= url
.upUrl();
209 parentUrl
.adjustPath(KUrl::RemoveTrailingSlash
);
210 while (!parentUrl
.isParentOf(baseUrl
)) {
211 if (m_dirLister
->directories().contains(parentUrl
)) {
212 m_dirLister
->updateDirectory(parentUrl
);
214 m_dirLister
->openUrl(parentUrl
, true, false);
216 parentUrl
= parentUrl
.upUrl();
217 parentUrl
.adjustPath(KUrl::RemoveTrailingSlash
);
222 #include "treeviewsidebarpage.moc"