File indexing completed on 2025-01-19 05:18:32

0001 /*
0002  * Copyright (c) 2018 Sune Vuorela <sune@vuorela.dk>
0003  *
0004  * Permission is hereby granted, free of charge, to any person
0005  * obtaining a copy of this software and associated documentation
0006  * files (the "Software"), to deal in the Software without
0007  * restriction, including without limitation the rights to use,
0008  * copy, modify, merge, publish, distribute, sublicense, and/or sell
0009  * copies of the Software, and to permit persons to whom the
0010  * Software is furnished to do so, subject to the following
0011  * conditions:
0012  *
0013  * The above copyright notice and this permission notice shall be
0014  * included in all copies or substantial portions of the Software.
0015  *
0016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0017  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
0018  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0019  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
0020  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
0021  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0022  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0023  * OTHER DEALINGS IN THE SOFTWARE.
0024  */
0025 #include "fspane.h"
0026 #include <memory>
0027 #include <QTreeView>
0028 #include <QFileSystemModel>
0029 #include <QVBoxLayout>
0030 #include <QIdentityProxyModel>
0031 
0032 #include <QDebug>
0033 
0034 class OneColumnProxy : public QIdentityProxyModel
0035 {
0036 public:
0037     OneColumnProxy(QObject* parent = nullptr) : QIdentityProxyModel(parent) {}
0038     int columnCount(const QModelIndex& ) const override {
0039         return 1;
0040     }
0041 };
0042 
0043 class FileNameTitleMapper : public QIdentityProxyModel
0044 {
0045 public:
0046     FileNameTitleMapper(QObject* parent = nullptr) : QIdentityProxyModel(parent) {}
0047     void setFileNameTitleMap(QHash<QString,QString> map) {
0048         beginResetModel();
0049         m_map = map;
0050         endResetModel();
0051     }
0052     QHash<QString,QString> m_map;
0053     QVariant data(const QModelIndex& idx, int role) const override {
0054         if(role == Qt::DisplayRole) {
0055             QVariant d = QIdentityProxyModel::data(idx,QFileSystemModel::FilePathRole);
0056             QString str = d.toString();
0057             auto it = m_map.constFind(str);
0058             if (it != m_map.constEnd()) {
0059                 return it.value();
0060             }
0061         }
0062         return QIdentityProxyModel::data(idx,role);
0063     }
0064 };
0065 
0066 FsPane::FsPane(QWidget* parent) : QWidget(parent)
0067 {
0068     auto layout = std::make_unique<QVBoxLayout>();
0069     
0070     auto tree = std::make_unique<QTreeView>();
0071     m_dirModel = std::make_unique<QFileSystemModel>();
0072     m_dirModel->setNameFilters(QStringList() << QStringLiteral("*.recipe.md"));
0073     m_dirModel->setNameFilterDisables(false);
0074     
0075     m_filenamemapper = std::make_unique<FileNameTitleMapper>();
0076     m_filenamemapper->setSourceModel(m_dirModel.get());
0077     
0078     m_proxy = std::make_unique<OneColumnProxy>();
0079     m_proxy->setSourceModel(m_filenamemapper.get());
0080     
0081     
0082     tree->setModel(m_proxy.get());
0083     tree->setHeaderHidden(true);
0084     
0085     connect(tree.get(), &QTreeView::clicked, [this] (const QModelIndex& idx)
0086     {
0087         auto var = idx.data(QFileSystemModel::FilePathRole);
0088         Q_EMIT this->fileSelected(var.toString());
0089     });
0090 
0091     m_tree = tree.get();
0092     layout->addWidget(tree.release());
0093     
0094     setLayout(layout.release());
0095 }
0096 
0097 FsPane::~FsPane()
0098 {
0099     // for smart pointers
0100 }
0101 
0102 void FsPane::setRootPath(const QString& string)
0103 {
0104     auto path = m_dirModel->setRootPath(string);
0105 
0106     path = m_filenamemapper->mapFromSource(path);
0107     path = m_proxy->mapFromSource(path);
0108     if (path.isValid()) {
0109         m_tree->setModel(m_proxy.get());
0110         m_tree->setRootIndex(path);
0111         m_rootPath = string;
0112     } else {
0113         m_tree->setModel(nullptr);
0114         m_rootPath = QString();
0115     }
0116 }
0117 
0118 void FsPane::setFileNameTitleMap(QHash<QString, QString> titlemap)
0119 {
0120     m_filenamemapper->setFileNameTitleMap(titlemap);
0121     setRootPath(m_rootPath);
0122 }