From 090477fa684319385f844b8bd43181b676527724 Mon Sep 17 00:00:00 2001 From: Arin Alexander Date: Wed, 13 Sep 2017 17:16:54 +0300 Subject: [PATCH] Initial commit --- limereport/items/lrtextitemeditor.cpp | 1 - limereport/limereport.pri | 5 +- limereport/lrreportdesignwidget.cpp | 12 ++ limereport/lrreportdesignwidget.h | 5 +- limereport/scripteditor/lrscripteditor.cpp | 189 +++++++++++++++++++++ limereport/scripteditor/lrscripteditor.h | 56 ++++++ limereport/scripteditor/lrscripteditor.ui | 139 +++++++++++++++ 7 files changed, 404 insertions(+), 3 deletions(-) create mode 100644 limereport/scripteditor/lrscripteditor.cpp create mode 100644 limereport/scripteditor/lrscripteditor.h create mode 100644 limereport/scripteditor/lrscripteditor.ui diff --git a/limereport/items/lrtextitemeditor.cpp b/limereport/items/lrtextitemeditor.cpp index 76e6891..f46333d 100644 --- a/limereport/items/lrtextitemeditor.cpp +++ b/limereport/items/lrtextitemeditor.cpp @@ -33,7 +33,6 @@ #include "lrdatasourcemanager.h" #include "lrscriptenginemanager.h" #include "lrdatadesignintf.h" -#include "lrdatasourcemanager.h" #include #include diff --git a/limereport/limereport.pri b/limereport/limereport.pri index 69af57f..d27d6f7 100644 --- a/limereport/limereport.pri +++ b/limereport/limereport.pri @@ -59,6 +59,7 @@ SOURCES += \ $$REPORT_PATH/objectinspector/lrpropertydelegate.cpp \ $$REPORT_PATH/objectsbrowser/lrobjectbrowser.cpp \ $$REPORT_PATH/scriptbrowser/lrscriptbrowser.cpp \ + $$REPORT_PATH/scripteditor/lrscripteditor.cpp \ $$REPORT_PATH/items/lrsubitemparentpropitem.cpp \ $$REPORT_PATH/items/lralignpropitem.cpp \ $$REPORT_PATH/items/lrhorizontallayout.cpp \ @@ -161,6 +162,7 @@ HEADERS += \ $$REPORT_PATH/objectinspector/lrpropertydelegate.h \ $$REPORT_PATH/objectsbrowser/lrobjectbrowser.h \ $$REPORT_PATH/scriptbrowser/lrscriptbrowser.h \ + $$REPORT_PATH/scripteditor/lrscripteditor.h \ $$REPORT_PATH/items/editors/lritemeditorwidget.h \ $$REPORT_PATH/items/editors/lrfonteditorwidget.h \ $$REPORT_PATH/items/editors/lrtextalignmenteditorwidget.h \ @@ -235,7 +237,8 @@ FORMS += \ $$REPORT_PATH/scriptbrowser/lrscriptbrowser.ui \ $$REPORT_PATH/items/lrchartitemeditor.ui \ $$REPORT_PATH/translationeditor/translationeditor.ui \ - $$PWD/translationeditor/languageselectdialog.ui + $$REPORT_PATH/translationeditor/languageselectdialog.ui + $$REPORT_PATH/scripteditor/lrscripteditor.ui RESOURCES += \ $$REPORT_PATH/objectinspector/lobjectinspector.qrc \ diff --git a/limereport/lrreportdesignwidget.cpp b/limereport/lrreportdesignwidget.cpp index 9472f13..2b7f160 100644 --- a/limereport/lrreportdesignwidget.cpp +++ b/limereport/lrreportdesignwidget.cpp @@ -35,6 +35,7 @@ #include "lrsettingdialog.h" #include "dialogdesigner/lrdialogdesigner.h" #include "translationeditor/translationeditor.h" +#include "scripteditor/lrscripteditor.h" #include #include @@ -246,6 +247,12 @@ void ReportDesignWidget::createTabs(){ m_tabWidget->setTabWhatsThis(pageIndex,"script"); m_tabWidget->setCurrentIndex(0); + m_newScriptEditor = new ScriptEditor(this); + m_newScriptEditor->setReportEngine(m_report); + pageIndex = m_tabWidget->addTab(m_newScriptEditor,QIcon(),tr("New Script Editor")); + m_tabWidget->setTabWhatsThis(pageIndex,"script"); + m_tabWidget->setCurrentIndex(0); + #ifdef HAVE_QTDESIGNER_INTEGRATION QWidget* dialogDesigner; foreach(DialogDescriber::Ptr dialogDesc, m_report->scriptContext()->dialogDescribers()){ @@ -787,6 +794,11 @@ void ReportDesignWidget::slotCurrentTabChanged(int index) if (activeTabType() == Translations){ m_traslationEditor->setReportEngine(report()); } + + if (activeTabType() == Script){ + m_newScriptEditor->initCompleter(); + } + emit activePageChanged(); } diff --git a/limereport/lrreportdesignwidget.h b/limereport/lrreportdesignwidget.h index 4d3b25d..d710537 100644 --- a/limereport/lrreportdesignwidget.h +++ b/limereport/lrreportdesignwidget.h @@ -51,6 +51,8 @@ class ReportDesignWindow; class DialogDesignerManager; class DialogDesigner; class TranslationEditor; +class ScriptEditor; + class ReportDesignWidget : public QWidget { @@ -190,6 +192,7 @@ private: ReportEnginePrivate* m_report; QGraphicsView *m_view; QTextEdit* m_scriptEditor; + ScriptEditor* m_newScriptEditor; TranslationEditor* m_traslationEditor; #ifdef HAVE_QTDESIGNER_INTEGRATION DialogDesignerManager* m_dialogDesignerManager; @@ -205,5 +208,5 @@ private: bool m_dialogChanged; }; -} +} // namespace LimeReport #endif // LRREPORTDESIGNWIDGET_H diff --git a/limereport/scripteditor/lrscripteditor.cpp b/limereport/scripteditor/lrscripteditor.cpp new file mode 100644 index 0000000..80bbd27 --- /dev/null +++ b/limereport/scripteditor/lrscripteditor.cpp @@ -0,0 +1,189 @@ +#include "lrscripteditor.h" +#include "ui_lrscripteditor.h" +#include + +#include "lrdatasourcemanager.h" +#include "lrscriptenginemanager.h" +#include "lrdatadesignintf.h" +#include "lrreportengine_p.h" + +namespace LimeReport{ + +TextEditorWithCompleater::TextEditorWithCompleater(QWidget *parent) + : QTextEdit(parent),m_compleater(0) +{ +} + +void TextEditorWithCompleater::setCompleter(QCompleter *value) +{ + if (value) disconnect(value,0,this,0); + m_compleater = value; + if (!m_compleater) return; + m_compleater->setWidget(this); + m_compleater->setCompletionMode(QCompleter::PopupCompletion); + m_compleater->setCaseSensitivity(Qt::CaseInsensitive); + connect(m_compleater,SIGNAL(activated(QString)),this,SLOT(insertCompletion(QString))); +} + +void TextEditorWithCompleater::keyPressEvent(QKeyEvent *e) +{ + if (m_compleater && m_compleater->popup()->isVisible()) { + switch (e->key()) { + case Qt::Key_Enter: + case Qt::Key_Return: + case Qt::Key_Escape: + case Qt::Key_Tab: + case Qt::Key_Backtab: + e->ignore(); + return; + default: + break; + } + } + + bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_Space); + if (!m_compleater || !isShortcut) QTextEdit::keyPressEvent(e); + + const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier); + if (!m_compleater || (ctrlOrShift && e->text().isEmpty())) + return; + + static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="); // end of word + bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift; + + QString completionPrefix = textUnderCursor(); + + if (!isShortcut && (hasModifier || e->text().isEmpty()|| completionPrefix.length() < 3 + || eow.contains(e->text().right(1)))) { + m_compleater->popup()->hide(); + return; + } + + if (completionPrefix != m_compleater->completionPrefix()) { + m_compleater->setCompletionPrefix(completionPrefix); + m_compleater->popup()->setCurrentIndex(m_compleater->completionModel()->index(0, 0)); + } + + QRect cr = cursorRect(); + cr.setWidth(m_compleater->popup()->sizeHintForColumn(0) + + m_compleater->popup()->verticalScrollBar()->sizeHint().width()); + m_compleater->complete(cr); + +} + +void TextEditorWithCompleater::focusInEvent(QFocusEvent *e) +{ + if (m_compleater) m_compleater->setWidget(this); + QTextEdit::focusInEvent(e); +} + +QString TextEditorWithCompleater::textUnderCursor() const +{ + QTextCursor tc = textCursor(); + tc.select(QTextCursor::WordUnderCursor); + return tc.selectedText(); +} + +void TextEditorWithCompleater::insertCompletion(const QString &completion) +{ + if (m_compleater->widget() != this) + return; + QTextCursor tc = textCursor(); + int extra = completion.length() - m_compleater->completionPrefix().length(); + tc.movePosition(QTextCursor::Left); + tc.movePosition(QTextCursor::EndOfWord); + tc.insertText(completion.right(extra)); + setTextCursor(tc); +} + +ScriptEditor::ScriptEditor(QWidget *parent) : + QWidget(parent), + ui(new Ui::ScriptEditor) +{ + ui->setupUi(this); + 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<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)); +} + +void ScriptEditor::addItemToCompleater(const QString& pageName, BaseDesignIntf* item, QStringList& dataWords) +{ + BandDesignIntf* band = dynamic_cast(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"; + } +} + +} // namespace LimeReport diff --git a/limereport/scripteditor/lrscripteditor.h b/limereport/scripteditor/lrscripteditor.h new file mode 100644 index 0000000..7674025 --- /dev/null +++ b/limereport/scripteditor/lrscripteditor.h @@ -0,0 +1,56 @@ +#ifndef LRSCRIPTEDITOR_H +#define LRSCRIPTEDITOR_H + +#include +#include +#include +#include +#include + +namespace LimeReport{ + +class ReportEnginePrivate; +class BaseDesignIntf; + +class TextEditorWithCompleater :public QTextEdit +{ + Q_OBJECT +public: + TextEditorWithCompleater(QWidget* parent=0); + void setCompleter(QCompleter* value); + QCompleter* compleater() const{ return m_compleater;} +protected: + virtual void keyPressEvent(QKeyEvent *e); + virtual void focusInEvent(QFocusEvent *e); +private: + QString textUnderCursor() const; +private slots: + void insertCompletion(const QString& completion); +private: + QCompleter* m_compleater; +}; + +namespace Ui { +class ScriptEditor; +} + +class ScriptEditor : public QWidget +{ + Q_OBJECT +public: + explicit ScriptEditor(QWidget *parent = 0); + ~ScriptEditor(); + void setReportEngine(ReportEnginePrivate* reportEngine); + void initCompleter(); +private: + void addItemToCompleater(const QString& pageName, BaseDesignIntf* item, QStringList& dataWords); +private: + Ui::ScriptEditor *ui; + ReportEnginePrivate* m_reportEngine; + QCompleter* m_completer; + +}; + +} // namespace LimeReport + +#endif // LRSCRIPTEDITOR_H diff --git a/limereport/scripteditor/lrscripteditor.ui b/limereport/scripteditor/lrscripteditor.ui new file mode 100644 index 0000000..5a5af17 --- /dev/null +++ b/limereport/scripteditor/lrscripteditor.ui @@ -0,0 +1,139 @@ + + + ScriptEditor + + + + 0 + 0 + 706 + 541 + + + + Form + + + + + + + 0 + 0 + + + + Content + + + + 2 + + + 2 + + + 2 + + + 2 + + + 2 + + + + + + 0 + 0 + + + + Qt::Horizontal + + + + + 12 + + + + + + QTabWidget::South + + + 0 + + + + Data + + + + 3 + + + 3 + + + 3 + + + 3 + + + 3 + + + + + false + + + + + + + + Functions + + + + + + Qt::Vertical + + + + false + + + + + + + + + + + + + + + + + + + + + + TextEditorWithCompleater + QTextEdit +
lrscripteditor.h
+
+
+ + +