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
) :
48 TreeViewSidebarPage::~TreeViewSidebarPage()
54 void TreeViewSidebarPage::setUrl(const KUrl
& url
)
56 if (!url
.isValid() || (url
== SidebarPage::url())) {
60 SidebarPage::setUrl(url
);
61 if (m_dirLister
!= 0) {
66 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
68 if (m_dirLister
== 0) {
69 // Postpone the creating of the dir lister to the first show event.
70 // This assures that no performance and memory overhead is given when the TreeView is not
71 // used at all (see TreeViewSidebarPage::setUrl()).
72 m_dirLister
= new KDirLister();
73 m_dirLister
->setDirOnlyMode(true);
74 m_dirLister
->setAutoUpdate(true);
75 m_dirLister
->setMainWindow(this);
76 m_dirLister
->setDelayedMimeTypes(true);
77 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
79 Q_ASSERT(m_dirModel
== 0);
80 m_dirModel
= new KDirModel();
81 m_dirModel
->setDirLister(m_dirLister
);
82 m_dirModel
->setDropsAllowed(KDirModel::DropOnDirectory
);
84 Q_ASSERT(m_proxyModel
== 0);
85 m_proxyModel
= new DolphinSortFilterProxyModel(this);
86 m_proxyModel
->setSourceModel(m_dirModel
);
88 Q_ASSERT(m_treeView
== 0);
89 m_treeView
= new SidebarTreeView(this);
90 m_treeView
->setModel(m_proxyModel
);
91 m_proxyModel
->setSorting(DolphinView::SortByName
);
92 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
94 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
95 this, SLOT(updateActiveView(const QModelIndex
&)));
96 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
97 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
99 QVBoxLayout
* layout
= new QVBoxLayout(this);
100 layout
->setMargin(0);
101 layout
->addWidget(m_treeView
);
105 SidebarPage::showEvent(event
);
108 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
110 SidebarPage::contextMenuEvent(event
);
112 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
113 if (!index
.isValid()) {
114 // only open a context menu above a directory item
118 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
119 KFileItem
* item
= m_dirModel
->itemForIndex(dirModelIndex
);
121 emit
changeSelection(KFileItemList());
122 TreeViewContextMenu
contextMenu(this, item
);
126 void TreeViewSidebarPage::expandSelectionParent()
128 disconnect(m_dirLister
, SIGNAL(completed()),
129 this, SLOT(expandSelectionParent()));
131 // expand the parent folder of the selected item
132 KUrl parentUrl
= url().upUrl();
133 if (!m_dirLister
->url().isParentOf(parentUrl
)) {
137 QModelIndex index
= m_dirModel
->indexForUrl(parentUrl
);
138 if (index
.isValid()) {
139 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
140 m_treeView
->setExpanded(proxyIndex
, true);
142 // select the item and assure that the item is visible
143 index
= m_dirModel
->indexForUrl(url());
144 if (index
.isValid()) {
145 proxyIndex
= m_proxyModel
->mapFromSource(index
);
146 m_treeView
->scrollTo(proxyIndex
);
148 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
149 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
154 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
156 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
157 const KFileItem
* item
= m_dirModel
->itemForIndex(dirIndex
);
159 const KUrl
& url
= item
->url();
164 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
165 const QModelIndex
& index
)
167 if (index
.isValid()) {
168 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
169 KFileItem
* item
= m_dirModel
->itemForIndex(dirIndex
);
172 emit
urlsDropped(urls
, item
->url());
177 void TreeViewSidebarPage::loadTree(const KUrl
& url
)
179 Q_ASSERT(m_dirLister
!= 0);
181 // adjust the root of the tree to the base bookmark
182 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
183 KUrl baseUrl
= placesModel
->url(placesModel
->closestItem(url
));
184 if (!baseUrl
.isValid()) {
185 // it's possible that no closest item is available and hence an
186 // empty URL is returned
190 if (m_dirLister
->url() != baseUrl
) {
192 m_dirLister
->openUrl(baseUrl
);
195 // select the folder which contains the given URL
196 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
197 selModel
->clearSelection();
199 const QModelIndex index
= m_dirModel
->indexForUrl(url
);
200 if (index
.isValid()) {
201 // the item with the given URL is already part of the model
202 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
203 m_treeView
->scrollTo(proxyIndex
);
204 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
206 // The item with the given URL is not loaded by the model yet. Iterate
207 // backward to the base URL and trigger the loading of the items for
208 // each hierarchy level.
209 connect(m_dirLister
, SIGNAL(completed()),
210 this, SLOT(expandSelectionParent()));
212 // Implementation note: It is important to remove the trailing slash from
213 // the parent URL, as the directories from the dir lister (KDirLister::directories())
214 // don't have a trailing slash and hence KUrl::List::contains() would fail...
215 KUrl parentUrl
= url
.upUrl();
216 parentUrl
.adjustPath(KUrl::RemoveTrailingSlash
);
217 while (!parentUrl
.isParentOf(baseUrl
)) {
218 if (m_dirLister
->directories().contains(parentUrl
)) {
219 m_dirLister
->updateDirectory(parentUrl
);
221 m_dirLister
->openUrl(parentUrl
, true, false);
223 parentUrl
= parentUrl
.upUrl();
224 parentUrl
.adjustPath(KUrl::RemoveTrailingSlash
);
229 #include "treeviewsidebarpage.moc"