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
);
170 m_treeView
->setAutoHorizontalScroll(FoldersPanelSettings::autoScrolling());
172 new FolderExpander(m_treeView
, m_proxyModel
);
174 connect(m_treeView
, SIGNAL(clicked(const QModelIndex
&)),
175 this, SLOT(updateActiveView(const QModelIndex
&)));
176 connect(m_treeView
, SIGNAL(urlsDropped(const QModelIndex
&, QDropEvent
*)),
177 this, SLOT(dropUrls(const QModelIndex
&, QDropEvent
*)));
178 connect(m_treeView
, SIGNAL(pressed(const QModelIndex
&)),
179 this, SLOT(updateMouseButtons()));
181 connect(m_treeView
->horizontalScrollBar(), SIGNAL(sliderMoved(int)),
182 this, SLOT(slotHorizontalScrollBarMoved(int)));
183 connect(m_treeView
->verticalScrollBar(), SIGNAL(sliderMoved(int)),
184 this, SLOT(slotVerticalScrollBarMoved(int)));
186 QVBoxLayout
* layout
= new QVBoxLayout(this);
187 layout
->setMargin(0);
188 layout
->addWidget(m_treeView
);
192 Panel::showEvent(event
);
195 void FoldersPanel::contextMenuEvent(QContextMenuEvent
* event
)
197 Panel::contextMenuEvent(event
);
200 const QModelIndex index
= m_treeView
->indexAt(event
->pos());
201 if (index
.isValid()) {
202 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
203 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
206 QPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, item
);
211 void FoldersPanel::keyPressEvent(QKeyEvent
* event
)
213 const int key
= event
->key();
214 if ((key
== Qt::Key_Enter
) || (key
== Qt::Key_Return
)) {
216 updateActiveView(m_treeView
->currentIndex());
218 Panel::keyPressEvent(event
);
222 void FoldersPanel::updateActiveView(const QModelIndex
& index
)
224 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
225 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
226 if (!item
.isNull()) {
227 emit
changeUrl(item
.url(), m_mouseButtons
);
231 void FoldersPanel::dropUrls(const QModelIndex
& index
, QDropEvent
* event
)
233 if (index
.isValid()) {
234 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
235 KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
236 Q_ASSERT(!item
.isNull());
238 DragAndDropHelper::instance().dropUrls(item
, item
.url(), event
, this);
243 void FoldersPanel::expandToDir(const QModelIndex
& index
)
245 m_treeView
->setExpanded(index
, true);
246 selectLeafDirectory();
249 void FoldersPanel::scrollToLeaf()
251 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
252 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
253 if (proxyIndex
.isValid()) {
254 m_treeView
->scrollTo(proxyIndex
);
258 void FoldersPanel::updateMouseButtons()
260 m_mouseButtons
= QApplication::mouseButtons();
263 void FoldersPanel::slotDirListerCompleted()
265 m_treeView
->resizeColumnToContents(DolphinModel::Name
);
267 if (m_setLeafVisible
) {
268 // Invoke scrollToLeaf() asynchronously. This assures that
269 // the horizontal scrollbar is shown after resizing the column
270 // (otherwise the scrollbar might hide the leaf).
271 QTimer::singleShot(0, this, SLOT(scrollToLeaf()));
272 m_setLeafVisible
= false;
276 void FoldersPanel::slotHorizontalScrollBarMoved(int value
)
279 // Disable the auto-scrolling until the vertical scrollbar has
280 // been moved by the user.
281 m_treeView
->setAutoHorizontalScroll(false);
284 void FoldersPanel::slotVerticalScrollBarMoved(int value
)
287 // Enable the auto-scrolling again (it might have been disabled by
288 // moving the horizontal scrollbar).
289 m_treeView
->setAutoHorizontalScroll(FoldersPanelSettings::autoScrolling());
292 void FoldersPanel::loadTree(const KUrl
& url
)
294 Q_ASSERT(m_dirLister
!= 0);
298 if (url
.isLocalFile()) {
299 // use the root directory as base for local URLs (#150941)
300 baseUrl
= QDir::rootPath();
302 // clear the path for non-local URLs and use it as base
304 baseUrl
.setPath(QString('/'));
307 if (m_dirLister
->url() != baseUrl
) {
309 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
311 m_dolphinModel
->expandToUrl(m_leafDir
);
314 void FoldersPanel::selectLeafDirectory()
316 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_leafDir
);
317 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
318 if (proxyIndex
.isValid()) {
319 QItemSelectionModel
* selModel
= m_treeView
->selectionModel();
320 selModel
->setCurrentIndex(proxyIndex
, QItemSelectionModel::ClearAndSelect
);
324 #include "folderspanel.moc"