diff --git a/batchprocessing.cpp b/batchprocessing.cpp index f2d369e..fad976b 100644 --- a/batchprocessing.cpp +++ b/batchprocessing.cpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include "scriptengine.h" #ifdef Q_OS_LINUX @@ -170,7 +172,7 @@ void BatchProcessing::runScript() if(selectedItems.size()) { _engineThread = new Script::ScriptEngineThread(this); - connect(_engineThread, &Script::ScriptEngineThread::newMessage, _ui->log, &QTextEdit::append); + connect(_engineThread, &Script::ScriptEngineThread::newMessage, this, &BatchProcessing::newMessage); connect(_engineThread, &Script::ScriptEngineThread::finished, this, &BatchProcessing::scriptFinished); QStringList paths; for(int i=0; i<_ui->pathsList->count(); i++) @@ -206,3 +208,11 @@ void BatchProcessing::scriptFinished() delete _engineThread; _engineThread = nullptr; } + +void BatchProcessing::newMessage(const QString &message, bool error) +{ + QColor color = _ui->log->textColor(); + if(error)_ui->log->setTextColor(Qt::red); + _ui->log->append(message); + if(error)_ui->log->setTextColor(color); +} diff --git a/batchprocessing.h b/batchprocessing.h index 6abe9d7..859ea63 100644 --- a/batchprocessing.h +++ b/batchprocessing.h @@ -31,6 +31,7 @@ public slots: void runScript(); void stopScript(); void scriptFinished(); + void newMessage(const QString &message, bool error); }; #endif // BATCHPROCESSING_H diff --git a/mainwindow.cpp b/mainwindow.cpp index 82bcd7c..167ba2d 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -152,11 +152,11 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) fileMenu->addAction(tr("Index directory"), this, SLOT(indexDir())); fileMenu->addAction(tr("Reindex files"), this, SLOT(reindex())); fileMenu->addAction(tr("Export database to CSV"), this, &MainWindow::exportCSV); - fileMenu->addAction(tr("Batch processing"), [this](){ + /*fileMenu->addAction(tr("Batch processing"), [this](){ BatchProcessing *batchProcessing = new BatchProcessing(this); batchProcessing->exec(); delete batchProcessing; - }, Qt::Key_B | Qt::CTRL); + }, Qt::Key_B | Qt::CTRL);*/ fileMenu->addSeparator(); QAction *liveModeAction = fileMenu->addAction(tr("Live mode"), this, SLOT(liveMode(bool))); liveModeAction->setCheckable(true); diff --git a/scriptengine.cpp b/scriptengine.cpp index 55e8e18..aa8ce80 100644 --- a/scriptengine.cpp +++ b/scriptengine.cpp @@ -20,7 +20,7 @@ void ScriptEngine::setParams(const QString &scriptPath, const QStringList &paths { _scriptPath = scriptPath; _paths = paths; - _outputDir = outputDir; + _outputDir = outputDir + "/"; } void ScriptEngine::reportError(const QString &message) @@ -38,9 +38,14 @@ void ScriptEngine::interrupt() _jsEngine->setInterrupted(true); } +void ScriptEngine::logError(const QString &message) +{ + emit newMessage(message, true); +} + void ScriptEngine::log(const QString &message) { - emit newMessage(message); + emit newMessage(message, false); } void ScriptEngine::mark(File *file) @@ -79,7 +84,7 @@ void ScriptEngine::run() { QString error = result.property("name").toString() + " on line " + result.property("lineNumber").toString() + " : " + result.toString(); error += "\n" + result.property("stack").toString(); - emit newMessage(error); + emit newMessage(error, true); } emit finished(); @@ -108,6 +113,26 @@ void File::loadFitsKeywords() } } +bool File::mkpath(const QString &path) const +{ + QFileInfo info(path); + if(!info.isRelative()) + { + _engine->logError("Destination path is not relative"); + return false; + } + QDir dir(_engine->outputDir()); + if(dir.mkpath(info.path())) + { + return true; + } + else + { + _engine->logError("Failed to create dir " + info.path()); + return false; + } +} + File::File(const QString &path, Script::ScriptEngine *engine) : _engine(engine), _path(path), @@ -173,12 +198,26 @@ bool File::isMarked() const bool File::copy(const QString &newpath) const { - return QFile::copy(_path, _engine->outputDir() + newpath); + if(mkpath(newpath)) + { + if(QFile::copy(_path, _engine->outputDir() + newpath)) + return true; + _engine->logError("Failed copy to " + newpath); + return false; + } + return false; } bool File::move(const QString &newpath) const { - return QFile::rename(_path, _engine->outputDir() + newpath); + if(mkpath(newpath)) + { + if(QFile::rename(_path, _engine->outputDir() + newpath)) + return true; + _engine->logError("Failed move to " + newpath); + return false; + } + return false; } bool File::convertTo(const QString &format) diff --git a/scriptengine.h b/scriptengine.h index 8cdcd5b..b823406 100644 --- a/scriptengine.h +++ b/scriptengine.h @@ -26,6 +26,7 @@ public: void reportError(const QString &message); const QString& outputDir() const; void interrupt(); + void logError(const QString &message); Q_INVOKABLE void log(const QString &message); Q_INVOKABLE void mark(File *file); Q_INVOKABLE void unmark(File *file); @@ -33,7 +34,7 @@ public: public slots: void run(); signals: - void newMessage(const QString &message); + void newMessage(const QString &message, bool error); void finished(); }; @@ -49,7 +50,7 @@ public: void start(); void interrupt(); signals: - void newMessage(const QString &message); + void newMessage(const QString &message, bool error); void finished(); }; @@ -62,6 +63,7 @@ class File : public QObject bool _fitsKeywordsLoaded = false; QMap _fitsKeywords; void loadFitsKeywords(); + bool mkpath(const QString &path) const; public: explicit File(const QString &path, ScriptEngine *engine); Q_INVOKABLE QString fileName() const;