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>
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 void FoldersPanel::setShowHiddenFiles(bool show
)
75 FoldersPanelSettings::setShowHiddenFiles(show
);
76 if (m_dirLister
!= 0) {
77 m_dirLister
->setShowingDotFiles(show
);
78 m_dirLister
->openUrl(m_dirLister
->url(), KDirLister::Reload
);
82 bool FoldersPanel::showHiddenFiles() const
84 return FoldersPanelSettings::showHiddenFiles();
87 void FoldersPanel::setAutoScrolling(bool enable
)
89 m_treeView
->setAutoHorizontalScroll(enable
);
90 FoldersPanelSettings::setAutoScrolling(enable
);
93 bool FoldersPanel::autoScrolling() const
95 return FoldersPanelSettings::autoScrolling();
98 void FoldersPanel::rename(const KFileItem
& item
)
100 if (DolphinSettings::instance().generalSettings()->renameInline()) {
101 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
102 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
103 m_treeView
->edit(proxyIndex
);
105 RenameDialog
* dialog
= new RenameDialog(this, KFileItemList() << item
);
106 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
109 dialog
->activateWindow();
113 bool FoldersPanel::urlChanged()
115 if (!url().isValid() || url().protocol().contains("search")) {
116 // Skip results shown by a search, as possible identical
117 // directory names are useless without parent-path information.
121 if (m_dirLister
!= 0) {
122 m_setLeafVisible
= true;
129 void FoldersPanel::showEvent(QShowEvent
* event
)
131 if (event
->spontaneous()) {
132 Panel::showEvent(event
);
136 if (m_dirLister
== 0) {
137 // Postpone the creating of the dir lister to the first show event.
138 // This assures that no performance and memory overhead is given when the TreeView is not
139 // used at all (see FoldersPanel::setUrl()).
140 m_dirLister
= new KDirLister();
141 m_dirLister
->setDirOnlyMode(true);
142 m_dirLister
->setAutoUpdate(true);
143 m_dirLister
->setMainWindow(window());
144 m_dirLister
->setDelayedMimeTypes(true);
145 m_dirLister
->setAutoErrorHandlingEnabled(false, this);
146 m_dirLister
->setShowingDotFiles(FoldersPanelSettings::showHiddenFiles());
147 connect(m_dirLister
, SIGNAL(completed()), this, SLOT(slotDirListerCompleted()));
149 Q_ASSERT(m_dolphinModel
== 0);
150 m_dolphinModel
= new DolphinModel(this);
151 m_dolphinModel
->setDirLister(m_dirLister
);
152 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
153 connect(m_dolphinModel
, SIGNAL(expand(const QModelIndex
&)),
154 this, SLOT(expandToDir(const QModelIndex
&)));
156 Q_ASSERT(m_proxyModel
== 0);
157 m_proxyModel
= new DolphinSortFilterProxyModel(this);
158 m_proxyModel
->setSourceModel(m_dolphinModel
);
160 Q_ASSERT(m_treeView
== 0);
161 m_treeView
= new PanelTreeView(this);
162 m_treeView
->setModel(m_proxyModel
);
163 m_proxyModel
->setSorting(DolphinView::SortByName
);
164 m_proxyModel
->setSortOrder(Qt::AscendingOrder
);
165 m_treeView
->setAutoHorizontalScroll(FoldersPanelSettings::autoScrolling());
167 new FolderExpander(m_treeView
, m_proxyModel
);
169 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
170 this, SLOT(updateActiveView(const QModelIndex
&)));
171 connect(m_treeView
, SIGNAL(urlsDropped(const QModelIndex
&, QDropEvent
*)),
172 this, SLOT(dropUrls(const QModelIndex
&, QDropEvent
*)));
173 connect(m_treeView
, SIGNAL(pressed(const QModelIndex
&)),
174 this, SLOT(updateMouseButtons()));
176 connect(m_treeView
->horizontalScrollBar(), SIGNAL(sliderMoved(int)),
177 this, SLOT(slotHorizontalScrollBarMoved(int)));
178 connect(m_treeView
->verticalScrollBar(), SIGNAL(valueChanged(int)),
179 this, SLOT(slotVerticalScrollBarMoved(int)));
181 QVBoxLayout
* layout
= new QVBoxLayout(this);
182 layout
->setMargin(0);
183 layout
->addWidget(m_treeView
);
187 Panel::showEvent(event
);
190 void FoldersPanel::contextMenuEvent(QContextMenuEvent
* event
)
192 Panel::contextMenuEvent(event
);
195 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
196 if (index
.isValid()) {
197 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
198 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
201 QPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, item
);
206 void FoldersPanel::keyPressEvent(QKeyEvent
* event
)
208 const int key
= event
->key();
209 if ((key
== Qt::Key_Enter
) || (key
== Qt::Key_Return
)) {
211 updateActiveView(m_treeView
->currentIndex());
213 Panel::keyPressEvent(event
);
217 void FoldersPanel::updateActiveView(const QModelIndex
& index
)
219 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
220 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
221 if (!item
.isNull()) {
222 emit
changeUrl(item
.url(), m_mouseButtons
);
226 void FoldersPanel::dropUrls(const QModelIndex
& index
, QDropEvent
* event
)
228 if (index
.isValid()) {
229 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
230 KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
231 Q_ASSERT(!item
.isNull());
233 DragAndDropHelper::instance().dropUrls(item
, item
.url(), event
, this);
238 void FoldersPanel::expandToDir(const QModelIndex
& index
)
240 m_treeView
->setExpanded(index
, true);
241 selectLeafDirectory();
244 void FoldersPanel::scrollToLeaf()
246 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
247 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
248 if (proxyIndex
.isValid()) {
249 m_treeView
->scrollTo(proxyIndex
);
253 void FoldersPanel::updateMouseButtons()
255 m_mouseButtons
= QApplication::mouseButtons();
258 void FoldersPanel::slotDirListerCompleted()
260 m_treeView
->resizeColumnToContents(DolphinModel::Name
);
263 void FoldersPanel::slotHorizontalScrollBarMoved(int value
)
266 // Disable the auto-scrolling until the vertical scrollbar has
267 // been moved by the user.
268 m_treeView
->setAutoHorizontalScroll(false);
271 void FoldersPanel::slotVerticalScrollBarMoved(int value
)
274 // Enable the auto-scrolling again (it might have been disabled by
275 // moving the horizontal scrollbar).
276 m_treeView
->setAutoHorizontalScroll(FoldersPanelSettings::autoScrolling());
279 void FoldersPanel::loadTree(const KUrl
& url
)
281 Q_ASSERT(m_dirLister
!= 0);
285 if (url
.isLocalFile()) {
286 // use the root directory as base for local URLs (#150941)
287 baseUrl
= QDir::rootPath();
289 // clear the path for non-local URLs and use it as base
291 baseUrl
.setPath(QString('/'));
294 if (m_dirLister
->url() != baseUrl
) {
296 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
298 m_dolphinModel
->expandToUrl(m_leafDir
);
301 void FoldersPanel::selectLeafDirectory()
303 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
304 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
306 if (proxyIndex
.isValid()) {
307 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
308 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::ClearAndSelect
);
310 if (m_setLeafVisible
) {
311 // Invoke scrollToLeaf() asynchronously. This assures that
312 // the horizontal scrollbar is shown after resizing the column
313 // (otherwise the scrollbar might hide the leaf).
314 QTimer::singleShot(0, this, SLOT(scrollToLeaf()));
315 m_setLeafVisible
= false;
320 #include "folderspanel.moc"