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 "bookmarkselector.h"
23 #include "dolphincontextmenu.h"
24 #include "dolphinmainwindow.h"
25 #include "dolphinsortfilterproxymodel.h"
26 #include "dolphinview.h"
27 #include "sidebartreeview.h"
29 #include <kdirlister.h>
30 #include <kdirmodel.h>
31 #include <kfileitem.h>
33 #include <QHeaderView>
34 #include <QItemSelectionModel>
36 #include <QVBoxLayout>
38 // TODO: currently when using a proxy model the strange effect occurs
39 // that items get duplicated. Activate the following define to have the proxy
41 //#define USE_PROXY_MODEL
43 TreeViewSidebarPage::TreeViewSidebarPage(DolphinMainWindow
* mainWindow
,
45 SidebarPage(mainWindow
, parent
),
52 Q_ASSERT(mainWindow
!= 0);
54 m_dirLister
= new KDirLister();
55 m_dirLister
->setDirOnlyMode(true);
56 m_dirLister
->setAutoUpdate(true);
57 m_dirLister
->setMainWindow(this);
58 m_dirLister
->setDelayedMimeTypes(true);
59 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
61 m_dirModel
= new KDirModel();
62 m_dirModel
->setDirLister(m_dirLister
);
63 m_dirModel
->setDropsAllowed(KDirModel::DropOnDirectory
);
66 #if defined(USE_PROXY_MODEL)
67 m_proxyModel
= new DolphinSortFilterProxyModel(this);
68 m_proxyModel
->setSourceModel(m_dirModel
);
70 m_treeView
= new SidebarTreeView(mainWindow
, this);
71 m_treeView
->setModel(m_proxyModel
);
73 m_proxyModel
->setSorting(DolphinView::SortByName
);
74 m_proxyModel
->setSortOrder(Qt::Ascending
);
76 m_treeView
= new SidebarTreeView(mainWindow
, this);
77 m_treeView
->setModel(m_dirModel
);
80 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
81 this, SLOT(updateActiveView(const QModelIndex
&)));
82 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
83 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
85 QVBoxLayout
* layout
= new QVBoxLayout(this);
87 layout
->addWidget(m_treeView
);
90 TreeViewSidebarPage::~TreeViewSidebarPage()
96 void TreeViewSidebarPage::activeViewChanged()
98 connectToActiveView();
101 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
103 SidebarPage::showEvent(event
);
104 connectToActiveView();
107 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
109 SidebarPage::contextMenuEvent(event
);
111 // TODO: temporary deactivate the following code, as the wrong
112 // selection of cut/copy/paste actions is very confusing:
117 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
118 if (!index
.isValid()) {
119 // only open a context menu above a directory item
123 #if defined(USE_PROXY_MODEL)
124 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
125 item
= m_dirModel
->itemForIndex(dirModelIndex
);
127 item
= m_dirModel
->itemForIndex(index
);
130 #if defined(USE_PROXY_MODEL)
131 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(
132 m_treeView
->selectionModel()->selection());
134 const QItemSelection selection
= m_treeView
->selectionModel()->selection();
137 KFileItemList selectedItems
;
139 const QModelIndexList indexList
= selection
.indexes();
140 QModelIndexList::const_iterator end
= indexList
.end();
141 for (QModelIndexList::const_iterator it
= indexList
.begin(); it
!= end
; ++it
) {
142 Q_ASSERT((*it
).isValid());
144 KFileItem
* item
= m_dirModel
->itemForIndex(*it
);
146 selectedItems
.append(item
);
150 DolphinContextMenu
contextMenu(mainWindow(),
157 void TreeViewSidebarPage::updateSelection(const KUrl
& url
)
159 if (!url
.isValid() || (url
== m_selectedUrl
)) {
165 // adjust the root of the tree to the base bookmark
166 const KUrl baseUrl
= BookmarkSelector::baseBookmark(url
).url();
167 if (m_dirLister
->url() != baseUrl
) {
169 m_dirLister
->openUrl(baseUrl
);
172 // select the folder which contains the given URL
173 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
174 selModel
->clearSelection();
176 const KFileItem
item(S_IFDIR
, KFileItem::Unknown
, url
);
178 const QModelIndex index
= m_dirModel
->indexForItem(item
);
179 if (index
.isValid()) {
180 #if defined(USE_PROXY_MODEL)
181 // the item with the given URL is already part of the model
182 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
183 m_treeView
->scrollTo(proxyIndex
);
184 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
186 m_treeView
->scrollTo(index
);
187 selModel
->setCurrentIndex(index
, QItemSelectionModel::Select
);
191 // The item with the given URL is not loaded by the model yet. Iterate
192 // backward to the base URL and trigger the loading of the items for
193 // each hierarchy level.
194 connect(m_dirLister
, SIGNAL(completed()),
195 this, SLOT(expandSelectionParent()));
197 KUrl parentUrl
= url
.upUrl();
198 while (!parentUrl
.isParentOf(baseUrl
)) {
199 m_dirLister
->openUrl(parentUrl
, true, false);
200 parentUrl
= parentUrl
.upUrl();
205 void TreeViewSidebarPage::expandSelectionParent()
207 disconnect(m_dirLister
, SIGNAL(completed()),
208 this, SLOT(expandSelectionParent()));
210 // expand the parent folder of the selected item
211 const KFileItem
parentItem(S_IFDIR
, KFileItem::Unknown
, m_selectedUrl
.upUrl());
212 QModelIndex index
= m_dirModel
->indexForItem(parentItem
);
213 if (index
.isValid()) {
214 #if defined(USE_PROXY_MODEL)
215 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(index
);
216 m_treeView
->setExpanded(proxyIndex
, true);
218 m_treeView
->setExpanded(index
, true);
221 // select the item and assure that the item is visible
222 const KFileItem
selectedItem(S_IFDIR
, KFileItem::Unknown
, m_selectedUrl
);
223 index
= m_dirModel
->indexForItem(selectedItem
);
224 if (index
.isValid()) {
225 #if defined(USE_PROXY_MODEL)
226 proxyIndex
= m_proxyModel
->mapFromSource(index
);
227 m_treeView
->scrollTo(proxyIndex
);
229 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
230 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
232 m_treeView
->scrollTo(index
);
234 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
235 selModel
->setCurrentIndex(index
, QItemSelectionModel::Select
);
242 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
244 #if defined(USE_PROXY_MODEL)
245 const QModelIndex
& dirIndex
= m_proxyModel
->mapToSource(index
);
246 const KFileItem
* item
= m_dirModel
->itemForIndex(dirIndex
);
248 const KFileItem
* item
= m_dirModel
->itemForIndex(index
);
251 const KUrl
& url
= item
->url();
252 mainWindow()->activeView()->setUrl(url
);
256 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
257 const QModelIndex
& index
)
259 if (index
.isValid()) {
260 #if defined(USE_PROXY_MODEL)
261 const QModelIndex
& dirIndex
= m_proxyModel
->mapToSource(index
);
262 KFileItem
* item
= m_dirModel
->itemForIndex(dirIndex
);
264 KFileItem
* item
= m_dirModel
->itemForIndex(index
);
268 mainWindow()->dropUrls(urls
, item
->url());
273 void TreeViewSidebarPage::connectToActiveView()
275 const QWidget
* parent
= parentWidget();
276 if ((parent
== 0) || parent
->isHidden()) {
280 const DolphinView
* view
= mainWindow()->activeView();
281 const KUrl
& url
= view
->url();
283 connect(view
, SIGNAL(urlChanged(const KUrl
&)),
284 this, SLOT(updateSelection(const KUrl
&)));
286 updateSelection(url
);
289 #include "treeviewsidebarpage.moc"