]>
cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewsidebarpage.cpp
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 "dolphinmainwindow.h"
24 #include "dolphinsortfilterproxymodel.h"
25 #include "dolphinview.h"
26 #include "sidebartreeview.h"
28 #include <kdirlister.h>
29 #include <kdirmodel.h>
30 #include <kfileitem.h>
32 #include <QHeaderView>
33 #include <QItemSelectionModel>
35 #include <QVBoxLayout>
37 // TODO: currently when using a proxy model the strange effect occurs
38 // that items get duplicated. Activate the following define to have the proxy
40 //#define USE_PROXY_MODEL
42 TreeViewSidebarPage::TreeViewSidebarPage(DolphinMainWindow
* mainWindow
,
44 SidebarPage(mainWindow
, parent
),
51 Q_ASSERT(mainWindow
!= 0);
53 m_dirLister
= new KDirLister();
54 m_dirLister
->setDirOnlyMode(true);
55 m_dirLister
->setAutoUpdate(true);
56 m_dirLister
->setMainWindow(this);
57 m_dirLister
->setDelayedMimeTypes(true);
58 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
60 m_dirModel
= new KDirModel();
61 m_dirModel
->setDirLister(m_dirLister
);
62 m_dirModel
->setDropsAllowed(KDirModel::DropOnDirectory
);
65 #if defined(USE_PROXY_MODEL)
66 m_proxyModel
= new DolphinSortFilterProxyModel(this);
67 m_proxyModel
->setSourceModel(m_dirModel
);
69 m_treeView
= new SidebarTreeView(mainWindow
, this);
70 m_treeView
->setModel(m_proxyModel
);
72 m_proxyModel
->setSorting(DolphinView::SortByName
);
73 m_proxyModel
->setSortOrder(Qt::Ascending
);
75 m_treeView
= new SidebarTreeView(mainWindow
, this);
76 m_treeView
->setModel(m_dirModel
);
79 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
80 this, SLOT(updateActiveView(const QModelIndex
&)));
81 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
82 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
84 QVBoxLayout
* layout
= new QVBoxLayout(this);
86 layout
->addWidget(m_treeView
);
89 TreeViewSidebarPage::~TreeViewSidebarPage()
95 void TreeViewSidebarPage::activeViewChanged()
97 connectToActiveView();
100 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
102 SidebarPage::showEvent(event
);
103 connectToActiveView();
106 void TreeViewSidebarPage::updateSelection(const KUrl
& url
)
108 if (!url
.isValid() || (url
== m_selectedUrl
)) {
114 // adjust the root of the tree to the base bookmark
115 const KUrl baseUrl
= BookmarkSelector::baseBookmark(url
).url();
116 if (m_dirLister
->url() != baseUrl
) {
118 m_dirLister
->openUrl(baseUrl
);
121 // select the folder which contains the given URL
122 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
123 selModel
->clearSelection();
125 const KFileItem
item(S_IFDIR
, KFileItem::Unknown
, url
);
127 const QModelIndex index
= m_dirModel
->indexForItem(item
);
128 if (index
.isValid()) {
129 #if defined(USE_PROXY_MODEL)
130 // the item with the given URL is already part of the model
131 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
132 m_treeView
->scrollTo(proxyIndex
);
133 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
135 m_treeView
->scrollTo(index
);
136 selModel
->setCurrentIndex(index
, QItemSelectionModel::Select
);
140 // The item with the given URL is not loaded by the model yet. Iterate
141 // backward to the base URL and trigger the loading of the items for
142 // each hierarchy level.
143 connect(m_dirLister
, SIGNAL(completed()),
144 this, SLOT(expandSelectionParent()));
146 KUrl parentUrl
= url
.upUrl();
147 while (!parentUrl
.isParentOf(baseUrl
)) {
148 m_dirLister
->openUrl(parentUrl
, true, false);
149 parentUrl
= parentUrl
.upUrl();
154 void TreeViewSidebarPage::expandSelectionParent()
156 disconnect(m_dirLister
, SIGNAL(completed()),
157 this, SLOT(expandSelectionParent()));
159 // expand the parent folder of the selected item
160 const KFileItem
parentItem(S_IFDIR
, KFileItem::Unknown
, m_selectedUrl
.upUrl());
161 QModelIndex index
= m_dirModel
->indexForItem(parentItem
);
162 if (index
.isValid()) {
163 #if defined(USE_PROXY_MODEL)
164 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
165 m_treeView
->setExpanded(proxyIndex
, true);
167 m_treeView
->setExpanded(index
, true);
170 // select the item and assure that the item is visible
171 const KFileItem
selectedItem(S_IFDIR
, KFileItem::Unknown
, m_selectedUrl
);
172 index
= m_dirModel
->indexForItem(selectedItem
);
173 if (index
.isValid()) {
174 #if defined(USE_PROXY_MODEL)
175 proxyIndex
= m_proxyModel
->mapFromSource(index
);
176 m_treeView
->scrollTo(proxyIndex
);
178 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
179 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
181 m_treeView
->scrollTo(index
);
183 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
184 selModel
->setCurrentIndex(index
, QItemSelectionModel::Select
);
191 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
193 #if defined(USE_PROXY_MODEL)
194 const QModelIndex
& dirIndex
= m_proxyModel
->mapToSource(index
);
195 const KFileItem
* item
= m_dirModel
->itemForIndex(dirIndex
);
197 const KFileItem
* item
= m_dirModel
->itemForIndex(index
);
200 const KUrl
& url
= item
->url();
201 mainWindow()->activeView()->setUrl(url
);
205 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
206 const QModelIndex
& index
)
208 if (index
.isValid()) {
209 #if defined(USE_PROXY_MODEL)
210 const QModelIndex
& dirIndex
= m_proxyModel
->mapToSource(index
);
211 KFileItem
* item
= m_dirModel
->itemForIndex(dirIndex
);
213 KFileItem
* item
= m_dirModel
->itemForIndex(index
);
217 mainWindow()->dropUrls(urls
, item
->url());
222 void TreeViewSidebarPage::connectToActiveView()
224 const QWidget
* parent
= parentWidget();
225 if ((parent
== 0) || parent
->isHidden()) {
229 const DolphinView
* view
= mainWindow()->activeView();
230 const KUrl
& url
= view
->url();
232 connect(view
, SIGNAL(urlChanged(const KUrl
&)),
233 this, SLOT(updateSelection(const KUrl
&)));
235 updateSelection(url
);
238 #include "treeviewsidebarpage.moc"