2023-05-08 06:32:41 +00:00
|
|
|
|
#include <Python.h>
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QDir>
|
2023-06-07 02:15:08 +00:00
|
|
|
|
#include <QFile>
|
|
|
|
|
#include <QString>
|
|
|
|
|
#include <QTextStream>
|
|
|
|
|
#include <wchar.h>
|
|
|
|
|
#include "PyAgent.h"
|
2023-05-08 06:32:41 +00:00
|
|
|
|
#include "MainWindow/MainWindow.h"
|
|
|
|
|
#include "PyInterpreter.h"
|
|
|
|
|
#include "RecordScript.h"
|
|
|
|
|
#include "ScriptReader.h"
|
|
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
|
#include <atlconv.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <locale.h>
|
|
|
|
|
|
2023-06-07 02:15:08 +00:00
|
|
|
|
namespace Py {
|
|
|
|
|
PythonAgent* PythonAgent::_instance = nullptr;
|
2023-05-08 06:32:41 +00:00
|
|
|
|
|
2023-06-07 02:15:08 +00:00
|
|
|
|
PythonAgent* PythonAgent::getInstance()
|
2023-05-08 06:32:41 +00:00
|
|
|
|
{
|
2023-06-07 02:15:08 +00:00
|
|
|
|
if(_instance == nullptr)
|
2023-05-08 06:32:41 +00:00
|
|
|
|
_instance = new PythonAgent;
|
|
|
|
|
|
|
|
|
|
return _instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PythonAgent::connectSignals()
|
|
|
|
|
{
|
2023-06-07 02:15:08 +00:00
|
|
|
|
connect(this, SIGNAL(printInfo(Common::Message, QString)), _mainWindow,
|
|
|
|
|
SIGNAL(printMessageSig(Common::Message, QString)));
|
2023-05-08 06:32:41 +00:00
|
|
|
|
connect(this, SIGNAL(closeMainWindow()), _mainWindow, SIGNAL(closeMainWindow()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PythonAgent::appCodeList(QString code)
|
|
|
|
|
{
|
2023-06-07 02:15:08 +00:00
|
|
|
|
if(!_append)
|
2023-05-08 06:32:41 +00:00
|
|
|
|
return;
|
|
|
|
|
emit printInfo(Common::Message::Python, code);
|
|
|
|
|
_interpreter->codeListAppend(code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PythonAgent::PythonAgent()
|
|
|
|
|
{
|
|
|
|
|
_interpreter = new PyInterpreter;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-07 02:15:08 +00:00
|
|
|
|
void PythonAgent::initialize(GUI::MainWindow* m)
|
2023-05-08 06:32:41 +00:00
|
|
|
|
{
|
|
|
|
|
_mainWindow = m;
|
|
|
|
|
connectSignals();
|
|
|
|
|
Py_SetProgramName(L"FastCAE");
|
|
|
|
|
Py_Initialize();
|
2023-06-07 02:15:08 +00:00
|
|
|
|
if(!_interpreter->init(this))
|
2023-05-08 06:32:41 +00:00
|
|
|
|
emit printInfo(Common::Message::Error, tr("Python Initialize failed!"));
|
|
|
|
|
else
|
|
|
|
|
emit printInfo(Common::Message::Normal, tr("Python Initialized"));
|
|
|
|
|
_recordScript = new RecordThread;
|
|
|
|
|
_recordScript->start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PythonAgent::finalize()
|
|
|
|
|
{
|
2023-06-07 02:15:08 +00:00
|
|
|
|
if(_reader != nullptr) {
|
|
|
|
|
if(_reader->isRunning()) {
|
2023-05-08 06:32:41 +00:00
|
|
|
|
_reader->stop();
|
|
|
|
|
_reader->quit();
|
|
|
|
|
_reader->wait();
|
|
|
|
|
}
|
2023-06-07 02:15:08 +00:00
|
|
|
|
while(_reader->isRunning())
|
2023-05-08 06:32:41 +00:00
|
|
|
|
;
|
|
|
|
|
delete _reader;
|
|
|
|
|
_reader = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_recordScript->stop();
|
|
|
|
|
_recordScript->quit();
|
|
|
|
|
_recordScript->wait();
|
|
|
|
|
delete _recordScript;
|
|
|
|
|
|
2023-06-07 02:15:08 +00:00
|
|
|
|
if(_interpreter != nullptr)
|
2023-05-08 06:32:41 +00:00
|
|
|
|
delete _interpreter;
|
|
|
|
|
|
2023-06-07 02:15:08 +00:00
|
|
|
|
if(Py_IsInitialized())
|
2023-05-08 06:32:41 +00:00
|
|
|
|
Py_Finalize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PythonAgent::submit(QString code, bool s)
|
|
|
|
|
{
|
|
|
|
|
emit printInfo(Common::Message::Python, code);
|
|
|
|
|
// lock();
|
2023-06-07 02:15:08 +00:00
|
|
|
|
int ok = _interpreter->execCode(code, s);
|
|
|
|
|
if(ok == -1) {
|
|
|
|
|
if(_reader != nullptr)
|
2023-05-08 06:32:41 +00:00
|
|
|
|
_reader->restart();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PythonAgent::submit(QStringList codes, bool save /*= true*/)
|
|
|
|
|
{
|
|
|
|
|
const int n = codes.size();
|
2023-06-07 02:15:08 +00:00
|
|
|
|
for(int i = 0; i < n; ++i) {
|
2023-05-08 06:32:41 +00:00
|
|
|
|
this->submit(codes.at(i), save);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PythonAgent::saveScript(QString fileName)
|
|
|
|
|
{
|
|
|
|
|
QFile file(fileName);
|
2023-06-07 02:15:08 +00:00
|
|
|
|
if(!file.open(QIODevice::Text | QIODevice::WriteOnly)) {
|
2023-05-08 06:32:41 +00:00
|
|
|
|
emit printInfo(Common::Message::Error, tr("Script open failed"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
QTextStream stream(&file);
|
2023-06-07 02:15:08 +00:00
|
|
|
|
const int n = _interpreter->getCodeCount();
|
|
|
|
|
for(int i = 0; i < n; ++i) {
|
2023-05-08 06:32:41 +00:00
|
|
|
|
QString s = _interpreter->getCodeAt(i);
|
|
|
|
|
stream << s << endl;
|
|
|
|
|
}
|
|
|
|
|
file.close();
|
|
|
|
|
emit printInfo(Common::Message::Normal, tr("Script Saved %1").arg(fileName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PythonAgent::execScript(QString fileName)
|
|
|
|
|
{
|
2023-06-07 02:15:08 +00:00
|
|
|
|
if(_reader != nullptr)
|
2023-05-08 06:32:41 +00:00
|
|
|
|
return false;
|
|
|
|
|
_reader = new ScriptReader(fileName, this);
|
|
|
|
|
_recordScript->pause();
|
|
|
|
|
connect(_reader, SIGNAL(finished()), this, SLOT(readerFinished()));
|
|
|
|
|
_reader->start();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PythonAgent::readerFinished()
|
|
|
|
|
{
|
2023-06-07 02:15:08 +00:00
|
|
|
|
if(_reader != nullptr)
|
2023-05-08 06:32:41 +00:00
|
|
|
|
delete _reader;
|
|
|
|
|
_reader = nullptr;
|
|
|
|
|
_recordScript->reStart();
|
2023-06-07 02:15:08 +00:00
|
|
|
|
if(_noGUI)
|
2023-05-08 06:32:41 +00:00
|
|
|
|
emit closeMainWindow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PythonAgent::lock()
|
|
|
|
|
{
|
2023-06-07 02:15:08 +00:00
|
|
|
|
if(_reader != nullptr)
|
2023-05-08 06:32:41 +00:00
|
|
|
|
_reader->pause();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PythonAgent::unLock()
|
|
|
|
|
{
|
2023-06-07 02:15:08 +00:00
|
|
|
|
if(_reader != nullptr)
|
2023-05-08 06:32:41 +00:00
|
|
|
|
_reader->restart();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PythonAgent::isLocked()
|
|
|
|
|
{
|
|
|
|
|
return _islock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList PythonAgent::getcodelist()
|
|
|
|
|
{
|
2023-06-07 02:15:08 +00:00
|
|
|
|
if(_interpreter != nullptr)
|
2023-05-08 06:32:41 +00:00
|
|
|
|
return _interpreter->getCode();
|
|
|
|
|
return QStringList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PythonAgent::setNoGUI(bool nogui)
|
|
|
|
|
{
|
|
|
|
|
_noGUI = nogui;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PythonAgent::appendOn()
|
|
|
|
|
{
|
|
|
|
|
_append = true;
|
|
|
|
|
}
|
|
|
|
|
void PythonAgent::appendOff()
|
|
|
|
|
{
|
|
|
|
|
_append = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PythonAgent::execMessWinCode(QString code)
|
|
|
|
|
{
|
|
|
|
|
_interpreter->execCode(code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PythonAgent::backstageExec(QString code)
|
|
|
|
|
{
|
|
|
|
|
_interpreter->execCode(code, false);
|
|
|
|
|
}
|
2023-06-07 02:15:08 +00:00
|
|
|
|
} // namespace Py
|