amuse/Editor/Common.cpp

100 lines
3.1 KiB
C++
Raw Permalink Normal View History

#include "Common.hpp"
2018-08-04 02:31:47 +00:00
#include "MainWindow.hpp"
#include <QDir>
#include <QMessageBox>
#include <QObject>
2018-07-31 08:04:43 +00:00
#include <QProcess>
#include <QTransform>
2018-12-08 05:20:09 +00:00
boo::SystemString QStringToSysString(const QString& str) {
#ifdef _WIN32
2018-12-08 05:20:09 +00:00
return (wchar_t*)str.utf16();
#else
2018-12-08 05:20:09 +00:00
return str.toUtf8().toStdString();
#endif
}
2018-12-08 05:20:09 +00:00
QString SysStringToQString(const boo::SystemString& str) {
#ifdef _WIN32
2018-12-08 05:20:09 +00:00
return QString::fromStdWString(str);
#else
2018-12-08 05:20:09 +00:00
return QString::fromStdString(str);
#endif
}
2018-12-08 05:20:09 +00:00
bool MkPath(const QString& path, UIMessenger& messenger) {
QFileInfo fInfo(path);
return MkPath(fInfo.dir(), fInfo.fileName(), messenger);
}
2018-12-08 05:20:09 +00:00
bool MkPath(const QDir& dir, const QString& file, UIMessenger& messenger) {
if (!dir.mkpath(file)) {
QString msg = QString(MainWindow::tr("A directory at '%1/%2' could not be created.")).arg(dir.path()).arg(file);
messenger.critical(MainWindow::tr("Unable to create directory"), msg);
return false;
}
return true;
}
2018-07-31 08:04:43 +00:00
2018-12-08 05:20:09 +00:00
void ShowInGraphicalShell(QWidget* parent, const QString& pathIn) {
const QFileInfo fileInfo(pathIn);
// Mac, Windows support folder or file.
2018-07-31 08:04:43 +00:00
#if defined(Q_OS_WIN)
2018-12-08 05:20:09 +00:00
QString paths = QProcessEnvironment::systemEnvironment().value(QStringLiteral("Path"));
QString explorer;
for (QString path : paths.split(QStringLiteral(";"))) {
QFileInfo finfo(QDir(path), QStringLiteral("explorer.exe"));
if (finfo.exists()) {
explorer = finfo.filePath();
break;
2018-07-31 08:04:43 +00:00
}
2018-12-08 05:20:09 +00:00
}
if (explorer.isEmpty()) {
QMessageBox::warning(parent, MainWindow::tr("Launching Windows Explorer Failed"),
MainWindow::tr("Could not find explorer.exe in path to launch Windows Explorer."));
return;
}
QStringList param;
if (!fileInfo.isDir())
param += QLatin1String("/select,");
param += QDir::toNativeSeparators(fileInfo.canonicalFilePath());
QProcess::startDetached(explorer, param);
2018-07-31 08:04:43 +00:00
#elif defined(Q_OS_MAC)
2018-12-08 05:20:09 +00:00
QStringList scriptArgs;
scriptArgs << QLatin1String("-e")
<< QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
.arg(fileInfo.canonicalFilePath());
QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
scriptArgs.clear();
scriptArgs << QLatin1String("-e") << QLatin1String("tell application \"Finder\" to activate");
QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
2018-07-31 08:04:43 +00:00
#else
2018-12-08 05:20:09 +00:00
// we cannot select a file here, because no file browser really supports it...
const QString folder = fileInfo.isDir() ? fileInfo.absoluteFilePath() : fileInfo.filePath();
QProcess browserProc;
2020-09-08 23:23:55 +00:00
const QStringList browserArgs = QStringList() << QStringLiteral("%1").arg(QFileInfo(folder).path());
browserProc.startDetached(QStringLiteral("xdg-open"), browserArgs);
2018-07-31 08:04:43 +00:00
#endif
}
2018-12-08 05:20:09 +00:00
QString ShowInGraphicalShellString() {
2018-07-31 08:04:43 +00:00
#if defined(Q_OS_WIN)
2018-12-08 05:20:09 +00:00
return MainWindow::tr("Show in Explorer");
2018-07-31 08:04:43 +00:00
#elif defined(Q_OS_MAC)
2018-12-08 05:20:09 +00:00
return MainWindow::tr("Show in Finder");
2018-07-31 08:04:43 +00:00
#else
2018-12-08 05:20:09 +00:00
return MainWindow::tr("Show in Browser");
2018-07-31 08:04:43 +00:00
#endif
}
2018-08-06 04:20:42 +00:00
2018-12-08 05:20:09 +00:00
QTransform RectToRect(const QRectF& from, const QRectF& to) {
QPolygonF orig(from);
orig.pop_back();
QPolygonF resize(to);
resize.pop_back();
QTransform ret;
QTransform::quadToQuad(orig, resize, ret);
return ret;
2018-08-06 04:20:42 +00:00
}