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(KFileItemList());
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
);
171 const KUrl
& url
= item
->url();
176 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
177 const QModelIndex
& index
)
179 if (index
.isValid()) {
180 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
181 KFileItem
* item
= m_dirModel
->itemForIndex(dirIndex
);
184 emit
urlsDropped(urls
, item
->url());
189 void TreeViewSidebarPage::loadTree(const KUrl
& url
)
191 Q_ASSERT(m_dirLister
!= 0);
193 // adjust the root of the tree to the base bookmark
194 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
195 KUrl baseUrl
= placesModel
->url(placesModel
->closestItem(url
));
196 if (!baseUrl
.isValid()) {
197 // it's possible that no closest item is available and hence an
198 // empty URL is returned
202 if (m_dirLister
->url() != baseUrl
) {
204 m_dirLister
->openUrl(baseUrl
);
207 // select the folder which contains the given URL
208 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
209 selModel
->clearSelection();
211 const QModelIndex index
= m_dirModel
->indexForUrl(url
);
212 if (index
.isValid()) {
213 // the item with the given URL is already part of the model
214 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
215 m_treeView
->scrollTo(proxyIndex
);
216 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
218 // The item with the given URL is not loaded by the model yet. Iterate
219 // backward to the base URL and trigger the loading of the items for
220 // each hierarchy level.
221 connect(m_dirLister
, SIGNAL(completed()),
222 this, SLOT(expandSelectionParent()));
224 // Implementation note: It is important to remove the trailing slash from
225 // the parent URL, as the directories from the dir lister (KDirLister::directories())
226 // don't have a trailing slash and hence KUrl::List::contains() would fail...
227 KUrl parentUrl
= url
.upUrl();
228 parentUrl
.adjustPath(KUrl::RemoveTrailingSlash
);
229 while (!parentUrl
.isParentOf(baseUrl
)) {
230 if (m_dirLister
->directories().contains(parentUrl
)) {
231 m_dirLister
->updateDirectory(parentUrl
);
233 m_dirLister
->openUrl(parentUrl
, true, false);
235 parentUrl
= parentUrl
.upUrl();
236 parentUrl
.adjustPath(KUrl::RemoveTrailingSlash
);
241 #include "treeviewsidebarpage.moc"