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 "bookmarkselector.h"
23 #include "dolphincontextmenu.h"
24 #include "dolphinmainwindow.h"
25 #include "dolphinsortfilterproxymodel.h"
26 #include "dolphinview.h"
27 #include "sidebartreeview.h"
29 #include <kdirlister.h>
30 #include <kdirmodel.h>
31 #include <kfileitem.h>
33 #include <QHeaderView>
34 #include <QItemSelectionModel>
36 #include <QVBoxLayout>
38 // TODO: currently when using a proxy model the strange effect occurs
39 // that items get duplicated. Activate the following define to have the proxy
41 //#define USE_PROXY_MODEL
43 TreeViewSidebarPage::TreeViewSidebarPage(DolphinMainWindow
* mainWindow
,
45 SidebarPage(mainWindow
, parent
),
52 Q_ASSERT(mainWindow
!= 0);
54 m_dirLister
= new KDirLister();
55 m_dirLister
->setDirOnlyMode(true);
56 m_dirLister
->setAutoUpdate(true);
57 m_dirLister
->setMainWindow(this);
58 m_dirLister
->setDelayedMimeTypes(true);
59 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
61 m_dirModel
= new KDirModel();
62 m_dirModel
->setDirLister(m_dirLister
);
63 m_dirModel
->setDropsAllowed(KDirModel::DropOnDirectory
);
66 #if defined(USE_PROXY_MODEL)
67 m_proxyModel
= new DolphinSortFilterProxyModel(this);
68 m_proxyModel
->setSourceModel(m_dirModel
);
70 m_treeView
= new SidebarTreeView(mainWindow
, this);
71 m_treeView
->setModel(m_proxyModel
);
73 m_proxyModel
->setSorting(DolphinView::SortByName
);
74 m_proxyModel
->setSortOrder(Qt::Ascending
);
76 m_treeView
= new SidebarTreeView(mainWindow
, this);
77 m_treeView
->setModel(m_dirModel
);
80 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
81 this, SLOT(updateActiveView(const QModelIndex
&)));
82 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
83 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
85 QVBoxLayout
* layout
= new QVBoxLayout(this);
87 layout
->addWidget(m_treeView
);
90 TreeViewSidebarPage::~TreeViewSidebarPage()
96 void TreeViewSidebarPage::activeViewChanged()
98 connectToActiveView();
101 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
103 SidebarPage::showEvent(event
);
104 connectToActiveView();
107 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
109 SidebarPage::contextMenuEvent(event
);
111 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
112 if (!index
.isValid()) {
113 // only open a context menu above a directory item
117 #if defined(USE_PROXY_MODEL)
118 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
119 KFileItem
* item
= m_dirModel
->itemForIndex(dirModelIndex
);
121 KFileItem
* item
= m_dirModel
->itemForIndex(index
);
124 mainWindow()->activeView()->clearSelection();
125 DolphinContextMenu
contextMenu(mainWindow(),
128 DolphinContextMenu::SidebarView
);
132 void TreeViewSidebarPage::updateSelection(const KUrl
& url
)
134 if (!url
.isValid() || (url
== m_selectedUrl
)) {
140 // adjust the root of the tree to the base bookmark
141 const KUrl baseUrl
= BookmarkSelector::baseBookmark(url
).url();
142 if (m_dirLister
->url() != baseUrl
) {
144 m_dirLister
->openUrl(baseUrl
);
147 // select the folder which contains the given URL
148 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
149 selModel
->clearSelection();
151 const KFileItem
item(S_IFDIR
, KFileItem::Unknown
, url
);
153 const QModelIndex index
= m_dirModel
->indexForItem(item
);
154 if (index
.isValid()) {
155 #if defined(USE_PROXY_MODEL)
156 // the item with the given URL is already part of the model
157 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
158 m_treeView
->scrollTo(proxyIndex
);
159 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
161 m_treeView
->scrollTo(index
);
162 selModel
->setCurrentIndex(index
, QItemSelectionModel::Select
);
166 // The item with the given URL is not loaded by the model yet. Iterate
167 // backward to the base URL and trigger the loading of the items for
168 // each hierarchy level.
169 connect(m_dirLister
, SIGNAL(completed()),
170 this, SLOT(expandSelectionParent()));
172 KUrl parentUrl
= url
.upUrl();
173 while (!parentUrl
.isParentOf(baseUrl
)) {
174 m_dirLister
->openUrl(parentUrl
, true, false);
175 parentUrl
= parentUrl
.upUrl();
180 void TreeViewSidebarPage::expandSelectionParent()
182 disconnect(m_dirLister
, SIGNAL(completed()),
183 this, SLOT(expandSelectionParent()));
185 // expand the parent folder of the selected item
186 const KFileItem
parentItem(S_IFDIR
, KFileItem::Unknown
, m_selectedUrl
.upUrl());
187 QModelIndex index
= m_dirModel
->indexForItem(parentItem
);
188 if (index
.isValid()) {
189 #if defined(USE_PROXY_MODEL)
190 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
191 m_treeView
->setExpanded(proxyIndex
, true);
193 m_treeView
->setExpanded(index
, true);
196 // select the item and assure that the item is visible
197 const KFileItem
selectedItem(S_IFDIR
, KFileItem::Unknown
, m_selectedUrl
);
198 index
= m_dirModel
->indexForItem(selectedItem
);
199 if (index
.isValid()) {
200 #if defined(USE_PROXY_MODEL)
201 proxyIndex
= m_proxyModel
->mapFromSource(index
);
202 m_treeView
->scrollTo(proxyIndex
);
204 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
205 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
207 m_treeView
->scrollTo(index
);
209 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
210 selModel
->setCurrentIndex(index
, QItemSelectionModel::Select
);
217 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
219 #if defined(USE_PROXY_MODEL)
220 const QModelIndex
& dirIndex
= m_proxyModel
->mapToSource(index
);
221 const KFileItem
* item
= m_dirModel
->itemForIndex(dirIndex
);
223 const KFileItem
* item
= m_dirModel
->itemForIndex(index
);
226 const KUrl
& url
= item
->url();
227 mainWindow()->activeView()->setUrl(url
);
231 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
232 const QModelIndex
& index
)
234 if (index
.isValid()) {
235 #if defined(USE_PROXY_MODEL)
236 const QModelIndex
& dirIndex
= m_proxyModel
->mapToSource(index
);
237 KFileItem
* item
= m_dirModel
->itemForIndex(dirIndex
);
239 KFileItem
* item
= m_dirModel
->itemForIndex(index
);
243 mainWindow()->dropUrls(urls
, item
->url());
248 void TreeViewSidebarPage::connectToActiveView()
250 const QWidget
* parent
= parentWidget();
251 if ((parent
== 0) || parent
->isHidden()) {
255 const DolphinView
* view
= mainWindow()->activeView();
256 const KUrl
& url
= view
->url();
258 connect(view
, SIGNAL(urlChanged(const KUrl
&)),
259 this, SLOT(updateSelection(const KUrl
&)));
261 updateSelection(url
);
264 #include "treeviewsidebarpage.moc"