mirror of
https://github.com/python-LimeReport/LimeReport.git
synced 2024-12-25 13:04:38 +03:00
151 lines
4.4 KiB
C++
151 lines
4.4 KiB
C++
#include "lrscripteditor.h"
|
|
#include "ui_lrscripteditor.h"
|
|
#include <QJSValueIterator>
|
|
|
|
#include "lrdatasourcemanager.h"
|
|
#include "lrscriptenginemanager.h"
|
|
#include "lrdatadesignintf.h"
|
|
#include "lrreportengine_p.h"
|
|
|
|
namespace LimeReport{
|
|
|
|
ScriptEditor::ScriptEditor(QWidget *parent) :
|
|
QWidget(parent),
|
|
ui(new Ui::ScriptEditor)
|
|
{
|
|
ui->setupUi(this);
|
|
setFocusProxy(ui->textEdit);
|
|
m_completer = new QCompleter(this);
|
|
ui->textEdit->setCompleter(m_completer);
|
|
}
|
|
|
|
ScriptEditor::~ScriptEditor()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void ScriptEditor::setReportEngine(ReportEnginePrivate* reportEngine)
|
|
{
|
|
m_reportEngine = reportEngine;
|
|
DataSourceManager* dm = m_reportEngine->dataManager();
|
|
ScriptEngineManager& se = LimeReport::ScriptEngineManager::instance();
|
|
se.setDataManager(dm);
|
|
|
|
initCompleter();
|
|
|
|
if (dm){
|
|
if (dm->isNeedUpdateDatasourceModel())
|
|
dm->updateDatasourceModel();
|
|
ui->twData->setModel(dm->datasourcesModel());
|
|
ui->twScriptEngine->setModel(se.model());
|
|
}
|
|
|
|
}
|
|
|
|
void ScriptEditor::initCompleter()
|
|
{
|
|
QStringList dataWords;
|
|
DataSourceManager* dm = m_reportEngine->dataManager();
|
|
ScriptEngineManager& se = LimeReport::ScriptEngineManager::instance();
|
|
|
|
QJSValue globalObject = se.scriptEngine()->globalObject();
|
|
QJSValueIterator it(globalObject);
|
|
while (it.hasNext()){
|
|
it.next();
|
|
if (it.value().isCallable() ){
|
|
dataWords << it.name();
|
|
}
|
|
}
|
|
|
|
foreach(const QString &dsName,dm->dataSourceNames()){
|
|
dataWords << dsName;
|
|
foreach(const QString &field, dm->fieldNames(dsName)){
|
|
dataWords<<dsName+"."+field;
|
|
}
|
|
}
|
|
|
|
foreach (QString varName, dm->variableNames()) {
|
|
dataWords << varName.remove("#");
|
|
}
|
|
|
|
for ( int i = 0; i < m_reportEngine->pageCount(); ++i){
|
|
PageDesignIntf* page = m_reportEngine->pageAt(i);
|
|
dataWords << page->pageItem()->objectName();
|
|
foreach (BaseDesignIntf* item, page->pageItem()->childBaseItems()){
|
|
addItemToCompleater(page->pageItem()->objectName(), item, dataWords);
|
|
}
|
|
}
|
|
|
|
m_completer->setModel(new QStringListModel(dataWords,m_completer));
|
|
}
|
|
|
|
QByteArray ScriptEditor::saveState()
|
|
{
|
|
return ui->splitter->saveState();
|
|
}
|
|
|
|
void ScriptEditor::restoreState(QByteArray state)
|
|
{
|
|
ui->splitter->restoreState(state);
|
|
}
|
|
|
|
void ScriptEditor::setPlainText(const QString& text)
|
|
{
|
|
ui->textEdit->setPlainText(text);
|
|
}
|
|
|
|
QString ScriptEditor::toPlainText()
|
|
{
|
|
return ui->textEdit->toPlainText();
|
|
}
|
|
|
|
void ScriptEditor::addItemToCompleater(const QString& pageName, BaseDesignIntf* item, QStringList& dataWords)
|
|
{
|
|
BandDesignIntf* band = dynamic_cast<BandDesignIntf*>(item);
|
|
if (band){
|
|
dataWords << band->objectName();
|
|
dataWords << pageName+"_"+band->objectName();
|
|
dataWords << pageName+"_"+band->objectName()+".beforeRender";
|
|
dataWords << pageName+"_"+item->objectName()+".afterData";
|
|
dataWords << pageName+"_"+band->objectName()+".afterRender";
|
|
foreach (BaseDesignIntf* child, band->childBaseItems()){
|
|
addItemToCompleater(pageName, child, dataWords);
|
|
}
|
|
} else {
|
|
dataWords << item->objectName();
|
|
dataWords << pageName+"_"+item->objectName();
|
|
dataWords << pageName+"_"+item->objectName()+".beforeRender";
|
|
dataWords << pageName+"_"+item->objectName()+".afterData";
|
|
dataWords << pageName+"_"+item->objectName()+".afterRender";
|
|
}
|
|
}
|
|
|
|
void ScriptEditor::on_twData_doubleClicked(const QModelIndex &index)
|
|
{
|
|
if (!index.isValid()) return;
|
|
LimeReport::DataNode* node = static_cast<LimeReport::DataNode*>(index.internalPointer());
|
|
if (node->type()==LimeReport::DataNode::Field){
|
|
ui->textEdit->insertPlainText(QString("$D{%1.%2}").arg(node->parent()->name()).arg(node->name()));
|
|
}
|
|
if (node->type()==LimeReport::DataNode::Variable){
|
|
ui->textEdit->insertPlainText(QString("$V{%1}").arg(node->name()));
|
|
}
|
|
ui->textEdit->setFocus();
|
|
}
|
|
|
|
void ScriptEditor::on_twScriptEngine_doubleClicked(const QModelIndex &index)
|
|
{
|
|
if (!index.isValid()) return;
|
|
LimeReport::ScriptEngineNode* node = static_cast<LimeReport::ScriptEngineNode*>(index.internalPointer());
|
|
if (node->type()==LimeReport::ScriptEngineNode::Function){
|
|
ui->textEdit->insertPlainText(node->name()+"()");
|
|
}
|
|
ui->textEdit->setFocus();
|
|
}
|
|
|
|
} // namespace LimeReport
|
|
|
|
|
|
|
|
|