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()
51 delete m_dolphinModel
;
53 m_dirLister
= 0; // deleted by m_dolphinModel
56 QSize
TreeViewSidebarPage::sizeHint() const
58 return QSize(200, 400);
61 void TreeViewSidebarPage::setUrl(const KUrl
& url
)
63 if (!url
.isValid() || (url
== SidebarPage::url())) {
67 SidebarPage::setUrl(url
);
68 if (m_dirLister
!= 0) {
73 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
75 if (event
->spontaneous()) {
76 SidebarPage::showEvent(event
);
80 if (m_dirLister
== 0) {
81 // Postpone the creating of the dir lister to the first show event.
82 // This assures that no performance and memory overhead is given when the TreeView is not
83 // used at all (see TreeViewSidebarPage::setUrl()).
84 m_dirLister
= new KDirLister();
85 m_dirLister
->setDirOnlyMode(true);
86 m_dirLister
->setAutoUpdate(true);
87 m_dirLister
->setMainWindow(this);
88 m_dirLister
->setDelayedMimeTypes(true);
89 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
91 connect(m_dirLister
, SIGNAL(completed()),
92 this, SLOT(triggerLoadSubTree()));
94 Q_ASSERT(m_dolphinModel
== 0);
95 m_dolphinModel
= new DolphinModel(this);
96 m_dolphinModel
->setDirLister(m_dirLister
);
97 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
98 connect(m_dolphinModel
, SIGNAL(expand(const QModelIndex
&)),
99 this, SLOT(triggerExpanding()));
101 Q_ASSERT(m_proxyModel
== 0);
102 m_proxyModel
= new DolphinSortFilterProxyModel(this);
103 m_proxyModel
->setSourceModel(m_dolphinModel
);
105 Q_ASSERT(m_treeView
== 0);
106 m_treeView
= new SidebarTreeView(this);
107 m_treeView
->setModel(m_proxyModel
);
108 m_proxyModel
->setSorting(DolphinView::SortByName
);
109 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
111 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
112 this, SLOT(updateActiveView(const QModelIndex
&)));
113 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
114 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
116 QVBoxLayout
* layout
= new QVBoxLayout(this);
117 layout
->setMargin(0);
118 layout
->addWidget(m_treeView
);
122 SidebarPage::showEvent(event
);
125 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
127 SidebarPage::contextMenuEvent(event
);
129 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
130 if (!index
.isValid()) {
131 // only open a context menu above a directory item
135 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
136 KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
138 emit
changeSelection(KFileItemList());
139 TreeViewContextMenu
contextMenu(this, item
);
143 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
145 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
146 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
147 if (!item
.isNull()) {
148 emit
changeUrl(item
.url());
152 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
153 const QModelIndex
& index
)
155 if (index
.isValid()) {
156 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
157 KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
158 Q_ASSERT(!item
.isNull());
160 emit
urlsDropped(urls
, item
.url());
165 void TreeViewSidebarPage::triggerExpanding()
167 // the expanding of the folders may not be done in the context
169 QMetaObject::invokeMethod(this, "expandToLeafDir", Qt::QueuedConnection
);
172 void TreeViewSidebarPage::triggerLoadSubTree()
174 // the loading of the sub tree may not be done in the context
176 QMetaObject::invokeMethod(this, "loadSubTree", Qt::QueuedConnection
);
179 void TreeViewSidebarPage::expandToLeafDir()
181 // expand all directories until the parent directory of m_leafDir
182 const KUrl parentUrl
= m_leafDir
.upUrl();
183 QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(parentUrl
);
184 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
185 m_treeView
->setExpanded(proxyIndex
, true);
187 // assure that m_leafDir gets selected
188 dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
189 proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
190 m_treeView
->scrollTo(proxyIndex
);
192 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
193 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
197 void TreeViewSidebarPage::loadSubTree()
199 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
200 selModel
->clearSelection();
202 if (m_leafDir
.isParentOf(m_dirLister
->url())) {
203 // The leaf directory is not a child of the base URL, hence
204 // no sub directory must be loaded or selected.
208 const QModelIndex index
= m_dolphinModel
->indexForUrl(m_leafDir
);
209 if (index
.isValid()) {
210 // the item with the given URL is already part of the model
211 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
212 m_treeView
->scrollTo(proxyIndex
);
213 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
215 // Load all sub directories that need to get expanded for making
216 // the leaf directory visible. The slot triggerExpanding() will
217 // get invoked if the expanding has been finished.
218 m_dolphinModel
->expandToUrl(m_leafDir
);
222 void TreeViewSidebarPage::loadTree(const KUrl
& url
)
224 Q_ASSERT(m_dirLister
!= 0);
227 // adjust the root of the tree to the base place
228 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
229 KUrl baseUrl
= placesModel
->url(placesModel
->closestItem(url
));
230 if (!baseUrl
.isValid()) {
231 // it's possible that no closest item is available and hence an
232 // empty URL is returned
236 if (m_dirLister
->url() != baseUrl
) {
238 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
244 #include "treeviewsidebarpage.moc"