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 return QSize(200, 400);
60 void TreeViewSidebarPage::setUrl(const KUrl
& url
)
62 if (!url
.isValid() || (url
== SidebarPage::url())) {
66 SidebarPage::setUrl(url
);
67 if (m_dirLister
!= 0) {
72 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
74 if (event
->spontaneous()) {
75 SidebarPage::showEvent(event
);
79 if (m_dirLister
== 0) {
80 // Postpone the creating of the dir lister to the first show event.
81 // This assures that no performance and memory overhead is given when the TreeView is not
82 // used at all (see TreeViewSidebarPage::setUrl()).
83 m_dirLister
= new KDirLister();
84 m_dirLister
->setDirOnlyMode(true);
85 m_dirLister
->setAutoUpdate(true);
86 m_dirLister
->setMainWindow(this);
87 m_dirLister
->setDelayedMimeTypes(true);
88 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
90 connect(m_dirLister
, SIGNAL(completed()),
91 this, SLOT(triggerLoadSubTree()));
93 Q_ASSERT(m_dolphinModel
== 0);
94 m_dolphinModel
= new DolphinModel(this);
95 m_dolphinModel
->setDirLister(m_dirLister
);
96 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
97 connect(m_dolphinModel
, SIGNAL(expand(const QModelIndex
&)),
98 this, SLOT(triggerExpanding()));
100 Q_ASSERT(m_proxyModel
== 0);
101 m_proxyModel
= new DolphinSortFilterProxyModel(this);
102 m_proxyModel
->setSourceModel(m_dolphinModel
);
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 dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
135 KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
137 emit
changeSelection(KFileItemList());
138 TreeViewContextMenu
contextMenu(this, item
);
142 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
144 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
145 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
146 if (!item
.isNull()) {
147 emit
changeUrl(item
.url());
151 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
152 const QModelIndex
& index
)
154 if (index
.isValid()) {
155 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
156 KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
157 Q_ASSERT(!item
.isNull());
159 emit
urlsDropped(urls
, item
.url());
164 void TreeViewSidebarPage::triggerExpanding()
166 // the expanding of the folders may not be done in the context
168 QMetaObject::invokeMethod(this, "expandToLeafDir", Qt::QueuedConnection
);
171 void TreeViewSidebarPage::triggerLoadSubTree()
173 // the loading of the sub tree may not be done in the context
175 QMetaObject::invokeMethod(this, "loadSubTree", Qt::QueuedConnection
);
178 void TreeViewSidebarPage::expandToLeafDir()
180 // expand all directories until the parent directory of m_leafDir
181 const KUrl parentUrl
= m_leafDir
.upUrl();
182 QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(parentUrl
);
183 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
184 m_treeView
->setExpanded(proxyIndex
, true);
186 // assure that m_leafDir gets selected
187 dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
188 proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
189 m_treeView
->scrollTo(proxyIndex
);
191 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
192 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
196 void TreeViewSidebarPage::loadSubTree()
198 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
199 selModel
->clearSelection();
201 if (m_leafDir
.isParentOf(m_dirLister
->url())) {
202 // The leaf directory is not a child of the base URL, hence
203 // no sub directory must be loaded or selected.
207 const QModelIndex index
= m_dolphinModel
->indexForUrl(m_leafDir
);
208 if (index
.isValid()) {
209 // the item with the given URL is already part of the model
210 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
211 m_treeView
->scrollTo(proxyIndex
);
212 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
214 // Load all sub directories that need to get expanded for making
215 // the leaf directory visible. The slot triggerExpanding() will
216 // get invoked if the expanding has been finished.
217 m_dolphinModel
->expandToUrl(m_leafDir
);
221 void TreeViewSidebarPage::loadTree(const KUrl
& url
)
223 Q_ASSERT(m_dirLister
!= 0);
226 // adjust the root of the tree to the base place
227 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
228 KUrl baseUrl
= placesModel
->url(placesModel
->closestItem(url
));
229 if (!baseUrl
.isValid()) {
230 // it's possible that no closest item is available and hence an
231 // empty URL is returned
235 if (m_dirLister
->url() != baseUrl
) {
237 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
243 #include "treeviewsidebarpage.moc"