]>
cloud.milkyroute.net Git - dolphin.git/blob - src/treeviewsidebarpage.cpp
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 "bookmarkselector.h"
23 #include "dolphinmainwindow.h"
24 #include "dolphinview.h"
26 #include "kdirlister.h"
27 #include "kdirmodel.h"
29 #include <QHeaderView>
30 #include <QItemSelectionModel>
32 #include <QVBoxLayout>
34 TreeViewSidebarPage::TreeViewSidebarPage(DolphinMainWindow
* mainWindow
,
36 SidebarPage(mainWindow
, parent
),
41 Q_ASSERT(mainWindow
!= 0);
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);
50 m_dirModel
= new KDirModel();
51 m_dirModel
->setDirLister(m_dirLister
);
53 m_treeView
= new QTreeView(this);
54 m_treeView
->setModel(m_dirModel
);
55 m_treeView
->setSelectionMode(QAbstractItemView::SingleSelection
);
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();
65 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
66 this, SLOT(updateViewUrl(const QModelIndex
&)));
68 QVBoxLayout
* layout
= new QVBoxLayout(this);
69 layout
->addWidget(m_treeView
);
72 TreeViewSidebarPage::~TreeViewSidebarPage()
78 void TreeViewSidebarPage::activeViewChanged()
80 connectToActiveView();
83 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
85 SidebarPage::showEvent(event
);
86 connectToActiveView();
89 void TreeViewSidebarPage::updateSelection(const KUrl
& url
)
91 // adjust the root of the tree to the base bookmark
92 KUrl baseUrl
= BookmarkSelector::baseBookmark(url
).url();
93 if (m_dirLister
->url() != baseUrl
) {
95 m_dirLister
->openUrl(baseUrl
);
98 // select the folder which contains the given url
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();
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);
112 selModel
->setCurrentIndex(index
, QItemSelectionModel::Select
);
116 void TreeViewSidebarPage::updateViewUrl(const QModelIndex
& index
)
118 KFileItem
* item
= m_dirModel
->itemForIndex(index
);
120 const KUrl
& url
= item
->url();
121 mainWindow()->activeView()->setUrl(url
);
125 void TreeViewSidebarPage::connectToActiveView()
127 const QWidget
* parent
= parentWidget();
128 if ((parent
== 0) || parent
->isHidden()) {
132 DolphinView
* view
= mainWindow()->activeView();
133 const KUrl
& url
= view
->url();
136 m_dirLister
->openUrl(url
);
137 connect(view
, SIGNAL(urlChanged(const KUrl
&)),
138 this, SLOT(updateSelection(const KUrl
&)));
140 updateSelection(url
);
143 #include "treeviewsidebarpage.moc"