Various fixes to ScriptManager GUI. Handle relative paths more properly.

master
Tomatix 5 years ago
parent 5154ee0d8d
commit fa1d32dbc6
  1. 34
      Script/Manager.cpp
  2. 1
      Script/Manager.h
  3. 8
      Script/ManagerListModel.cpp
  4. 13
      Script/ManagerListModel.h

@ -1,6 +1,5 @@
#include "Manager.h"
#include "ui_Manager.h"
#include "config.h"
#include "MdiManager.h"
#include "IWin/IWinStatus.h"
#include "ConfigMgr.h"
@ -27,7 +26,7 @@ ScriptManager::ScriptManager(MdiManager* mdiManager, QWidget *parent) :
for (const QString& path : cfg.scripts()) {
bool ok = loadFromFile(path);
if (!ok) {
auto btn = QMessageBox::warning(this, tr("Load failed"), tr("The script located at:\n%1\nFailed to load. Keep the entry in the manager?").arg(path),
auto btn = QMessageBox::warning(this, tr("Load script"), tr("The script located at:\n%1\nFailed to load. Keep the entry in the manager?").arg(path),
QMessageBox::Yes | QMessageBox::No);
if (btn == QMessageBox::No) {
m_scripts.pop_back();
@ -94,11 +93,6 @@ ScriptManager* ScriptManager::instance()
return *Instance;
}
MdiManager* ScriptManager::getMdiManager()
{
return (*Instance)->mdiManager;
}
IWinStatus* ScriptManager::getContext()
{
return (*Instance)->m_context;
@ -151,20 +145,11 @@ bool ScriptManager::runCommand(const QString& command)
bool ScriptManager::loadFromFile(const QString& path)
{
auto fixedPath = path;
#if defined(Q_OS_LINUX)
if (path[0] != '/')
fixedPath = QStringLiteral("%1/%2").arg(LOCAL_PATH, path);
#elif defined(Q_OS_WIN32) | defined(Q_OS_WIN64)
if (path[1] != ':')
fixedPath = QStringLiteral("%1/%2").arg(LOCAL_PATH, path);
#else
#error Unsupported platform! Update Script/Manager.cpp (ScriptManager::loadFromFile) accordingly.
#endif
MAKE_FIXED_PATH(path, fixedPath);
QFile f(fixedPath);
if (!f.open(QIODevice::ReadOnly)) {
QMessageBox::warning(this, tr("Open script"), tr("Unable to open script file from\n%1").arg(fixedPath));
QMessageBox::warning(this, tr("Load script"), tr("Unable to open script file from\n%1").arg(fixedPath));
return false;
}
QByteArray data(f.readAll());
@ -236,9 +221,12 @@ void ScriptManager::printError(const std::string& msg)
void ScriptManager::reloadScript(Script& script, const QString& pathToScript)
{
QFile f(pathToScript);
auto fixedPath = pathToScript;
MAKE_FIXED_PATH(pathToScript, fixedPath);
QFile f(fixedPath);
if (!f.open(QIODevice::ReadOnly)) {
QMessageBox::warning(this, tr("Open script"), tr("Unable to open script file from\n%1").arg(pathToScript));
QMessageBox::warning(this, tr("Reload script"), tr("Unable to open script file for reloading\n%1").arg(fixedPath));
m_listModel.markWarning(pathToScript);
return;
}
@ -261,7 +249,7 @@ void ScriptManager::on_btnLoad_clicked()
QModelIndex existingIdx = m_listModel.pathIndex(path);
if (existingIdx.isValid()) {
auto btn = QMessageBox::warning(this, tr("Script already loaded"), tr("The script located at:\n%1\nIs already loaded. Reload script instead?").arg(path),
auto btn = QMessageBox::warning(this, tr("Load script"), tr("The script located at:\n%1\nIs already loaded. Reload script instead?").arg(path),
QMessageBox::Yes | QMessageBox::No);
if (btn == QMessageBox::Yes) {
auto it = m_scripts.begin();
@ -275,7 +263,7 @@ void ScriptManager::on_btnLoad_clicked()
bool ok = loadFromFile(path);
if (!ok) {
auto btn = QMessageBox::warning(this, tr("Load failed"), tr("The script located at:\n%1\nFailed to load. Keep the entry in the manager?").arg(path),
auto btn = QMessageBox::warning(this, tr("Load script"), tr("The script located at:\n%1\nFailed to load. Keep the entry in the manager?").arg(path),
QMessageBox::Yes | QMessageBox::No);
if (btn == QMessageBox::No) {
m_scripts.pop_back();
@ -312,7 +300,7 @@ void ScriptManager::on_btnRemove_clicked()
return;
{
const auto button = QMessageBox::question(this, tr("Confirm unload"), tr("Are you sure you want to unload the selected script?"));
const auto button = QMessageBox::question(this, tr("Unload script"), tr("Are you sure you want to unload the selected script?"));
if (button == QMessageBox::No)
return;
}

@ -32,7 +32,6 @@ public:
static void init(MdiManager* mdiManager, QWidget* parent);
static void setContext(IWinStatus* context);
static ScriptManager* instance();
static MdiManager* getMdiManager();
static IWinStatus* getContext();
~ScriptManager();
void contextMenuPopup(ScriptMenuType type, const QPoint& pos, SymbolScope& symbols);

@ -84,10 +84,16 @@ int ManagerListModel::rowCount(const QModelIndex&) const
int ManagerListModel::findEntry(const QString& entry) const
{
QString entryFixed = entry;
MAKE_FIXED_PATH(entry, entryFixed);
bool found = false;
int idx = 0;
for (const QString& item : m_list) {
if (item == entry) {
QString itemFixed = item;
MAKE_FIXED_PATH(item, itemFixed);
if (itemFixed == entryFixed) {
found = true;
break;
}

@ -3,6 +3,19 @@
#include <QAbstractListModel>
#include <QVector>
#include "config.h"
#if defined(Q_OS_LINUX)
#define ROOT_FILESYSTEM_CHECK(P) (P[0] == '/')
#elif defined(Q_OS_WIN32) | defined(Q_OS_WIN64)
#define ROOT_FILESYSTEM_CHECK(P) (P[1] == ':')
#else
#error Unsupported platform! Update Script/Manager.cpp (MAKE_FIXED_PATH) accordingly.
#endif
#define MAKE_FIXED_PATH(PATH_TO_FILE, FIXED_PATH) \
if (!ROOT_FILESYSTEM_CHECK(PATH_TO_FILE)) \
FIXED_PATH = QStringLiteral("%1/%2").arg(LOCAL_PATH, PATH_TO_FILE)
class ManagerListModel : public QAbstractListModel
{

Loading…
Cancel
Save