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 "dolphinmodel.h"
23 #include "dolphinsortfilterproxymodel.h"
24 #include "settings/dolphinsettings.h"
25 #include "dolphin_folderspanelsettings.h"
26 #include "dolphin_generalsettings.h"
27 #include "draganddrophelper.h"
28 #include "folderexpander.h"
29 #include "renamedialog.h"
30 #include "paneltreeview.h"
31 #include "treeviewcontextmenu.h"
33 #include <kfileplacesmodel.h>
34 #include <kdirlister.h>
35 #include <kfileitem.h>
36 #include <konq_operations.h>
38 #include <QApplication>
39 #include <QItemSelection>
42 #include <QModelIndex>
46 #include "views/dolphinview.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
);
100 QPointer
<RenameDialog
> dialog
= new RenameDialog(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
);
113 void FoldersPanel::setUrl(const KUrl
& url
)
115 if (!url
.isValid() || (url
== Panel::url())) {
120 if (m_dirLister
!= 0) {
121 m_setLeafVisible
= true;
126 void FoldersPanel::showEvent(QShowEvent
* event
)
128 if (event
->spontaneous()) {
129 Panel::showEvent(event
);
133 if (m_dirLister
== 0) {
134 // Postpone the creating of the dir lister to the first show event.
135 // This assures that no performance and memory overhead is given when the TreeView is not
136 // used at all (see FoldersPanel::setUrl()).
137 m_dirLister
= new KDirLister();
138 m_dirLister
->setDirOnlyMode(true);
139 m_dirLister
->setAutoUpdate(true);
140 m_dirLister
->setMainWindow(window());
141 m_dirLister
->setDelayedMimeTypes(true);
142 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
143 m_dirLister
->setShowingDotFiles(FoldersPanelSettings::showHiddenFiles());
145 Q_ASSERT(m_dolphinModel
== 0);
146 m_dolphinModel
= new DolphinModel(this);
147 m_dolphinModel
->setDirLister(m_dirLister
);
148 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
149 connect(m_dolphinModel
, SIGNAL(expand(const QModelIndex
&)),
150 this, SLOT(expandToDir(const QModelIndex
&)));
152 Q_ASSERT(m_proxyModel
== 0);
153 m_proxyModel
= new DolphinSortFilterProxyModel(this);
154 m_proxyModel
->setSourceModel(m_dolphinModel
);
156 Q_ASSERT(m_treeView
== 0);
157 m_treeView
= new PanelTreeView(this);
158 m_treeView
->setModel(m_proxyModel
);
159 m_proxyModel
->setSorting(DolphinView::SortByName
);
160 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
162 new FolderExpander(m_treeView
, m_proxyModel
);
164 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
165 this, SLOT(updateActiveView(const QModelIndex
&)));
166 connect(m_treeView
, SIGNAL(urlsDropped(const QModelIndex
&, QDropEvent
*)),
167 this, SLOT(dropUrls(const QModelIndex
&, QDropEvent
*)));
168 connect(m_treeView
, SIGNAL(pressed(const QModelIndex
&)),
169 this, SLOT(updateMouseButtons()));
171 QVBoxLayout
* layout
= new QVBoxLayout(this);
172 layout
->setMargin(0);
173 layout
->addWidget(m_treeView
);
177 Panel::showEvent(event
);
180 void FoldersPanel::contextMenuEvent(QContextMenuEvent
* event
)
182 Panel::contextMenuEvent(event
);
185 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
186 if (index
.isValid()) {
187 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
188 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
191 TreeViewContextMenu
contextMenu(this, item
);
195 void FoldersPanel::keyPressEvent(QKeyEvent
* event
)
197 const int key
= event
->key();
198 if ((key
== Qt::Key_Enter
) || (key
== Qt::Key_Return
)) {
200 updateActiveView(m_treeView
->currentIndex());
202 Panel::keyPressEvent(event
);
206 void FoldersPanel::updateActiveView(const QModelIndex
& index
)
208 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
209 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
210 if (!item
.isNull()) {
211 emit
changeUrl(item
.url(), m_mouseButtons
);
215 void FoldersPanel::dropUrls(const QModelIndex
& index
, QDropEvent
* event
)
217 if (index
.isValid()) {
218 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
219 KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
220 Q_ASSERT(!item
.isNull());
222 DragAndDropHelper::instance().dropUrls(item
, item
.url(), event
, this);
227 void FoldersPanel::expandToDir(const QModelIndex
& index
)
229 m_treeView
->setExpanded(index
, true);
230 selectLeafDirectory();
231 m_treeView
->resizeColumnToContents(DolphinModel::Name
);
234 void FoldersPanel::scrollToLeaf()
236 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
237 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
238 if (proxyIndex
.isValid()) {
239 m_treeView
->scrollTo(proxyIndex
);
243 void FoldersPanel::updateMouseButtons()
245 m_mouseButtons
= QApplication::mouseButtons();
248 void FoldersPanel::loadTree(const KUrl
& url
)
250 Q_ASSERT(m_dirLister
!= 0);
254 if (url
.isLocalFile()) {
255 // use the root directory as base for local URLs (#150941)
256 baseUrl
= QDir::rootPath();
258 // clear the path for non-local URLs and use it as base
260 baseUrl
.setPath(QString('/'));
263 if (m_dirLister
->url() != baseUrl
) {
265 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
267 m_dolphinModel
->expandToUrl(m_leafDir
);
270 void FoldersPanel::selectLeafDirectory()
272 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
273 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
274 if (!proxyIndex
.isValid()) {
278 if (m_setLeafVisible
) {
279 // Invoke m_treeView->scrollTo(proxyIndex) asynchronously by
280 // scrollToLeaf(). This assures that the scrolling is done after
281 // the horizontal scrollbar gets visible (otherwise the scrollbar
282 // might hide the leaf).
283 QTimer::singleShot(100, this, SLOT(scrollToLeaf()));
284 m_setLeafVisible
= false;
287 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
288 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::ClearAndSelect
);
291 #include "folderspanel.moc"