|
|
|
|
#include "FITKPythonInterface.h"
|
|
|
|
|
#include "PythonAgent.h"
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QFile>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QTextStream>
|
|
|
|
|
#include <QMutex>
|
|
|
|
|
#include <QMutexLocker>
|
|
|
|
|
|
|
|
|
|
namespace Python
|
|
|
|
|
{
|
|
|
|
|
QMutex FITKPythonInterface::m_mutex;
|
|
|
|
|
FITKPythonInterface* FITKPythonInterface::m_interface = nullptr;
|
|
|
|
|
|
|
|
|
|
FITKPythonInterface* FITKPythonInterface::getInstance()
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker muLocker(&m_mutex);
|
|
|
|
|
if (m_interface == nullptr)
|
|
|
|
|
{
|
|
|
|
|
m_interface = new FITKPythonInterface();
|
|
|
|
|
}
|
|
|
|
|
return m_interface;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FITKPythonInterface::addDecorator(QObject * obj)
|
|
|
|
|
{
|
|
|
|
|
//pythonQt中添加装饰器
|
|
|
|
|
if (obj == nullptr)return;
|
|
|
|
|
m_pyAgent->addPyQtDecorator(obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FITKPythonInterface::registerCPPClass(const char * typeName, const char * parentTypeName, const char * package)
|
|
|
|
|
{
|
|
|
|
|
//注册c++类
|
|
|
|
|
m_pyAgent->registerPyQtCPPClass(typeName, parentTypeName, package);
|
|
|
|
|
|
|
|
|
|
//python中导入注册的模块
|
|
|
|
|
m_pyAgent->submitScript(QString("from PythonQt.%1 import %2").arg(package).arg(typeName));
|
|
|
|
|
// importPython(package);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FITKPythonInterface::submit(const QString &script)
|
|
|
|
|
{
|
|
|
|
|
//单行脚本请求
|
|
|
|
|
m_pyAgent->submitScript(script);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FITKPythonInterface::submit(const QStringList &scripts)
|
|
|
|
|
{
|
|
|
|
|
//多行脚本请求
|
|
|
|
|
m_pyAgent->submitScript(scripts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FITKPythonInterface::submitFile(const QString &filePath)
|
|
|
|
|
{
|
|
|
|
|
//脚本文件请求
|
|
|
|
|
m_pyAgent->submitScriptFile(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FITKPythonInterface::saveScriptHistory(const QString &filePath)
|
|
|
|
|
{
|
|
|
|
|
return m_pyAgent->saveScriptHistory(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FITKPythonInterface::importPython(const QString package)
|
|
|
|
|
{
|
|
|
|
|
//导入python模块
|
|
|
|
|
m_pyAgent->submitScript(QString("from PythonQt import %1").arg(package));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget * FITKPythonInterface::getPyWidget()
|
|
|
|
|
{
|
|
|
|
|
return m_pyAgent->getPyWidget();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FITKPythonInterface::FITKPythonInterface()
|
|
|
|
|
{
|
|
|
|
|
//PythonQt代理类对象创建
|
|
|
|
|
m_pyAgent = new PythonAgent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FITKPythonInterface::~FITKPythonInterface()
|
|
|
|
|
{
|
|
|
|
|
if (m_pyAgent != nullptr)
|
|
|
|
|
{
|
|
|
|
|
delete m_pyAgent;
|
|
|
|
|
m_pyAgent = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|