File indexing completed on 2025-01-05 05:14:49

0001 /*
0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com>
0003 
0004 SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #include "filestreedialog.h"
0008 #include "actions/fileactions.h"
0009 #include "gitmanager.h"
0010 #include "models/treemodel.h"
0011 #include <KLocalizedString>
0012 #include <QFileIconProvider>
0013 
0014 FilesTreeDialog::FilesTreeDialog(Git::Manager *git, const QString &place, QWidget *parent)
0015     : AppDialog(git, parent)
0016     , mTreeModel(new TreeModel(this))
0017     , mPlace(place)
0018     , mActions(new FileActions(git, this))
0019 {
0020     setupUi(this);
0021 
0022     mActions->setPlace(place);
0023 
0024     mTreeModel->setSeparator(QStringLiteral("/"));
0025 
0026     const auto files = git->ls(place);
0027 
0028     mTreeModel->setShowRoot(true);
0029     mTreeModel->setLastPartAsData(true);
0030     QFileIconProvider p;
0031     mTreeModel->setDefaultIcon(p.icon(QFileIconProvider::Folder));
0032     mTreeModel->addData(files);
0033     treeView->setModel(mTreeModel);
0034 
0035     setWindowTitle(i18nc("@title:window", "Browse files: %1", place));
0036 
0037     lineEditBranchName->setText(place);
0038 
0039     listWidget->clear();
0040 
0041     const auto rootData = mTreeModel->rootData();
0042     for (const auto &f : rootData) {
0043         const QFileInfo fi(f);
0044         const auto icon = p.icon(fi);
0045         auto item = new QListWidgetItem(listWidget);
0046         item->setText(f);
0047         item->setIcon(icon);
0048         listWidget->addItem(item);
0049     }
0050 
0051     connect(treeView, &QTreeView::clicked, this, &FilesTreeDialog::slotTreeViewClicked);
0052     connect(listWidget, &QListWidget::customContextMenuRequested, this, &FilesTreeDialog::slotListWidgetCustomContextMenuRequested);
0053 }
0054 
0055 void FilesTreeDialog::slotTreeViewClicked(const QModelIndex &index)
0056 {
0057     QFileIconProvider p;
0058     listWidget->clear();
0059 
0060     const auto d = mTreeModel->data(index);
0061     for (const auto &f : d) {
0062         const QFileInfo fi(f);
0063         const auto icon = p.icon(fi);
0064         auto item = new QListWidgetItem(listWidget);
0065         item->setText(f);
0066         item->setIcon(icon);
0067         listWidget->addItem(item);
0068     }
0069 }
0070 
0071 void FilesTreeDialog::slotListWidgetCustomContextMenuRequested(const QPoint &pos)
0072 {
0073     auto path = mTreeModel->fullPath(treeView->currentIndex());
0074 
0075     if (path == QLatin1Char('/'))
0076         path = listWidget->currentItem()->text();
0077     else
0078         path += QLatin1Char('/') + listWidget->currentItem()->text();
0079 
0080     mActions->setFilePath(path);
0081     mActions->popup(listWidget->mapToGlobal(pos));
0082 }
0083 
0084 #include "moc_filestreedialog.cpp"