0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-25 00:54:39 +03:00
LimeReport/limereport/scripteditor/lrscripteditor.cpp

350 lines
11 KiB
C++
Raw Normal View History

2017-09-13 17:16:54 +03:00
#include "lrscripteditor.h"
#include "ui_lrscripteditor.h"
2017-10-26 12:24:06 +03:00
#ifdef USE_QJSENGINE
2017-09-13 17:16:54 +03:00
#include <QJSValueIterator>
2017-10-26 12:24:06 +03:00
#endif
2017-09-13 17:16:54 +03:00
#include "lrdatasourcemanager.h"
#include "lrscriptenginemanager.h"
#include "lrdatadesignintf.h"
#include "lrreportengine_p.h"
namespace LimeReport{
ScriptEditor::ScriptEditor(QWidget *parent) :
QWidget(parent),
2017-09-21 00:44:09 +03:00
ui(new Ui::ScriptEditor), m_reportEngine(0), m_page(0)
2017-09-13 17:16:54 +03:00
{
ui->setupUi(this);
2017-09-20 00:51:17 +03:00
setFocusProxy(ui->textEdit);
m_completer = new ReportStructureCompleater(this);
2017-09-13 17:16:54 +03:00
ui->textEdit->setCompleter(m_completer);
2017-09-21 00:44:09 +03:00
connect(ui->splitter, SIGNAL(splitterMoved(int,int)), this, SIGNAL(splitterMoved(int,int)));
2017-09-13 17:16:54 +03:00
}
ScriptEditor::~ScriptEditor()
{
delete ui;
}
2017-10-05 01:27:04 +03:00
void ScriptEditor::initEditor(DataSourceManager* dm)
2017-09-13 17:16:54 +03:00
{
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());
}
2017-10-05 01:27:04 +03:00
if (ui->twScriptEngine->selectionModel()){
connect(ui->twScriptEngine->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
this, SLOT(slotOnCurrentChanged(QModelIndex,QModelIndex)));
}
}
2017-11-24 00:13:47 +03:00
void ScriptEditor::setReportEngine(ReportEnginePrivateInterface* reportEngine)
2017-10-05 01:27:04 +03:00
{
m_reportEngine = reportEngine;
DataSourceManager* dm = m_reportEngine->dataManager();
initEditor(dm);
2017-09-13 17:16:54 +03:00
}
2017-09-21 00:44:09 +03:00
void ScriptEditor::setReportPage(PageDesignIntf* page)
{
m_page = page;
DataSourceManager* dm = page->datasourceManager();
2017-10-05 01:27:04 +03:00
initEditor(dm);
2017-09-21 00:44:09 +03:00
}
void ScriptEditor::setPageBand(BandDesignIntf* band)
{
if (band && ui->twData->model() && !band->datasourceName().isEmpty()){
QModelIndexList nodes = ui->twData->model()->match(
ui->twData->model()->index(0,0),
Qt::DisplayRole,
band->datasourceName(),
2,
Qt::MatchRecursive
);
if (!nodes.isEmpty()){
ui->twData->expand(nodes.at(0).parent());
ui->twData->expand(nodes.at(0));
}
}
}
2017-09-13 17:16:54 +03:00
void ScriptEditor::initCompleter()
{
// QStringList dataWords;
// DataSourceManager* dm = 0;
// if (m_reportEngine)
// dm = m_reportEngine->dataManager();
// if (m_page)
// dm = m_page->datasourceManager();
//#ifdef USE_QJSENGINE
// 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();
// }
// }
//#endif
// 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("#");
// }
// if (m_reportEngine){
// for ( int i = 0; i < m_reportEngine->pageCount(); ++i){
// PageDesignIntf* page = m_reportEngine->pageAt(i);
// dataWords << page->pageItem()->objectName();
// QMetaObject const * mo = page->pageItem()->metaObject();
// for(int i = mo->methodOffset(); i < mo->methodCount(); ++i)
// {
// if (mo->method(i).methodType() == QMetaMethod::Signal) {
// dataWords << page->pageItem()->objectName() +"."+QString::fromLatin1(mo->method(i).name());
// }
// }
// dataWords << page->pageItem()->objectName()+".beforeRender";
// dataWords << page->pageItem()->objectName()+".afterRender";
// foreach (BaseDesignIntf* item, page->pageItem()->childBaseItems()){
// addItemToCompleater(page->pageItem()->objectName(), item, dataWords);
// }
// }
// }
// dataWords.sort();
2017-09-21 00:44:09 +03:00
if (m_reportEngine)
m_completer->updateCompleaterModel(m_reportEngine);
else
m_completer->updateCompleaterModel(m_page->datasourceManager());
2017-09-13 17:16:54 +03:00
}
2017-09-20 00:51:17 +03:00
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);
}
2017-09-21 00:44:09 +03:00
void ScriptEditor::setEditorFont(QFont font)
{
ui->textEdit->setFont(font);
}
QFont ScriptEditor::editorFont()
{
return ui->textEdit->font();
}
2017-09-20 00:51:17 +03:00
QString ScriptEditor::toPlainText()
{
return ui->textEdit->toPlainText();
}
2017-09-13 17:16:54 +03:00
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";
}
}
2017-09-20 00:51:17 +03:00
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();
}
2017-10-05 01:27:04 +03:00
void ScriptEditor::slotOnCurrentChanged(const QModelIndex &to, const QModelIndex &)
{
LimeReport::ScriptEngineNode* node = static_cast<LimeReport::ScriptEngineNode*>(to.internalPointer());
if (node->type()==LimeReport::ScriptEngineNode::Function){
ui->lblDescription->setText(node->description());
}
}
QString ReportStructureCompleater::pathFromIndex(const QModelIndex &index) const
{
QStringList dataList;
for (QModelIndex i = index; i.isValid(); i = i.parent()) {
dataList.prepend(model()->data(i, Qt::DisplayRole).toString());
}
return dataList.join(".");
}
QStringList ReportStructureCompleater::splitPath(const QString &path) const
{
return path.split(".");
}
void ReportStructureCompleater::addAdditionalDatawords(DataSourceManager* dataManager){
foreach(const QString &dsName,dataManager->dataSourceNames()){
QStandardItem* dsNode = new QStandardItem;
dsNode->setText(dsName);
foreach(const QString &field, dataManager->fieldNames(dsName)){
QStandardItem* fieldNode = new QStandardItem;
fieldNode->setText(field);
dsNode->appendRow(fieldNode);
}
m_model.invisibleRootItem()->appendRow(dsNode);
}
foreach (QString varName, dataManager->variableNames()) {
QStandardItem* varNode = new QStandardItem;
varNode->setText(varName.remove("#"));
m_model.invisibleRootItem()->appendRow(varNode);
}
#ifdef USE_QJSENGINE
ScriptEngineManager& se = LimeReport::ScriptEngineManager::instance();
QJSValue globalObject = se.scriptEngine()->globalObject();
QJSValueIterator it(globalObject);
while (it.hasNext()){
it.next();
if (it.value().isCallable() ){
QStandardItem* itemNode = new QStandardItem;
itemNode->setText(it.name());
m_model.invisibleRootItem()->appendRow(itemNode);
}
}
#endif
}
void ReportStructureCompleater::updateCompleaterModel(ReportEnginePrivateInterface* report)
{
if (report){
m_model.clear();
addAdditionalDatawords(report->dataManager());
for ( int i = 0; i < report->pageCount(); ++i){
PageDesignIntf* page = report->pageAt(i);
QStandardItem* itemNode = new QStandardItem;
itemNode->setText(page->pageItem()->objectName());
m_model.invisibleRootItem()->appendRow(itemNode);
QStringList slotsNames = extractSlotNames(page->pageItem());
foreach(QString slotName, slotsNames){
QStandardItem* slotItem = new QStandardItem;
slotItem->setText(slotName);
itemNode->appendRow(slotItem);
}
foreach (BaseDesignIntf* item, page->pageItem()->childBaseItems()){
addChildItem(item, itemNode->text(), m_model.invisibleRootItem());
}
}
}
}
void ReportStructureCompleater::updateCompleaterModel(DataSourceManager *dataManager)
{
m_model.clear();
addAdditionalDatawords(dataManager);
}
QStringList ReportStructureCompleater::extractSlotNames(BaseDesignIntf *item)
{
QStringList result;
if (!item) return result;
QMetaObject const * mo = item->metaObject();
while (mo){
for(int i = mo->methodOffset(); i < mo->methodCount(); ++i)
{
if (mo->method(i).methodType() == QMetaMethod::Signal) {
result.append(QString::fromLatin1(mo->method(i).name()));
}
}
mo = mo->superClass();
}
result.sort();
return result;
}
void ReportStructureCompleater::addChildItem(BaseDesignIntf *item, const QString &pageName, QStandardItem *parent)
{
if (!item) return;
QStandardItem* itemNode = new QStandardItem;
itemNode->setText(pageName+"_"+item->objectName());
parent->appendRow(itemNode);
QStringList slotNames = extractSlotNames(item);
foreach(QString slotName, slotNames){
QStandardItem* slotItem = new QStandardItem;
slotItem->setText(slotName);
itemNode->appendRow(slotItem);
}
//BandDesignIntf* band = dynamic_cast<BandDesignIntf*>(item);
//if (band){
foreach (BaseDesignIntf* child, item->childBaseItems()){
addChildItem(child, pageName, parent);
}
//}
}
2017-09-13 17:16:54 +03:00
} // namespace LimeReport
2017-09-20 00:51:17 +03:00