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 QSize
TreeViewSidebarPage::sizeHint() const
56 QSize size
= SidebarPage::sizeHint();
61 void TreeViewSidebarPage::setUrl(const KUrl
& url
)
63 if (!url
.isValid() || (url
== SidebarPage::url())) {
67 SidebarPage::setUrl(url
);
68 if (m_dirLister
!= 0) {
73 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
75 if (event
->spontaneous()) {
76 SidebarPage::showEvent(event
);
80 if (m_dirLister
== 0) {
81 // Postpone the creating of the dir lister to the first show event.
82 // This assures that no performance and memory overhead is given when the TreeView is not
83 // used at all (see TreeViewSidebarPage::setUrl()).
84 m_dirLister
= new KDirLister();
85 m_dirLister
->setDirOnlyMode(true);
86 m_dirLister
->setAutoUpdate(true);
87 m_dirLister
->setMainWindow(this);
88 m_dirLister
->setDelayedMimeTypes(true);
89 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
91 Q_ASSERT(m_dirModel
== 0);
92 m_dirModel
= new KDirModel(this);
93 m_dirModel
->setDirLister(m_dirLister
);
94 m_dirModel
->setDropsAllowed(KDirModel::DropOnDirectory
);
96 Q_ASSERT(m_proxyModel
== 0);
97 m_proxyModel
= new DolphinSortFilterProxyModel(this);
98 m_proxyModel
->setSourceModel(m_dirModel
);
100 Q_ASSERT(m_treeView
== 0);
101 m_treeView
= new SidebarTreeView(this);
102 m_treeView
->setModel(m_proxyModel
);
103 m_proxyModel
->setSorting(DolphinView::SortByName
);
104 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
106 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
107 this, SLOT(updateActiveView(const QModelIndex
&)));
108 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
109 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
111 QVBoxLayout
* layout
= new QVBoxLayout(this);
112 layout
->setMargin(0);
113 layout
->addWidget(m_treeView
);
117 SidebarPage::showEvent(event
);
120 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
122 SidebarPage::contextMenuEvent(event
);
124 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
125 if (!index
.isValid()) {
126 // only open a context menu above a directory item
130 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
131 KFileItem item
= m_dirModel
->itemForIndex(dirModelIndex
);
133 emit
changeSelection(QList
<KFileItem
>());
134 TreeViewContextMenu
contextMenu(this, item
);
138 void TreeViewSidebarPage::expandSelectionParent()
140 disconnect(m_dirLister
, SIGNAL(completed()),
141 this, SLOT(expandSelectionParent()));
143 // expand the parent folder of the selected item
144 KUrl parentUrl
= url().upUrl();
145 if (!m_dirLister
->url().isParentOf(parentUrl
)) {
149 QModelIndex index
= m_dirModel
->indexForUrl(parentUrl
);
150 if (index
.isValid()) {
151 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
152 m_treeView
->setExpanded(proxyIndex
, true);
154 // select the item and assure that the item is visible
155 index
= m_dirModel
->indexForUrl(url());
156 if (index
.isValid()) {
157 proxyIndex
= m_proxyModel
->mapFromSource(index
);
158 m_treeView
->scrollTo(proxyIndex
);
160 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
161 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
166 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
168 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
169 const KFileItem item
= m_dirModel
->itemForIndex(dirIndex
);
170 if (!item
.isNull()) {
171 emit
changeUrl(item
.url());
175 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
176 const QModelIndex
& index
)
178 if (index
.isValid()) {
179 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
180 KFileItem item
= m_dirModel
->itemForIndex(dirIndex
);
181 Q_ASSERT(!item
.isNull());
183 emit
urlsDropped(urls
, item
.url());
188 void TreeViewSidebarPage::loadTree(const KUrl
& url
)
190 Q_ASSERT(m_dirLister
!= 0);
192 // adjust the root of the tree to the base bookmark
193 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
194 KUrl baseUrl
= placesModel
->url(placesModel
->closestItem(url
));
195 if (!baseUrl
.isValid()) {
196 // it's possible that no closest item is available and hence an
197 // empty URL is returned
201 if (m_dirLister
->url() != baseUrl
) {
203 m_dirLister
->openUrl(baseUrl
);
206 // select the folder which contains the given URL
207 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
208 selModel
->clearSelection();
210 const QModelIndex index
= m_dirModel
->indexForUrl(url
);
211 if (index
.isValid()) {
212 // the item with the given URL is already part of the model
213 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
214 m_treeView
->scrollTo(proxyIndex
);
215 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
217 // The item with the given URL is not loaded by the model yet. Iterate
218 // backward to the base URL and trigger the loading of the items for
219 // each hierarchy level.
220 connect(m_dirLister
, SIGNAL(completed()),
221 this, SLOT(expandSelectionParent()));
223 // Implementation note: It is important to remove the trailing slash from
224 // the parent URL, as the directories from the dir lister (KDirLister::directories())
225 // don't have a trailing slash and hence KUrl::List::contains() would fail...
226 KUrl parentUrl
= url
.upUrl();
227 parentUrl
.adjustPath(KUrl::RemoveTrailingSlash
);
228 while (!parentUrl
.isParentOf(baseUrl
)) {
229 if (m_dirLister
->directories().contains(parentUrl
)) {
230 m_dirLister
->updateDirectory(parentUrl
);
232 m_dirLister
->openUrl(parentUrl
, true, false);
234 parentUrl
= parentUrl
.upUrl();
235 parentUrl
.adjustPath(KUrl::RemoveTrailingSlash
);
240 #include "treeviewsidebarpage.moc"