Qt: fix opening folders by using util functions

This commit is contained in:
Megamouse 2022-12-10 18:29:44 +01:00
parent 8399516475
commit 6f306121a6
5 changed files with 41 additions and 9 deletions

View File

@ -1101,6 +1101,7 @@
</ClCompile>
<ClCompile Include="Emu\Io\recording_config.cpp">
<Filter>Emu\Io</Filter>
</ClCompile>
<ClCompile Include="Emu\Cell\Modules\HLE_PATCHES.cpp">
<Filter>Emu\Cell\Modules</Filter>
</ClCompile>

View File

@ -1,3 +1,4 @@
#include "stdafx.h"
#include "qt_utils.h"
#include <QApplication>
#include <QBitmap>
@ -12,6 +13,8 @@
#include "Utilities/File.h"
#include <cmath>
LOG_CHANNEL(gui_log, "GUI");
inline std::string sstr(const QString& _in) { return _in.toStdString(); }
constexpr auto qstr = QString::fromStdString;
@ -394,7 +397,6 @@ namespace gui
void open_dir(const std::string& spath)
{
fs::create_dir(spath);
const QString path = qstr(spath);
if (fs::is_file(spath))
@ -402,18 +404,46 @@ namespace gui
// open directory and select file
// https://stackoverflow.com/questions/3490336/how-to-reveal-in-finder-or-show-in-explorer-with-qt
#ifdef _WIN32
QProcess::startDetached("explorer.exe", { "/select,", QDir::toNativeSeparators(path) });
const QString cleaned_path = QDir::toNativeSeparators(path);
gui_log.notice("gui::utils::open_dir: About to open file path '%s' (original: '%s')", cleaned_path.toStdString(), spath);
if (!QProcess::startDetached("explorer.exe", {"/select,", cleaned_path}))
{
gui_log.error("gui::utils::open_dir: Failed to start explorer process");
}
#elif defined(__APPLE__)
gui_log.notice("gui::utils::open_dir: About to open file path '%s'", spath);
QProcess::execute("/usr/bin/osascript", { "-e", "tell application \"Finder\" to reveal POSIX file \"" + path + "\"" });
QProcess::execute("/usr/bin/osascript", { "-e", "tell application \"Finder\" to activate" });
#else
// open parent directory
QDesktopServices::openUrl(QUrl::fromLocalFile(qstr(fs::get_parent_dir(spath))));
const QUrl url = QUrl::fromLocalFile(qstr(fs::get_parent_dir(spath)));
const std::string url_path = url.toString().toStdString();
gui_log.notice("gui::utils::open_dir: About to open parent dir url '%s' for path '%s'", url_path, spath);
if (!QDesktopServices::openUrl(url))
{
gui_log.error("gui::utils::open_dir: Failed to open parent dir url '%s' for path '%s'", url_path, spath);
}
#endif
return;
}
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
if (!fs::is_dir && !fs::create_path(spath))
{
gui_log.error("gui::utils::open_dir: Failed to create path '%s' (%s)", spath, fs::g_tls_error);
return;
}
const QUrl url = QUrl::fromLocalFile(path);
const std::string url_path = url.toString().toStdString();
gui_log.notice("gui::utils::open_dir: About to open dir url '%s' for path '%s'", url_path, spath);
if (!QDesktopServices::openUrl(url))
{
gui_log.error("gui::utils::open_dir: Failed to open dir url '%s' for path '%s'", url_path, spath);
}
}
void open_dir(const QString& path)

View File

@ -210,7 +210,7 @@ void save_manager_dialog::Init()
}
const int idx_real = item->data(Qt::UserRole).toInt();
const QString path = qstr(m_dir + m_save_entries[idx_real].dirName + "/");
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
gui::utils::open_dir(path);
});
connect(slider_icon_size, &QAbstractSlider::valueChanged, this, &save_manager_dialog::SetIconSize);
connect(m_list->horizontalHeader(), &QHeaderView::sectionClicked, this, &save_manager_dialog::OnSort);
@ -488,7 +488,7 @@ void save_manager_dialog::ShowContextMenu(const QPoint &pos)
}
const int idx_real = item->data(Qt::UserRole).toInt();
const QString path = qstr(m_dir + m_save_entries[idx_real].dirName + "/");
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
gui::utils::open_dir(path);
});
menu->exec(m_list->viewport()->mapToGlobal(pos));

View File

@ -734,7 +734,7 @@ void trophy_manager_dialog::ShowTrophyTableContextMenu(const QPoint& pos)
connect(show_trophy_dir, &QAction::triggered, this, [this, db_ind]()
{
const QString path = qstr(m_trophies_db[db_ind]->path);
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
gui::utils::open_dir(path);
});
menu->addAction(show_trophy_dir);
@ -802,7 +802,7 @@ void trophy_manager_dialog::ShowGameTableContextMenu(const QPoint& pos)
connect(show_trophy_dir, &QAction::triggered, this, [this, db_ind]()
{
const QString path = qstr(m_trophies_db[db_ind]->path);
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
gui::utils::open_dir(path);
});
menu->addAction(show_trophy_dir);

View File

@ -17,6 +17,7 @@
#include "main_application.h"
#include "gui_settings.h"
#include "persistent_settings.h"
#include "qt_utils.h"
#include "Emu/System.h"
#include "Emu/system_utils.hpp"
@ -426,7 +427,7 @@ void user_manager_dialog::ShowContextMenu(const QPoint &pos)
connect(show_dir_act, &QAction::triggered, this, [this, key]()
{
const QString path = qstr(m_user_list[key].GetUserDir());
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
gui::utils::open_dir(path);
});
connect(user_id_act, &QAction::triggered, this, [this] {OnSort(0); });