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 m_proxyModel
->deleteLater();
53 m_dolphinModel
->deleteLater();
55 m_dirLister
= 0; // deleted by m_dolphinModel
58 QSize
TreeViewSidebarPage::sizeHint() const
60 return QSize(200, 400);
63 void TreeViewSidebarPage::setUrl(const KUrl
& url
)
65 if (!url
.isValid() || (url
== SidebarPage::url())) {
69 SidebarPage::setUrl(url
);
70 if (m_dirLister
!= 0) {
75 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
77 if (event
->spontaneous()) {
78 SidebarPage::showEvent(event
);
82 if (m_dirLister
== 0) {
83 // Postpone the creating of the dir lister to the first show event.
84 // This assures that no performance and memory overhead is given when the TreeView is not
85 // used at all (see TreeViewSidebarPage::setUrl()).
86 m_dirLister
= new KDirLister();
87 m_dirLister
->setDirOnlyMode(true);
88 m_dirLister
->setAutoUpdate(true);
89 m_dirLister
->setMainWindow(this);
90 m_dirLister
->setDelayedMimeTypes(true);
91 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
93 connect(m_dirLister
, SIGNAL(completed()),
94 this, SLOT(triggerLoadSubTree()));
96 Q_ASSERT(m_dolphinModel
== 0);
97 m_dolphinModel
= new DolphinModel(this);
98 m_dolphinModel
->setDirLister(m_dirLister
);
99 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
100 connect(m_dolphinModel
, SIGNAL(expand(const QModelIndex
&)),
101 this, SLOT(triggerExpanding()));
103 Q_ASSERT(m_proxyModel
== 0);
104 m_proxyModel
= new DolphinSortFilterProxyModel(this);
105 m_proxyModel
->setSourceModel(m_dolphinModel
);
107 Q_ASSERT(m_treeView
== 0);
108 m_treeView
= new SidebarTreeView(this);
109 m_treeView
->setModel(m_proxyModel
);
110 m_proxyModel
->setSorting(DolphinView::SortByName
);
111 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
113 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
114 this, SLOT(updateActiveView(const QModelIndex
&)));
115 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
116 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
118 QVBoxLayout
* layout
= new QVBoxLayout(this);
119 layout
->setMargin(0);
120 layout
->addWidget(m_treeView
);
124 SidebarPage::showEvent(event
);
127 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
129 SidebarPage::contextMenuEvent(event
);
131 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
132 if (!index
.isValid()) {
133 // only open a context menu above a directory item
137 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
138 KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
140 emit
changeSelection(KFileItemList());
141 TreeViewContextMenu
contextMenu(this, item
);
145 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
147 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
148 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
149 if (!item
.isNull()) {
150 emit
changeUrl(item
.url());
154 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
155 const QModelIndex
& index
)
157 if (index
.isValid()) {
158 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
159 KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
160 Q_ASSERT(!item
.isNull());
162 emit
urlsDropped(urls
, item
.url());
167 void TreeViewSidebarPage::triggerExpanding()
169 // the expanding of the folders may not be done in the context
171 QMetaObject::invokeMethod(this, "expandToLeafDir", Qt::QueuedConnection
);
174 void TreeViewSidebarPage::triggerLoadSubTree()
176 // the loading of the sub tree may not be done in the context
178 QMetaObject::invokeMethod(this, "loadSubTree", Qt::QueuedConnection
);
181 void TreeViewSidebarPage::expandToLeafDir()
183 // expand all directories until the parent directory of m_leafDir
184 const KUrl parentUrl
= m_leafDir
.upUrl();
185 QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(parentUrl
);
186 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
187 m_treeView
->setExpanded(proxyIndex
, true);
189 // assure that m_leafDir gets selected
190 dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
191 proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
192 m_treeView
->scrollTo(proxyIndex
);
194 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
195 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
199 void TreeViewSidebarPage::loadSubTree()
201 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
202 selModel
->clearSelection();
204 if (m_leafDir
.isParentOf(m_dirLister
->url())) {
205 // The leaf directory is not a child of the base URL, hence
206 // no sub directory must be loaded or selected.
210 const QModelIndex index
= m_dolphinModel
->indexForUrl(m_leafDir
);
211 if (index
.isValid()) {
212 // the item with the given URL is already part of the model
213 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
214 m_treeView
->scrollTo(proxyIndex
);
215 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
217 // Load all sub directories that need to get expanded for making
218 // the leaf directory visible. The slot triggerExpanding() will
219 // get invoked if the expanding has been finished.
220 m_dolphinModel
->expandToUrl(m_leafDir
);
224 void TreeViewSidebarPage::loadTree(const KUrl
& url
)
226 Q_ASSERT(m_dirLister
!= 0);
229 // adjust the root of the tree to the base place
230 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
231 KUrl baseUrl
= placesModel
->url(placesModel
->closestItem(url
));
232 if (!baseUrl
.isValid()) {
233 // it's possible that no closest item is available and hence an
234 // empty URL is returned
238 if (m_dirLister
->url() != baseUrl
) {
240 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
246 #include "treeviewsidebarpage.moc"