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 "folderspanel.h"
22 #include "settings/dolphinsettings.h"
23 #include "dolphin_folderspanelsettings.h"
24 #include "dolphin_generalsettings.h"
25 #include "paneltreeview.h"
26 #include "treeviewcontextmenu.h"
28 #include <kfileplacesmodel.h>
29 #include <kdirlister.h>
30 #include <kfileitem.h>
31 #include <konq_operations.h>
33 #include <QApplication>
34 #include <QItemSelection>
37 #include <QModelIndex>
41 #include <views/draganddrophelper.h>
42 #include <views/dolphinmodel.h>
43 #include <views/dolphinsortfilterproxymodel.h>
44 #include <views/dolphinview.h>
45 #include <views/folderexpander.h>
46 #include <views/renamedialog.h>
48 FoldersPanel::FoldersPanel(QWidget
* parent
) :
50 m_setLeafVisible(false),
51 m_mouseButtons(Qt::NoButton
),
58 setLayoutDirection(Qt::LeftToRight
);
61 FoldersPanel::~FoldersPanel()
63 FoldersPanelSettings::self()->writeConfig();
67 delete m_dolphinModel
;
69 m_dirLister
= 0; // deleted by m_dolphinModel
72 QSize
FoldersPanel::sizeHint() const
74 return QSize(200, 400);
77 void FoldersPanel::setShowHiddenFiles(bool show
)
79 FoldersPanelSettings::setShowHiddenFiles(show
);
80 if (m_dirLister
!= 0) {
81 m_dirLister
->setShowingDotFiles(show
);
82 m_dirLister
->openUrl(m_dirLister
->url(), KDirLister::Reload
);
86 bool FoldersPanel::showHiddenFiles() const
88 return FoldersPanelSettings::showHiddenFiles();
91 void FoldersPanel::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
);
98 RenameDialog
* dialog
= new RenameDialog(this, KFileItemList() << item
);
99 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
102 dialog
->activateWindow();
106 void FoldersPanel::setUrl(const KUrl
& url
)
108 if (!url
.isValid() || (url
== Panel::url())) {
113 if (m_dirLister
!= 0) {
114 m_setLeafVisible
= true;
119 void FoldersPanel::showEvent(QShowEvent
* event
)
121 if (event
->spontaneous()) {
122 Panel::showEvent(event
);
126 if (m_dirLister
== 0) {
127 // Postpone the creating of the dir lister to the first show event.
128 // This assures that no performance and memory overhead is given when the TreeView is not
129 // used at all (see FoldersPanel::setUrl()).
130 m_dirLister
= new KDirLister();
131 m_dirLister
->setDirOnlyMode(true);
132 m_dirLister
->setAutoUpdate(true);
133 m_dirLister
->setMainWindow(window());
134 m_dirLister
->setDelayedMimeTypes(true);
135 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
136 m_dirLister
->setShowingDotFiles(FoldersPanelSettings::showHiddenFiles());
138 Q_ASSERT(m_dolphinModel
== 0);
139 m_dolphinModel
= new DolphinModel(this);
140 m_dolphinModel
->setDirLister(m_dirLister
);
141 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
142 connect(m_dolphinModel
, SIGNAL(expand(const QModelIndex
&)),
143 this, SLOT(expandToDir(const QModelIndex
&)));
145 Q_ASSERT(m_proxyModel
== 0);
146 m_proxyModel
= new DolphinSortFilterProxyModel(this);
147 m_proxyModel
->setSourceModel(m_dolphinModel
);
149 Q_ASSERT(m_treeView
== 0);
150 m_treeView
= new PanelTreeView(this);
151 m_treeView
->setModel(m_proxyModel
);
152 m_proxyModel
->setSorting(DolphinView::SortByName
);
153 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
155 new FolderExpander(m_treeView
, m_proxyModel
);
157 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
158 this, SLOT(updateActiveView(const QModelIndex
&)));
159 connect(m_treeView
, SIGNAL(urlsDropped(const QModelIndex
&, QDropEvent
*)),
160 this, SLOT(dropUrls(const QModelIndex
&, QDropEvent
*)));
161 connect(m_treeView
, SIGNAL(pressed(const QModelIndex
&)),
162 this, SLOT(updateMouseButtons()));
164 QVBoxLayout
* layout
= new QVBoxLayout(this);
165 layout
->setMargin(0);
166 layout
->addWidget(m_treeView
);
170 Panel::showEvent(event
);
173 void FoldersPanel::contextMenuEvent(QContextMenuEvent
* event
)
175 Panel::contextMenuEvent(event
);
178 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
179 if (index
.isValid()) {
180 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
181 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
184 TreeViewContextMenu
contextMenu(this, item
);
188 void FoldersPanel::keyPressEvent(QKeyEvent
* event
)
190 const int key
= event
->key();
191 if ((key
== Qt::Key_Enter
) || (key
== Qt::Key_Return
)) {
193 updateActiveView(m_treeView
->currentIndex());
195 Panel::keyPressEvent(event
);
199 void FoldersPanel::updateActiveView(const QModelIndex
& index
)
201 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
202 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
203 if (!item
.isNull()) {
204 emit
changeUrl(item
.url(), m_mouseButtons
);
208 void FoldersPanel::dropUrls(const QModelIndex
& index
, QDropEvent
* event
)
210 if (index
.isValid()) {
211 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
212 KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
213 Q_ASSERT(!item
.isNull());
215 DragAndDropHelper::instance().dropUrls(item
, item
.url(), event
, this);
220 void FoldersPanel::expandToDir(const QModelIndex
& index
)
222 m_treeView
->setExpanded(index
, true);
223 selectLeafDirectory();
224 m_treeView
->resizeColumnToContents(DolphinModel::Name
);
227 void FoldersPanel::scrollToLeaf()
229 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
230 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
231 if (proxyIndex
.isValid()) {
232 m_treeView
->scrollTo(proxyIndex
);
236 void FoldersPanel::updateMouseButtons()
238 m_mouseButtons
= QApplication::mouseButtons();
241 void FoldersPanel::loadTree(const KUrl
& url
)
243 Q_ASSERT(m_dirLister
!= 0);
247 if (url
.isLocalFile()) {
248 // use the root directory as base for local URLs (#150941)
249 baseUrl
= QDir::rootPath();
251 // clear the path for non-local URLs and use it as base
253 baseUrl
.setPath(QString('/'));
256 if (m_dirLister
->url() != baseUrl
) {
258 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
260 m_dolphinModel
->expandToUrl(m_leafDir
);
263 void FoldersPanel::selectLeafDirectory()
265 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
266 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
267 if (!proxyIndex
.isValid()) {
271 if (m_setLeafVisible
) {
272 // Invoke m_treeView->scrollTo(proxyIndex) asynchronously by
273 // scrollToLeaf(). This assures that the scrolling is done after
274 // the horizontal scrollbar gets visible (otherwise the scrollbar
275 // might hide the leaf).
276 QTimer::singleShot(100, this, SLOT(scrollToLeaf()));
277 m_setLeafVisible
= false;
280 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
281 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::ClearAndSelect
);
284 #include "folderspanel.moc"