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