]> cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewsidebarpage.cpp
Optimize PNG files again.
[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 "dolphinmodel.h"
23 #include "dolphinsortfilterproxymodel.h"
24 #include "dolphinview.h"
25 #include "dolphinsettings.h"
26 #include "sidebartreeview.h"
27 #include "treeviewcontextmenu.h"
28
29 #include <kfileplacesmodel.h>
30 #include <kdirlister.h>
31 #include <kfileitem.h>
32
33 #include <QItemSelection>
34 #include <QTreeView>
35 #include <QBoxLayout>
36 #include <QModelIndex>
37
38 TreeViewSidebarPage::TreeViewSidebarPage(QWidget* parent) :
39 SidebarPage(parent),
40 m_dirLister(0),
41 m_dolphinModel(0),
42 m_proxyModel(0),
43 m_treeView(0),
44 m_leafDir()
45 {
46 }
47
48 TreeViewSidebarPage::~TreeViewSidebarPage()
49 {
50 delete m_proxyModel;
51 m_proxyModel = 0;
52 delete m_dolphinModel;
53 m_dolphinModel = 0;
54 m_dirLister = 0; // deleted by m_dolphinModel
55 }
56
57 QSize TreeViewSidebarPage::sizeHint() const
58 {
59 return QSize(200, 400);
60 }
61
62 void TreeViewSidebarPage::setUrl(const KUrl& url)
63 {
64 if (!url.isValid() || (url == SidebarPage::url())) {
65 return;
66 }
67
68 SidebarPage::setUrl(url);
69 if (m_dirLister != 0) {
70 loadTree(url);
71 }
72 }
73
74 void TreeViewSidebarPage::showEvent(QShowEvent* event)
75 {
76 if (event->spontaneous()) {
77 SidebarPage::showEvent(event);
78 return;
79 }
80
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);
91
92 connect(m_dirLister, SIGNAL(completed()),
93 this, SLOT(triggerLoadSubTree()));
94
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()));
101
102 Q_ASSERT(m_proxyModel == 0);
103 m_proxyModel = new DolphinSortFilterProxyModel(this);
104 m_proxyModel->setSourceModel(m_dolphinModel);
105
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);
111
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&)));
116
117 QVBoxLayout* layout = new QVBoxLayout(this);
118 layout->setMargin(0);
119 layout->addWidget(m_treeView);
120 }
121
122 loadTree(url());
123 SidebarPage::showEvent(event);
124 }
125
126 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent* event)
127 {
128 SidebarPage::contextMenuEvent(event);
129
130 const QModelIndex index = m_treeView->indexAt(event->pos());
131 if (!index.isValid()) {
132 // only open a context menu above a directory item
133 return;
134 }
135
136 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
137 KFileItem item = m_dolphinModel->itemForIndex(dolphinModelIndex);
138
139 emit changeSelection(KFileItemList());
140 TreeViewContextMenu contextMenu(this, item);
141 contextMenu.open();
142 }
143
144 void TreeViewSidebarPage::updateActiveView(const QModelIndex& index)
145 {
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());
150 }
151 }
152
153 void TreeViewSidebarPage::dropUrls(const KUrl::List& urls,
154 const QModelIndex& index)
155 {
156 if (index.isValid()) {
157 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
158 KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
159 Q_ASSERT(!item.isNull());
160 if (item.isDir()) {
161 emit urlsDropped(urls, item.url());
162 }
163 }
164 }
165
166 void TreeViewSidebarPage::triggerExpanding()
167 {
168 // the expanding of the folders may not be done in the context
169 // of this slot
170 QMetaObject::invokeMethod(this, "expandToLeafDir", Qt::QueuedConnection);
171 }
172
173 void TreeViewSidebarPage::triggerLoadSubTree()
174 {
175 // the loading of the sub tree may not be done in the context
176 // of this slot
177 QMetaObject::invokeMethod(this, "loadSubTree", Qt::QueuedConnection);
178 }
179
180 void TreeViewSidebarPage::expandToLeafDir()
181 {
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);
187
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);
192
193 QItemSelectionModel* selModel = m_treeView->selectionModel();
194 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
195 }
196
197
198 void TreeViewSidebarPage::loadSubTree()
199 {
200 QItemSelectionModel* selModel = m_treeView->selectionModel();
201 selModel->clearSelection();
202
203 if (m_leafDir.isParentOf(m_dirLister->url())) {
204 // The leaf directory is not a child of the base URL, hence
205 // no sub directory must be loaded or selected.
206 return;
207 }
208
209 const QModelIndex index = m_dolphinModel->indexForUrl(m_leafDir);
210 if (index.isValid()) {
211 // the item with the given URL is already part of the model
212 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(index);
213 m_treeView->scrollTo(proxyIndex);
214 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
215 } else {
216 // Load all sub directories that need to get expanded for making
217 // the leaf directory visible. The slot triggerExpanding() will
218 // get invoked if the expanding has been finished.
219 m_dolphinModel->expandToUrl(m_leafDir);
220 }
221 }
222
223 void TreeViewSidebarPage::loadTree(const KUrl& url)
224 {
225 Q_ASSERT(m_dirLister != 0);
226 m_leafDir = url;
227
228 // adjust the root of the tree to the base place
229 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
230 KUrl baseUrl = placesModel->url(placesModel->closestItem(url));
231 if (!baseUrl.isValid()) {
232 // it's possible that no closest item is available and hence an
233 // empty URL is returned
234 baseUrl = url;
235 }
236
237 if (m_dirLister->url() != baseUrl) {
238 m_dirLister->stop();
239 m_dirLister->openUrl(baseUrl, KDirLister::Reload);
240 } else {
241 loadSubTree();
242 }
243 }
244
245 #include "treeviewsidebarpage.moc"