]>
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
),
42 Q_ASSERT(mainWindow
!= 0);
44 m_dirLister
= new KDirLister();
45 m_dirLister
->setDirOnlyMode(true);
46 m_dirLister
->setAutoUpdate(true);
47 m_dirLister
->setMainWindow(this);
48 m_dirLister
->setDelayedMimeTypes(true);
49 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
51 m_dirModel
= new KDirModel();
52 m_dirModel
->setDirLister(m_dirLister
);
54 m_treeView
= new QTreeView(this);
55 m_treeView
->setModel(m_dirModel
);
56 m_treeView
->setSelectionMode(QAbstractItemView::SingleSelection
);
57 m_treeView
->setEditTriggers(QAbstractItemView::NoEditTriggers
);
59 // hide all columns except of the 'Name' column
60 m_treeView
->hideColumn(KDirModel::Size
);
61 m_treeView
->hideColumn(KDirModel::ModifiedTime
);
62 m_treeView
->hideColumn(KDirModel::Permissions
);
63 m_treeView
->hideColumn(KDirModel::Owner
);
64 m_treeView
->hideColumn(KDirModel::Group
);
65 m_treeView
->header()->hide();
67 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
68 this, SLOT(updateActiveView(const QModelIndex
&)));
69 connect(m_treeView
, SIGNAL(doubleClicked(const QModelIndex
&)),
70 this, SLOT(slotDoubleClicked(const QModelIndex
&)));
72 QVBoxLayout
* layout
= new QVBoxLayout(this);
73 layout
->addWidget(m_treeView
);
76 TreeViewSidebarPage::~TreeViewSidebarPage()
82 void TreeViewSidebarPage::activeViewChanged()
84 connectToActiveView();
87 void TreeViewSidebarPage::showEvent(QShowEvent
* event
)
89 SidebarPage::showEvent(event
);
90 connectToActiveView();
93 void TreeViewSidebarPage::updateSelection(const KUrl
& url
)
95 if (!url
.isValid() || (url
== m_selectedUrl
)) {
101 // adjust the root of the tree to the base bookmark
102 const KUrl baseUrl
= BookmarkSelector::baseBookmark(url
).url();
103 if (m_dirLister
->url() != baseUrl
) {
105 m_dirLister
->openUrl(baseUrl
);
108 // select the folder which contains the given URL
109 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
110 selModel
->clearSelection();
112 const KFileItem
item(S_IFDIR
, KFileItem::Unknown
, url
);
113 const QModelIndex index
= m_dirModel
->indexForItem(item
);
114 if (index
.isValid()) {
115 // the item with the given URL is already part of the model
116 m_treeView
->scrollTo(index
);
117 selModel
->setCurrentIndex(index
, QItemSelectionModel::Select
);
120 // The item with the given URL is not loaded by the model yet. Iterate
121 // backward to the base URL and trigger the loading of the items for
122 // each hierarchy level.
123 connect(m_dirLister
, SIGNAL(completed()),
124 this, SLOT(expandSelectionParent()));
126 KUrl parentUrl
= url
.upUrl();
127 while (parentUrl
!= baseUrl
) {
128 m_dirLister
->openUrl(parentUrl
, true, false);
129 parentUrl
= parentUrl
.upUrl();
134 void TreeViewSidebarPage::expandSelectionParent()
136 disconnect(m_dirLister
, SIGNAL(completed()),
137 this, SLOT(expandSelectionParent()));
139 // expand the parent folder of the selected item
140 const KFileItem
parentItem(S_IFDIR
, KFileItem::Unknown
, m_selectedUrl
.upUrl());
141 QModelIndex index
= m_dirModel
->indexForItem(parentItem
);
142 if (index
.isValid()) {
143 m_treeView
->setExpanded(index
, true);
145 // select the item and assure that the item is visible
146 const KFileItem
selectedItem(S_IFDIR
, KFileItem::Unknown
, m_selectedUrl
);
147 index
= m_dirModel
->indexForItem(selectedItem
);
148 if (index
.isValid()) {
149 m_treeView
->scrollTo(index
);
151 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
152 selModel
->setCurrentIndex(index
, QItemSelectionModel::Select
);
157 void TreeViewSidebarPage::updateActiveView(const QModelIndex
& index
)
159 const KFileItem
* item
= m_dirModel
->itemForIndex(index
);
161 const KUrl
& url
= item
->url();
162 mainWindow()->activeView()->setUrl(url
);
166 void TreeViewSidebarPage::connectToActiveView()
168 const QWidget
* parent
= parentWidget();
169 if ((parent
== 0) || parent
->isHidden()) {
173 const DolphinView
* view
= mainWindow()->activeView();
174 const KUrl
& url
= view
->url();
176 connect(view
, SIGNAL(urlChanged(const KUrl
&)),
177 this, SLOT(updateSelection(const KUrl
&)));
179 updateSelection(url
);
182 #include "treeviewsidebarpage.moc"