]>
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(doubleClicked(const QModelIndex
&)),
82 this, SLOT(slotDoubleClicked(const QModelIndex
&)));
84 QVBoxLayout
* layout
= new QVBoxLayout(this);
85 layout
->addWidget(m_treeView
);
88 TreeViewSidebarPage::~TreeViewSidebarPage()
94 void TreeViewSidebarPage::activeViewChanged()
96 connectToActiveView();
99 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
101 SidebarPage::showEvent(event
);
102 connectToActiveView();
105 void TreeViewSidebarPage::updateSelection(const KUrl
& url
)
107 if (!url
.isValid() || (url
== m_selectedUrl
)) {
113 // adjust the root of the tree to the base bookmark
114 const KUrl baseUrl
= BookmarkSelector::baseBookmark(url
).url();
115 if (m_dirLister
->url() != baseUrl
) {
117 m_dirLister
->openUrl(baseUrl
);
120 // select the folder which contains the given URL
121 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
122 selModel
->clearSelection();
124 const KFileItem
item(S_IFDIR
, KFileItem::Unknown
, url
);
126 const QModelIndex index
= m_dirModel
->indexForItem(item
);
127 if (index
.isValid()) {
128 #if defined(USE_PROXY_MODEL)
129 // the item with the given URL is already part of the model
130 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
131 m_treeView
->scrollTo(proxyIndex
);
132 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
134 m_treeView
->scrollTo(index
);
135 selModel
->setCurrentIndex(index
, QItemSelectionModel::Select
);
139 // The item with the given URL is not loaded by the model yet. Iterate
140 // backward to the base URL and trigger the loading of the items for
141 // each hierarchy level.
142 connect(m_dirLister
, SIGNAL(completed()),
143 this, SLOT(expandSelectionParent()));
145 KUrl parentUrl
= url
.upUrl();
146 while (!parentUrl
.isParentOf(baseUrl
)) {
147 m_dirLister
->openUrl(parentUrl
, true, false);
148 parentUrl
= parentUrl
.upUrl();
153 void TreeViewSidebarPage::expandSelectionParent()
155 disconnect(m_dirLister
, SIGNAL(completed()),
156 this, SLOT(expandSelectionParent()));
158 // expand the parent folder of the selected item
159 const KFileItem
parentItem(S_IFDIR
, KFileItem::Unknown
, m_selectedUrl
.upUrl());
160 QModelIndex index
= m_dirModel
->indexForItem(parentItem
);
161 if (index
.isValid()) {
162 #if defined(USE_PROXY_MODEL)
163 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
164 m_treeView
->setExpanded(proxyIndex
, true);
166 m_treeView
->setExpanded(index
, true);
169 // select the item and assure that the item is visible
170 const KFileItem
selectedItem(S_IFDIR
, KFileItem::Unknown
, m_selectedUrl
);
171 index
= m_dirModel
->indexForItem(selectedItem
);
172 if (index
.isValid()) {
173 #if defined(USE_PROXY_MODEL)
174 proxyIndex
= m_proxyModel
->mapFromSource(index
);
175 m_treeView
->scrollTo(proxyIndex
);
177 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
178 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
180 m_treeView
->scrollTo(index
);
182 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
183 selModel
->setCurrentIndex(index
, QItemSelectionModel::Select
);
190 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
192 #if defined(USE_PROXY_MODEL)
193 const QModelIndex
& dirIndex
= m_proxyModel
->mapToSource(index
);
194 const KFileItem
* item
= m_dirModel
->itemForIndex(dirIndex
);
196 const KFileItem
* item
= m_dirModel
->itemForIndex(index
);
199 const KUrl
& url
= item
->url();
200 mainWindow()->activeView()->setUrl(url
);
204 void TreeViewSidebarPage::connectToActiveView()
206 const QWidget
* parent
= parentWidget();
207 if ((parent
== 0) || parent
->isHidden()) {
211 const DolphinView
* view
= mainWindow()->activeView();
212 const KUrl
& url
= view
->url();
214 connect(view
, SIGNAL(urlChanged(const KUrl
&)),
215 this, SLOT(updateSelection(const KUrl
&)));
217 updateSelection(url
);
220 #include "treeviewsidebarpage.moc"