1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz <peter.penz19@gmail.com> *
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>
35 #include <QItemSelection>
36 #include <QModelIndex>
42 #include <views/draganddrophelper.h>
43 #include <views/dolphinmodel.h>
44 #include <views/dolphinsortfilterproxymodel.h>
45 #include <views/dolphinview.h>
46 #include <views/folderexpander.h>
47 #include <views/renamedialog.h>
49 FoldersPanel::FoldersPanel(QWidget
* parent
) :
51 m_setLeafVisible(false),
52 m_mouseButtons(Qt::NoButton
),
59 setLayoutDirection(Qt::LeftToRight
);
62 FoldersPanel::~FoldersPanel()
64 FoldersPanelSettings::self()->writeConfig();
68 delete m_dolphinModel
;
70 m_dirLister
= 0; // deleted by m_dolphinModel
73 QSize
FoldersPanel::sizeHint() const
75 return QSize(200, 400);
78 void FoldersPanel::setShowHiddenFiles(bool show
)
80 FoldersPanelSettings::setShowHiddenFiles(show
);
81 if (m_dirLister
!= 0) {
82 m_dirLister
->setShowingDotFiles(show
);
83 m_dirLister
->openUrl(m_dirLister
->url(), KDirLister::Reload
);
87 bool FoldersPanel::showHiddenFiles() const
89 return FoldersPanelSettings::showHiddenFiles();
92 void FoldersPanel::setAutoScrolling(bool enable
)
94 m_treeView
->setAutoHorizontalScroll(enable
);
95 FoldersPanelSettings::setAutoScrolling(enable
);
98 bool FoldersPanel::autoScrolling() const
100 return FoldersPanelSettings::autoScrolling();
103 void FoldersPanel::rename(const KFileItem
& item
)
105 if (DolphinSettings::instance().generalSettings()->renameInline()) {
106 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
107 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
108 m_treeView
->edit(proxyIndex
);
110 RenameDialog
* dialog
= new RenameDialog(this, KFileItemList() << item
);
111 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
114 dialog
->activateWindow();
118 bool FoldersPanel::urlChanged()
120 if (!url().isValid() || url().protocol().contains("search")) {
121 // Skip results shown by a search, as possible identical
122 // directory names are useless without parent-path information.
126 if (m_dirLister
!= 0) {
127 m_setLeafVisible
= true;
134 void FoldersPanel::showEvent(QShowEvent
* event
)
136 if (event
->spontaneous()) {
137 Panel::showEvent(event
);
141 if (m_dirLister
== 0) {
142 // Postpone the creating of the dir lister to the first show event.
143 // This assures that no performance and memory overhead is given when the TreeView is not
144 // used at all (see FoldersPanel::setUrl()).
145 m_dirLister
= new KDirLister();
146 m_dirLister
->setDirOnlyMode(true);
147 m_dirLister
->setAutoUpdate(true);
148 m_dirLister
->setMainWindow(window());
149 m_dirLister
->setDelayedMimeTypes(true);
150 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
151 m_dirLister
->setShowingDotFiles(FoldersPanelSettings::showHiddenFiles());
152 connect(m_dirLister
, SIGNAL(completed()), this, SLOT(slotDirListerCompleted()));
154 Q_ASSERT(m_dolphinModel
== 0);
155 m_dolphinModel
= new DolphinModel(this);
156 m_dolphinModel
->setDirLister(m_dirLister
);
157 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
158 connect(m_dolphinModel
, SIGNAL(expand(const QModelIndex
&)),
159 this, SLOT(expandToDir(const QModelIndex
&)));
161 Q_ASSERT(m_proxyModel
== 0);
162 m_proxyModel
= new DolphinSortFilterProxyModel(this);
163 m_proxyModel
->setSourceModel(m_dolphinModel
);
165 Q_ASSERT(m_treeView
== 0);
166 m_treeView
= new PanelTreeView(this);
167 m_treeView
->setModel(m_proxyModel
);
168 m_proxyModel
->setSorting(DolphinView::SortByName
);
169 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
171 new FolderExpander(m_treeView
, m_proxyModel
);
173 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
174 this, SLOT(updateActiveView(const QModelIndex
&)));
175 connect(m_treeView
, SIGNAL(urlsDropped(const QModelIndex
&, QDropEvent
*)),
176 this, SLOT(dropUrls(const QModelIndex
&, QDropEvent
*)));
177 connect(m_treeView
, SIGNAL(pressed(const QModelIndex
&)),
178 this, SLOT(updateMouseButtons()));
180 QVBoxLayout
* layout
= new QVBoxLayout(this);
181 layout
->setMargin(0);
182 layout
->addWidget(m_treeView
);
184 setAutoScrolling(FoldersPanelSettings::autoScrolling());
185 setShowHiddenFiles(FoldersPanelSettings::showHiddenFiles());
189 Panel::showEvent(event
);
192 void FoldersPanel::contextMenuEvent(QContextMenuEvent
* event
)
194 Panel::contextMenuEvent(event
);
197 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
198 if (index
.isValid()) {
199 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
200 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
203 QPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, item
);
208 void FoldersPanel::keyPressEvent(QKeyEvent
* event
)
210 const int key
= event
->key();
211 if ((key
== Qt::Key_Enter
) || (key
== Qt::Key_Return
)) {
213 updateActiveView(m_treeView
->currentIndex());
215 Panel::keyPressEvent(event
);
219 void FoldersPanel::updateActiveView(const QModelIndex
& index
)
221 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
222 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
223 if (!item
.isNull()) {
224 emit
changeUrl(item
.url(), m_mouseButtons
);
228 void FoldersPanel::dropUrls(const QModelIndex
& index
, QDropEvent
* event
)
230 if (index
.isValid()) {
231 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
232 KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
233 Q_ASSERT(!item
.isNull());
235 DragAndDropHelper::instance().dropUrls(item
, item
.url(), event
, this);
240 void FoldersPanel::expandToDir(const QModelIndex
& index
)
242 m_treeView
->setExpanded(index
, true);
243 selectLeafDirectory();
246 void FoldersPanel::scrollToLeaf()
248 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
249 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
250 if (proxyIndex
.isValid()) {
251 m_treeView
->scrollTo(proxyIndex
);
255 void FoldersPanel::updateMouseButtons()
257 m_mouseButtons
= QApplication::mouseButtons();
260 void FoldersPanel::slotDirListerCompleted()
262 m_treeView
->resizeColumnToContents(DolphinModel::Name
);
264 if (m_setLeafVisible
) {
265 // Invoke scrollToLeaf() asynchronously. This assures that
266 // the horizontal scrollbar is shown after resizing the column
267 // (otherwise the scrollbar might hide the leaf).
268 QTimer::singleShot(0, this, SLOT(scrollToLeaf()));
269 m_setLeafVisible
= false;
273 void FoldersPanel::loadTree(const KUrl
& url
)
275 Q_ASSERT(m_dirLister
!= 0);
279 if (url
.isLocalFile()) {
280 // use the root directory as base for local URLs (#150941)
281 baseUrl
= QDir::rootPath();
283 // clear the path for non-local URLs and use it as base
285 baseUrl
.setPath(QString('/'));
288 if (m_dirLister
->url() != baseUrl
) {
290 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
292 m_dolphinModel
->expandToUrl(m_leafDir
);
295 void FoldersPanel::selectLeafDirectory()
297 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
298 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
299 if (proxyIndex
.isValid()) {
300 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
301 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::ClearAndSelect
);
305 #include "folderspanel.moc"