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>
This commit is contained in:
Kaung Zin Hein
2025-02-24 11:15:28 -05:00
committed by GitHub
parent e55f0cef4b
commit df2c6637e6
2 changed files with 51 additions and 0 deletions

View File

@@ -29,11 +29,14 @@
# include <QApplication>
# include <QContextMenuEvent>
# include <QCursor>
# include <QDir>
# include <QFileInfo>
# include <QHeaderView>
# include <QMenu>
# include <QMessageBox>
# include <QPainter>
# include <QPixmap>
# include <QProcess>
# include <QThread>
# include <QTimer>
# include <QToolTip>
@@ -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<DocumentItem*>(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"));

View File

@@ -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;