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 <QtGui/QHeaderView>
35 #include <QtGui/QItemSelection>
36 #include <QtGui/QTreeView>
37 #include <QtGui/QBoxLayout>
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 (event
->spontaneous()) {
69 SidebarPage::showEvent(event
);
73 if (m_dirLister
== 0) {
74 // Postpone the creating of the dir lister to the first show event.
75 // This assures that no performance and memory overhead is given when the TreeView is not
76 // used at all (see TreeViewSidebarPage::setUrl()).
77 m_dirLister
= new KDirLister();
78 m_dirLister
->setDirOnlyMode(true);
79 m_dirLister
->setAutoUpdate(true);
80 m_dirLister
->setMainWindow(this);
81 m_dirLister
->setDelayedMimeTypes(true);
82 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
84 Q_ASSERT(m_dirModel
== 0);
85 m_dirModel
= new KDirModel(this);
86 m_dirModel
->setDirLister(m_dirLister
);
87 m_dirModel
->setDropsAllowed(KDirModel::DropOnDirectory
);
89 Q_ASSERT(m_proxyModel
== 0);
90 m_proxyModel
= new DolphinSortFilterProxyModel(this);
91 m_proxyModel
->setSourceModel(m_dirModel
);
93 Q_ASSERT(m_treeView
== 0);
94 m_treeView
= new SidebarTreeView(this);
95 m_treeView
->setModel(m_proxyModel
);
96 m_proxyModel
->setSorting(DolphinView::SortByName
);
97 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
99 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
100 this, SLOT(updateActiveView(const QModelIndex
&)));
101 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
102 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
104 QVBoxLayout
* layout
= new QVBoxLayout(this);
105 layout
->setMargin(0);
106 layout
->addWidget(m_treeView
);
110 SidebarPage::showEvent(event
);
113 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
115 SidebarPage::contextMenuEvent(event
);
117 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
118 if (!index
.isValid()) {
119 // only open a context menu above a directory item
123 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
124 KFileItem
* item
= m_dirModel
->itemForIndex(dirModelIndex
);
126 emit
changeSelection(KFileItemList());
127 TreeViewContextMenu
contextMenu(this, item
);
131 void TreeViewSidebarPage::expandSelectionParent()
133 disconnect(m_dirLister
, SIGNAL(completed()),
134 this, SLOT(expandSelectionParent()));
136 // expand the parent folder of the selected item
137 KUrl parentUrl
= url().upUrl();
138 if (!m_dirLister
->url().isParentOf(parentUrl
)) {
142 QModelIndex index
= m_dirModel
->indexForUrl(parentUrl
);
143 if (index
.isValid()) {
144 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
145 m_treeView
->setExpanded(proxyIndex
, true);
147 // select the item and assure that the item is visible
148 index
= m_dirModel
->indexForUrl(url());
149 if (index
.isValid()) {
150 proxyIndex
= m_proxyModel
->mapFromSource(index
);
151 m_treeView
->scrollTo(proxyIndex
);
153 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
154 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
159 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
161 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
162 const KFileItem
* item
= m_dirModel
->itemForIndex(dirIndex
);
164 const KUrl
& url
= item
->url();
169 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
170 const QModelIndex
& index
)
172 if (index
.isValid()) {
173 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
174 KFileItem
* item
= m_dirModel
->itemForIndex(dirIndex
);
177 emit
urlsDropped(urls
, item
->url());
182 void TreeViewSidebarPage::loadTree(const KUrl
& url
)
184 Q_ASSERT(m_dirLister
!= 0);
186 // adjust the root of the tree to the base bookmark
187 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
188 KUrl baseUrl
= placesModel
->url(placesModel
->closestItem(url
));
189 if (!baseUrl
.isValid()) {
190 // it's possible that no closest item is available and hence an
191 // empty URL is returned
195 if (m_dirLister
->url() != baseUrl
) {
197 m_dirLister
->openUrl(baseUrl
);
200 // select the folder which contains the given URL
201 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
202 selModel
->clearSelection();
204 const QModelIndex index
= m_dirModel
->indexForUrl(url
);
205 if (index
.isValid()) {
206 // the item with the given URL is already part of the model
207 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
208 m_treeView
->scrollTo(proxyIndex
);
209 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
211 // The item with the given URL is not loaded by the model yet. Iterate
212 // backward to the base URL and trigger the loading of the items for
213 // each hierarchy level.
214 connect(m_dirLister
, SIGNAL(completed()),
215 this, SLOT(expandSelectionParent()));
217 // Implementation note: It is important to remove the trailing slash from
218 // the parent URL, as the directories from the dir lister (KDirLister::directories())
219 // don't have a trailing slash and hence KUrl::List::contains() would fail...
220 KUrl parentUrl
= url
.upUrl();
221 parentUrl
.adjustPath(KUrl::RemoveTrailingSlash
);
222 while (!parentUrl
.isParentOf(baseUrl
)) {
223 if (m_dirLister
->directories().contains(parentUrl
)) {
224 m_dirLister
->updateDirectory(parentUrl
);
226 m_dirLister
->openUrl(parentUrl
, true, false);
228 parentUrl
= parentUrl
.upUrl();
229 parentUrl
.adjustPath(KUrl::RemoveTrailingSlash
);
234 #include "treeviewsidebarpage.moc"