]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/folderspanel.cpp
Merged very early alpha-version of Dolphin 2.0
[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/dolphinview.h>
43 #include <views/folderexpander.h>
44 #include <views/renamedialog.h>
45
46 FoldersPanel::FoldersPanel(QWidget* parent) :
47 Panel(parent),
48 m_setLeafVisible(false),
49 m_mouseButtons(Qt::NoButton),
50 m_dirLister(0),
51 //m_dolphinModel(0),
52 //m_proxyModel(0),
53 m_treeView(0),
54 m_leafDir()
55 {
56 setLayoutDirection(Qt::LeftToRight);
57 }
58
59 FoldersPanel::~FoldersPanel()
60 {
61 FoldersPanelSettings::self()->writeConfig();
62
63 //delete m_proxyModel;
64 //m_proxyModel = 0;
65 //delete m_dolphinModel;
66 //m_dolphinModel = 0;
67 delete m_dirLister;
68 m_dirLister = 0;
69 }
70
71 void FoldersPanel::setHiddenFilesShown(bool show)
72 {
73 FoldersPanelSettings::setHiddenFilesShown(show);
74 if (m_dirLister) {
75 m_dirLister->setShowingDotFiles(show);
76 m_dirLister->openUrl(m_dirLister->url(), KDirLister::Reload);
77 }
78 }
79
80 bool FoldersPanel::hiddenFilesShown() const
81 {
82 return FoldersPanelSettings::hiddenFilesShown();
83 }
84
85 void FoldersPanel::setAutoScrolling(bool enable)
86 {
87 m_treeView->setAutoHorizontalScroll(enable);
88 FoldersPanelSettings::setAutoScrolling(enable);
89 }
90
91 bool FoldersPanel::autoScrolling() const
92 {
93 return FoldersPanelSettings::autoScrolling();
94 }
95
96 void FoldersPanel::rename(const KFileItem& item)
97 {
98 if (DolphinSettings::instance().generalSettings()->renameInline()) {
99 //const QModelIndex dirIndex = m_dolphinModel->indexForItem(item);
100 //const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
101 //m_treeView->edit(proxyIndex);
102 } else {
103 RenameDialog* dialog = new RenameDialog(this, KFileItemList() << item);
104 dialog->setAttribute(Qt::WA_DeleteOnClose);
105 dialog->show();
106 dialog->raise();
107 dialog->activateWindow();
108 }
109 }
110
111 bool FoldersPanel::urlChanged()
112 {
113 if (!url().isValid() || url().protocol().contains("search")) {
114 // Skip results shown by a search, as possible identical
115 // directory names are useless without parent-path information.
116 return false;
117 }
118
119 if (m_dirLister) {
120 m_setLeafVisible = true;
121 loadTree(url());
122 }
123
124 return true;
125 }
126
127 void FoldersPanel::showEvent(QShowEvent* event)
128 {
129 if (event->spontaneous()) {
130 Panel::showEvent(event);
131 return;
132 }
133
134 if (!m_dirLister) {
135 // Postpone the creating of the dir lister to the first show event.
136 // This assures that no performance and memory overhead is given when the TreeView is not
137 // used at all (see FoldersPanel::setUrl()).
138 m_dirLister = new KDirLister();
139 m_dirLister->setDirOnlyMode(true);
140 m_dirLister->setAutoUpdate(true);
141 m_dirLister->setMainWindow(window());
142 m_dirLister->setDelayedMimeTypes(true);
143 m_dirLister->setAutoErrorHandlingEnabled(false, this);
144 m_dirLister->setShowingDotFiles(FoldersPanelSettings::hiddenFilesShown());
145 connect(m_dirLister, SIGNAL(completed()), this, SLOT(slotDirListerCompleted()));
146
147 /*Q_ASSERT(!m_dolphinModel);
148 m_dolphinModel = new DolphinModel(this);
149 m_dolphinModel->setDirLister(m_dirLister);
150 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
151 connect(m_dolphinModel, SIGNAL(expand(const QModelIndex&)),
152 this, SLOT(expandToDir(const QModelIndex&)));
153
154 Q_ASSERT(!m_proxyModel);
155 m_proxyModel = new DolphinSortFilterProxyModel(this);
156 m_proxyModel->setSourceModel(m_dolphinModel);
157
158 Q_ASSERT(!m_treeView);
159 m_treeView = new PanelTreeView(this);
160 m_treeView->setModel(m_proxyModel);
161 m_proxyModel->setSorting(DolphinView::SortByName);
162 m_proxyModel->setSortOrder(Qt::AscendingOrder);
163 m_treeView->setAutoHorizontalScroll(FoldersPanelSettings::autoScrolling());
164
165 new FolderExpander(m_treeView, m_proxyModel);
166
167 connect(m_treeView, SIGNAL(clicked(const QModelIndex&)),
168 this, SLOT(updateActiveView(const QModelIndex&)));
169 connect(m_treeView, SIGNAL(urlsDropped(const QModelIndex&, QDropEvent*)),
170 this, SLOT(dropUrls(const QModelIndex&, QDropEvent*)));
171 connect(m_treeView, SIGNAL(pressed(const QModelIndex&)),
172 this, SLOT(updateMouseButtons()));
173
174 connect(m_treeView->horizontalScrollBar(), SIGNAL(sliderMoved(int)),
175 this, SLOT(slotHorizontalScrollBarMoved(int)));
176 connect(m_treeView->verticalScrollBar(), SIGNAL(valueChanged(int)),
177 this, SLOT(slotVerticalScrollBarMoved(int)));
178
179 QVBoxLayout* layout = new QVBoxLayout(this);
180 layout->setMargin(0);
181 layout->addWidget(m_treeView);*/
182 }
183
184 loadTree(url());
185 Panel::showEvent(event);
186 }
187
188 void FoldersPanel::contextMenuEvent(QContextMenuEvent* event)
189 {
190 Panel::contextMenuEvent(event);
191
192 KFileItem item;
193 /*const QModelIndex index = m_treeView->indexAt(event->pos());
194 if (index.isValid()) {
195 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
196 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
197 }*/
198
199 QPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, item);
200 contextMenu->open();
201 delete contextMenu;
202 }
203
204 void FoldersPanel::keyPressEvent(QKeyEvent* event)
205 {
206 const int key = event->key();
207 if ((key == Qt::Key_Enter) || (key == Qt::Key_Return)) {
208 event->accept();
209 updateActiveView(m_treeView->currentIndex());
210 } else {
211 Panel::keyPressEvent(event);
212 }
213 }
214
215 void FoldersPanel::updateActiveView(const QModelIndex& index)
216 {
217 Q_UNUSED(index);
218 /*const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
219 const KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
220 if (!item.isNull()) {
221 emit changeUrl(item.url(), m_mouseButtons);
222 }*/
223 }
224
225 void FoldersPanel::dropUrls(const QModelIndex& index, QDropEvent* event)
226 {
227 Q_UNUSED(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());
232 if (item.isDir()) {
233 Q_UNUSED(event);
234 //DragAndDropHelper::instance().dropUrls(item, item.url(), event, this);
235 }*/
236 }
237 }
238
239 void FoldersPanel::expandToDir(const QModelIndex& index)
240 {
241 m_treeView->setExpanded(index, true);
242 selectLeafDirectory();
243 }
244
245 void FoldersPanel::scrollToLeaf()
246 {
247 /*const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
248 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
249 if (proxyIndex.isValid()) {
250 m_treeView->scrollTo(proxyIndex);
251 }*/
252 }
253
254 void FoldersPanel::updateMouseButtons()
255 {
256 m_mouseButtons = QApplication::mouseButtons();
257 }
258
259 void FoldersPanel::slotDirListerCompleted()
260 {
261 // m_treeView->resizeColumnToContents(DolphinModel::Name);
262 }
263
264 void FoldersPanel::slotHorizontalScrollBarMoved(int value)
265 {
266 Q_UNUSED(value);
267 // Disable the auto-scrolling until the vertical scrollbar has
268 // been moved by the user.
269 m_treeView->setAutoHorizontalScroll(false);
270 }
271
272 void FoldersPanel::slotVerticalScrollBarMoved(int value)
273 {
274 Q_UNUSED(value);
275 // Enable the auto-scrolling again (it might have been disabled by
276 // moving the horizontal scrollbar).
277 m_treeView->setAutoHorizontalScroll(FoldersPanelSettings::autoScrolling());
278 }
279
280 void FoldersPanel::loadTree(const KUrl& url)
281 {
282 Q_ASSERT(m_dirLister);
283 m_leafDir = url;
284
285 KUrl baseUrl;
286 if (url.isLocalFile()) {
287 // use the root directory as base for local URLs (#150941)
288 baseUrl = QDir::rootPath();
289 } else {
290 // clear the path for non-local URLs and use it as base
291 baseUrl = url;
292 baseUrl.setPath(QString('/'));
293 }
294
295 if (m_dirLister->url() != baseUrl) {
296 m_dirLister->stop();
297 m_dirLister->openUrl(baseUrl, KDirLister::Reload);
298 }
299 //m_dolphinModel->expandToUrl(m_leafDir);
300 }
301
302 void FoldersPanel::selectLeafDirectory()
303 {
304 /*const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
305 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
306
307 if (proxyIndex.isValid()) {
308 QItemSelectionModel* selModel = m_treeView->selectionModel();
309 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::ClearAndSelect);
310
311 if (m_setLeafVisible) {
312 // Invoke scrollToLeaf() asynchronously. This assures that
313 // the horizontal scrollbar is shown after resizing the column
314 // (otherwise the scrollbar might hide the leaf).
315 QTimer::singleShot(0, this, SLOT(scrollToLeaf()));
316 m_setLeafVisible = false;
317 }
318 }*/
319 }
320
321 #include "folderspanel.moc"