]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/folderspanel.cpp
Reactivate the "Open folders during drag operations" feature
[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 "treeviewcontextmenu.h"
25
26 #include <kitemviews/kitemlistselectionmanager.h>
27 #include <kitemviews/kfileitemlistview.h>
28 #include <kitemviews/kfileitemlistwidget.h>
29 #include <kitemviews/kitemlistcontainer.h>
30 #include <kitemviews/kitemlistcontroller.h>
31 #include <kitemviews/kfileitemmodel.h>
32
33 #include <KDirLister>
34 #include <KFileItem>
35 #include <konq_operations.h>
36
37 #include <QApplication>
38 #include <QBoxLayout>
39 #include <QGraphicsView>
40 #include <QPropertyAnimation>
41 #include <QTimer>
42
43 #include <views/renamedialog.h>
44
45 #include <KDebug>
46
47 FoldersPanel::FoldersPanel(QWidget* parent) :
48 Panel(parent),
49 m_updateCurrentItem(false),
50 m_dirLister(0),
51 m_controller(0)
52 {
53 setLayoutDirection(Qt::LeftToRight);
54 }
55
56 FoldersPanel::~FoldersPanel()
57 {
58 FoldersPanelSettings::self()->writeConfig();
59
60 if (m_controller) {
61 KItemListView* view = m_controller->view();
62 m_controller->setView(0);
63 delete view;
64 }
65
66 delete m_dirLister;
67 m_dirLister = 0;
68 }
69
70 void FoldersPanel::setHiddenFilesShown(bool show)
71 {
72 FoldersPanelSettings::setHiddenFilesShown(show);
73 if (m_dirLister) {
74 KFileItemModel* model = fileItemModel();
75 const QSet<KUrl> expandedUrls = model->expandedUrls();
76 m_dirLister->setShowingDotFiles(show);
77 m_dirLister->openUrl(m_dirLister->url(), KDirLister::Reload);
78 model->setExpanded(expandedUrls);
79 }
80 }
81
82 bool FoldersPanel::hiddenFilesShown() const
83 {
84 return FoldersPanelSettings::hiddenFilesShown();
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 // TODO: Inline renaming is not supported anymore in Dolphin 2.0
101 if (false /* GeneralSettings::renameInline() */) {
102 //const QModelIndex dirIndex = m_dolphinModel->indexForItem(item);
103 //const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
104 //m_treeView->edit(proxyIndex);
105 } else {
106 RenameDialog* dialog = new RenameDialog(this, KFileItemList() << item);
107 dialog->setAttribute(Qt::WA_DeleteOnClose);
108 dialog->show();
109 dialog->raise();
110 dialog->activateWindow();
111 }
112 }
113
114 bool FoldersPanel::urlChanged()
115 {
116 if (!url().isValid() || url().protocol().contains("search")) {
117 // Skip results shown by a search, as possible identical
118 // directory names are useless without parent-path information.
119 return false;
120 }
121
122 if (m_dirLister) {
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) {
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::hiddenFilesShown());
147
148 KFileItemListView* view = new KFileItemListView();
149 view->setWidgetCreator(new KItemListWidgetCreator<KFileItemListWidget>());
150
151 KItemListStyleOption styleOption = view->styleOption();
152 styleOption.margin = 2;
153 styleOption.iconSize = KIconLoader::SizeSmall;
154 view->setStyleOption(styleOption);
155
156 const qreal itemHeight = qMax(int(KIconLoader::SizeSmall), styleOption.fontMetrics.height());
157 view->setItemSize(QSizeF(-1, itemHeight + 2 * styleOption.margin));
158 view->setItemLayout(KFileItemListView::DetailsLayout);
159 // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree
160 // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when
161 // opening the folders panel.
162 view->setOpacity(0);
163
164 KFileItemModel* model = new KFileItemModel(m_dirLister, this);
165 // Use a QueuedConnection to give the view the possibility to react first on the
166 // finished loading.
167 connect(model, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()), Qt::QueuedConnection);
168
169 KItemListContainer* container = new KItemListContainer(this);
170 m_controller = container->controller();
171 m_controller->setView(view);
172 m_controller->setModel(model);
173 m_controller->setSelectionBehavior(KItemListController::SingleSelection);
174 m_controller->setAutoActivationDelay(750);
175
176 connect(m_controller, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
177 connect(m_controller, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
178 connect(m_controller, SIGNAL(itemContextMenuRequested(int,QPointF)), this, SLOT(slotItemContextMenuRequested(int,QPointF)));
179 connect(m_controller, SIGNAL(viewContextMenuRequested(QPointF)), this, SLOT(slotViewContextMenuRequested(QPointF)));
180
181 // TODO: Check whether it makes sense to make an explicit API for KItemListContainer
182 // to make the background transparent.
183 container->setFrameShape(QFrame::NoFrame);
184 QGraphicsView* graphicsView = qobject_cast<QGraphicsView*>(container->viewport());
185 if (graphicsView) {
186 // Make the background of the container transparent and apply the window-text color
187 // to the text color, so that enough contrast is given for all color
188 // schemes
189 QPalette p = graphicsView->palette();
190 p.setColor(QPalette::Active, QPalette::Text, p.color(QPalette::Active, QPalette::WindowText));
191 p.setColor(QPalette::Inactive, QPalette::Text, p.color(QPalette::Inactive, QPalette::WindowText));
192 p.setColor(QPalette::Disabled, QPalette::Text, p.color(QPalette::Disabled, QPalette::WindowText));
193 graphicsView->setPalette(p);
194 graphicsView->viewport()->setAutoFillBackground(false);
195 }
196
197 QVBoxLayout* layout = new QVBoxLayout(this);
198 layout->setMargin(0);
199 layout->addWidget(container);
200 }
201
202 loadTree(url());
203 Panel::showEvent(event);
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::slotItemActivated(int index)
218 {
219 const KFileItem item = fileItemModel()->fileItem(index);
220 if (!item.isNull()) {
221 emit changeUrl(item.url(), Qt::LeftButton);
222 }
223 }
224
225 void FoldersPanel::slotItemMiddleClicked(int index)
226 {
227 const KFileItem item = fileItemModel()->fileItem(index);
228 if (!item.isNull()) {
229 emit changeUrl(item.url(), Qt::MiddleButton);
230 }
231 }
232
233 void FoldersPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
234 {
235 Q_UNUSED(pos);
236
237 const KFileItem fileItem = fileItemModel()->fileItem(index);
238
239 QWeakPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, fileItem);
240 contextMenu.data()->open();
241 if (contextMenu.data()) {
242 delete contextMenu.data();
243 }
244 }
245
246 void FoldersPanel::slotViewContextMenuRequested(const QPointF& pos)
247 {
248 Q_UNUSED(pos);
249
250 QWeakPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, KFileItem());
251 contextMenu.data()->open();
252 if (contextMenu.data()) {
253 delete contextMenu.data();
254 }
255 }
256
257 void FoldersPanel::slotLoadingCompleted()
258 {
259 if (m_controller->view()->opacity() == 0) {
260 // The loading of the initial tree after opening the Folders panel
261 // has been finished. Trigger the increasing of the opacity after
262 // a short delay to give the view the chance to finish its internal
263 // animations.
264 // TODO: Check whether it makes sense to allow accessing the
265 // view-internal delay for usecases like this.
266 QTimer::singleShot(250, this, SLOT(startFadeInAnimation()));
267 }
268
269 if (!m_updateCurrentItem) {
270 return;
271 }
272
273 const int index = fileItemModel()->index(url());
274 updateCurrentItem(index);
275 m_updateCurrentItem = false;
276 }
277
278 void FoldersPanel::slotHorizontalScrollBarMoved(int value)
279 {
280 Q_UNUSED(value);
281 // Disable the auto-scrolling until the vertical scrollbar has
282 // been moved by the user.
283 //m_treeView->setAutoHorizontalScroll(false);
284 }
285
286 void FoldersPanel::slotVerticalScrollBarMoved(int value)
287 {
288 Q_UNUSED(value);
289 // Enable the auto-scrolling again (it might have been disabled by
290 // moving the horizontal scrollbar).
291 //m_treeView->setAutoHorizontalScroll(FoldersPanelSettings::autoScrolling());
292 }
293
294 void FoldersPanel::startFadeInAnimation()
295 {
296 QPropertyAnimation* anim = new QPropertyAnimation(m_controller->view(), "opacity", this);
297 anim->setStartValue(0);
298 anim->setEndValue(1);
299 anim->setEasingCurve(QEasingCurve::InOutQuad);
300 anim->start(QAbstractAnimation::DeleteWhenStopped);
301 anim->setDuration(200);
302 }
303
304 void FoldersPanel::loadTree(const KUrl& url)
305 {
306 Q_ASSERT(m_dirLister);
307
308 m_updateCurrentItem = false;
309
310 KUrl baseUrl;
311 if (url.isLocalFile()) {
312 // Use the root directory as base for local URLs (#150941)
313 baseUrl = QDir::rootPath();
314 } else {
315 // Clear the path for non-local URLs and use it as base
316 baseUrl = url;
317 baseUrl.setPath(QString('/'));
318 }
319
320 if (m_dirLister->url() != baseUrl) {
321 m_updateCurrentItem = true;
322 m_dirLister->stop();
323 m_dirLister->openUrl(baseUrl, KDirLister::Reload);
324 }
325
326 KFileItemModel* model = fileItemModel();
327 const int index = model->index(url);
328 if (index >= 0) {
329 updateCurrentItem(index);
330 } else {
331 m_updateCurrentItem = true;
332 model->setExpanded(QSet<KUrl>() << url);
333 // slotLoadingCompleted() will be invoked after the model has
334 // expanded the url
335 }
336 }
337
338 void FoldersPanel::updateCurrentItem(int index)
339 {
340 KItemListSelectionManager* selectionManager = m_controller->selectionManager();
341 selectionManager->setCurrentItem(index);
342 selectionManager->clearSelection();
343 selectionManager->setSelected(index);
344
345 m_controller->view()->scrollToItem(index);
346 }
347
348 KFileItemModel* FoldersPanel::fileItemModel() const
349 {
350 return static_cast<KFileItemModel*>(m_controller->model());
351 }
352
353 #include "folderspanel.moc"