]> cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewsidebarpage.cpp
coding style cleanup: fix wrong indenting of members in constructor
[dolphin.git] / src / treeviewsidebarpage.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "treeviewsidebarpage.h"
21
22 #include "dolphinmainwindow.h"
23 #include "dolphinsortfilterproxymodel.h"
24 #include "dolphinview.h"
25 #include "sidebartreeview.h"
26 #include "treeviewcontextmenu.h"
27
28 #include <kfileplacesmodel.h>
29 #include <kdirlister.h>
30 #include <kdirmodel.h>
31 #include <kfileitem.h>
32
33 #include <QHeaderView>
34 #include <QItemSelectionModel>
35 #include <QTreeView>
36 #include <QVBoxLayout>
37 #include "dolphinsettings.h"
38
39 TreeViewSidebarPage::TreeViewSidebarPage(QWidget* parent) :
40 SidebarPage(parent),
41 m_dirLister(0),
42 m_dirModel(0),
43 m_proxyModel(0),
44 m_treeView(0)
45 {
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);
52
53 m_dirModel = new KDirModel();
54 m_dirModel->setDirLister(m_dirLister);
55 m_dirModel->setDropsAllowed(KDirModel::DropOnDirectory);
56
57 m_proxyModel = new DolphinSortFilterProxyModel(this);
58 m_proxyModel->setSourceModel(m_dirModel);
59
60 m_treeView = new SidebarTreeView(this);
61 m_treeView->setModel(m_proxyModel);
62
63 m_proxyModel->setSorting(DolphinView::SortByName);
64 m_proxyModel->setSortOrder(Qt::AscendingOrder);
65
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&)));
70
71 QVBoxLayout* layout = new QVBoxLayout(this);
72 layout->setMargin(0);
73 layout->addWidget(m_treeView);
74 }
75
76 TreeViewSidebarPage::~TreeViewSidebarPage()
77 {
78 delete m_dirLister;
79 m_dirLister = 0;
80 }
81
82 void TreeViewSidebarPage::setUrl(const KUrl& url)
83 {
84 if (!url.isValid() || (url == SidebarPage::url())) {
85 return;
86 }
87
88 SidebarPage::setUrl(url);
89
90 // adjust the root of the tree to the base bookmark
91 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
92 KUrl baseUrl = placesModel->url(placesModel->closestItem(url));
93 if (!baseUrl.isValid()) {
94 // it's possible that no closest item is available and hence an
95 // empty URL is returned
96 baseUrl = url;
97 }
98
99 if (m_dirLister->url() != baseUrl) {
100 m_dirLister->stop();
101 m_dirLister->openUrl(baseUrl);
102 }
103
104 // select the folder which contains the given URL
105 QItemSelectionModel* selModel = m_treeView->selectionModel();
106 selModel->clearSelection();
107
108 const QModelIndex index = m_dirModel->indexForUrl(url);
109 if (index.isValid()) {
110 // the item with the given URL is already part of the model
111 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(index);
112 m_treeView->scrollTo(proxyIndex);
113 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
114 } else {
115 // The item with the given URL is not loaded by the model yet. Iterate
116 // backward to the base URL and trigger the loading of the items for
117 // each hierarchy level.
118 connect(m_dirLister, SIGNAL(completed()),
119 this, SLOT(expandSelectionParent()));
120
121 KUrl parentUrl = url.upUrl();
122 while (!parentUrl.isParentOf(baseUrl)) {
123 m_dirLister->openUrl(parentUrl, true, false);
124 parentUrl = parentUrl.upUrl();
125 }
126 }
127
128 }
129
130 void TreeViewSidebarPage::showEvent(QShowEvent* event)
131 {
132 SidebarPage::showEvent(event);
133 }
134
135 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent* event)
136 {
137 SidebarPage::contextMenuEvent(event);
138
139 const QModelIndex index = m_treeView->indexAt(event->pos());
140 if (!index.isValid()) {
141 // only open a context menu above a directory item
142 return;
143 }
144
145 const QModelIndex dirModelIndex = m_proxyModel->mapToSource(index);
146 KFileItem* item = m_dirModel->itemForIndex(dirModelIndex);
147
148 emit changeSelection(KFileItemList());
149 TreeViewContextMenu contextMenu(this, item);
150 contextMenu.open();
151 }
152
153 void TreeViewSidebarPage::expandSelectionParent()
154 {
155 disconnect(m_dirLister, SIGNAL(completed()),
156 this, SLOT(expandSelectionParent()));
157
158 // expand the parent folder of the selected item
159 KUrl parentUrl = url().upUrl();
160 if (!m_dirLister->url().isParentOf(parentUrl)) {
161 return;
162 }
163
164 QModelIndex index = m_dirModel->indexForUrl(parentUrl);
165 if (index.isValid()) {
166 QModelIndex proxyIndex = m_proxyModel->mapFromSource(index);
167 m_treeView->setExpanded(proxyIndex, true);
168
169 // select the item and assure that the item is visible
170 index = m_dirModel->indexForUrl(url());
171 if (index.isValid()) {
172 proxyIndex = m_proxyModel->mapFromSource(index);
173 m_treeView->scrollTo(proxyIndex);
174
175 QItemSelectionModel* selModel = m_treeView->selectionModel();
176 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
177 }
178 }
179 }
180
181 void TreeViewSidebarPage::updateActiveView(const QModelIndex& index)
182 {
183 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
184 const KFileItem* item = m_dirModel->itemForIndex(dirIndex);
185 if (item != 0) {
186 const KUrl& url = item->url();
187 emit changeUrl(url);
188 }
189 }
190
191 void TreeViewSidebarPage::dropUrls(const KUrl::List& urls,
192 const QModelIndex& index)
193 {
194 if (index.isValid()) {
195 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
196 KFileItem* item = m_dirModel->itemForIndex(dirIndex);
197 Q_ASSERT(item != 0);
198 if (item->isDir()) {
199 emit urlsDropped(urls, item->url());
200 }
201 }
202 }
203
204 #include "treeviewsidebarpage.moc"