]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/folderspanel.cpp
Initial draft for bringing back the "Folders" panel
[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 <QTimer>
41
42 #include <views/renamedialog.h>
43
44 #include <KDebug>
45
46 FoldersPanel::FoldersPanel(QWidget* parent) :
47 Panel(parent),
48 m_setLeafVisible(false),
49 m_mouseButtons(Qt::NoButton),
50 m_dirLister(0),
51 m_controller(0),
52 m_leafDir()
53 {
54 setLayoutDirection(Qt::LeftToRight);
55 }
56
57 FoldersPanel::~FoldersPanel()
58 {
59 FoldersPanelSettings::self()->writeConfig();
60
61 KItemListView* view = m_controller->view();
62 m_controller->setView(0);
63 delete view;
64
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 KFileItemModel* model = fileItemModel();
74 const QSet<KUrl> expandedUrls = model->expandedUrls();
75 m_dirLister->setShowingDotFiles(show);
76 m_dirLister->openUrl(m_dirLister->url(), KDirLister::Reload);
77 model->setExpanded(expandedUrls);
78 }
79 }
80
81 bool FoldersPanel::hiddenFilesShown() const
82 {
83 return FoldersPanelSettings::hiddenFilesShown();
84 }
85
86 void FoldersPanel::setAutoScrolling(bool enable)
87 {
88 //m_treeView->setAutoHorizontalScroll(enable);
89 FoldersPanelSettings::setAutoScrolling(enable);
90 }
91
92 bool FoldersPanel::autoScrolling() const
93 {
94 return FoldersPanelSettings::autoScrolling();
95 }
96
97 void FoldersPanel::rename(const KFileItem& item)
98 {
99 // TODO: Inline renaming is not supported anymore in Dolphin 2.0
100 if (false /* GeneralSettings::renameInline() */) {
101 //const QModelIndex dirIndex = m_dolphinModel->indexForItem(item);
102 //const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
103 //m_treeView->edit(proxyIndex);
104 } else {
105 RenameDialog* dialog = new RenameDialog(this, KFileItemList() << item);
106 dialog->setAttribute(Qt::WA_DeleteOnClose);
107 dialog->show();
108 dialog->raise();
109 dialog->activateWindow();
110 }
111 }
112
113 bool FoldersPanel::urlChanged()
114 {
115 if (!url().isValid() || url().protocol().contains("search")) {
116 // Skip results shown by a search, as possible identical
117 // directory names are useless without parent-path information.
118 return false;
119 }
120
121 if (m_dirLister) {
122 m_setLeafVisible = true;
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
160 KFileItemModel* model = new KFileItemModel(m_dirLister, this);
161 // Use a QueuedConnection to give the view the possibility to react first on the
162 // finished loading.
163 connect(model, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()), Qt::QueuedConnection);
164
165 KItemListContainer* container = new KItemListContainer(this);
166 m_controller = container->controller();
167 m_controller->setView(view);
168 m_controller->setModel(model);
169
170 // TODO: Check whether it makes sense to make an explicit API for KItemListContainer
171 // to make the background transparent.
172 container->setFrameShape(QFrame::NoFrame);
173 QGraphicsView* graphicsView = qobject_cast<QGraphicsView*>(container->viewport());
174 if (graphicsView) {
175 // Make the background of the container transparent and apply the window-text color
176 // to the text color, so that enough contrast is given for all color
177 // schemes
178 QPalette p = graphicsView->palette();
179 p.setColor(QPalette::Active, QPalette::Text, p.color(QPalette::Active, QPalette::WindowText));
180 p.setColor(QPalette::Inactive, QPalette::Text, p.color(QPalette::Inactive, QPalette::WindowText));
181 p.setColor(QPalette::Disabled, QPalette::Text, p.color(QPalette::Disabled, QPalette::WindowText));
182 graphicsView->setPalette(p);
183 graphicsView->viewport()->setAutoFillBackground(false);
184 }
185
186 QVBoxLayout* layout = new QVBoxLayout(this);
187 layout->setMargin(0);
188 layout->addWidget(container);
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::updateMouseButtons()
223 {
224 m_mouseButtons = QApplication::mouseButtons();
225 }
226
227 void FoldersPanel::slotLoadingCompleted()
228 {
229 const int index = fileItemModel()->index(url());
230 if (index >= 0) {
231 m_controller->selectionManager()->setCurrentItem(index);
232 }
233 }
234
235 void FoldersPanel::slotHorizontalScrollBarMoved(int value)
236 {
237 Q_UNUSED(value);
238 // Disable the auto-scrolling until the vertical scrollbar has
239 // been moved by the user.
240 //m_treeView->setAutoHorizontalScroll(false);
241 }
242
243 void FoldersPanel::slotVerticalScrollBarMoved(int value)
244 {
245 Q_UNUSED(value);
246 // Enable the auto-scrolling again (it might have been disabled by
247 // moving the horizontal scrollbar).
248 //m_treeView->setAutoHorizontalScroll(FoldersPanelSettings::autoScrolling());
249 }
250
251 void FoldersPanel::loadTree(const KUrl& url)
252 {
253 Q_ASSERT(m_dirLister);
254 m_leafDir = url;
255
256 KUrl baseUrl;
257 if (url.isLocalFile()) {
258 // Use the root directory as base for local URLs (#150941)
259 baseUrl = QDir::rootPath();
260 } else {
261 // Clear the path for non-local URLs and use it as base
262 baseUrl = url;
263 baseUrl.setPath(QString('/'));
264 }
265
266 if (m_dirLister->url() != baseUrl) {
267 m_dirLister->stop();
268 m_dirLister->openUrl(baseUrl, KDirLister::Reload);
269 }
270
271 KFileItemModel* model = fileItemModel();
272 const int index = model->index(url);
273 if (index >= 0) {
274 m_controller->selectionManager()->setCurrentItem(index);
275 } else {
276 model->setExpanded(QSet<KUrl>() << url);
277 }
278 }
279
280 void FoldersPanel::selectLeafDirectory()
281 {
282 /*const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
283 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
284
285 if (proxyIndex.isValid()) {
286 QItemSelectionModel* selModel = m_treeView->selectionModel();
287 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::ClearAndSelect);
288
289 if (m_setLeafVisible) {
290 // Invoke scrollToLeaf() asynchronously. This assures that
291 // the horizontal scrollbar is shown after resizing the column
292 // (otherwise the scrollbar might hide the leaf).
293 QTimer::singleShot(0, this, SLOT(scrollToLeaf()));
294 m_setLeafVisible = false;
295 }
296 }*/
297 }
298
299 KFileItemModel* FoldersPanel::fileItemModel() const
300 {
301 return static_cast<KFileItemModel*>(m_controller->model());
302 }
303
304 #include "folderspanel.moc"