From 32f543898677587979cb49b7fe50e2ce1b7a6aa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Micha=C3=ABl=20Celerier?= Date: Tue, 2 Apr 2024 20:58:54 -0400 Subject: [PATCH] [library] Refactor, allow to copy the file path of a file with right click menu, fix project view not having right click --- .../Library/LibraryWidget.cpp | 46 +++++++++++++++++++ .../Library/LibraryWidget.hpp | 7 +++ .../Library/ProjectLibraryWidget.cpp | 2 + .../Library/ProjectLibraryWidget.hpp | 3 +- .../Library/SystemLibraryWidget.cpp | 31 +------------ .../Library/SystemLibraryWidget.hpp | 3 ++ 6 files changed, 62 insertions(+), 30 deletions(-) diff --git a/src/plugins/score-plugin-library/Library/LibraryWidget.cpp b/src/plugins/score-plugin-library/Library/LibraryWidget.cpp index e5dea3e6c2..2946da04b3 100644 --- a/src/plugins/score-plugin-library/Library/LibraryWidget.cpp +++ b/src/plugins/score-plugin-library/Library/LibraryWidget.cpp @@ -2,11 +2,17 @@ // it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com #include "LibraryWidget.hpp" +#include #include +#include #include +#include +#include #include +#include +#include namespace Library { @@ -36,4 +42,44 @@ std::vector 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(); + }); +} } diff --git a/src/plugins/score-plugin-library/Library/LibraryWidget.hpp b/src/plugins/score-plugin-library/Library/LibraryWidget.hpp index 2de4180f13..61d4c369c6 100644 --- a/src/plugins/score-plugin-library/Library/LibraryWidget.hpp +++ b/src/plugins/score-plugin-library/Library/LibraryWidget.hpp @@ -2,9 +2,13 @@ #include #include +class QFileSystemModel; + namespace Library { class LibraryInterface; +class FileSystemModel; +class FileSystemRecursiveFilterProxy; inline void setup_treeview(QTreeView& tv) { tv.setHeaderHidden(true); @@ -17,4 +21,7 @@ inline void setup_treeview(QTreeView& tv) } std::vector libraryInterface(const QString& path); + +void setupFilesystemContextMenu( + QTreeView& m_tv, FileSystemModel& model, FileSystemRecursiveFilterProxy& proxy); } diff --git a/src/plugins/score-plugin-library/Library/ProjectLibraryWidget.cpp b/src/plugins/score-plugin-library/Library/ProjectLibraryWidget.cpp index 1b784ae7bd..2557dc9f94 100644 --- a/src/plugins/score-plugin-library/Library/ProjectLibraryWidget.cpp +++ b/src/plugins/score-plugin-library/Library/ProjectLibraryWidget.cpp @@ -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() { } diff --git a/src/plugins/score-plugin-library/Library/ProjectLibraryWidget.hpp b/src/plugins/score-plugin-library/Library/ProjectLibraryWidget.hpp index 7efb6225b6..288c34f34e 100644 --- a/src/plugins/score-plugin-library/Library/ProjectLibraryWidget.hpp +++ b/src/plugins/score-plugin-library/Library/ProjectLibraryWidget.hpp @@ -11,6 +11,7 @@ struct DocumentMetadata; namespace Library { +class FileSystemModel; class FileSystemRecursiveFilterProxy; class ProjectLibraryWidget : public QWidget { @@ -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; diff --git a/src/plugins/score-plugin-library/Library/SystemLibraryWidget.cpp b/src/plugins/score-plugin-library/Library/SystemLibraryWidget.cpp index a952ee5097..5ba2249564 100644 --- a/src/plugins/score-plugin-library/Library/SystemLibraryWidget.cpp +++ b/src/plugins/score-plugin-library/Library/SystemLibraryWidget.cpp @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -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, diff --git a/src/plugins/score-plugin-library/Library/SystemLibraryWidget.hpp b/src/plugins/score-plugin-library/Library/SystemLibraryWidget.hpp index 319c314642..56a5f5482a 100644 --- a/src/plugins/score-plugin-library/Library/SystemLibraryWidget.hpp +++ b/src/plugins/score-plugin-library/Library/SystemLibraryWidget.hpp @@ -27,4 +27,7 @@ class SystemLibraryWidget : public QWidget QWidget m_preview; QWidget* m_previewChild{}; }; + +void setupFilesystemContextMenu( + QTreeView& m_tv, FileSystemModel& model, FileSystemRecursiveFilterProxy& proxy); }