]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/folderspanel.cpp
Use capitalized KDE includes
[dolphin.git] / src / panels / folders / folderspanel.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz <peter.penz19@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "folderspanel.h"
21
22 #include "settings/dolphinsettings.h"
23 #include "dolphin_folderspanelsettings.h"
24 #include "dolphin_generalsettings.h"
25 #include "paneltreeview.h"
26 #include "treeviewcontextmenu.h"
27
28 #include <KFilePlacesModel>
29 #include <KDirLister>
30 #include <KFileItem>
31 #include <konq_operations.h>
32
33 #include <QApplication>
34 #include <QBoxLayout>
35 #include <QItemSelection>
36 #include <QModelIndex>
37 #include <QPointer>
38 #include <QTreeView>
39 #include <QScrollBar>
40 #include <QTimer>
41
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>
48
49 FoldersPanel::FoldersPanel(QWidget* parent) :
50 Panel(parent),
51 m_setLeafVisible(false),
52 m_mouseButtons(Qt::NoButton),
53 m_dirLister(0),
54 m_dolphinModel(0),
55 m_proxyModel(0),
56 m_treeView(0),
57 m_leafDir()
58 {
59 setLayoutDirection(Qt::LeftToRight);
60 }
61
62 FoldersPanel::~FoldersPanel()
63 {
64 FoldersPanelSettings::self()->writeConfig();
65
66 delete m_proxyModel;
67 m_proxyModel = 0;
68 delete m_dolphinModel;
69 m_dolphinModel = 0;
70 m_dirLister = 0; // deleted by m_dolphinModel
71 }
72
73 void FoldersPanel::setShowHiddenFiles(bool show)
74 {
75 FoldersPanelSettings::setShowHiddenFiles(show);
76 if (m_dirLister != 0) {
77 m_dirLister->setShowingDotFiles(show);
78 m_dirLister->openUrl(m_dirLister->url(), KDirLister::Reload);
79 }
80 }
81
82 bool FoldersPanel::showHiddenFiles() const
83 {
84 return FoldersPanelSettings::showHiddenFiles();
85 }
86
87 void FoldersPanel::setAutoScrolling(bool enable)
88 {
89 m_treeView->setAutoHorizontalScroll(enable);
90 FoldersPanelSettings::setAutoScrolling(enable);
91 }
92
93 bool FoldersPanel::autoScrolling() const
94 {
95 return FoldersPanelSettings::autoScrolling();
96 }
97
98 void FoldersPanel::rename(const KFileItem& item)
99 {
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);
104 } else {
105 RenameDialog* dialog = new RenameDialog(this, KFileItemList() << item);
106 dialog->setAttribute(Qt::WA_DeleteOnClose);
107 dialog->show();
108 dialog->raise();
109 dialog->activateWindow();
110 }
111 }
112
113 bool FoldersPanel::urlChanged()
114 {
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.
118 return false;
119 }
120
121 if (m_dirLister != 0) {
122 m_setLeafVisible = true;
123 loadTree(url());
124 }
125
126 return true;
127 }
128
129 void FoldersPanel::showEvent(QShowEvent* event)
130 {
131 if (event->spontaneous()) {
132 Panel::showEvent(event);
133 return;
134 }
135
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()));
148
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&)));
155
156 Q_ASSERT(m_proxyModel == 0);
157 m_proxyModel = new DolphinSortFilterProxyModel(this);
158 m_proxyModel->setSourceModel(m_dolphinModel);
159
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());
166
167 new FolderExpander(m_treeView, m_proxyModel);
168
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()));
175
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)));
180
181 QVBoxLayout* layout = new QVBoxLayout(this);
182 layout->setMargin(0);
183 layout->addWidget(m_treeView);
184 }
185
186 loadTree(url());
187 Panel::showEvent(event);
188 }
189
190 void FoldersPanel::contextMenuEvent(QContextMenuEvent* event)
191 {
192 Panel::contextMenuEvent(event);
193
194 KFileItem item;
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);
199 }
200
201 QPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, item);
202 contextMenu->open();
203 delete contextMenu;
204 }
205
206 void FoldersPanel::keyPressEvent(QKeyEvent* event)
207 {
208 const int key = event->key();
209 if ((key == Qt::Key_Enter) || (key == Qt::Key_Return)) {
210 event->accept();
211 updateActiveView(m_treeView->currentIndex());
212 } else {
213 Panel::keyPressEvent(event);
214 }
215 }
216
217 void FoldersPanel::updateActiveView(const QModelIndex& index)
218 {
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);
223 }
224 }
225
226 void FoldersPanel::dropUrls(const QModelIndex& index, QDropEvent* event)
227 {
228 if (index.isValid()) {
229 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
230 KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
231 Q_ASSERT(!item.isNull());
232 if (item.isDir()) {
233 DragAndDropHelper::instance().dropUrls(item, item.url(), event, this);
234 }
235 }
236 }
237
238 void FoldersPanel::expandToDir(const QModelIndex& index)
239 {
240 m_treeView->setExpanded(index, true);
241 selectLeafDirectory();
242 }
243
244 void FoldersPanel::scrollToLeaf()
245 {
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);
250 }
251 }
252
253 void FoldersPanel::updateMouseButtons()
254 {
255 m_mouseButtons = QApplication::mouseButtons();
256 }
257
258 void FoldersPanel::slotDirListerCompleted()
259 {
260 m_treeView->resizeColumnToContents(DolphinModel::Name);
261 }
262
263 void FoldersPanel::slotHorizontalScrollBarMoved(int value)
264 {
265 Q_UNUSED(value);
266 // Disable the auto-scrolling until the vertical scrollbar has
267 // been moved by the user.
268 m_treeView->setAutoHorizontalScroll(false);
269 }
270
271 void FoldersPanel::slotVerticalScrollBarMoved(int value)
272 {
273 Q_UNUSED(value);
274 // Enable the auto-scrolling again (it might have been disabled by
275 // moving the horizontal scrollbar).
276 m_treeView->setAutoHorizontalScroll(FoldersPanelSettings::autoScrolling());
277 }
278
279 void FoldersPanel::loadTree(const KUrl& url)
280 {
281 Q_ASSERT(m_dirLister != 0);
282 m_leafDir = url;
283
284 KUrl baseUrl;
285 if (url.isLocalFile()) {
286 // use the root directory as base for local URLs (#150941)
287 baseUrl = QDir::rootPath();
288 } else {
289 // clear the path for non-local URLs and use it as base
290 baseUrl = url;
291 baseUrl.setPath(QString('/'));
292 }
293
294 if (m_dirLister->url() != baseUrl) {
295 m_dirLister->stop();
296 m_dirLister->openUrl(baseUrl, KDirLister::Reload);
297 }
298 m_dolphinModel->expandToUrl(m_leafDir);
299 }
300
301 void FoldersPanel::selectLeafDirectory()
302 {
303 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
304 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
305
306 if (proxyIndex.isValid()) {
307 QItemSelectionModel* selModel = m_treeView->selectionModel();
308 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::ClearAndSelect);
309
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;
316 }
317 }
318 }
319
320 #include "folderspanel.moc"