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 DolphinContextMenu
contextMenu(mainWindow(),
127 DolphinContextMenu::SidebarView
);
131 void TreeViewSidebarPage::updateSelection(const KUrl
& url
)
133 if (!url
.isValid() || (url
== m_selectedUrl
)) {
139 // adjust the root of the tree to the base bookmark
140 const KUrl baseUrl
= BookmarkSelector::baseBookmark(url
).url();
141 if (m_dirLister
->url() != baseUrl
) {
143 m_dirLister
->openUrl(baseUrl
);
146 // select the folder which contains the given URL
147 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
148 selModel
->clearSelection();
150 const KFileItem
item(S_IFDIR
, KFileItem::Unknown
, url
);
152 const QModelIndex index
= m_dirModel
->indexForItem(item
);
153 if (index
.isValid()) {
154 #if defined(USE_PROXY_MODEL)
155 // the item with the given URL is already part of the model
156 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
157 m_treeView
->scrollTo(proxyIndex
);
158 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
160 m_treeView
->scrollTo(index
);
161 selModel
->setCurrentIndex(index
, QItemSelectionModel::Select
);
165 // The item with the given URL is not loaded by the model yet. Iterate
166 // backward to the base URL and trigger the loading of the items for
167 // each hierarchy level.
168 connect(m_dirLister
, SIGNAL(completed()),
169 this, SLOT(expandSelectionParent()));
171 KUrl parentUrl
= url
.upUrl();
172 while (!parentUrl
.isParentOf(baseUrl
)) {
173 m_dirLister
->openUrl(parentUrl
, true, false);
174 parentUrl
= parentUrl
.upUrl();
179 void TreeViewSidebarPage::expandSelectionParent()
181 disconnect(m_dirLister
, SIGNAL(completed()),
182 this, SLOT(expandSelectionParent()));
184 // expand the parent folder of the selected item
185 const KFileItem
parentItem(S_IFDIR
, KFileItem::Unknown
, m_selectedUrl
.upUrl());
186 QModelIndex index
= m_dirModel
->indexForItem(parentItem
);
187 if (index
.isValid()) {
188 #if defined(USE_PROXY_MODEL)
189 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
190 m_treeView
->setExpanded(proxyIndex
, true);
192 m_treeView
->setExpanded(index
, true);
195 // select the item and assure that the item is visible
196 const KFileItem
selectedItem(S_IFDIR
, KFileItem::Unknown
, m_selectedUrl
);
197 index
= m_dirModel
->indexForItem(selectedItem
);
198 if (index
.isValid()) {
199 #if defined(USE_PROXY_MODEL)
200 proxyIndex
= m_proxyModel
->mapFromSource(index
);
201 m_treeView
->scrollTo(proxyIndex
);
203 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
204 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
206 m_treeView
->scrollTo(index
);
208 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
209 selModel
->setCurrentIndex(index
, QItemSelectionModel::Select
);
216 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
218 #if defined(USE_PROXY_MODEL)
219 const QModelIndex
& dirIndex
= m_proxyModel
->mapToSource(index
);
220 const KFileItem
* item
= m_dirModel
->itemForIndex(dirIndex
);
222 const KFileItem
* item
= m_dirModel
->itemForIndex(index
);
225 const KUrl
& url
= item
->url();
226 mainWindow()->activeView()->setUrl(url
);
230 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
231 const QModelIndex
& index
)
233 if (index
.isValid()) {
234 #if defined(USE_PROXY_MODEL)
235 const QModelIndex
& dirIndex
= m_proxyModel
->mapToSource(index
);
236 KFileItem
* item
= m_dirModel
->itemForIndex(dirIndex
);
238 KFileItem
* item
= m_dirModel
->itemForIndex(index
);
242 mainWindow()->dropUrls(urls
, item
->url());
247 void TreeViewSidebarPage::connectToActiveView()
249 const QWidget
* parent
= parentWidget();
250 if ((parent
== 0) || parent
->isHidden()) {
254 const DolphinView
* view
= mainWindow()->activeView();
255 const KUrl
& url
= view
->url();
257 connect(view
, SIGNAL(urlChanged(const KUrl
&)),
258 this, SLOT(updateSelection(const KUrl
&)));
260 updateSelection(url
);
263 #include "treeviewsidebarpage.moc"