1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz <peter.penz19@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "folderspanel.h"
22 #include "dolphin_folderspanelsettings.h"
23 #include "dolphin_generalsettings.h"
24 #include "treeviewcontextmenu.h"
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>
35 #include <konq_operations.h>
37 #include <QApplication>
39 #include <QGraphicsView>
42 #include <views/renamedialog.h>
46 FoldersPanel::FoldersPanel(QWidget
* parent
) :
48 m_setLeafVisible(false),
49 m_mouseButtons(Qt::NoButton
),
54 setLayoutDirection(Qt::LeftToRight
);
57 FoldersPanel::~FoldersPanel()
59 FoldersPanelSettings::self()->writeConfig();
61 KItemListView
* view
= m_controller
->view();
62 m_controller
->setView(0);
69 void FoldersPanel::setHiddenFilesShown(bool show
)
71 FoldersPanelSettings::setHiddenFilesShown(show
);
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
);
81 bool FoldersPanel::hiddenFilesShown() const
83 return FoldersPanelSettings::hiddenFilesShown();
86 void FoldersPanel::setAutoScrolling(bool enable
)
88 //m_treeView->setAutoHorizontalScroll(enable);
89 FoldersPanelSettings::setAutoScrolling(enable
);
92 bool FoldersPanel::autoScrolling() const
94 return FoldersPanelSettings::autoScrolling();
97 void FoldersPanel::rename(const KFileItem
& item
)
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);
105 RenameDialog
* dialog
= new RenameDialog(this, KFileItemList() << item
);
106 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
109 dialog
->activateWindow();
113 bool FoldersPanel::urlChanged()
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.
122 m_setLeafVisible
= true;
129 void FoldersPanel::showEvent(QShowEvent
* event
)
131 if (event
->spontaneous()) {
132 Panel::showEvent(event
);
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());
148 KFileItemListView
* view
= new KFileItemListView();
149 view
->setWidgetCreator(new KItemListWidgetCreator
<KFileItemListWidget
>());
151 KItemListStyleOption styleOption
= view
->styleOption();
152 styleOption
.margin
= 2;
153 styleOption
.iconSize
= KIconLoader::SizeSmall
;
154 view
->setStyleOption(styleOption
);
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
);
160 KFileItemModel
* model
= new KFileItemModel(m_dirLister
, this);
161 // Use a QueuedConnection to give the view the possibility to react first on the
163 connect(model
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()), Qt::QueuedConnection
);
165 KItemListContainer
* container
= new KItemListContainer(this);
166 m_controller
= container
->controller();
167 m_controller
->setView(view
);
168 m_controller
->setModel(model
);
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());
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
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);
186 QVBoxLayout
* layout
= new QVBoxLayout(this);
187 layout
->setMargin(0);
188 layout
->addWidget(container
);
192 Panel::showEvent(event
);
195 void FoldersPanel::contextMenuEvent(QContextMenuEvent
* event
)
197 Panel::contextMenuEvent(event
);
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);
206 QPointer
<TreeViewContextMenu
> contextMenu
= new TreeViewContextMenu(this, item
);
211 void FoldersPanel::keyPressEvent(QKeyEvent
* event
)
213 const int key
= event
->key();
214 if ((key
== Qt::Key_Enter
) || (key
== Qt::Key_Return
)) {
216 //updateActiveView(m_treeView->currentIndex());
218 Panel::keyPressEvent(event
);
222 void FoldersPanel::updateMouseButtons()
224 m_mouseButtons
= QApplication::mouseButtons();
227 void FoldersPanel::slotLoadingCompleted()
229 const int index
= fileItemModel()->index(url());
231 m_controller
->selectionManager()->setCurrentItem(index
);
235 void FoldersPanel::slotHorizontalScrollBarMoved(int value
)
238 // Disable the auto-scrolling until the vertical scrollbar has
239 // been moved by the user.
240 //m_treeView->setAutoHorizontalScroll(false);
243 void FoldersPanel::slotVerticalScrollBarMoved(int value
)
246 // Enable the auto-scrolling again (it might have been disabled by
247 // moving the horizontal scrollbar).
248 //m_treeView->setAutoHorizontalScroll(FoldersPanelSettings::autoScrolling());
251 void FoldersPanel::loadTree(const KUrl
& url
)
253 Q_ASSERT(m_dirLister
);
257 if (url
.isLocalFile()) {
258 // Use the root directory as base for local URLs (#150941)
259 baseUrl
= QDir::rootPath();
261 // Clear the path for non-local URLs and use it as base
263 baseUrl
.setPath(QString('/'));
266 if (m_dirLister
->url() != baseUrl
) {
268 m_dirLister
->openUrl(baseUrl
, KDirLister::Reload
);
271 KFileItemModel
* model
= fileItemModel();
272 const int index
= model
->index(url
);
274 m_controller
->selectionManager()->setCurrentItem(index
);
276 model
->setExpanded(QSet
<KUrl
>() << url
);
280 void FoldersPanel::selectLeafDirectory()
282 /*const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_leafDir);
283 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
285 if (proxyIndex.isValid()) {
286 QItemSelectionModel* selModel = m_treeView->selectionModel();
287 selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::ClearAndSelect);
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;
299 KFileItemModel
* FoldersPanel::fileItemModel() const
301 return static_cast<KFileItemModel
*>(m_controller
->model());
304 #include "folderspanel.moc"