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 "kbookmarkmanager.h"
23 #include "dolphinmainwindow.h"
24 #include "dolphinsortfilterproxymodel.h"
25 #include "dolphinview.h"
26 #include "sidebartreeview.h"
27 #include "treeviewcontextmenu.h"
29 #include <kdirlister.h>
30 #include <kdirmodel.h>
31 #include <kfileitem.h>
33 #include <QHeaderView>
34 #include <QItemSelectionModel>
36 #include <QVBoxLayout>
37 #include "dolphinsettings.h"
39 // TODO: currently when using a proxy model the strange effect occurs
40 // that items get duplicated. Activate the following define to have the proxy
42 //#define USE_PROXY_MODEL
44 TreeViewSidebarPage::TreeViewSidebarPage(QWidget
* parent
) :
51 m_dirLister
= new KDirLister();
52 m_dirLister
->setDirOnlyMode(true);
53 m_dirLister
->setAutoUpdate(true);
54 m_dirLister
->setMainWindow(this);
55 m_dirLister
->setDelayedMimeTypes(true);
56 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
58 m_dirModel
= new KDirModel();
59 m_dirModel
->setDirLister(m_dirLister
);
60 m_dirModel
->setDropsAllowed(KDirModel::DropOnDirectory
);
63 #if defined(USE_PROXY_MODEL)
64 m_proxyModel
= new DolphinSortFilterProxyModel(this);
65 m_proxyModel
->setSourceModel(m_dirModel
);
67 m_treeView
= new SidebarTreeView(this);
68 m_treeView
->setModel(m_proxyModel
);
70 m_proxyModel
->setSorting(DolphinView::SortByName
);
71 m_proxyModel
->setSortOrder(Qt::Ascending
);
73 m_treeView
= new SidebarTreeView(this);
74 m_treeView
->setModel(m_dirModel
);
77 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
78 this, SLOT(updateActiveView(const QModelIndex
&)));
79 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
80 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
82 QVBoxLayout
* layout
= new QVBoxLayout(this);
84 layout
->addWidget(m_treeView
);
87 TreeViewSidebarPage::~TreeViewSidebarPage()
93 void TreeViewSidebarPage::setUrl(const KUrl
& url
)
95 if (!url
.isValid() || (url
== m_url
)) {
101 // adjust the root of the tree to the base bookmark
102 KBookmarkManager
* bookmarkManager
= DolphinSettings::instance().bookmarkManager();
103 const KUrl baseUrl
= bookmarkManager
->root().closestBookmark(url
).url();
104 if (m_dirLister
->url() != baseUrl
) {
106 m_dirLister
->openUrl(baseUrl
);
109 // select the folder which contains the given URL
110 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
111 selModel
->clearSelection();
113 const QModelIndex index
= m_dirModel
->indexForUrl(url
);
114 if (index
.isValid()) {
115 #if defined(USE_PROXY_MODEL)
116 // the item with the given URL is already part of the model
117 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
118 m_treeView
->scrollTo(proxyIndex
);
119 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
121 m_treeView
->scrollTo(index
);
122 selModel
->setCurrentIndex(index
, QItemSelectionModel::Select
);
126 // The item with the given URL is not loaded by the model yet. Iterate
127 // backward to the base URL and trigger the loading of the items for
128 // each hierarchy level.
129 connect(m_dirLister
, SIGNAL(completed()),
130 this, SLOT(expandSelectionParent()));
132 KUrl parentUrl
= url
.upUrl();
133 while (!parentUrl
.isParentOf(baseUrl
)) {
134 m_dirLister
->openUrl(parentUrl
, true, false);
135 parentUrl
= parentUrl
.upUrl();
141 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
143 SidebarPage::showEvent(event
);
146 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
148 SidebarPage::contextMenuEvent(event
);
150 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
151 if (!index
.isValid()) {
152 // only open a context menu above a directory item
156 #if defined(USE_PROXY_MODEL)
157 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
158 KFileItem
* item
= m_dirModel
->itemForIndex(dirModelIndex
);
160 KFileItem
* item
= m_dirModel
->itemForIndex(index
);
163 emit
changeSelection(KFileItemList());
164 TreeViewContextMenu
contextMenu(this, item
);
168 void TreeViewSidebarPage::expandSelectionParent()
170 disconnect(m_dirLister
, SIGNAL(completed()),
171 this, SLOT(expandSelectionParent()));
173 // expand the parent folder of the selected item
174 KUrl parentUrl
= m_url
.upUrl();
175 if (!m_dirLister
->url().isParentOf(parentUrl
)) {
179 QModelIndex index
= m_dirModel
->indexForUrl(parentUrl
);
180 if (index
.isValid()) {
181 #if defined(USE_PROXY_MODEL)
182 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
183 m_treeView
->setExpanded(proxyIndex
, true);
185 m_treeView
->setExpanded(index
, true);
188 // select the item and assure that the item is visible
189 index
= m_dirModel
->indexForUrl(m_url
);
190 if (index
.isValid()) {
191 #if defined(USE_PROXY_MODEL)
192 proxyIndex
= m_proxyModel
->mapFromSource(index
);
193 m_treeView
->scrollTo(proxyIndex
);
195 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
196 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
198 m_treeView
->scrollTo(index
);
200 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
201 selModel
->setCurrentIndex(index
, QItemSelectionModel::Select
);
207 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
209 #if defined(USE_PROXY_MODEL)
210 const QModelIndex
& dirIndex
= m_proxyModel
->mapToSource(index
);
211 const KFileItem
* item
= m_dirModel
->itemForIndex(dirIndex
);
213 const KFileItem
* item
= m_dirModel
->itemForIndex(index
);
216 const KUrl
& url
= item
->url();
221 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
222 const QModelIndex
& index
)
224 if (index
.isValid()) {
225 #if defined(USE_PROXY_MODEL)
226 const QModelIndex
& dirIndex
= m_proxyModel
->mapToSource(index
);
227 KFileItem
* item
= m_dirModel
->itemForIndex(dirIndex
);
229 KFileItem
* item
= m_dirModel
->itemForIndex(index
);
233 emit
urlsDropped(urls
, item
->url());
238 #include "treeviewsidebarpage.moc"