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 "dolphinmodel.h"
23 #include "dolphinmainwindow.h"
24 #include "dolphinsortfilterproxymodel.h"
25 #include "dolphinview.h"
26 #include "dolphinsettings.h"
27 #include "sidebartreeview.h"
28 #include "treeviewcontextmenu.h"
30 #include <kfileplacesmodel.h>
31 #include <kdirlister.h>
32 #include <kfileitem.h>
34 #include <QItemSelection>
37 #include <QModelIndex>
39 TreeViewSidebarPage::TreeViewSidebarPage(QWidget
* parent
) :
41 m_dirListerCompleted(false),
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 m_dirListerCompleted
= true;
94 connect(m_dirLister
, SIGNAL(started(const KUrl
&)),
95 this, SLOT(slotDirListerStarted(const KUrl
&)));
96 connect(m_dirLister
, SIGNAL(completed()),
97 this, SLOT(slotDirListerCompleted()));
99 Q_ASSERT(m_dolphinModel
== 0);
100 m_dolphinModel
= new DolphinModel(this);
101 m_dolphinModel
->setDirLister(m_dirLister
);
102 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
103 connect(m_dolphinModel
, SIGNAL(expand(const QModelIndex
&)),
104 this, SLOT(triggerExpanding(const QModelIndex
&)));
106 Q_ASSERT(m_proxyModel
== 0);
107 m_proxyModel
= new DolphinSortFilterProxyModel(this);
108 m_proxyModel
->setSourceModel(m_dolphinModel
);
110 Q_ASSERT(m_treeView
== 0);
111 m_treeView
= new SidebarTreeView(this);
112 m_treeView
->setModel(m_proxyModel
);
113 m_proxyModel
->setSorting(DolphinView::SortByName
);
114 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
116 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
117 this, SLOT(updateActiveView(const QModelIndex
&)));
118 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
119 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
121 QVBoxLayout
* layout
= new QVBoxLayout(this);
122 layout
->setMargin(0);
123 layout
->addWidget(m_treeView
);
127 SidebarPage::showEvent(event
);
130 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
132 SidebarPage::contextMenuEvent(event
);
134 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
135 if (!index
.isValid()) {
136 // only open a context menu above a directory item
140 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
141 KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
143 emit
changeSelection(QList
<KFileItem
>());
144 TreeViewContextMenu
contextMenu(this, item
);
148 void TreeViewSidebarPage::expandSelectionParent()
150 disconnect(m_dirLister
, SIGNAL(completed()),
151 this, SLOT(expandSelectionParent()));
153 // expand the parent folder of the selected item
154 KUrl parentUrl
= url().upUrl();
155 if (!m_dirLister
->url().isParentOf(parentUrl
)) {
159 QModelIndex index
= m_dolphinModel
->indexForUrl(parentUrl
);
160 if (index
.isValid()) {
161 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
162 m_treeView
->setExpanded(proxyIndex
, true);
164 // select the item and assure that the item is visible
165 index
= m_dolphinModel
->indexForUrl(url());
166 if (index
.isValid()) {
167 proxyIndex
= m_proxyModel
->mapFromSource(index
);
168 m_treeView
->scrollTo(proxyIndex
);
170 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
171 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
176 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
178 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
179 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
180 if (!item
.isNull()) {
181 emit
changeUrl(item
.url());
185 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
186 const QModelIndex
& index
)
188 if (index
.isValid()) {
189 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
190 KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
191 Q_ASSERT(!item
.isNull());
193 emit
urlsDropped(urls
, item
.url());
198 void TreeViewSidebarPage::triggerExpanding(const QModelIndex
& index
)
201 // the expanding of the folders may not be done in the context
203 QMetaObject::invokeMethod(this, "expandToLeafDir", Qt::QueuedConnection
);
206 void TreeViewSidebarPage::expandToLeafDir()
208 // expand all directories until the parent directory of m_leafDir
209 const KUrl parentUrl
= m_leafDir
.upUrl();
210 QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(parentUrl
);
211 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
212 m_treeView
->setExpanded(proxyIndex
, true);
214 // assure that m_leafDir gets selected
215 dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
216 proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
217 m_treeView
->scrollTo(proxyIndex
);
219 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
220 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
223 void TreeViewSidebarPage::loadSubTree()
225 disconnect(m_dirLister
, SIGNAL(completed()),
226 this, SLOT(loadSubTree()));
228 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
229 selModel
->clearSelection();
231 if (m_leafDir
.isParentOf(m_dirLister
->url())) {
232 // The leaf directory is not a child of the base URL, hence
233 // no sub directory must be loaded or selected.
237 const QModelIndex index
= m_dolphinModel
->indexForUrl(m_leafDir
);
238 if (index
.isValid()) {
239 // the item with the given URL is already part of the model
240 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
241 m_treeView
->scrollTo(proxyIndex
);
242 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
244 // Load all sub directories that need to get expanded for making
245 // the leaf directory visible. The slot triggerExpanding() will
246 // get invoked if the expanding has been finished.
247 Q_ASSERT(m_dirListerCompleted
);
248 m_dolphinModel
->expandToUrl(m_leafDir
);
252 void TreeViewSidebarPage::slotDirListerStarted(const KUrl
& url
)
255 m_dirListerCompleted
= false;
258 void TreeViewSidebarPage::slotDirListerCompleted()
260 m_dirListerCompleted
= true;
264 void TreeViewSidebarPage::loadTree(const KUrl
& url
)
266 Q_ASSERT(m_dirLister
!= 0);
269 // adjust the root of the tree to the base place
270 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
271 KUrl baseUrl
= placesModel
->url(placesModel
->closestItem(url
));
272 if (!baseUrl
.isValid()) {
273 // it's possible that no closest item is available and hence an
274 // empty URL is returned
278 connect(m_dirLister
, SIGNAL(completed()),
279 this, SLOT(loadSubTree()));
280 if ((m_dirLister
->url() != baseUrl
) || !m_dirListerCompleted
) {
282 m_dirLister
->openUrl(baseUrl
);
288 #include "treeviewsidebarpage.moc"