Skip to content

Commit

Permalink
[library] Refactor, allow to copy the file path of a file with right …
Browse files Browse the repository at this point in the history
…click menu, fix project view not having right click
  • Loading branch information
jcelerier committed Apr 3, 2024
1 parent 53f1ecc commit 32f5438
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 30 deletions.
46 changes: 46 additions & 0 deletions src/plugins/score-plugin-library/Library/LibraryWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#include "LibraryWidget.hpp"

#include <Library/FileSystemModel.hpp>
#include <Library/LibraryInterface.hpp>
#include <Library/RecursiveFilterProxy.hpp>

#include <score/application/GUIApplicationContext.hpp>

#include <QClipboard>
#include <QDesktopServices>
#include <QFileInfo>
#include <QMenu>
#include <QUrl>

namespace Library
{
Expand Down Expand Up @@ -36,4 +42,44 @@ std::vector<LibraryInterface*> libraryInterface(const QString& path)
return libs;
}

void setupFilesystemContextMenu(
QTreeView& m_tv, FileSystemModel& model, FileSystemRecursiveFilterProxy& proxy)
{
m_tv.connect(&m_tv, &QTreeView::customContextMenuRequested, &m_tv, [&](QPoint pos) {
auto idx = m_tv.indexAt(pos);
if(!idx.isValid())
return;
auto source = proxy.mapToSource(idx);
if(!source.isValid())
return;
QFileInfo fi{model.filePath(source)};

auto folder_path = fi.isDir() ? fi.absoluteFilePath() : fi.absolutePath();

auto menu = new QMenu{&m_tv};
auto file_expl = menu->addAction(QObject::tr("Open in file explorer"));
m_tv.connect(file_expl, &QAction::triggered, &m_tv, [=] {
QDesktopServices::openUrl(QUrl::fromLocalFile(folder_path));
});

if constexpr(FileSystemModel::supportsDisablingSorting())
{
auto sorting = new QAction(QObject::tr("Sort"));
sorting->setCheckable(true);
sorting->setChecked(model.isSorting());
menu->addAction(sorting);
m_tv.connect(sorting, &QAction::triggered, &m_tv, [&model](bool checked) {
model.setSorting(checked);
});
}

auto copy_path = menu->addAction(QObject::tr("Copy path"));
m_tv.connect(copy_path, &QAction::triggered, &m_tv, [=] {
QGuiApplication::clipboard()->setText(fi.absoluteFilePath());
});

menu->exec(m_tv.mapToGlobal(pos));
menu->deleteLater();
});
}
}
7 changes: 7 additions & 0 deletions src/plugins/score-plugin-library/Library/LibraryWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
#include <QTreeView>

#include <vector>
class QFileSystemModel;

namespace Library
{
class LibraryInterface;
class FileSystemModel;
class FileSystemRecursiveFilterProxy;
inline void setup_treeview(QTreeView& tv)
{
tv.setHeaderHidden(true);
Expand All @@ -17,4 +21,7 @@ inline void setup_treeview(QTreeView& tv)
}

std::vector<LibraryInterface*> libraryInterface(const QString& path);

void setupFilesystemContextMenu(
QTreeView& m_tv, FileSystemModel& model, FileSystemRecursiveFilterProxy& proxy);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ ProjectLibraryWidget::ProjectLibraryWidget(
});
m_tv.setAcceptDrops(true);
m_tv.setSelectionMode(QAbstractItemView::SelectionMode::ExtendedSelection);
m_tv.setContextMenuPolicy(Qt::CustomContextMenu);
setupFilesystemContextMenu(m_tv, *m_model, *m_proxy);
}

ProjectLibraryWidget::~ProjectLibraryWidget() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct DocumentMetadata;

namespace Library
{
class FileSystemModel;
class FileSystemRecursiveFilterProxy;
class ProjectLibraryWidget : public QWidget
{
Expand All @@ -22,7 +23,7 @@ class ProjectLibraryWidget : public QWidget
void unsetRoot();

private:
QFileSystemModel* m_model{};
FileSystemModel* m_model{};
FileSystemRecursiveFilterProxy* m_proxy{};
QTreeView m_tv;
QMetaObject::Connection m_con;
Expand Down
31 changes: 2 additions & 29 deletions src/plugins/score-plugin-library/Library/SystemLibraryWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <core/presenter/DocumentManager.hpp>

#include <QApplication>
#include <QClipboard>
#include <QDesktopServices>
#include <QMenu>
#include <QVBoxLayout>
Expand Down Expand Up @@ -57,35 +58,7 @@ SystemLibraryWidget::SystemLibraryWidget(
auto sel = m_tv.selectionModel();

m_tv.setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
connect(&m_tv, &QTreeView::customContextMenuRequested, this, [&](QPoint pos) {
auto idx = m_tv.indexAt(pos);
if(!idx.isValid())
return;
auto source = m_proxy->mapToSource(idx);
QFileInfo path{m_model->filePath(source)};

auto folder_path = path.isDir() ? path.absoluteFilePath() : path.absolutePath();

auto menu = new QMenu{&m_tv};
auto file_expl = menu->addAction(tr("Open in file explorer"));
connect(file_expl, &QAction::triggered, this, [=] {
QDesktopServices::openUrl(QUrl::fromLocalFile(folder_path));
});

if constexpr(FileSystemModel::supportsDisablingSorting())
{
auto sorting = new QAction(tr("Sort"));
sorting->setCheckable(true);
sorting->setChecked(m_model->isSorting());
menu->addAction(sorting);
connect(sorting, &QAction::triggered, this, [this](bool checked) {
m_model->setSorting(checked);
});
}

menu->exec(m_tv.mapToGlobal(pos));
menu->deleteLater();
});
setupFilesystemContextMenu(m_tv, *m_model, *m_proxy);

connect(
sel, &QItemSelectionModel::currentRowChanged, this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ class SystemLibraryWidget : public QWidget
QWidget m_preview;
QWidget* m_previewChild{};
};

void setupFilesystemContextMenu(
QTreeView& m_tv, FileSystemModel& model, FileSystemRecursiveFilterProxy& proxy);
}

0 comments on commit 32f5438

Please sign in to comment.