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 <QApplication>
35 #include <QItemSelection>
38 #include <QModelIndex>
42 TreeViewSidebarPage::TreeViewSidebarPage(QWidget
* parent
) :
44 m_setLeafVisible(false),
45 m_mouseButtons(Qt::NoButton
),
54 TreeViewSidebarPage::~TreeViewSidebarPage()
56 FoldersPanelSettings::self()->writeConfig();
60 delete m_dolphinModel
;
62 m_dirLister
= 0; // deleted by m_dolphinModel
65 QSize
TreeViewSidebarPage::sizeHint() const
67 return QSize(200, 400);
70 void TreeViewSidebarPage::setShowHiddenFiles(bool show
)
72 FoldersPanelSettings::setShowHiddenFiles(show
);
73 if (m_dirLister
!= 0) {
74 m_dirLister
->setShowingDotFiles(show
);
75 m_dirLister
->openUrl(m_dirLister
->url(), KDirLister::Reload
);
79 bool TreeViewSidebarPage::showHiddenFiles() const
81 return FoldersPanelSettings::showHiddenFiles();
84 void TreeViewSidebarPage::setUrl(const KUrl
& url
)
86 if (!url
.isValid() || (url
== SidebarPage::url())) {
90 SidebarPage::setUrl(url
);
91 if (m_dirLister
!= 0) {
92 m_setLeafVisible
= true;
97 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
99 if (event
->spontaneous()) {
100 SidebarPage::showEvent(event
);
104 if (m_dirLister
== 0) {
105 // Postpone the creating of the dir lister to the first show event.
106 // This assures that no performance and memory overhead is given when the TreeView is not
107 // used at all (see TreeViewSidebarPage::setUrl()).
108 m_dirLister
= new KDirLister();
109 m_dirLister
->setDirOnlyMode(true);
110 m_dirLister
->setAutoUpdate(true);
111 m_dirLister
->setMainWindow(window());
112 m_dirLister
->setDelayedMimeTypes(true);
113 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
114 m_dirLister
->setShowingDotFiles(FoldersPanelSettings::showHiddenFiles());
116 connect(m_dirLister
, SIGNAL(completed()),
117 this, SLOT(triggerLoadSubTree()));
119 Q_ASSERT(m_dolphinModel
== 0);
120 m_dolphinModel
= new DolphinModel(this);
121 m_dolphinModel
->setDirLister(m_dirLister
);
122 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
123 connect(m_dolphinModel
, SIGNAL(expand(const QModelIndex
&)),
124 this, SLOT(triggerExpanding()));
126 Q_ASSERT(m_proxyModel
== 0);
127 m_proxyModel
= new DolphinSortFilterProxyModel(this);
128 m_proxyModel
->setSourceModel(m_dolphinModel
);
130 Q_ASSERT(m_treeView
== 0);
131 m_treeView
= new SidebarTreeView(this);
132 m_treeView
->setModel(m_proxyModel
);
133 m_proxyModel
->setSorting(DolphinView::SortByName
);
134 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
136 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
137 this, SLOT(updateActiveView(const QModelIndex
&)));
138 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
139 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
140 connect(m_treeView
, SIGNAL(pressed(const QModelIndex
&)),
141 this, SLOT(updateMouseButtons()));
143 QVBoxLayout
* layout
= new QVBoxLayout(this);
144 layout
->setMargin(0);
145 layout
->addWidget(m_treeView
);
149 SidebarPage::showEvent(event
);
152 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
154 SidebarPage::contextMenuEvent(event
);
157 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
158 if (index
.isValid()) {
159 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
160 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
161 emit
changeSelection(KFileItemList());
164 TreeViewContextMenu
contextMenu(this, item
);
168 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
170 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
171 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
172 if (!item
.isNull()) {
173 emit
changeUrl(item
.url(), m_mouseButtons
);
177 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
178 const QModelIndex
& index
)
180 if (index
.isValid()) {
181 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
182 KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
183 Q_ASSERT(!item
.isNull());
185 emit
urlsDropped(urls
, item
.url());
190 void TreeViewSidebarPage::triggerExpanding()
192 // the expanding of the folders may not be done in the context
194 QMetaObject::invokeMethod(this, "expandToLeafDir", Qt::QueuedConnection
);
197 void TreeViewSidebarPage::triggerLoadSubTree()
199 // the loading of the sub tree may not be done in the context
201 QMetaObject::invokeMethod(this, "loadSubTree", Qt::QueuedConnection
);
204 void TreeViewSidebarPage::expandToLeafDir()
206 // expand all directories until the parent directory of m_leafDir
207 const KUrl parentUrl
= m_leafDir
.upUrl();
208 QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(parentUrl
);
209 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
210 m_treeView
->setExpanded(proxyIndex
, true);
211 selectLeafDirectory();
215 void TreeViewSidebarPage::loadSubTree()
217 m_treeView
->selectionModel()->clearSelection();
219 if (m_leafDir
.isParentOf(m_dirLister
->url())) {
220 // The leaf directory is not a child of the base URL, hence
221 // no sub directory must be loaded or selected.
225 const QModelIndex index
= m_dolphinModel
->indexForUrl(m_leafDir
);
226 if (index
.isValid()) {
227 selectLeafDirectory();
229 // Load all sub directories that need to get expanded for making
230 // the leaf directory visible. The slot triggerExpanding() will
231 // get invoked if the expanding has been finished.
232 m_dolphinModel
->expandToUrl(m_leafDir
);
236 void TreeViewSidebarPage::scrollToLeaf()
238 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
239 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
240 if (proxyIndex
.isValid()) {
241 m_treeView
->scrollTo(proxyIndex
);
245 void TreeViewSidebarPage::updateMouseButtons()
247 m_mouseButtons
= QApplication::mouseButtons();
250 void TreeViewSidebarPage::loadTree(const KUrl
& url
)
252 Q_ASSERT(m_dirLister
!= 0);
256 if (url
.isLocalFile()) {
257 // use the root directory as base for local URLs
258 baseUrl
= QDir::rootPath();
260 // clear the path for non-local URLs and use it as base
262 baseUrl
.setPath(QString());
265 if (m_dirLister
->url() != baseUrl
) {
267 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
273 void TreeViewSidebarPage::selectLeafDirectory()
275 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
276 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
277 if (!proxyIndex
.isValid()) {
281 if (m_setLeafVisible
) {
282 // Invoke m_treeView->scrollTo(proxyIndex) asynchronously by
283 // scrollToLeaf(). This assures that the scrolling is done after
284 // the horizontal scrollbar gets visible (otherwise the scrollbar
285 // might hide the leaf).
286 QTimer::singleShot(100, this, SLOT(scrollToLeaf()));
287 m_setLeafVisible
= false;
290 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
291 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
294 #include "treeviewsidebarpage.moc"