From 1fba89c1bc46520dfbb51a5d5d55c3ad881269da Mon Sep 17 00:00:00 2001 From: Treeway <56928485+Treeway7@users.noreply.github.com> Date: Mon, 11 Sep 2023 21:00:33 -0500 Subject: [PATCH] Show in File Explorer context menu option (#82) * Add Open in File Explorer in context menu * Fixed and refactored file explorer option * Added file explorer translation * Show in Explorer: Better Linux support --- mainwindow.cpp | 61 ++++++++++++++++++++++++++++++-- mainwindow.h | 4 +++ mainwindow.ui | 2 +- resource/translations/English.ts | 4 +++ 4 files changed, 68 insertions(+), 3 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 6fcd10a4..aa1a8748 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -526,17 +526,21 @@ void MainWindow::createLevelListContextMenu(const QPoint &pos) QAction openLevel(tr("Open In Level Editor"), this); QAction sarcExplorer(tr("Open In Sarc Explorer"), this); + QAction fileExplorer(tr("Show In File Explorer"), this); QAction removeLevel(tr("Remove Level"), this); connect(&openLevel, SIGNAL(triggered()), this, SLOT(openLevelFromConextMenu())); connect(&sarcExplorer, SIGNAL(triggered()), this, SLOT(openInSarcExplorer())); + connect(&fileExplorer, SIGNAL(triggered()), this, SLOT(showInFileExplorer())); connect(&removeLevel, SIGNAL(triggered()), this, SLOT(on_removeLevelBtn_clicked())); openLevel.setData(QVariant(pos)); sarcExplorer.setData(QVariant(pos)); + fileExplorer.setData(QVariant(pos)); contextMenu.addAction(&openLevel); contextMenu.addAction(&sarcExplorer); + contextMenu.addAction(&fileExplorer); contextMenu.addSeparator(); contextMenu.addAction(&removeLevel); @@ -552,17 +556,21 @@ void MainWindow::createTilesetListContextMenu(const QPoint &pos) QAction openTileset(tr("Open In Tileset Editor"), this); QAction sarcExplorer(tr("Open In Sarc Explorer"), this); + QAction fileExplorer(tr("Show In File Explorer"), this); QAction removeTileset(tr("Remove Tileset"), this); connect(&openTileset, SIGNAL(triggered()), this, SLOT(openTilesetFromConextMenu())); connect(&sarcExplorer, SIGNAL(triggered()), this, SLOT(openInSarcExplorer())); + connect(&fileExplorer, SIGNAL(triggered()), this, SLOT(showInFileExplorer())); connect(&removeTileset, SIGNAL(triggered()), this, SLOT(on_removeTilesetBtn_clicked())); openTileset.setData(QVariant(pos)); sarcExplorer.setData(QVariant(pos)); + fileExplorer.setData(QVariant(pos)); contextMenu.addAction(&openTileset); contextMenu.addAction(&sarcExplorer); + contextMenu.addAction(&fileExplorer); contextMenu.addSeparator(); contextMenu.addAction(&removeTileset); @@ -595,6 +603,56 @@ void MainWindow::openTilesetFromConextMenu() void MainWindow::openInSarcExplorer() { QAction* action = qobject_cast(sender()); + QString path = getFilePath(action); + + SarcExplorerWindow* sarcExplorer = new SarcExplorerWindow(this, path, settings); + sarcExplorer->show(); +} + +void MainWindow::showInFileExplorer() +{ + QAction* action = qobject_cast(sender()); + QString path = getFilePath(action); + + QFileInfo info(path); +#if defined(Q_OS_WIN) + QStringList args; + if (!info.isDir()) + args << "/select,"; + args << QDir::toNativeSeparators(path); + if (QProcess::startDetached("explorer", args)) + return; +#elif defined(Q_OS_MAC) + QStringList args; + args << "-e"; + args << "tell application \"Finder\""; + args << "-e"; + args << "activate"; + args << "-e"; + args << "select POSIX file \"" + path + "\""; + args << "-e"; + args << "end tell"; + args << "-e"; + args << "return"; + if (!QProcess::execute("/usr/bin/osascript", args)) + return; +#elif defined(Q_OS_LINUX) + QStringList args; + args << "--session"; + args << "--dest=org.freedesktop.FileManager1"; + args << "--type=method_call"; + args << "/org/freedesktop/FileManager1"; + args << "org.freedesktop.FileManager1.ShowItems"; + args << "array:string:file://" + path; + args << "string:"; + if (QProcess::startDetached("dbus-send", args)) + return; +#endif + QDesktopServices::openUrl(QUrl::fromLocalFile(info.isDir()? path : info.path())); +} + +QString MainWindow::getFilePath(QAction* action) +{ QPoint pos = action->data().toPoint(); QString path; @@ -613,6 +671,5 @@ void MainWindow::openInSarcExplorer() if (!path.endsWith(".sarc")) path.append(".sarc"); - SarcExplorerWindow* sarcExplorer = new SarcExplorerWindow(this, path, settings); - sarcExplorer->show(); + return path; } diff --git a/mainwindow.h b/mainwindow.h index f36261ac..d4e83e74 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -83,6 +83,8 @@ private slots: void openTilesetFromConextMenu(); void openInSarcExplorer(); + + void showInFileExplorer(); private: Ui::MainWindow *ui; @@ -102,6 +104,8 @@ private slots: void setNightmode(bool nightmode); void changeEvent(QEvent* event); + + QString getFilePath(QAction* action); }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index e4daa5bb..b1458220 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -198,7 +198,7 @@ 0 0 436 - 22 + 21 diff --git a/resource/translations/English.ts b/resource/translations/English.ts index e2305b5c..244956e7 100644 --- a/resource/translations/English.ts +++ b/resource/translations/English.ts @@ -671,6 +671,10 @@ Connection to %1 failed! Open In Tileset Editor + + Show In File Explorer + + File