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
) :
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 connect(m_dirLister
, SIGNAL(started(const KUrl
&)),
93 this, SLOT(slotDirListerStarted(const KUrl
&)));
94 connect(m_dirLister
, SIGNAL(completed()),
95 this, SLOT(triggerLoadSubTree()));
97 Q_ASSERT(m_dolphinModel
== 0);
98 m_dolphinModel
= new DolphinModel(this);
99 m_dolphinModel
->setDirLister(m_dirLister
);
100 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
101 connect(m_dolphinModel
, SIGNAL(expand(const QModelIndex
&)),
102 this, SLOT(triggerExpanding()));
104 Q_ASSERT(m_proxyModel
== 0);
105 m_proxyModel
= new DolphinSortFilterProxyModel(this);
106 m_proxyModel
->setSourceModel(m_dolphinModel
);
108 Q_ASSERT(m_treeView
== 0);
109 m_treeView
= new SidebarTreeView(this);
110 m_treeView
->setModel(m_proxyModel
);
111 m_proxyModel
->setSorting(DolphinView::SortByName
);
112 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
114 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
115 this, SLOT(updateActiveView(const QModelIndex
&)));
116 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
117 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
119 QVBoxLayout
* layout
= new QVBoxLayout(this);
120 layout
->setMargin(0);
121 layout
->addWidget(m_treeView
);
125 SidebarPage::showEvent(event
);
128 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
130 SidebarPage::contextMenuEvent(event
);
132 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
133 if (!index
.isValid()) {
134 // only open a context menu above a directory item
138 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
139 KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
141 emit
changeSelection(KFileItemList());
142 TreeViewContextMenu
contextMenu(this, item
);
146 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
148 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
149 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
150 if (!item
.isNull()) {
151 emit
changeUrl(item
.url());
155 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
156 const QModelIndex
& index
)
158 if (index
.isValid()) {
159 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
160 KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
161 Q_ASSERT(!item
.isNull());
163 emit
urlsDropped(urls
, item
.url());
168 void TreeViewSidebarPage::triggerExpanding()
170 // the expanding of the folders may not be done in the context
172 QMetaObject::invokeMethod(this, "expandToLeafDir", Qt::QueuedConnection
);
175 void TreeViewSidebarPage::triggerLoadSubTree()
177 // the loading of the sub tree may not be done in the context
179 QMetaObject::invokeMethod(this, "loadSubTree", Qt::QueuedConnection
);
182 void TreeViewSidebarPage::expandToLeafDir()
184 // expand all directories until the parent directory of m_leafDir
185 const KUrl parentUrl
= m_leafDir
.upUrl();
186 QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(parentUrl
);
187 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
188 m_treeView
->setExpanded(proxyIndex
, true);
190 // assure that m_leafDir gets selected
191 dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
192 proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
193 m_treeView
->scrollTo(proxyIndex
);
195 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
196 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
200 void TreeViewSidebarPage::loadSubTree()
202 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
203 selModel
->clearSelection();
205 if (m_leafDir
.isParentOf(m_dirLister
->url())) {
206 // The leaf directory is not a child of the base URL, hence
207 // no sub directory must be loaded or selected.
211 const QModelIndex index
= m_dolphinModel
->indexForUrl(m_leafDir
);
212 if (index
.isValid()) {
213 // the item with the given URL is already part of the model
214 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
215 m_treeView
->scrollTo(proxyIndex
);
216 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
218 // Load all sub directories that need to get expanded for making
219 // the leaf directory visible. The slot triggerExpanding() will
220 // get invoked if the expanding has been finished.
221 m_dolphinModel
->expandToUrl(m_leafDir
);
225 void TreeViewSidebarPage::loadTree(const KUrl
& url
)
227 Q_ASSERT(m_dirLister
!= 0);
230 // adjust the root of the tree to the base place
231 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
232 KUrl baseUrl
= placesModel
->url(placesModel
->closestItem(url
));
233 if (!baseUrl
.isValid()) {
234 // it's possible that no closest item is available and hence an
235 // empty URL is returned
239 if (m_dirLister
->url() != baseUrl
) {
241 m_dirLister
->openUrl(baseUrl
, false, true);
247 #include "treeviewsidebarpage.moc"