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>
39 TreeViewSidebarPage::TreeViewSidebarPage(QWidget
* parent
) :
49 TreeViewSidebarPage::~TreeViewSidebarPage()
55 QSize
TreeViewSidebarPage::sizeHint() const
57 QSize size
= SidebarPage::sizeHint();
62 void TreeViewSidebarPage::setUrl(const KUrl
& url
)
64 if (!url
.isValid() || (url
== SidebarPage::url())) {
68 SidebarPage::setUrl(url
);
69 if (m_dirLister
!= 0) {
74 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
76 if (event
->spontaneous()) {
77 SidebarPage::showEvent(event
);
81 if (m_dirLister
== 0) {
82 // Postpone the creating of the dir lister to the first show event.
83 // This assures that no performance and memory overhead is given when the TreeView is not
84 // used at all (see TreeViewSidebarPage::setUrl()).
85 m_dirLister
= new KDirLister();
86 m_dirLister
->setDirOnlyMode(true);
87 m_dirLister
->setAutoUpdate(true);
88 m_dirLister
->setMainWindow(this);
89 m_dirLister
->setDelayedMimeTypes(true);
90 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
92 Q_ASSERT(m_dirModel
== 0);
93 m_dirModel
= new KDirModel(this);
94 m_dirModel
->setDirLister(m_dirLister
);
95 m_dirModel
->setDropsAllowed(KDirModel::DropOnDirectory
);
96 connect(m_dirModel
, SIGNAL(expand(const QModelIndex
&)),
97 this, SLOT(triggerExpanding(const QModelIndex
&)));
99 Q_ASSERT(m_proxyModel
== 0);
100 m_proxyModel
= new DolphinSortFilterProxyModel(this);
101 m_proxyModel
->setSourceModel(m_dirModel
);
103 Q_ASSERT(m_treeView
== 0);
104 m_treeView
= new SidebarTreeView(this);
105 m_treeView
->setModel(m_proxyModel
);
106 m_proxyModel
->setSorting(DolphinView::SortByName
);
107 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
109 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
110 this, SLOT(updateActiveView(const QModelIndex
&)));
111 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
112 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
114 QVBoxLayout
* layout
= new QVBoxLayout(this);
115 layout
->setMargin(0);
116 layout
->addWidget(m_treeView
);
120 SidebarPage::showEvent(event
);
123 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
125 SidebarPage::contextMenuEvent(event
);
127 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
128 if (!index
.isValid()) {
129 // only open a context menu above a directory item
133 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
134 KFileItem item
= m_dirModel
->itemForIndex(dirModelIndex
);
136 emit
changeSelection(QList
<KFileItem
>());
137 TreeViewContextMenu
contextMenu(this, item
);
141 void TreeViewSidebarPage::expandSelectionParent()
143 disconnect(m_dirLister
, SIGNAL(completed()),
144 this, SLOT(expandSelectionParent()));
146 // expand the parent folder of the selected item
147 KUrl parentUrl
= url().upUrl();
148 if (!m_dirLister
->url().isParentOf(parentUrl
)) {
152 QModelIndex index
= m_dirModel
->indexForUrl(parentUrl
);
153 if (index
.isValid()) {
154 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
155 m_treeView
->setExpanded(proxyIndex
, true);
157 // select the item and assure that the item is visible
158 index
= m_dirModel
->indexForUrl(url());
159 if (index
.isValid()) {
160 proxyIndex
= m_proxyModel
->mapFromSource(index
);
161 m_treeView
->scrollTo(proxyIndex
);
163 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
164 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
169 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
171 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
172 const KFileItem item
= m_dirModel
->itemForIndex(dirIndex
);
173 if (!item
.isNull()) {
174 emit
changeUrl(item
.url());
178 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
179 const QModelIndex
& index
)
181 if (index
.isValid()) {
182 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
183 KFileItem item
= m_dirModel
->itemForIndex(dirIndex
);
184 Q_ASSERT(!item
.isNull());
186 emit
urlsDropped(urls
, item
.url());
191 void TreeViewSidebarPage::triggerExpanding(const QModelIndex
& index
)
194 // the expanding of the folders may not be done in the context
196 QMetaObject::invokeMethod(this, "expandToLeafDir", Qt::QueuedConnection
);
199 void TreeViewSidebarPage::expandToLeafDir()
201 // expand all directories until the parent directory of m_leafDir
202 const KUrl parentUrl
= m_leafDir
.upUrl();
203 QModelIndex dirIndex
= m_dirModel
->indexForUrl(parentUrl
);
204 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
205 m_treeView
->setExpanded(proxyIndex
, true);
207 // assure that m_leafDir gets selected
208 dirIndex
= m_dirModel
->indexForUrl(m_leafDir
);
209 proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
210 m_treeView
->scrollTo(proxyIndex
);
212 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
213 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
216 void TreeViewSidebarPage::loadSubTree()
218 disconnect(m_dirLister
, SIGNAL(completed()),
219 this, SLOT(loadSubTree()));
221 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
222 selModel
->clearSelection();
224 if (m_leafDir
.isParentOf(m_dirLister
->url())) {
225 // The leaf directory is not a child of the base URL, hence
226 // no sub directory must be loaded or selected.
230 const QModelIndex index
= m_dirModel
->indexForUrl(m_leafDir
);
231 if (index
.isValid()) {
232 // the item with the given URL is already part of the model
233 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
234 m_treeView
->scrollTo(proxyIndex
);
235 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
237 // Load all sub directories that need to get expanded for making
238 // the leaf directory visible. The slot triggerExpanding() will
239 // get invoked if the expanding has been finished.
240 m_dirModel
->expandToUrl(m_leafDir
);
244 void TreeViewSidebarPage::loadTree(const KUrl
& url
)
246 Q_ASSERT(m_dirLister
!= 0);
249 // adjust the root of the tree to the base place
250 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
251 KUrl baseUrl
= placesModel
->url(placesModel
->closestItem(url
));
252 if (!baseUrl
.isValid()) {
253 // it's possible that no closest item is available and hence an
254 // empty URL is returned
258 connect(m_dirLister
, SIGNAL(completed()),
259 this, SLOT(loadSubTree()));
260 if (m_dirLister
->url() != baseUrl
) {
262 m_dirLister
->openUrl(baseUrl
);
268 #include "treeviewsidebarpage.moc"