File indexing completed on 2025-01-26 05:24:14

0001 /*
0002     This file is part of the Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2009 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "filesystembrowserview.hpp"
0010 
0011 // lib
0012 #include "filesystembrowsertool.hpp"
0013 // KF
0014 #include <KUrlNavigator>
0015 #include <KFilePlacesModel>
0016 #include <KDirOperator>
0017 #include <KActionCollection>
0018 #include <KToolBar>
0019 #include <KLocalizedString>
0020 // Qt
0021 #include <QVBoxLayout>
0022 #include <QUrl>
0023 #include <QDir>
0024 
0025 namespace Kasten {
0026 
0027 FileSystemBrowserView::FileSystemBrowserView(FileSystemBrowserTool* tool, QWidget* parent)
0028     : QWidget(parent)
0029     , mTool(tool)
0030 {
0031     QMetaObject::invokeMethod(this, "init", Qt::QueuedConnection);
0032 }
0033 
0034 FileSystemBrowserView::~FileSystemBrowserView() = default;
0035 
0036 void FileSystemBrowserView::init()
0037 {
0038     auto* layout = new QVBoxLayout(this);
0039     layout->setContentsMargins(0, 0, 0, 0);
0040     layout->setSpacing(0);
0041 
0042     // tool bar
0043     mToolbar = new KToolBar(this);
0044     mToolbar->setMovable(false);
0045     mToolbar->setToolButtonStyle(Qt::ToolButtonIconOnly);
0046     mToolbar->setIconDimensions(16);
0047     mToolbar->setContextMenuPolicy(Qt::NoContextMenu);
0048     layout->addWidget(mToolbar);
0049 
0050     // url bar
0051     auto* filePlacesModel = new KFilePlacesModel(this);
0052     mUrlNavigator = new KUrlNavigator(filePlacesModel, QUrl::fromLocalFile(QDir::homePath()), this);
0053     connect(mUrlNavigator, &KUrlNavigator::urlChanged, this, &FileSystemBrowserView::setDirOperatorUrl);
0054     layout->addWidget(mUrlNavigator);
0055 
0056     // view
0057     mDirOperator = new KDirOperator(QUrl::fromLocalFile(QDir::homePath()), this);
0058     mDirOperator->setViewMode(KFile::Detail);
0059     connect(mDirOperator, &KDirOperator::urlEntered, this, &FileSystemBrowserView::setNavigatorUrl);
0060     connect(mDirOperator, &KDirOperator::fileSelected, this, &FileSystemBrowserView::openFile);
0061     layout->addWidget(mDirOperator);
0062 
0063     // fill toolbar
0064     constexpr KDirOperator::Action toolbarActions[] = {
0065         KDirOperator::Back,
0066         KDirOperator::Forward,
0067         KDirOperator::Up,
0068         KDirOperator::Home,
0069         KDirOperator::ShortView,
0070         KDirOperator::DetailedView,
0071         KDirOperator::TreeView,
0072     };
0073 
0074     for (const KDirOperator::Action toolbarAction : toolbarActions) {
0075         QAction* action = mDirOperator->action(toolbarAction);
0076         if (action) {
0077             mToolbar->addAction(action);
0078         }
0079     }
0080 
0081     mActionCollection = new KActionCollection(this);
0082     QAction* syncDirAction = mActionCollection->addAction(QStringLiteral("sync_dir"),
0083                                                           this, &FileSystemBrowserView::syncCurrentDocumentDirectory);
0084     syncDirAction->setIcon(QIcon::fromTheme(QStringLiteral("go-parent-folder")));
0085     syncDirAction->setText(i18nc("@action:intoolbar", "Folder of Current Document"));
0086     connect(mTool, &FileSystemBrowserTool::hasCurrentUrlChanged, syncDirAction, &QAction::setEnabled);
0087     syncDirAction->setEnabled(mTool->hasCurrentUrl());
0088     mToolbar->addAction(syncDirAction);
0089 }
0090 
0091 void FileSystemBrowserView::setDirOperatorUrl(const QUrl& url)
0092 {
0093     mDirOperator->setUrl(url, true);
0094 }
0095 
0096 void FileSystemBrowserView::setNavigatorUrl(const QUrl& url)
0097 {
0098     mUrlNavigator->setLocationUrl(url);
0099 }
0100 
0101 void FileSystemBrowserView::syncCurrentDocumentDirectory()
0102 {
0103     const QUrl url = mTool->currentUrl();
0104 
0105     if (!url.isEmpty()) {
0106         setNavigatorUrl(url);
0107     }
0108 }
0109 
0110 void FileSystemBrowserView::openFile(const KFileItem& fileItem)
0111 {
0112     mTool->open(fileItem.url());
0113 }
0114 
0115 }
0116 
0117 #include "moc_filesystembrowserview.cpp"