]> cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewsidebarpage.cpp
Some basic fixes to stay synchronized between the tree view and the currently active...
[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 "bookmarkselector.h"
23 #include "dolphinmainwindow.h"
24 #include "dolphinview.h"
25
26 #include "kdirlister.h"
27 #include "kdirmodel.h"
28
29 #include <QHeaderView>
30 #include <QItemSelectionModel>
31 #include <QTreeView>
32 #include <QVBoxLayout>
33
34 TreeViewSidebarPage::TreeViewSidebarPage(DolphinMainWindow* mainWindow,
35 QWidget* parent) :
36 SidebarPage(mainWindow, parent),
37 m_dirLister(0),
38 m_dirModel(0),
39 m_treeView(0)
40 {
41 Q_ASSERT(mainWindow != 0);
42
43 m_dirLister = new KDirLister();
44 m_dirLister->setDirOnlyMode(true);
45 m_dirLister->setAutoUpdate(true);
46 m_dirLister->setMainWindow(this);
47 m_dirLister->setDelayedMimeTypes(true);
48 m_dirLister->setAutoErrorHandlingEnabled(false, this);
49
50 m_dirModel = new KDirModel();
51 m_dirModel->setDirLister(m_dirLister);
52
53 m_treeView = new QTreeView(this);
54 m_treeView->setModel(m_dirModel);
55 m_treeView->setSelectionMode(QAbstractItemView::SingleSelection);
56
57 // hide all columns except of the 'Name' column
58 m_treeView->hideColumn(KDirModel::Size);
59 m_treeView->hideColumn(KDirModel::ModifiedTime);
60 m_treeView->hideColumn(KDirModel::Permissions);
61 m_treeView->hideColumn(KDirModel::Owner);
62 m_treeView->hideColumn(KDirModel::Group);
63 m_treeView->header()->hide();
64
65 connect(m_treeView, SIGNAL(clicked(const QModelIndex&)),
66 this, SLOT(updateViewUrl(const QModelIndex&)));
67
68 QVBoxLayout* layout = new QVBoxLayout(this);
69 layout->addWidget(m_treeView);
70 }
71
72 TreeViewSidebarPage::~TreeViewSidebarPage()
73 {
74 delete m_dirLister;
75 m_dirLister = 0;
76 }
77
78 void TreeViewSidebarPage::activeViewChanged()
79 {
80 connectToActiveView();
81 }
82
83 void TreeViewSidebarPage::showEvent(QShowEvent* event)
84 {
85 SidebarPage::showEvent(event);
86 connectToActiveView();
87 }
88
89 void TreeViewSidebarPage::updateSelection(const KUrl& url)
90 {
91 // adjust the root of the tree to the base bookmark
92 KUrl baseUrl = BookmarkSelector::baseBookmark(url).url();
93 if (m_dirLister->url() != baseUrl) {
94 m_dirLister->stop();
95 m_dirLister->openUrl(baseUrl);
96 }
97
98 // select the folder which contains the given url
99
100 // TODO: check how Konqi does it before reinventing the wheel. The directory
101 // must already be loaded _before_ the index can be retrieved by
102 // KDirModel::indexForItem().
103 QItemSelectionModel* selModel = m_treeView->selectionModel();
104 selModel->clearSelection();
105
106 KFileItem item(S_IFDIR, KFileItem::Unknown, url);
107 const QModelIndex index = m_dirModel->indexForItem(item);
108 if (index.isValid()) {
109 m_treeView->scrollTo(index);
110 m_treeView->setExpanded(index, true);
111
112 selModel->setCurrentIndex(index, QItemSelectionModel::Select);
113 }
114 }
115
116 void TreeViewSidebarPage::updateViewUrl(const QModelIndex& index)
117 {
118 KFileItem* item = m_dirModel->itemForIndex(index);
119 if (item != 0) {
120 const KUrl& url = item->url();
121 mainWindow()->activeView()->setUrl(url);
122 }
123 }
124
125 void TreeViewSidebarPage::connectToActiveView()
126 {
127 const QWidget* parent = parentWidget();
128 if ((parent == 0) || parent->isHidden()) {
129 return;
130 }
131
132 DolphinView* view = mainWindow()->activeView();
133 const KUrl& url = view->url();
134
135 m_dirLister->stop();
136 m_dirLister->openUrl(url);
137 connect(view, SIGNAL(urlChanged(const KUrl&)),
138 this, SLOT(updateSelection(const KUrl&)));
139
140 updateSelection(url);
141 }
142
143 #include "treeviewsidebarpage.moc"