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 "dolphindropcontroller.h"
23 #include "dolphinmodel.h"
24 #include "dolphinsortfilterproxymodel.h"
25 #include "dolphinview.h"
26 #include "dolphinsettings.h"
27 #include "dolphin_folderspanelsettings.h"
28 #include "dolphin_generalsettings.h"
29 #include "folderexpander.h"
30 #include "renamedialog.h"
31 #include "sidebartreeview.h"
32 #include "treeviewcontextmenu.h"
34 #include <kfileplacesmodel.h>
35 #include <kdirlister.h>
36 #include <kfileitem.h>
37 #include <konq_operations.h>
39 #include <QApplication>
40 #include <QItemSelection>
43 #include <QModelIndex>
47 TreeViewSidebarPage::TreeViewSidebarPage(QWidget
* parent
) :
49 m_setLeafVisible(false),
50 m_mouseButtons(Qt::NoButton
),
57 setLayoutDirection(Qt::LeftToRight
);
60 TreeViewSidebarPage::~TreeViewSidebarPage()
62 FoldersPanelSettings::self()->writeConfig();
66 delete m_dolphinModel
;
68 m_dirLister
= 0; // deleted by m_dolphinModel
71 QSize
TreeViewSidebarPage::sizeHint() const
73 return QSize(200, 400);
76 void TreeViewSidebarPage::setShowHiddenFiles(bool show
)
78 FoldersPanelSettings::setShowHiddenFiles(show
);
79 if (m_dirLister
!= 0) {
80 m_dirLister
->setShowingDotFiles(show
);
81 m_dirLister
->openUrl(m_dirLister
->url(), KDirLister::Reload
);
85 bool TreeViewSidebarPage::showHiddenFiles() const
87 return FoldersPanelSettings::showHiddenFiles();
91 void TreeViewSidebarPage::rename(const KFileItem
& item
)
93 if (DolphinSettings::instance().generalSettings()->renameInline()) {
94 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
95 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
96 m_treeView
->edit(proxyIndex
);
100 RenameDialog
dialog(this, items
);
101 if (dialog
.exec() == QDialog::Accepted
) {
102 const QString
& newName
= dialog
.newName();
103 if (!newName
.isEmpty()) {
104 KUrl newUrl
= item
.url();
105 newUrl
.setFileName(newName
);
106 KonqOperations::rename(this, item
.url(), newUrl
);
112 void TreeViewSidebarPage::setUrl(const KUrl
& url
)
114 if (!url
.isValid() || (url
== SidebarPage::url())) {
118 SidebarPage::setUrl(url
);
119 if (m_dirLister
!= 0) {
120 m_setLeafVisible
= true;
125 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
127 if (event
->spontaneous()) {
128 SidebarPage::showEvent(event
);
132 if (m_dirLister
== 0) {
133 // Postpone the creating of the dir lister to the first show event.
134 // This assures that no performance and memory overhead is given when the TreeView is not
135 // used at all (see TreeViewSidebarPage::setUrl()).
136 m_dirLister
= new KDirLister();
137 m_dirLister
->setDirOnlyMode(true);
138 m_dirLister
->setAutoUpdate(true);
139 m_dirLister
->setMainWindow(window());
140 m_dirLister
->setDelayedMimeTypes(true);
141 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
142 m_dirLister
->setShowingDotFiles(FoldersPanelSettings::showHiddenFiles());
144 connect(m_dirLister
, SIGNAL(completed()),
145 this, SLOT(triggerLoadSubTree()));
147 Q_ASSERT(m_dolphinModel
== 0);
148 m_dolphinModel
= new DolphinModel(this);
149 m_dolphinModel
->setDirLister(m_dirLister
);
150 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
151 connect(m_dolphinModel
, SIGNAL(expand(const QModelIndex
&)),
152 this, SLOT(triggerExpanding()));
154 Q_ASSERT(m_proxyModel
== 0);
155 m_proxyModel
= new DolphinSortFilterProxyModel(this);
156 m_proxyModel
->setSourceModel(m_dolphinModel
);
158 Q_ASSERT(m_treeView
== 0);
159 m_treeView
= new SidebarTreeView(this);
160 m_treeView
->setModel(m_proxyModel
);
161 m_proxyModel
->setSorting(DolphinView::SortByName
);
162 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
164 new FolderExpander(m_treeView
, m_proxyModel
);
166 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
167 this, SLOT(updateActiveView(const QModelIndex
&)));
168 connect(m_treeView
, SIGNAL(urlsDropped(const QModelIndex
&, QDropEvent
*)),
169 this, SLOT(dropUrls(const QModelIndex
&, QDropEvent
*)));
170 connect(m_treeView
, SIGNAL(pressed(const QModelIndex
&)),
171 this, SLOT(updateMouseButtons()));
173 QVBoxLayout
* layout
= new QVBoxLayout(this);
174 layout
->setMargin(0);
175 layout
->addWidget(m_treeView
);
179 SidebarPage::showEvent(event
);
182 void TreeViewSidebarPage::contextMenuEvent(QContextMenuEvent
* event
)
184 SidebarPage::contextMenuEvent(event
);
187 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
188 if (index
.isValid()) {
189 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
190 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
191 emit
changeSelection(KFileItemList());
194 TreeViewContextMenu
contextMenu(this, item
);
198 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
200 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
201 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
202 if (!item
.isNull()) {
203 emit
changeUrl(item
.url(), m_mouseButtons
);
207 void TreeViewSidebarPage::dropUrls(const QModelIndex
& index
, QDropEvent
* event
)
209 if (index
.isValid()) {
210 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
211 KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
212 Q_ASSERT(!item
.isNull());
214 DolphinDropController::dropUrls(item
, item
.url(), event
, this);
219 void TreeViewSidebarPage::triggerExpanding()
221 // the expanding of the folders may not be done in the context
223 QMetaObject::invokeMethod(this, "expandToLeafDir", Qt::QueuedConnection
);
226 void TreeViewSidebarPage::triggerLoadSubTree()
228 // the loading of the sub tree may not be done in the context
230 QMetaObject::invokeMethod(this, "loadSubTree", Qt::QueuedConnection
);
233 void TreeViewSidebarPage::expandToLeafDir()
235 // expand all directories until the parent directory of m_leafDir
236 const KUrl parentUrl
= m_leafDir
.upUrl();
237 QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(parentUrl
);
238 QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
239 m_treeView
->setExpanded(proxyIndex
, true);
240 selectLeafDirectory();
244 void TreeViewSidebarPage::loadSubTree()
246 m_treeView
->selectionModel()->clearSelection();
248 if (m_leafDir
.isParentOf(m_dirLister
->url())) {
249 // The leaf directory is not a child of the base URL, hence
250 // no sub directory must be loaded or selected.
254 const QModelIndex index
= m_dolphinModel
->indexForUrl(m_leafDir
);
255 if (index
.isValid()) {
256 selectLeafDirectory();
258 // Load all sub directories that need to get expanded for making
259 // the leaf directory visible. The slot triggerExpanding() will
260 // get invoked if the expanding has been finished.
261 m_dolphinModel
->expandToUrl(m_leafDir
);
265 void TreeViewSidebarPage::scrollToLeaf()
267 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
268 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
269 if (proxyIndex
.isValid()) {
270 m_treeView
->scrollTo(proxyIndex
);
274 void TreeViewSidebarPage::updateMouseButtons()
276 m_mouseButtons
= QApplication::mouseButtons();
279 void TreeViewSidebarPage::loadTree(const KUrl
& url
)
281 Q_ASSERT(m_dirLister
!= 0);
285 if (url
.isLocalFile()) {
286 // use the root directory as base for local URLs
287 baseUrl
= QDir::rootPath();
289 // clear the path for non-local URLs and use it as base
291 baseUrl
.setPath(QString());
294 if (m_dirLister
->url() != baseUrl
) {
296 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
302 void TreeViewSidebarPage::selectLeafDirectory()
304 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
305 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
306 if (!proxyIndex
.isValid()) {
310 if (m_setLeafVisible
) {
311 // Invoke m_treeView->scrollTo(proxyIndex) asynchronously by
312 // scrollToLeaf(). This assures that the scrolling is done after
313 // the horizontal scrollbar gets visible (otherwise the scrollbar
314 // might hide the leaf).
315 QTimer::singleShot(100, this, SLOT(scrollToLeaf()));
316 m_setLeafVisible
= false;
319 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
320 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::Select
);
323 #include "treeviewsidebarpage.moc"