fix openDirectory

This commit is contained in:
SamGondelman 2019-04-25 12:24:02 -07:00
parent 31b3f0e8f1
commit 0e4ea4aff2
4 changed files with 29 additions and 8 deletions

View file

@ -2443,6 +2443,7 @@ Application::Application(int& argc, char** argv, QElapsedTimer& startupTimer, bo
DependencyManager::get<TabletScriptingInterface>()->preloadSounds(); DependencyManager::get<TabletScriptingInterface>()->preloadSounds();
DependencyManager::get<Keyboard>()->createKeyboard(); DependencyManager::get<Keyboard>()->createKeyboard();
FileDialogHelper::setOpenDirectoryOperator([this](const QString& path) { openDirectory(path); });
QDesktopServices::setUrlHandler("file", this, "showUrlHandler"); QDesktopServices::setUrlHandler("file", this, "showUrlHandler");
QDesktopServices::setUrlHandler("", this, "showUrlHandler"); QDesktopServices::setUrlHandler("", this, "showUrlHandler");
auto drives = QDir::drives(); auto drives = QDir::drives();
@ -9272,6 +9273,26 @@ QString Application::getGraphicsCardType() {
return GPUIdent::getInstance()->getName(); return GPUIdent::getInstance()->getName();
} }
void Application::openDirectory(const QString& path) {
if (QThread::currentThread() != thread()) {
QMetaObject::invokeMethod(this, "openDirectory", Q_ARG(const QString&, path));
return;
}
QString dirPath = path;
const QString FILE_SCHEME = "file:///";
if (dirPath.startsWith(FILE_SCHEME)) {
dirPath.remove(0, FILE_SCHEME.length());
}
QFileInfo fileInfo(dirPath);
if (fileInfo.isDir()) {
auto scheme = QUrl(path).scheme();
QDesktopServices::unsetUrlHandler(scheme);
QDesktopServices::openUrl(path);
QDesktopServices::setUrlHandler(scheme, this, "showUrlHandler");
}
}
void Application::showUrlHandler(const QUrl& url) { void Application::showUrlHandler(const QUrl& url) {
if (QThread::currentThread() != thread()) { if (QThread::currentThread() != thread()) {
QMetaObject::invokeMethod(this, "showUrlHandler", Q_ARG(const QUrl&, url)); QMetaObject::invokeMethod(this, "showUrlHandler", Q_ARG(const QUrl&, url));

View file

@ -345,6 +345,8 @@ public:
void toggleAwayMode(); void toggleAwayMode();
#endif #endif
void openDirectory(const QString& path);
signals: signals:
void svoImportRequested(const QString& url); void svoImportRequested(const QString& url);

View file

@ -17,6 +17,7 @@
#include <QtCore/QRegularExpression> #include <QtCore/QRegularExpression>
#include <QDesktopServices> #include <QDesktopServices>
std::function<void(const QString&)> FileDialogHelper::_openDirectoryOperator = nullptr;
QUrl FileDialogHelper::home() { QUrl FileDialogHelper::home() {
return pathToUrl(QStandardPaths::standardLocations(QStandardPaths::HomeLocation)[0]); return pathToUrl(QStandardPaths::standardLocations(QStandardPaths::HomeLocation)[0]);
@ -111,14 +112,8 @@ QStringList FileDialogHelper::drives() {
} }
void FileDialogHelper::openDirectory(const QString& path) { void FileDialogHelper::openDirectory(const QString& path) {
QString dirPath = path; if (_openDirectoryOperator) {
const QString FILE_SCHEME = "file://"; _openDirectoryOperator(path);
if (dirPath.startsWith(FILE_SCHEME)) {
dirPath.remove(0, FILE_SCHEME.length());
}
QFileInfo fileInfo(dirPath);
if (fileInfo.isDir()) {
QDesktopServices::openUrl(path);
} }
} }

View file

@ -16,6 +16,7 @@
#include <QtCore/QStringList> #include <QtCore/QStringList>
#include <QtCore/QUrl> #include <QtCore/QUrl>
#include <functional>
class FileDialogHelper : public QObject { class FileDialogHelper : public QObject {
Q_OBJECT Q_OBJECT
@ -62,6 +63,7 @@ public:
Q_INVOKABLE QUrl saveHelper(const QString& saveText, const QUrl& currentFolder, const QStringList& selectionFilters); Q_INVOKABLE QUrl saveHelper(const QString& saveText, const QUrl& currentFolder, const QStringList& selectionFilters);
Q_INVOKABLE QList<QUrl> urlToList(const QUrl& url); Q_INVOKABLE QList<QUrl> urlToList(const QUrl& url);
static void setOpenDirectoryOperator(std::function<void(const QString&)> openDirectoryOperator) { _openDirectoryOperator = openDirectoryOperator; }
Q_INVOKABLE void openDirectory(const QString& path); Q_INVOKABLE void openDirectory(const QString& path);
Q_INVOKABLE void monitorDirectory(const QString& path); Q_INVOKABLE void monitorDirectory(const QString& path);
@ -72,6 +74,7 @@ signals:
private: private:
QFileSystemWatcher _fsWatcher; QFileSystemWatcher _fsWatcher;
QString _fsWatcherPath; QString _fsWatcherPath;
static std::function<void(const QString&)> _openDirectoryOperator;
}; };