|
|
|
|
#include "PythonAgent.h"
|
|
|
|
|
#include "FITKCore/FITKDirFileTools.h"
|
|
|
|
|
#include <QFile>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QMutex>
|
|
|
|
|
#include <QMutexLocker>
|
|
|
|
|
#include <QWidget>
|
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
#include <QTextStream>
|
|
|
|
|
#include <PythonQt.h>
|
|
|
|
|
#include <PythonQt_QtAll.h>
|
|
|
|
|
#include <PythonQtScriptingConsole.h>
|
|
|
|
|
|
|
|
|
|
namespace Python
|
|
|
|
|
{
|
|
|
|
|
QMutex PythonAgent::m_mutex;
|
|
|
|
|
|
|
|
|
|
PythonAgent::PythonAgent()
|
|
|
|
|
{
|
|
|
|
|
//PythonQt初始化
|
|
|
|
|
PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
|
|
|
|
|
PythonQt_QtAll::init();
|
|
|
|
|
|
|
|
|
|
//PythonQt中Main模块获取
|
|
|
|
|
m_mainModule = PythonQt::self()->getMainModule();
|
|
|
|
|
|
|
|
|
|
//添加Mian模块至可视化界面中
|
|
|
|
|
m_Console = new PythonQtScriptingConsole(nullptr, m_mainModule);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PythonAgent::~PythonAgent()
|
|
|
|
|
{
|
|
|
|
|
if (m_Console != nullptr){
|
|
|
|
|
delete m_Console;
|
|
|
|
|
m_Console = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PythonAgent::addPyQtDecorator(QObject * obj)
|
|
|
|
|
{
|
|
|
|
|
if (obj == nullptr)return;
|
|
|
|
|
PythonQt::self()->addDecorators(obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PythonAgent::registerPyQtCPPClass(const char * typeName, const char * parentTypeName, const char * package)
|
|
|
|
|
{
|
|
|
|
|
//添加互斥锁
|
|
|
|
|
QMutexLocker mutexLoc(&m_mutex);
|
|
|
|
|
|
|
|
|
|
if (classIsExist(typeName)){
|
|
|
|
|
qDebug() << "Failed to register " << typeName << " to PythonQt. The typename already exists!";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
PythonQt::self()->registerCPPClass(typeName, parentTypeName, package);
|
|
|
|
|
m_regClassNames << typeName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PythonAgent::submitScriptFile(const QString &ScriptFile)
|
|
|
|
|
{
|
|
|
|
|
QFile file(ScriptFile);
|
|
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))return;
|
|
|
|
|
QFileInfo fileInfo(file);
|
|
|
|
|
|
|
|
|
|
//获取脚本名称与脚本路径
|
|
|
|
|
QString filePath = fileInfo.absolutePath();
|
|
|
|
|
QString fileName = fileInfo.fileName();
|
|
|
|
|
|
|
|
|
|
//python系统模块导入,添加添加脚本目录
|
|
|
|
|
submitScript("import sys");
|
|
|
|
|
submitScript(QString("sys.path.append(\"%1\")").arg(filePath));
|
|
|
|
|
submitScript(QString("import %1").arg(fileName.remove(".py")));
|
|
|
|
|
file.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PythonAgent::submitScript(const QString &Script)
|
|
|
|
|
{
|
|
|
|
|
//添加互斥锁
|
|
|
|
|
QMutexLocker mutexLoc(&m_mutex);
|
|
|
|
|
|
|
|
|
|
m_Console->insertCompletion(Script);
|
|
|
|
|
m_Console->executeLine(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PythonAgent::submitScript(const QStringList &Scripts)
|
|
|
|
|
{
|
|
|
|
|
//添加互斥锁
|
|
|
|
|
QMutexLocker mutexLoc(&m_mutex);
|
|
|
|
|
|
|
|
|
|
for (auto scr : Scripts){
|
|
|
|
|
m_Console->insertCompletion(scr);
|
|
|
|
|
m_Console->executeLine(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PythonAgent::saveScriptHistory(const QString &filePath)
|
|
|
|
|
{
|
|
|
|
|
//针对目标文件及其目录不存在逻辑处理
|
|
|
|
|
QFile file(filePath);
|
|
|
|
|
if (!file.exists()){
|
|
|
|
|
QFileInfo fileinfo(file);
|
|
|
|
|
QDir dir;
|
|
|
|
|
if (!dir.exists(fileinfo.absolutePath())) Core::CreateDir(fileinfo.absolutePath());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!file.open(QIODevice::WriteOnly))return false;
|
|
|
|
|
|
|
|
|
|
//书写历史脚本至目标文件
|
|
|
|
|
QTextStream fstream(&file);
|
|
|
|
|
for (auto script : m_Console->history()){
|
|
|
|
|
fstream << script << endl;
|
|
|
|
|
}
|
|
|
|
|
file.close();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget * PythonAgent::getPyWidget()
|
|
|
|
|
{
|
|
|
|
|
return m_Console;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PythonAgent::classIsExist(const char * typeName)
|
|
|
|
|
{
|
|
|
|
|
if (m_regClassNames.contains(typeName))return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|