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 "dolphinsortfilterproxymodel.h"
24 #include "dolphinview.h"
25 #include "dolphinsettings.h"
26 #include "dolphin_folderspanelsettings.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>
41 TreeViewSidebarPage::TreeViewSidebarPage(QWidget
* parent
) :
43 m_setLeafVisible(false),
52 TreeViewSidebarPage::~TreeViewSidebarPage()
54 FoldersPanelSettings::self()->writeConfig();
58 delete m_dolphinModel
;
60 m_dirLister
= 0; // deleted by m_dolphinModel
63 QSize
TreeViewSidebarPage::sizeHint() const
65 return QSize(200, 400);
68 void TreeViewSidebarPage::setShowHiddenFiles(bool show
)
70 FoldersPanelSettings::setShowHiddenFiles(show
);
71 if (m_dirLister
!= 0) {
72 m_dirLister
->setShowingDotFiles(show
);
73 m_dirLister
->openUrl(m_dirLister
->url(), KDirLister::Reload
);
77 bool TreeViewSidebarPage::showHiddenFiles() const
79 return FoldersPanelSettings::showHiddenFiles();
82 void TreeViewSidebarPage::setUrl(const KUrl
& url
)
84 if (!url
.isValid() || (url
== SidebarPage::url())) {
88 SidebarPage::setUrl(url
);
89 if (m_dirLister
!= 0) {
90 m_setLeafVisible
= true;
95 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
97 if (event
->spontaneous()) {
98 SidebarPage::showEvent(event
);
102 if (m_dirLister
== 0) {
103 // Postpone the creating of the dir lister to the first show event.
104 // This assures that no performance and memory overhead is given when the TreeView is not
105 // used at all (see TreeViewSidebarPage::setUrl()).
106 m_dirLister
= new KDirLister();
107 m_dirLister
->setDirOnlyMode(true);
108 m_dirLister
->setAutoUpdate(true);
109 m_dirLister
->setMainWindow(window());
110 m_dirLister
->setDelayedMimeTypes(true);
111 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
112 m_dirLister
->setShowingDotFiles(FoldersPanelSettings::showHiddenFiles());
114 connect(m_dirLister
, SIGNAL(completed()),
115 this, SLOT(triggerLoadSubTree()));
117 Q_ASSERT(m_dolphinModel
== 0);
118 m_dolphinModel
= new DolphinModel(this);
119 m_dolphinModel
->setDirLister(m_dirLister
);
120 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
121 connect(m_dolphinModel
, SIGNAL(expand(const QModelIndex
&)),
122 this, SLOT(triggerExpanding()));
124 Q_ASSERT(m_proxyModel
== 0);
125 m_proxyModel
= new DolphinSortFilterProxyModel(this);
126 m_proxyModel
->setSourceModel(m_dolphinModel
);
128 Q_ASSERT(m_treeView
== 0);
129 m_treeView
= new SidebarTreeView(this);
130 m_treeView
->setModel(m_proxyModel
);
131 m_proxyModel
->setSorting(DolphinView::SortByName
);
132 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
134 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
135 this, SLOT(updateActiveView(const QModelIndex
&)));
136 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
137 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
139 QVBoxLayout
* layout
= new QVBoxLayout(this);
140 layout
->setMargin(0);
141 layout
->addWidget(m_treeView
);
145 SidebarPage::showEvent(event
);
148 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
150 SidebarPage::contextMenuEvent(event
);
153 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
154 if (index
.isValid()) {
155 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
156 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
157 emit
changeSelection(KFileItemList());
160 TreeViewContextMenu
contextMenu(this, item
);
164 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
166 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
167 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
168 if (!item
.isNull()) {
169 emit
changeUrl(item
.url());
173 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
174 const QModelIndex
& index
)
176 if (index
.isValid()) {
177 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
178 KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
179 Q_ASSERT(!item
.isNull());
181 emit
urlsDropped(urls
, item
.url());
186 void TreeViewSidebarPage::triggerExpanding()
188 // the expanding of the folders may not be done in the context
190 QMetaObject::invokeMethod(this, "expandToLeafDir", Qt::QueuedConnection
);
193 void TreeViewSidebarPage::triggerLoadSubTree()
195 // the loading of the sub tree may not be done in the context
197 QMetaObject::invokeMethod(this, "loadSubTree", Qt::QueuedConnection
);
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_dolphinModel
->indexForUrl(parentUrl
);
205 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
206 m_treeView
->setExpanded(proxyIndex
, true);
207 selectLeafDirectory();
211 void TreeViewSidebarPage::loadSubTree()
213 if (m_leafDir
.isParentOf(m_dirLister
->url())) {
214 // The leaf directory is not a child of the base URL, hence
215 // no sub directory must be loaded or selected.
219 const QModelIndex index
= m_dolphinModel
->indexForUrl(m_leafDir
);
220 if (index
.isValid()) {
221 selectLeafDirectory();
223 // Load all sub directories that need to get expanded for making
224 // the leaf directory visible. The slot triggerExpanding() will
225 // get invoked if the expanding has been finished.
226 m_dolphinModel
->expandToUrl(m_leafDir
);
230 void TreeViewSidebarPage::scrollToLeaf()
232 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
233 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
234 if (proxyIndex
.isValid()) {
235 m_treeView
->scrollTo(proxyIndex
);
239 void TreeViewSidebarPage::loadTree(const KUrl
& url
)
241 Q_ASSERT(m_dirLister
!= 0);
245 if (url
.isLocalFile()) {
246 // use the root directory as base for local URLs
247 baseUrl
= QDir::rootPath();
249 // clear the path for non-local URLs and use it as base
251 baseUrl
.setPath(QString());
254 if (m_dirLister
->url() != baseUrl
) {
256 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
262 void TreeViewSidebarPage::selectLeafDirectory()
264 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
265 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
266 if (!proxyIndex
.isValid()) {
270 if (m_setLeafVisible
) {
271 // Invoke m_treeView->scrollTo(proxyIndex) asynchronously by
272 // scrollToLeaf(). This assures that the scrolling is done after
273 // the horizontal scrollbar gets visible (otherwise the scrollbar
274 // might hide the leaf).
275 QTimer::singleShot(100, this, SLOT(scrollToLeaf()));
276 m_setLeafVisible
= false;
279 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
280 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
283 #include "treeviewsidebarpage.moc"