From df2c6637e63439b97e79a9b3797dec12e71e2967 Mon Sep 17 00:00:00 2001 From: Kaung Zin Hein <83657429+Zen-cronic@users.noreply.github.com> Date: Mon, 24 Feb 2025 11:15:28 -0500 Subject: [PATCH] Gui: "Open File Location/Reveal in Finder" in Tree view (#19805) * Add: barebone openFileLocation on linux * Feat: Add handling for different types of os * Fix: Use preprocessors * Fix: directive typo --------- Signed-off-by: Kaung Zin Hein <83657429+Zen-cronic@users.noreply.github.com> --- src/Gui/Tree.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Gui/Tree.h | 2 ++ 2 files changed, 51 insertions(+) diff --git a/src/Gui/Tree.cpp b/src/Gui/Tree.cpp index 03473cc240..a989561eec 100644 --- a/src/Gui/Tree.cpp +++ b/src/Gui/Tree.cpp @@ -29,11 +29,14 @@ # include # include # include +# include +# include # include # include # include # include # include +# include # include # include # include @@ -683,6 +686,10 @@ TreeWidget::TreeWidget(const char* name, QWidget* parent) connect(this->searchObjectsAction, &QAction::triggered, this, &TreeWidget::onSearchObjects); + this->openFileLocationAction = new QAction(this); + connect(this->openFileLocationAction, &QAction::triggered, + this, &TreeWidget::onOpenFileLocation); + //NOLINTBEGIN // Setup connections connectNewDocument = Application::Instance->signalNewDocument.connect(std::bind(&TreeWidget::slotNewDocument, this, sp::_1, sp::_2)); @@ -1032,6 +1039,7 @@ void TreeWidget::contextMenuEvent(QContextMenuEvent* e) showHiddenAction->setChecked(docitem->showHidden()); contextMenu.addAction(this->showHiddenAction); + contextMenu.addAction(this->openFileLocationAction); contextMenu.addAction(this->searchObjectsAction); contextMenu.addAction(this->closeDocAction); if (doc->testStatus(App::Document::PartialDoc)) @@ -2876,6 +2884,39 @@ void TreeWidget::onCloseDoc() } } +void TreeWidget::onOpenFileLocation() +{ + auto docitem = static_cast(this->contextItem); + App::Document* doc = docitem->document()->getDocument(); + std::string name = doc->FileName.getValue(); + + const QFileInfo fileInfo(QString::fromStdString(name)); + if (!fileInfo.exists()) { + QMessageBox::warning(this, tr("Error"), tr("File does not exist.")); + return; + } + + const QString filePath = fileInfo.canonicalPath(); + bool success = false; + +#if defined(Q_OS_MAC) + success = QProcess::startDetached(QStringLiteral("open"), {filePath}); +#elif defined(Q_OS_WIN) + QStringList param; + if (!fileInfo.isDir()) { + param += QStringLiteral("/select,"); + } + param += QDir::toNativeSeparators(filePath); + success = QProcess::startDetached(QStringLiteral("explorer.exe"), param); +#else + success = QProcess::startDetached(QStringLiteral("xdg-open"), {filePath}); +#endif + + if (!success) { + QMessageBox::warning(this, tr("Error"), tr("Failed to open directory.")); + } +} + void TreeWidget::slotRenameDocument(const Gui::Document& Doc) { // do nothing here @@ -3330,6 +3371,14 @@ void TreeWidget::setupText() this->closeDocAction->setText(tr("Close document")); this->closeDocAction->setStatusTip(tr("Close the document")); +#ifdef Q_OS_MAC + this->openFileLocationAction->setText(tr("Reveal in Finder")); + this->openFileLocationAction->setStatusTip(tr("Reveal the current file location in Finder")); +#else + this->openFileLocationAction->setText(tr("Open File Location")); + this->openFileLocationAction->setStatusTip(tr("Open the current file location")); +#endif + this->reloadDocAction->setText(tr("Reload document")); this->reloadDocAction->setStatusTip(tr("Reload a partially loaded document")); diff --git a/src/Gui/Tree.h b/src/Gui/Tree.h index 6c3c37ad99..1c0c9dada9 100644 --- a/src/Gui/Tree.h +++ b/src/Gui/Tree.h @@ -190,6 +190,7 @@ protected Q_SLOTS: void onShowHidden(); void onToggleVisibilityInTree(); void onSearchObjects(); + void onOpenFileLocation(); private Q_SLOTS: void onItemSelectionChanged(); @@ -240,6 +241,7 @@ private: QAction* reloadDocAction; QAction* closeDocAction; QAction* searchObjectsAction; + QAction* openFileLocationAction; QTreeWidgetItem *contextItem; App::DocumentObject *searchObject; Gui::Document *searchDoc;