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 "sidebartreeview.h"
27 #include "treeviewcontextmenu.h"
29 #include <kfileplacesmodel.h>
30 #include <kdirlister.h>
31 #include <kfileitem.h>
33 #include <QItemSelection>
36 #include <QModelIndex>
38 TreeViewSidebarPage::TreeViewSidebarPage(QWidget
* parent
) :
48 TreeViewSidebarPage::~TreeViewSidebarPage()
52 delete m_dolphinModel
;
54 m_dirLister
= 0; // deleted by m_dolphinModel
57 QSize
TreeViewSidebarPage::sizeHint() const
59 return QSize(200, 400);
62 void TreeViewSidebarPage::setUrl(const KUrl
& url
)
64 if (!url
.isValid() || (url
== SidebarPage::url())) {
68 SidebarPage::setUrl(url
);
69 if (m_dirLister
!= 0) {
74 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
76 if (event
->spontaneous()) {
77 SidebarPage::showEvent(event
);
81 if (m_dirLister
== 0) {
82 // Postpone the creating of the dir lister to the first show event.
83 // This assures that no performance and memory overhead is given when the TreeView is not
84 // used at all (see TreeViewSidebarPage::setUrl()).
85 m_dirLister
= new KDirLister();
86 m_dirLister
->setDirOnlyMode(true);
87 m_dirLister
->setAutoUpdate(true);
88 m_dirLister
->setMainWindow(this);
89 m_dirLister
->setDelayedMimeTypes(true);
90 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
92 connect(m_dirLister
, SIGNAL(completed()),
93 this, SLOT(triggerLoadSubTree()));
95 Q_ASSERT(m_dolphinModel
== 0);
96 m_dolphinModel
= new DolphinModel(this);
97 m_dolphinModel
->setDirLister(m_dirLister
);
98 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
99 connect(m_dolphinModel
, SIGNAL(expand(const QModelIndex
&)),
100 this, SLOT(triggerExpanding()));
102 Q_ASSERT(m_proxyModel
== 0);
103 m_proxyModel
= new DolphinSortFilterProxyModel(this);
104 m_proxyModel
->setSourceModel(m_dolphinModel
);
106 Q_ASSERT(m_treeView
== 0);
107 m_treeView
= new SidebarTreeView(this);
108 m_treeView
->setModel(m_proxyModel
);
109 m_proxyModel
->setSorting(DolphinView::SortByName
);
110 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
112 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
113 this, SLOT(updateActiveView(const QModelIndex
&)));
114 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
115 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
117 QVBoxLayout
* layout
= new QVBoxLayout(this);
118 layout
->setMargin(0);
119 layout
->addWidget(m_treeView
);
123 SidebarPage::showEvent(event
);
126 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
128 SidebarPage::contextMenuEvent(event
);
130 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
131 if (!index
.isValid()) {
132 // only open a context menu above a directory item
136 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
137 KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
139 emit
changeSelection(KFileItemList());
140 TreeViewContextMenu
contextMenu(this, item
);
144 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
146 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
147 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
148 if (!item
.isNull()) {
149 emit
changeUrl(item
.url());
153 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
154 const QModelIndex
& index
)
156 if (index
.isValid()) {
157 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
158 KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
159 Q_ASSERT(!item
.isNull());
161 emit
urlsDropped(urls
, item
.url());
166 void TreeViewSidebarPage::triggerExpanding()
168 // the expanding of the folders may not be done in the context
170 QMetaObject::invokeMethod(this, "expandToLeafDir", Qt::QueuedConnection
);
173 void TreeViewSidebarPage::triggerLoadSubTree()
175 // the loading of the sub tree may not be done in the context
177 QMetaObject::invokeMethod(this, "loadSubTree", Qt::QueuedConnection
);
180 void TreeViewSidebarPage::expandToLeafDir()
182 // expand all directories until the parent directory of m_leafDir
183 const KUrl parentUrl
= m_leafDir
.upUrl();
184 QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(parentUrl
);
185 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
186 m_treeView
->setExpanded(proxyIndex
, true);
188 // assure that m_leafDir gets selected
189 dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
190 proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
191 m_treeView
->scrollTo(proxyIndex
);
193 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
194 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
196 m_treeView
->resizeColumnToContents(DolphinModel::Name
);
200 void TreeViewSidebarPage::loadSubTree()
202 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
203 selModel
->clearSelection();
205 if (m_leafDir
.isParentOf(m_dirLister
->url())) {
206 m_treeView
->resizeColumnToContents(DolphinModel::Name
);
208 // The leaf directory is not a child of the base URL, hence
209 // no sub directory must be loaded or selected.
213 const QModelIndex index
= m_dolphinModel
->indexForUrl(m_leafDir
);
214 if (index
.isValid()) {
215 // the item with the given URL is already part of the model
216 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
217 m_treeView
->scrollTo(proxyIndex
);
218 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
220 // Load all sub directories that need to get expanded for making
221 // the leaf directory visible. The slot triggerExpanding() will
222 // get invoked if the expanding has been finished.
223 m_dolphinModel
->expandToUrl(m_leafDir
);
226 m_treeView
->resizeColumnToContents(DolphinModel::Name
);
229 void TreeViewSidebarPage::loadTree(const KUrl
& url
)
231 Q_ASSERT(m_dirLister
!= 0);
234 // adjust the root of the tree to the base place
235 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
236 KUrl baseUrl
= placesModel
->url(placesModel
->closestItem(url
));
237 if (!baseUrl
.isValid()) {
238 // it's possible that no closest item is available and hence an
239 // empty URL is returned
243 if (m_dirLister
->url() != baseUrl
) {
245 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
251 #include "treeviewsidebarpage.moc"