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 "dolphinmainwindow.h"
23 #include "dolphinsortfilterproxymodel.h"
24 #include "dolphinview.h"
25 #include "sidebartreeview.h"
26 #include "treeviewcontextmenu.h"
28 #include <kfileplacesmodel.h>
29 #include <kdirlister.h>
30 #include <kdirmodel.h>
31 #include <kfileitem.h>
33 #include <QHeaderView>
34 #include <QItemSelectionModel>
36 #include <QVBoxLayout>
37 #include "dolphinsettings.h"
39 TreeViewSidebarPage::TreeViewSidebarPage(QWidget
* parent
) :
46 m_dirLister
= new KDirLister();
47 m_dirLister
->setDirOnlyMode(true);
48 m_dirLister
->setAutoUpdate(true);
49 m_dirLister
->setMainWindow(this);
50 m_dirLister
->setDelayedMimeTypes(true);
51 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
53 m_dirModel
= new KDirModel();
54 m_dirModel
->setDirLister(m_dirLister
);
55 m_dirModel
->setDropsAllowed(KDirModel::DropOnDirectory
);
57 m_proxyModel
= new DolphinSortFilterProxyModel(this);
58 m_proxyModel
->setSourceModel(m_dirModel
);
60 m_treeView
= new SidebarTreeView(this);
61 m_treeView
->setModel(m_proxyModel
);
63 m_proxyModel
->setSorting(DolphinView::SortByName
);
64 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
66 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
67 this, SLOT(updateActiveView(const QModelIndex
&)));
68 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
69 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
71 QVBoxLayout
* layout
= new QVBoxLayout(this);
73 layout
->addWidget(m_treeView
);
76 TreeViewSidebarPage::~TreeViewSidebarPage()
82 void TreeViewSidebarPage::setUrl(const KUrl
& url
)
84 if (!url
.isValid() || (url
== m_url
)) {
90 kDebug() << "-------------- Treeview: url = " << url
<< endl
;
92 // adjust the root of the tree to the base bookmark
93 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
94 KUrl baseUrl
= placesModel
->url(placesModel
->closestItem(url
));
95 if (!baseUrl
.isValid()) {
96 // it's possible that no closest item is available and hence an
97 // empty URL is returned
101 if (m_dirLister
->url() != baseUrl
) {
103 m_dirLister
->openUrl(baseUrl
);
106 // select the folder which contains the given URL
107 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
108 selModel
->clearSelection();
110 const QModelIndex index
= m_dirModel
->indexForUrl(url
);
111 if (index
.isValid()) {
112 // the item with the given URL is already part of the model
113 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
114 m_treeView
->scrollTo(proxyIndex
);
115 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
117 // The item with the given URL is not loaded by the model yet. Iterate
118 // backward to the base URL and trigger the loading of the items for
119 // each hierarchy level.
120 connect(m_dirLister
, SIGNAL(completed()),
121 this, SLOT(expandSelectionParent()));
123 KUrl parentUrl
= url
.upUrl();
124 while (!parentUrl
.isParentOf(baseUrl
)) {
125 m_dirLister
->openUrl(parentUrl
, true, false);
126 parentUrl
= parentUrl
.upUrl();
132 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
134 SidebarPage::showEvent(event
);
137 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
139 SidebarPage::contextMenuEvent(event
);
141 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
142 if (!index
.isValid()) {
143 // only open a context menu above a directory item
147 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
148 KFileItem
* item
= m_dirModel
->itemForIndex(dirModelIndex
);
150 emit
changeSelection(KFileItemList());
151 TreeViewContextMenu
contextMenu(this, item
);
155 void TreeViewSidebarPage::expandSelectionParent()
157 disconnect(m_dirLister
, SIGNAL(completed()),
158 this, SLOT(expandSelectionParent()));
160 // expand the parent folder of the selected item
161 KUrl parentUrl
= m_url
.upUrl();
162 if (!m_dirLister
->url().isParentOf(parentUrl
)) {
166 QModelIndex index
= m_dirModel
->indexForUrl(parentUrl
);
167 if (index
.isValid()) {
168 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
169 m_treeView
->setExpanded(proxyIndex
, true);
171 // select the item and assure that the item is visible
172 index
= m_dirModel
->indexForUrl(m_url
);
173 if (index
.isValid()) {
174 proxyIndex
= m_proxyModel
->mapFromSource(index
);
175 m_treeView
->scrollTo(proxyIndex
);
177 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
178 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
183 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
185 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
186 const KFileItem
* item
= m_dirModel
->itemForIndex(dirIndex
);
188 const KUrl
& url
= item
->url();
193 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
194 const QModelIndex
& index
)
196 if (index
.isValid()) {
197 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
198 KFileItem
* item
= m_dirModel
->itemForIndex(dirIndex
);
201 emit
urlsDropped(urls
, item
->url());
206 #include "treeviewsidebarpage.moc"