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 "dolphinmainwindow.h"
23 #include "dolphinsortfilterproxymodel.h"
24 #include "dolphinview.h"
25 #include "dolphinsettings.h"
26 #include "sidebartreeview.h"
27 #include "treeviewcontextmenu.h"
29 #include <kfileplacesmodel.h>
30 #include <kdirlister.h>
31 #include <kdirmodel.h>
32 #include <kfileitem.h>
34 #include <QItemSelection>
37 #include <QModelIndex>
40 TreeViewSidebarPage::TreeViewSidebarPage(QWidget
* parent
) :
50 TreeViewSidebarPage::~TreeViewSidebarPage()
56 QSize
TreeViewSidebarPage::sizeHint() const
58 QSize size
= SidebarPage::sizeHint();
63 void TreeViewSidebarPage::setUrl(const KUrl
& url
)
65 if (!url
.isValid() || (url
== SidebarPage::url())) {
69 SidebarPage::setUrl(url
);
70 if (m_dirLister
!= 0) {
75 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
77 if (event
->spontaneous()) {
78 SidebarPage::showEvent(event
);
82 if (m_dirLister
== 0) {
83 // Postpone the creating of the dir lister to the first show event.
84 // This assures that no performance and memory overhead is given when the TreeView is not
85 // used at all (see TreeViewSidebarPage::setUrl()).
86 m_dirLister
= new KDirLister();
87 m_dirLister
->setDirOnlyMode(true);
88 m_dirLister
->setAutoUpdate(true);
89 m_dirLister
->setMainWindow(this);
90 m_dirLister
->setDelayedMimeTypes(true);
91 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
93 Q_ASSERT(m_dirModel
== 0);
94 m_dirModel
= new KDirModel(this);
95 m_dirModel
->setDirLister(m_dirLister
);
96 m_dirModel
->setDropsAllowed(KDirModel::DropOnDirectory
);
97 connect(m_dirModel
, SIGNAL(expand(const QModelIndex
&)),
98 this, SLOT(triggerExpanding(const QModelIndex
&)));
100 Q_ASSERT(m_proxyModel
== 0);
101 m_proxyModel
= new DolphinSortFilterProxyModel(this);
102 m_proxyModel
->setSourceModel(m_dirModel
);
104 Q_ASSERT(m_treeView
== 0);
105 m_treeView
= new SidebarTreeView(this);
106 m_treeView
->setModel(m_proxyModel
);
107 m_proxyModel
->setSorting(DolphinView::SortByName
);
108 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
110 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
111 this, SLOT(updateActiveView(const QModelIndex
&)));
112 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
113 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
115 QVBoxLayout
* layout
= new QVBoxLayout(this);
116 layout
->setMargin(0);
117 layout
->addWidget(m_treeView
);
121 SidebarPage::showEvent(event
);
124 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
126 SidebarPage::contextMenuEvent(event
);
128 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
129 if (!index
.isValid()) {
130 // only open a context menu above a directory item
134 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
135 KFileItem item
= m_dirModel
->itemForIndex(dirModelIndex
);
137 emit
changeSelection(QList
<KFileItem
>());
138 TreeViewContextMenu
contextMenu(this, item
);
142 void TreeViewSidebarPage::expandSelectionParent()
144 disconnect(m_dirLister
, SIGNAL(completed()),
145 this, SLOT(expandSelectionParent()));
147 // expand the parent folder of the selected item
148 KUrl parentUrl
= url().upUrl();
149 if (!m_dirLister
->url().isParentOf(parentUrl
)) {
153 QModelIndex index
= m_dirModel
->indexForUrl(parentUrl
);
154 if (index
.isValid()) {
155 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
156 m_treeView
->setExpanded(proxyIndex
, true);
158 // select the item and assure that the item is visible
159 index
= m_dirModel
->indexForUrl(url());
160 if (index
.isValid()) {
161 proxyIndex
= m_proxyModel
->mapFromSource(index
);
162 m_treeView
->scrollTo(proxyIndex
);
164 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
165 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
170 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
172 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
173 const KFileItem item
= m_dirModel
->itemForIndex(dirIndex
);
174 if (!item
.isNull()) {
175 emit
changeUrl(item
.url());
179 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
180 const QModelIndex
& index
)
182 if (index
.isValid()) {
183 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
184 KFileItem item
= m_dirModel
->itemForIndex(dirIndex
);
185 Q_ASSERT(!item
.isNull());
187 emit
urlsDropped(urls
, item
.url());
192 void TreeViewSidebarPage::triggerExpanding(const QModelIndex
& index
)
195 // the expanding of the folders may not be done in the context
197 QTimer::singleShot(0, this, SLOT(expandToLeafDir()));
200 void TreeViewSidebarPage::expandToLeafDir()
202 // expand all directories until the parent directory of m_leafDir
203 const KUrl parentUrl
= m_leafDir
.upUrl();
204 QModelIndex dirIndex
= m_dirModel
->indexForUrl(parentUrl
);
205 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
206 m_treeView
->setExpanded(proxyIndex
, true);
208 // assure that m_leafDir gets selected
209 dirIndex
= m_dirModel
->indexForUrl(m_leafDir
);
210 proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
211 m_treeView
->scrollTo(proxyIndex
);
213 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
214 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
217 void TreeViewSidebarPage::loadSubTree()
219 disconnect(m_dirLister
, SIGNAL(completed()),
220 this, SLOT(loadSubTree()));
222 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
223 selModel
->clearSelection();
225 if (m_leafDir
.isParentOf(m_dirLister
->url())) {
226 // The leaf directory is not a child of the base URL, hence
227 // no sub directory must be loaded or selected.
231 const QModelIndex index
= m_dirModel
->indexForUrl(m_leafDir
);
232 if (index
.isValid()) {
233 // the item with the given URL is already part of the model
234 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
235 m_treeView
->scrollTo(proxyIndex
);
236 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
238 // Load all sub directories that need to get expanded for making
239 // the leaf directory visible. The slot triggerExpanding() will
240 // get invoked if the expanding has been finished.
241 m_dirModel
->expandToUrl(m_leafDir
);
245 void TreeViewSidebarPage::loadTree(const KUrl
& url
)
247 Q_ASSERT(m_dirLister
!= 0);
250 // adjust the root of the tree to the base place
251 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
252 KUrl baseUrl
= placesModel
->url(placesModel
->closestItem(url
));
253 if (!baseUrl
.isValid()) {
254 // it's possible that no closest item is available and hence an
255 // empty URL is returned
259 connect(m_dirLister
, SIGNAL(completed()),
260 this, SLOT(loadSubTree()));
261 if (m_dirLister
->url() != baseUrl
) {
263 m_dirLister
->openUrl(baseUrl
);
269 #include "treeviewsidebarpage.moc"