]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/folderspanel.cpp
KFileItemModel: interface cleanups
[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 <KFileItem>
34 #include <konq_operations.h>
35
36 #include <QApplication>
37 #include <QBoxLayout>
38 #include <QDropEvent>
39 #include <QGraphicsSceneDragDropEvent>
40 #include <QGraphicsView>
41 #include <QPropertyAnimation>
42 #include <QTimer>
43
44 #include <views/draganddrophelper.h>
45 #include <views/renamedialog.h>
46
47 #include <KDebug>
48
49 FoldersPanel::FoldersPanel(QWidget* parent) :
50 Panel(parent),
51 m_updateCurrentItem(false),
52 m_controller(0)
53 {
54 setLayoutDirection(Qt::LeftToRight);
55 }
56
57 FoldersPanel::~FoldersPanel()
58 {
59 FoldersPanelSettings::self()->writeConfig();
60
61 if (m_controller) {
62 KItemListView* view = m_controller->view();
63 m_controller->setView(0);
64 delete view;
65 }
66 }
67
68 void FoldersPanel::setShowHiddenFiles(bool show)
69 {
70 FoldersPanelSettings::setHiddenFilesShown(show);
71 fileItemModel()->setShowHiddenFiles(show);
72 }
73
74 bool FoldersPanel::showHiddenFiles() const
75 {
76 return FoldersPanelSettings::hiddenFilesShown();
77 }
78
79 void FoldersPanel::setAutoScrolling(bool enable)
80 {
81 // TODO: Not supported yet in Dolphin 2.0
82 FoldersPanelSettings::setAutoScrolling(enable);
83 }
84
85 bool FoldersPanel::autoScrolling() const
86 {
87 return FoldersPanelSettings::autoScrolling();
88 }
89
90 void FoldersPanel::rename(const KFileItem& item)
91 {
92 // TODO: Inline renaming is not supported anymore in Dolphin 2.0
93 if (false /* 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_controller) {
115 loadTree(url());
116 }
117
118 return true;
119 }
120
121 void FoldersPanel::showEvent(QShowEvent* event)
122 {
123 if (event->spontaneous()) {
124 Panel::showEvent(event);
125 return;
126 }
127
128 if (!m_controller) {
129 // Postpone the creating of the dir lister to the first show event.
130 // This assures that no performance and memory overhead is given when the TreeView is not
131 // used at all (see FoldersPanel::setUrl()).
132 KFileItemListView* view = new KFileItemListView();
133 view->setWidgetCreator(new KItemListWidgetCreator<KFileItemListWidget>());
134
135 KItemListStyleOption styleOption = view->styleOption();
136 styleOption.padding = 2;
137 styleOption.iconSize = KIconLoader::SizeSmall;
138 styleOption.extendedSelectionRegion = true;
139 view->setStyleOption(styleOption);
140
141 const qreal itemHeight = qMax(int(KIconLoader::SizeSmall), styleOption.fontMetrics.height());
142 view->setItemSize(QSizeF(-1, itemHeight + 2 * styleOption.padding));
143 view->setItemLayout(KFileItemListView::DetailsLayout);
144 view->setSupportsItemExpanding(true);
145 // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree
146 // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when
147 // opening the folders panel.
148 view->setOpacity(0);
149
150 KFileItemModel* model = new KFileItemModel(this);
151 model->setShowDirectoriesOnly(true);
152 model->setShowHiddenFiles(FoldersPanelSettings::hiddenFilesShown());
153 // Use a QueuedConnection to give the view the possibility to react first on the
154 // finished loading.
155 connect(model, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotLoadingCompleted()), Qt::QueuedConnection);
156
157 KItemListContainer* container = new KItemListContainer(this);
158 m_controller = container->controller();
159 m_controller->setView(view);
160 m_controller->setModel(model);
161 m_controller->setSelectionBehavior(KItemListController::SingleSelection);
162 m_controller->setAutoActivationDelay(750);
163 m_controller->setSingleClickActivation(true);
164
165 connect(m_controller, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
166 connect(m_controller, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
167 connect(m_controller, SIGNAL(itemContextMenuRequested(int,QPointF)), this, SLOT(slotItemContextMenuRequested(int,QPointF)));
168 connect(m_controller, SIGNAL(viewContextMenuRequested(QPointF)), this, SLOT(slotViewContextMenuRequested(QPointF)));
169 connect(m_controller, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent*)));
170
171 // TODO: Check whether it makes sense to make an explicit API for KItemListContainer
172 // to make the background transparent.
173 container->setFrameShape(QFrame::NoFrame);
174 QGraphicsView* graphicsView = qobject_cast<QGraphicsView*>(container->viewport());
175 if (graphicsView) {
176 // Make the background of the container transparent and apply the window-text color
177 // to the text color, so that enough contrast is given for all color
178 // schemes
179 QPalette p = graphicsView->palette();
180 p.setColor(QPalette::Active, QPalette::Text, p.color(QPalette::Active, QPalette::WindowText));
181 p.setColor(QPalette::Inactive, QPalette::Text, p.color(QPalette::Inactive, QPalette::WindowText));
182 p.setColor(QPalette::Disabled, QPalette::Text, p.color(QPalette::Disabled, QPalette::WindowText));
183 graphicsView->setPalette(p);
184 graphicsView->viewport()->setAutoFillBackground(false);
185 }
186
187 QVBoxLayout* layout = new QVBoxLayout(this);
188 layout->setMargin(0);
189 layout->addWidget(container);
190 }
191
192 loadTree(url());
193 Panel::showEvent(event);
194 }
195
196 void FoldersPanel::keyPressEvent(QKeyEvent* event)
197 {
198 const int key = event->key();
199 if ((key == Qt::Key_Enter) || (key == Qt::Key_Return)) {
200 event->accept();
201 } else {
202 Panel::keyPressEvent(event);
203 }
204 }
205
206 void FoldersPanel::slotItemActivated(int index)
207 {
208 const KFileItem item = fileItemModel()->fileItem(index);
209 if (!item.isNull()) {
210 emit changeUrl(item.url(), Qt::LeftButton);
211 }
212 }
213
214 void FoldersPanel::slotItemMiddleClicked(int index)
215 {
216 const KFileItem item = fileItemModel()->fileItem(index);
217 if (!item.isNull()) {
218 emit changeUrl(item.url(), Qt::MiddleButton);
219 }
220 }
221
222 void FoldersPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
223 {
224 Q_UNUSED(pos);
225
226 const KFileItem fileItem = fileItemModel()->fileItem(index);
227
228 QWeakPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, fileItem);
229 contextMenu.data()->open();
230 if (contextMenu.data()) {
231 delete contextMenu.data();
232 }
233 }
234
235 void FoldersPanel::slotViewContextMenuRequested(const QPointF& pos)
236 {
237 Q_UNUSED(pos);
238
239 QWeakPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, KFileItem());
240 contextMenu.data()->open();
241 if (contextMenu.data()) {
242 delete contextMenu.data();
243 }
244 }
245
246 void FoldersPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
247 {
248 if (index >= 0) {
249 KFileItem destItem = fileItemModel()->fileItem(index);
250 if (destItem.isNull()) {
251 return;
252 }
253
254 QDropEvent dropEvent(event->pos().toPoint(),
255 event->possibleActions(),
256 event->mimeData(),
257 event->buttons(),
258 event->modifiers());
259
260 DragAndDropHelper::dropUrls(destItem, destItem.url(), &dropEvent);
261 }
262 }
263
264 void FoldersPanel::slotLoadingCompleted()
265 {
266 if (m_controller->view()->opacity() == 0) {
267 // The loading of the initial tree after opening the Folders panel
268 // has been finished. Trigger the increasing of the opacity after
269 // a short delay to give the view the chance to finish its internal
270 // animations.
271 // TODO: Check whether it makes sense to allow accessing the
272 // view-internal delay for usecases like this.
273 QTimer::singleShot(250, this, SLOT(startFadeInAnimation()));
274 }
275
276 if (!m_updateCurrentItem) {
277 return;
278 }
279
280 const int index = fileItemModel()->index(url());
281 updateCurrentItem(index);
282 m_updateCurrentItem = false;
283 }
284
285 void FoldersPanel::startFadeInAnimation()
286 {
287 QPropertyAnimation* anim = new QPropertyAnimation(m_controller->view(), "opacity", this);
288 anim->setStartValue(0);
289 anim->setEndValue(1);
290 anim->setEasingCurve(QEasingCurve::InOutQuad);
291 anim->start(QAbstractAnimation::DeleteWhenStopped);
292 anim->setDuration(200);
293 }
294
295 void FoldersPanel::loadTree(const KUrl& url)
296 {
297 Q_ASSERT(m_controller);
298
299 m_updateCurrentItem = false;
300
301 KUrl baseUrl;
302 if (url.isLocalFile()) {
303 // Use the root directory as base for local URLs (#150941)
304 baseUrl = QDir::rootPath();
305 } else {
306 // Clear the path for non-local URLs and use it as base
307 baseUrl = url;
308 baseUrl.setPath(QString('/'));
309 }
310
311 KFileItemModel* model = fileItemModel();
312 if (model->directory() != baseUrl) {
313 m_updateCurrentItem = true;
314 model->refreshDirectory(baseUrl);
315 }
316
317 const int index = model->index(url);
318 if (index >= 0) {
319 updateCurrentItem(index);
320 } else {
321 m_updateCurrentItem = true;
322 model->expandParentDirectories(url);
323 // slotLoadingCompleted() will be invoked after the model has
324 // expanded the url
325 }
326 }
327
328 void FoldersPanel::updateCurrentItem(int index)
329 {
330 KItemListSelectionManager* selectionManager = m_controller->selectionManager();
331 selectionManager->setCurrentItem(index);
332 selectionManager->clearSelection();
333 selectionManager->setSelected(index);
334
335 m_controller->view()->scrollToItem(index);
336 }
337
338 KFileItemModel* FoldersPanel::fileItemModel() const
339 {
340 return static_cast<KFileItemModel*>(m_controller->model());
341 }
342
343 #include "folderspanel.moc"