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 "dolphin_folderspanelsettings.h"
27 #include "dolphin_generalsettings.h"
28 #include "renamedialog.h"
29 #include "sidebartreeview.h"
30 #include "treeviewcontextmenu.h"
32 #include <kfileplacesmodel.h>
33 #include <kdirlister.h>
34 #include <kfileitem.h>
35 #include <konq_operations.h>
37 #include <QApplication>
38 #include <QItemSelection>
41 #include <QModelIndex>
45 TreeViewSidebarPage::TreeViewSidebarPage(QWidget
* parent
) :
47 m_setLeafVisible(false),
48 m_mouseButtons(Qt::NoButton
),
57 TreeViewSidebarPage::~TreeViewSidebarPage()
59 FoldersPanelSettings::self()->writeConfig();
63 delete m_dolphinModel
;
65 m_dirLister
= 0; // deleted by m_dolphinModel
68 QSize
TreeViewSidebarPage::sizeHint() const
70 return QSize(200, 400);
73 void TreeViewSidebarPage::setShowHiddenFiles(bool show
)
75 FoldersPanelSettings::setShowHiddenFiles(show
);
76 if (m_dirLister
!= 0) {
77 m_dirLister
->setShowingDotFiles(show
);
78 m_dirLister
->openUrl(m_dirLister
->url(), KDirLister::Reload
);
82 bool TreeViewSidebarPage::showHiddenFiles() const
84 return FoldersPanelSettings::showHiddenFiles();
88 void TreeViewSidebarPage::rename(const KFileItem
& item
)
90 if (DolphinSettings::instance().generalSettings()->renameInline()) {
91 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
92 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
93 m_treeView
->edit(proxyIndex
);
97 RenameDialog
dialog(this, items
);
98 if (dialog
.exec() == QDialog::Accepted
) {
99 const QString
& newName
= dialog
.newName();
100 if (!newName
.isEmpty()) {
101 KUrl newUrl
= item
.url();
102 newUrl
.setFileName(newName
);
103 KonqOperations::rename(this, item
.url(), newUrl
);
109 void TreeViewSidebarPage::setUrl(const KUrl
& url
)
111 if (!url
.isValid() || (url
== SidebarPage::url())) {
115 SidebarPage::setUrl(url
);
116 if (m_dirLister
!= 0) {
117 m_setLeafVisible
= true;
122 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
124 if (event
->spontaneous()) {
125 SidebarPage::showEvent(event
);
129 if (m_dirLister
== 0) {
130 // Postpone the creating of the dir lister to the first show event.
131 // This assures that no performance and memory overhead is given when the TreeView is not
132 // used at all (see TreeViewSidebarPage::setUrl()).
133 m_dirLister
= new KDirLister();
134 m_dirLister
->setDirOnlyMode(true);
135 m_dirLister
->setAutoUpdate(true);
136 m_dirLister
->setMainWindow(window());
137 m_dirLister
->setDelayedMimeTypes(true);
138 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
139 m_dirLister
->setShowingDotFiles(FoldersPanelSettings::showHiddenFiles());
141 connect(m_dirLister
, SIGNAL(completed()),
142 this, SLOT(triggerLoadSubTree()));
144 Q_ASSERT(m_dolphinModel
== 0);
145 m_dolphinModel
= new DolphinModel(this);
146 m_dolphinModel
->setDirLister(m_dirLister
);
147 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
148 connect(m_dolphinModel
, SIGNAL(expand(const QModelIndex
&)),
149 this, SLOT(triggerExpanding()));
151 Q_ASSERT(m_proxyModel
== 0);
152 m_proxyModel
= new DolphinSortFilterProxyModel(this);
153 m_proxyModel
->setSourceModel(m_dolphinModel
);
155 Q_ASSERT(m_treeView
== 0);
156 m_treeView
= new SidebarTreeView(this);
157 m_treeView
->setModel(m_proxyModel
);
158 m_proxyModel
->setSorting(DolphinView::SortByName
);
159 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
161 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
162 this, SLOT(updateActiveView(const QModelIndex
&)));
163 connect(m_treeView
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&)),
164 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&)));
165 connect(m_treeView
, SIGNAL(pressed(const QModelIndex
&)),
166 this, SLOT(updateMouseButtons()));
168 QVBoxLayout
* layout
= new QVBoxLayout(this);
169 layout
->setMargin(0);
170 layout
->addWidget(m_treeView
);
174 SidebarPage::showEvent(event
);
177 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
179 SidebarPage::contextMenuEvent(event
);
182 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
183 if (index
.isValid()) {
184 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
185 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
186 emit
changeSelection(KFileItemList());
189 TreeViewContextMenu
contextMenu(this, item
);
193 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
195 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
196 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
197 if (!item
.isNull()) {
198 emit
changeUrl(item
.url(), m_mouseButtons
);
202 void TreeViewSidebarPage::dropUrls(const KUrl::List
& urls
,
203 const QModelIndex
& index
)
205 if (index
.isValid()) {
206 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
207 KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
208 Q_ASSERT(!item
.isNull());
210 emit
urlsDropped(urls
, item
.url());
215 void TreeViewSidebarPage::triggerExpanding()
217 // the expanding of the folders may not be done in the context
219 QMetaObject::invokeMethod(this, "expandToLeafDir", Qt::QueuedConnection
);
222 void TreeViewSidebarPage::triggerLoadSubTree()
224 // the loading of the sub tree may not be done in the context
226 QMetaObject::invokeMethod(this, "loadSubTree", Qt::QueuedConnection
);
229 void TreeViewSidebarPage::expandToLeafDir()
231 // expand all directories until the parent directory of m_leafDir
232 const KUrl parentUrl
= m_leafDir
.upUrl();
233 QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(parentUrl
);
234 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
235 m_treeView
->setExpanded(proxyIndex
, true);
236 selectLeafDirectory();
240 void TreeViewSidebarPage::loadSubTree()
242 m_treeView
->selectionModel()->clearSelection();
244 if (m_leafDir
.isParentOf(m_dirLister
->url())) {
245 // The leaf directory is not a child of the base URL, hence
246 // no sub directory must be loaded or selected.
250 const QModelIndex index
= m_dolphinModel
->indexForUrl(m_leafDir
);
251 if (index
.isValid()) {
252 selectLeafDirectory();
254 // Load all sub directories that need to get expanded for making
255 // the leaf directory visible. The slot triggerExpanding() will
256 // get invoked if the expanding has been finished.
257 m_dolphinModel
->expandToUrl(m_leafDir
);
261 void TreeViewSidebarPage::scrollToLeaf()
263 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
264 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
265 if (proxyIndex
.isValid()) {
266 m_treeView
->scrollTo(proxyIndex
);
270 void TreeViewSidebarPage::updateMouseButtons()
272 m_mouseButtons
= QApplication::mouseButtons();
275 void TreeViewSidebarPage::loadTree(const KUrl
& url
)
277 Q_ASSERT(m_dirLister
!= 0);
281 if (url
.isLocalFile()) {
282 // use the root directory as base for local URLs
283 baseUrl
= QDir::rootPath();
285 // clear the path for non-local URLs and use it as base
287 baseUrl
.setPath(QString());
290 if (m_dirLister
->url() != baseUrl
) {
292 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
298 void TreeViewSidebarPage::selectLeafDirectory()
300 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
301 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
302 if (!proxyIndex
.isValid()) {
306 if (m_setLeafVisible
) {
307 // Invoke m_treeView->scrollTo(proxyIndex) asynchronously by
308 // scrollToLeaf(). This assures that the scrolling is done after
309 // the horizontal scrollbar gets visible (otherwise the scrollbar
310 // might hide the leaf).
311 QTimer::singleShot(100, this, SLOT(scrollToLeaf()));
312 m_setLeafVisible
= false;
315 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
316 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
319 #include "treeviewsidebarpage.moc"