]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/folderspanel.cpp
Delete obsolete class DolphinSettings
[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 "dolphin_folderspanelsettings.h"
23 #include "dolphin_generalsettings.h"
24 #include "paneltreeview.h"
25 #include "treeviewcontextmenu.h"
26
27 #include <KDirLister>
28 #include <KFileItem>
29 #include <konq_operations.h>
30
31 #include <QApplication>
32 #include <QBoxLayout>
33 #include <QItemSelection>
34 #include <QModelIndex>
35 #include <QPointer>
36 #include <QTreeView>
37 #include <QScrollBar>
38 #include <QTimer>
39
40 #include <views/dolphinview.h>
41 #include <views/folderexpander.h>
42 #include <views/renamedialog.h>
43
44 FoldersPanel::FoldersPanel(QWidget* parent) :
45 Panel(parent),
46 m_setLeafVisible(false),
47 m_mouseButtons(Qt::NoButton),
48 m_dirLister(0),
49 //m_dolphinModel(0),
50 //m_proxyModel(0),
51 m_treeView(0),
52 m_leafDir()
53 {
54 setLayoutDirection(Qt::LeftToRight);
55 }
56
57 FoldersPanel::~FoldersPanel()
58 {
59 FoldersPanelSettings::self()->writeConfig();
60
61 //delete m_proxyModel;
62 //m_proxyModel = 0;
63 //delete m_dolphinModel;
64 //m_dolphinModel = 0;
65 delete m_dirLister;
66 m_dirLister = 0;
67 }
68
69 void FoldersPanel::setHiddenFilesShown(bool show)
70 {
71 FoldersPanelSettings::setHiddenFilesShown(show);
72 if (m_dirLister) {
73 m_dirLister->setShowingDotFiles(show);
74 m_dirLister->openUrl(m_dirLister->url(), KDirLister::Reload);
75 }
76 }
77
78 bool FoldersPanel::hiddenFilesShown() const
79 {
80 return FoldersPanelSettings::hiddenFilesShown();
81 }
82
83 void FoldersPanel::setAutoScrolling(bool enable)
84 {
85 m_treeView->setAutoHorizontalScroll(enable);
86 FoldersPanelSettings::setAutoScrolling(enable);
87 }
88
89 bool FoldersPanel::autoScrolling() const
90 {
91 return FoldersPanelSettings::autoScrolling();
92 }
93
94 void FoldersPanel::rename(const KFileItem& item)
95 {
96 // TODO: Inline renaming is not supported anymore in Dolphin 2.0
97 if (false /* GeneralSettings::renameInline() */) {
98 //const QModelIndex dirIndex = m_dolphinModel->indexForItem(item);
99 //const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
100 //m_treeView->edit(proxyIndex);
101 } else {
102 RenameDialog* dialog = new RenameDialog(this, KFileItemList() << item);
103 dialog->setAttribute(Qt::WA_DeleteOnClose);
104 dialog->show();
105 dialog->raise();
106 dialog->activateWindow();
107 }
108 }
109
110 bool FoldersPanel::urlChanged()
111 {
112 if (!url().isValid() || url().protocol().contains("search")) {
113 // Skip results shown by a search, as possible identical
114 // directory names are useless without parent-path information.
115 return false;
116 }
117
118 if (m_dirLister) {
119 m_setLeafVisible = true;
120 loadTree(url());
121 }
122
123 return true;
124 }
125
126 void FoldersPanel::showEvent(QShowEvent* event)
127 {
128 if (event->spontaneous()) {
129 Panel::showEvent(event);
130 return;
131 }
132
133 if (!m_dirLister) {
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::hiddenFilesShown());
144 connect(m_dirLister, SIGNAL(completed()), this, SLOT(slotDirListerCompleted()));
145
146 /*Q_ASSERT(!m_dolphinModel);
147 m_dolphinModel = new DolphinModel(this);
148 m_dolphinModel->setDirLister(m_dirLister);
149 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
150 connect(m_dolphinModel, SIGNAL(expand(QModelIndex)),
151 this, SLOT(expandToDir(QModelIndex)));
152
153 Q_ASSERT(!m_proxyModel);
154 m_proxyModel = new DolphinSortFilterProxyModel(this);
155 m_proxyModel->setSourceModel(m_dolphinModel);
156
157 Q_ASSERT(!m_treeView);
158 m_treeView = new PanelTreeView(this);
159 m_treeView->setModel(m_proxyModel);
160 m_proxyModel->setSorting(DolphinView::SortByName);
161 m_proxyModel->setSortOrder(Qt::AscendingOrder);
162 m_treeView->setAutoHorizontalScroll(FoldersPanelSettings::autoScrolling());
163
164 new FolderExpander(m_treeView, m_proxyModel);
165
166 connect(m_treeView, SIGNAL(clicked(QModelIndex)),
167 this, SLOT(updateActiveView(QModelIndex)));
168 connect(m_treeView, SIGNAL(urlsDropped(QModelIndex,QDropEvent*)),
169 this, SLOT(dropUrls(QModelIndex,QDropEvent*)));
170 connect(m_treeView, SIGNAL(pressed(QModelIndex)),
171 this, SLOT(updateMouseButtons()));
172
173 connect(m_treeView->horizontalScrollBar(), SIGNAL(sliderMoved(int)),
174 this, SLOT(slotHorizontalScrollBarMoved(int)));
175 connect(m_treeView->verticalScrollBar(), SIGNAL(valueChanged(int)),
176 this, SLOT(slotVerticalScrollBarMoved(int)));
177
178 QVBoxLayout* layout = new QVBoxLayout(this);
179 layout->setMargin(0);
180 layout->addWidget(m_treeView);*/
181 }
182
183 loadTree(url());
184 Panel::showEvent(event);
185 }
186
187 void FoldersPanel::contextMenuEvent(QContextMenuEvent* event)
188 {
189 Panel::contextMenuEvent(event);
190
191 KFileItem item;
192 /*const QModelIndex index = m_treeView->indexAt(event->pos());
193 if (index.isValid()) {
194 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
195 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
196 }*/
197
198 QPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, item);
199 contextMenu->open();
200 delete contextMenu;
201 }
202
203 void FoldersPanel::keyPressEvent(QKeyEvent* event)
204 {
205 const int key = event->key();
206 if ((key == Qt::Key_Enter) || (key == Qt::Key_Return)) {
207 event->accept();
208 updateActiveView(m_treeView->currentIndex());
209 } else {
210 Panel::keyPressEvent(event);
211 }
212 }
213
214 void FoldersPanel::updateActiveView(const QModelIndex& index)
215 {
216 Q_UNUSED(index);
217 /*const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
218 const KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
219 if (!item.isNull()) {
220 emit changeUrl(item.url(), m_mouseButtons);
221 }*/
222 }
223
224 void FoldersPanel::dropUrls(const QModelIndex& index, QDropEvent* event)
225 {
226 Q_UNUSED(event);
227 if (index.isValid()) {
228 /*const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
229 KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
230 Q_ASSERT(!item.isNull());
231 if (item.isDir()) {
232 Q_UNUSED(event);
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);
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"