]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/folderspanel.cpp
Don't assure the visibility of the leaf, if another directory has been opened by...
[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.h>
29 #include <kdirlister.h>
30 #include <kfileitem.h>
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 QSize FoldersPanel::sizeHint() const
74 {
75 return QSize(200, 400);
76 }
77
78 void FoldersPanel::setShowHiddenFiles(bool show)
79 {
80 FoldersPanelSettings::setShowHiddenFiles(show);
81 if (m_dirLister != 0) {
82 m_dirLister->setShowingDotFiles(show);
83 m_dirLister->openUrl(m_dirLister->url(), KDirLister::Reload);
84 }
85 }
86
87 bool FoldersPanel::showHiddenFiles() const
88 {
89 return FoldersPanelSettings::showHiddenFiles();
90 }
91
92 void FoldersPanel::setAutoScrolling(bool enable)
93 {
94 m_treeView->setAutoHorizontalScroll(enable);
95 FoldersPanelSettings::setAutoScrolling(enable);
96 }
97
98 bool FoldersPanel::autoScrolling() const
99 {
100 return FoldersPanelSettings::autoScrolling();
101 }
102
103 void FoldersPanel::rename(const KFileItem& item)
104 {
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);
109 } else {
110 RenameDialog* dialog = new RenameDialog(this, KFileItemList() << item);
111 dialog->setAttribute(Qt::WA_DeleteOnClose);
112 dialog->show();
113 dialog->raise();
114 dialog->activateWindow();
115 }
116 }
117
118 bool FoldersPanel::urlChanged()
119 {
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.
123 return false;
124 }
125
126 if (m_dirLister != 0) {
127 m_setLeafVisible = true;
128 loadTree(url());
129 }
130
131 return true;
132 }
133
134 void FoldersPanel::showEvent(QShowEvent* event)
135 {
136 if (event->spontaneous()) {
137 Panel::showEvent(event);
138 return;
139 }
140
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()));
153
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&)));
160
161 Q_ASSERT(m_proxyModel == 0);
162 m_proxyModel = new DolphinSortFilterProxyModel(this);
163 m_proxyModel->setSourceModel(m_dolphinModel);
164
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());
171
172 new FolderExpander(m_treeView, m_proxyModel);
173
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()));
180
181 connect(m_treeView->horizontalScrollBar(), SIGNAL(sliderMoved(int)),
182 this, SLOT(slotHorizontalScrollBarMoved(int)));
183 connect(m_treeView->verticalScrollBar(), SIGNAL(valueChanged(int)),
184 this, SLOT(slotVerticalScrollBarMoved(int)));
185
186 QVBoxLayout* layout = new QVBoxLayout(this);
187 layout->setMargin(0);
188 layout->addWidget(m_treeView);
189 }
190
191 loadTree(url());
192 Panel::showEvent(event);
193 }
194
195 void FoldersPanel::contextMenuEvent(QContextMenuEvent* event)
196 {
197 Panel::contextMenuEvent(event);
198
199 KFileItem item;
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);
204 }
205
206 QPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, item);
207 contextMenu->open();
208 delete contextMenu;
209 }
210
211 void FoldersPanel::keyPressEvent(QKeyEvent* event)
212 {
213 const int key = event->key();
214 if ((key == Qt::Key_Enter) || (key == Qt::Key_Return)) {
215 event->accept();
216 updateActiveView(m_treeView->currentIndex());
217 } else {
218 Panel::keyPressEvent(event);
219 }
220 }
221
222 void FoldersPanel::updateActiveView(const QModelIndex& index)
223 {
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);
228 }
229 }
230
231 void FoldersPanel::dropUrls(const QModelIndex& index, QDropEvent* event)
232 {
233 if (index.isValid()) {
234 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
235 KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
236 Q_ASSERT(!item.isNull());
237 if (item.isDir()) {
238 DragAndDropHelper::instance().dropUrls(item, item.url(), event, this);
239 }
240 }
241 }
242
243 void FoldersPanel::expandToDir(const QModelIndex& index)
244 {
245 m_treeView->setExpanded(index, true);
246 selectLeafDirectory();
247 }
248
249 void FoldersPanel::scrollToLeaf()
250 {
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);
255 }
256 }
257
258 void FoldersPanel::updateMouseButtons()
259 {
260 m_mouseButtons = QApplication::mouseButtons();
261 }
262
263 void FoldersPanel::slotDirListerCompleted()
264 {
265 m_treeView->resizeColumnToContents(DolphinModel::Name);
266 }
267
268 void FoldersPanel::slotHorizontalScrollBarMoved(int value)
269 {
270 Q_UNUSED(value);
271 // Disable the auto-scrolling until the vertical scrollbar has
272 // been moved by the user.
273 m_treeView->setAutoHorizontalScroll(false);
274 }
275
276 void FoldersPanel::slotVerticalScrollBarMoved(int value)
277 {
278 Q_UNUSED(value);
279 // Enable the auto-scrolling again (it might have been disabled by
280 // moving the horizontal scrollbar).
281 m_treeView->setAutoHorizontalScroll(FoldersPanelSettings::autoScrolling());
282 }
283
284 void FoldersPanel::loadTree(const KUrl& url)
285 {
286 Q_ASSERT(m_dirLister != 0);
287 m_leafDir = url;
288
289 KUrl baseUrl;
290 if (url.isLocalFile()) {
291 // use the root directory as base for local URLs (#150941)
292 baseUrl = QDir::rootPath();
293 } else {
294 // clear the path for non-local URLs and use it as base
295 baseUrl = url;
296 baseUrl.setPath(QString('/'));
297 }
298
299 if (m_dirLister->url() != baseUrl) {
300 m_dirLister->stop();
301 m_dirLister->openUrl(baseUrl, KDirLister::Reload);
302 }
303 m_dolphinModel->expandToUrl(m_leafDir);
304 }
305
306 void FoldersPanel::selectLeafDirectory()
307 {
308 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
309 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
310
311 if (proxyIndex.isValid()) {
312 QItemSelectionModel* selModel = m_treeView->selectionModel();
313 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::ClearAndSelect);
314
315 if (m_setLeafVisible) {
316 // Invoke scrollToLeaf() asynchronously. This assures that
317 // the horizontal scrollbar is shown after resizing the column
318 // (otherwise the scrollbar might hide the leaf).
319 QTimer::singleShot(0, this, SLOT(scrollToLeaf()));
320 m_setLeafVisible = false;
321 }
322 }
323 }
324
325 #include "folderspanel.moc"