diff --git a/limereport/items/lrtextitemeditor.cpp b/limereport/items/lrtextitemeditor.cpp index f46333d..37ac44d 100644 --- a/limereport/items/lrtextitemeditor.cpp +++ b/limereport/items/lrtextitemeditor.cpp @@ -33,6 +33,7 @@ #include "lrdatasourcemanager.h" #include "lrscriptenginemanager.h" #include "lrdatadesignintf.h" +#include "lrscripteditor.h" #include #include @@ -45,10 +46,9 @@ TextItemEditor::TextItemEditor(LimeReport::TextItem *item, LimeReport::PageDesig { ui->setupUi(this); initUI(); - m_teContent->setPlainText(item->content()); - m_teContent->setFocus(); setWindowIcon(QIcon(":/items/images/TextItem")); readSetting(); + connect(ui->codeEditor, SIGNAL(splitterMoved(int,int)), this, SLOT(slotSplitterMoved(int,int)) ); } TextItemEditor::~TextItemEditor() @@ -96,10 +96,20 @@ void TextItemEditor::moveEvent(QMoveEvent*) #endif } +void TextItemEditor::closeEvent(QCloseEvent* event) +{ + if (settings()!=0){ + settings()->beginGroup("TextItemEditor"); + settings()->setValue("CodeEditorState",ui->codeEditor->saveState()); + settings()->endGroup(); + } + QWidget::closeEvent(event); +} + void TextItemEditor::on_pbOk_clicked() { - if (m_textItem->content()!=m_teContent->toPlainText()){ - m_textItem->setContent(m_teContent->toPlainText()); + if (m_textItem->content()!= ui->codeEditor->toPlainText()){ + m_textItem->setContent(ui->codeEditor->toPlainText()); } close(); } @@ -107,84 +117,19 @@ void TextItemEditor::on_pbOk_clicked() void TextItemEditor::initUI() { QStringList dataWords; - m_teContent = ui->textEdit; - m_completer = new QCompleter(this); - m_teContent->setCompleter(m_completer); - - m_datasourcesMenu = new QMenu(this); + ui->toolButton->setChecked(false); + ui->gbSettings->setVisible(false); LimeReport::DataSourceManager* dm = m_page->datasourceManager(); LimeReport::ScriptEngineManager& se = LimeReport::ScriptEngineManager::instance(); se.setDataManager(dm); - if (dm){ - if (dm->isNeedUpdateDatasourceModel()) - dm->updateDatasourceModel(); - ui->twData->setModel(dm->datasourcesModel()); - ui->twScriptEngine->setModel(se.model()); - - foreach(const QString &dsName,dm->dataSourceNames()){ - foreach(const QString &field, dm->fieldNames(dsName)){ - dataWords<tabWidget->setVisible(false); + ScriptEditor* scriptEditor = dynamic_cast(ui->codeEditor); + if (scriptEditor){ + scriptEditor->setReportPage(m_page); + scriptEditor->setPageBand(findParentBand()); + scriptEditor->setPlainText(m_textItem->content()); } - - foreach (LimeReport::ScriptFunctionDesc functionDesc, se.functionsDescribers()) { - dataWords<setModel(new QStringListModel(dataWords,m_completer)); - ui->gbSettings->setVisible(false); - - if (ui->twScriptEngine->selectionModel()){ - connect(ui->twScriptEngine->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), - this, SLOT(slotScriptItemsSelectionChanged(QModelIndex,QModelIndex))); - } - - BandDesignIntf* band = findParentBand(); - 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)); - } - } -} - -QStringListModel *TextItemEditor::getDataSources() -{ - LimeReport::DataSourceManager* dm = m_page->datasourceManager(); - QStringList dataSources; - foreach(QString dsName,dm->dataSourceNames()){ - dataSources<datasourceManager(); - foreach(QString field, dm->fieldNames(datasource)){ - fields<(sender()); - m_teContent->insertPlainText(action->whatsThis()); -} - void TextItemEditor::readSetting() { if (settings()==0) return; @@ -209,16 +148,16 @@ void TextItemEditor::readSetting() if (v.isValid()){ restoreGeometry(v.toByteArray()); } - v = settings()->value("State"); + v = settings()->value("CodeEditorState"); if (v.isValid()){ - ui->splitter->restoreState(v.toByteArray()); + ui->codeEditor->restoreState(v.toByteArray()); } QVariant fontName = settings()->value("FontName"); if (fontName.isValid()){ QVariant fontSize = settings()->value("FontSize"); - ui->textEdit->setFont(QFont(fontName.toString(),fontSize.toInt())); - ui->editorFont->setCurrentFont(ui->textEdit->font()); + ui->codeEditor->setEditorFont(QFont(fontName.toString(),fontSize.toInt())); + ui->editorFont->setCurrentFont(ui->codeEditor->editorFont()); ui->editorFontSize->setValue(fontSize.toInt()); } settings()->endGroup(); @@ -231,136 +170,19 @@ void TextItemEditor::writeSetting() if (settings()!=0){ settings()->beginGroup("TextItemEditor"); settings()->setValue("Geometry",saveGeometry()); - settings()->setValue("State",ui->splitter->saveState()); + settings()->setValue("CodeEditorState",ui->codeEditor->saveState()); settings()->endGroup(); } } - -CompleaterTextEditor::CompleaterTextEditor(QWidget *parent) - : QTextEdit(parent),m_compleater(0) -{ -} - -void CompleaterTextEditor::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 CompleaterTextEditor::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_E); - 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 CompleaterTextEditor::focusInEvent(QFocusEvent *e) -{ - if (m_compleater) m_compleater->setWidget(this); - QTextEdit::focusInEvent(e); -} - -QString CompleaterTextEditor::textUnderCursor() const -{ - QTextCursor tc = textCursor(); - tc.select(QTextCursor::WordUnderCursor); - return tc.selectedText(); -} - - -void CompleaterTextEditor::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); -} - -void TextItemEditor::on_twData_doubleClicked(const QModelIndex &index) -{ - if (!index.isValid()) return; - LimeReport::DataNode* node = static_cast(index.internalPointer()); - if (node->type()==LimeReport::DataNode::Field){ - m_teContent->insertPlainText(QString("$D{%1.%2}").arg(node->parent()->name()).arg(node->name())); - } - if (node->type()==LimeReport::DataNode::Variable){ - m_teContent->insertPlainText(QString("$V{%1}").arg(node->name())); - } -} - -void TextItemEditor::on_twScriptEngine_doubleClicked(const QModelIndex &index) -{ - if (!index.isValid()) return; - LimeReport::ScriptEngineNode* node = static_cast(index.internalPointer()); - if (node->type()==LimeReport::ScriptEngineNode::Function){ - m_teContent->insertPlainText(node->name()+"()"); - } -} - -void TextItemEditor::on_splitter_splitterMoved(int , int ) -{ -#ifdef unix - writeSetting(); -#endif -} - void TextItemEditor::on_editorFont_currentFontChanged(const QFont &f) { if (m_isReadingSetting) return; QFont tmp = f; tmp.setPointSize(ui->editorFontSize->value()); - ui->textEdit->setFont(tmp); + ui->codeEditor->setEditorFont(tmp); settings()->beginGroup("TextItemEditor"); - settings()->setValue("FontName",ui->textEdit->font().family()); + settings()->setValue("FontName",ui->codeEditor->editorFont().family()); settings()->setValue("FontSize",ui->editorFontSize->value()); settings()->endGroup(); } @@ -368,9 +190,9 @@ void TextItemEditor::on_editorFont_currentFontChanged(const QFont &f) void TextItemEditor::on_editorFontSize_valueChanged(int arg1) { if (m_isReadingSetting) return; - ui->textEdit->setFont(QFont(ui->textEdit->font().family(),arg1)); + ui->codeEditor->setEditorFont(QFont(ui->codeEditor->editorFont().family(),arg1)); settings()->beginGroup("TextItemEditor"); - settings()->setValue("FontName",ui->textEdit->font().family()); + settings()->setValue("FontName",ui->codeEditor->editorFont().family()); settings()->setValue("FontSize",ui->editorFontSize->value()); settings()->endGroup(); } @@ -380,21 +202,9 @@ void TextItemEditor::on_toolButton_clicked(bool checked) ui->gbSettings->setVisible(checked); } - -void TextItemEditor::on_twScriptEngine_activated(const QModelIndex &index) +void TextItemEditor::slotSplitterMoved(int, int) { - LimeReport::ScriptEngineNode* node = static_cast(index.internalPointer()); - if (node->type()==LimeReport::ScriptEngineNode::Function){ - ui->lblDescription->setText(node->name()); - } -} - -void TextItemEditor::slotScriptItemsSelectionChanged(const QModelIndex &to, const QModelIndex) -{ - LimeReport::ScriptEngineNode* node = static_cast(to.internalPointer()); - if (node->type()==LimeReport::ScriptEngineNode::Function){ - ui->lblDescription->setText(node->description()); - } + writeSetting(); } BandDesignIntf *TextItemEditor::findParentBand() diff --git a/limereport/items/lrtextitemeditor.h b/limereport/items/lrtextitemeditor.h index cf02781..43b41ad 100644 --- a/limereport/items/lrtextitemeditor.h +++ b/limereport/items/lrtextitemeditor.h @@ -43,23 +43,23 @@ namespace Ui { class TextItemEditor; } -class CompleaterTextEditor :public QTextEdit -{ - Q_OBJECT -public: - CompleaterTextEditor(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; -}; +//class CompleaterTextEditor :public QTextEdit +//{ +// Q_OBJECT +//public: +// CompleaterTextEditor(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; +//}; class TextItemEditor : public QWidget { @@ -73,33 +73,23 @@ public: protected: void resizeEvent(QResizeEvent *); void moveEvent(QMoveEvent *); + void closeEvent(QCloseEvent *event); BandDesignIntf* findParentBand(); private slots: void on_pbOk_clicked(); void on_pbCancel_clicked(); - void slotFieldSelected(); - void on_twData_doubleClicked(const QModelIndex &index); - void on_twScriptEngine_doubleClicked(const QModelIndex &index); - void on_splitter_splitterMoved(int, int); void on_editorFont_currentFontChanged(const QFont &f); void on_editorFontSize_valueChanged(int arg1); void on_toolButton_clicked(bool checked); - void on_twScriptEngine_activated(const QModelIndex &index); - void slotScriptItemsSelectionChanged(const QModelIndex &to, const QModelIndex); + void slotSplitterMoved(int, int); private: void initUI(); void readSetting(); void writeSetting(); - QStringListModel* getDataSources(); - QStringListModel* getPrefixes(); - QStringListModel* getColumns(QString datasource); private: Ui::TextItemEditor *ui; LimeReport::TextItem* m_textItem; LimeReport::PageDesignIntf* m_page; - QMenu* m_datasourcesMenu; - CompleaterTextEditor* m_teContent; - QCompleter* m_completer; QSettings* m_settings; bool m_ownedSettings; bool m_isReadingSetting; diff --git a/limereport/items/lrtextitemeditor.ui b/limereport/items/lrtextitemeditor.ui index 9030fdb..f9d74e4 100644 --- a/limereport/items/lrtextitemeditor.ui +++ b/limereport/items/lrtextitemeditor.ui @@ -9,15 +9,15 @@ 0 0 - 539 - 436 + 457 + 366 Text Item Editor - + :/items/images/insert-text_3.png:/items/images/insert-text_3.png @@ -33,9 +33,6 @@ Content - - 2 - 2 @@ -49,84 +46,13 @@ 2 - - - - 0 - 0 - + + + + 0 + 0 + - - Qt::Horizontal - - - - - 12 - - - - - - QTabWidget::South - - - 0 - - - - Data - - - - 3 - - - 3 - - - 3 - - - 3 - - - 3 - - - - - false - - - - - - - - Functions - - - - - - Qt::Vertical - - - - false - - - - - - - - - - - - @@ -183,7 +109,7 @@ ... - + :/items/images/settings.png:/items/images/settings.png @@ -233,17 +159,16 @@ - CompleaterTextEditor - QTextEdit -
lrtextitemeditor.h
+ LimeReport::ScriptEditor + QWidget +
lrscripteditor.h
+ 1
pbOk pbCancel - - - + diff --git a/limereport/scripteditor/lrscripteditor.cpp b/limereport/scripteditor/lrscripteditor.cpp index 461a8f9..263eff3 100644 --- a/limereport/scripteditor/lrscripteditor.cpp +++ b/limereport/scripteditor/lrscripteditor.cpp @@ -11,12 +11,13 @@ namespace LimeReport{ ScriptEditor::ScriptEditor(QWidget *parent) : QWidget(parent), - ui(new Ui::ScriptEditor) + ui(new Ui::ScriptEditor), m_reportEngine(0), m_page(0) { ui->setupUi(this); setFocusProxy(ui->textEdit); m_completer = new QCompleter(this); ui->textEdit->setCompleter(m_completer); + connect(ui->splitter, SIGNAL(splitterMoved(int,int)), this, SIGNAL(splitterMoved(int,int))); } ScriptEditor::~ScriptEditor() @@ -42,10 +43,50 @@ void ScriptEditor::setReportEngine(ReportEnginePrivate* reportEngine) } +void ScriptEditor::setReportPage(PageDesignIntf* page) +{ + m_page = page; + DataSourceManager* dm = page->datasourceManager(); + 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::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)); + } + } +} + void ScriptEditor::initCompleter() { QStringList dataWords; - DataSourceManager* dm = m_reportEngine->dataManager(); + + DataSourceManager* dm = 0; + if (m_reportEngine) + dm = m_reportEngine->dataManager(); + if (m_page) + dm = m_page->datasourceManager(); + ScriptEngineManager& se = LimeReport::ScriptEngineManager::instance(); QJSValue globalObject = se.scriptEngine()->globalObject(); @@ -68,11 +109,13 @@ void ScriptEditor::initCompleter() 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); + if (m_reportEngine){ + 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); + } } } @@ -94,6 +137,16 @@ void ScriptEditor::setPlainText(const QString& text) ui->textEdit->setPlainText(text); } +void ScriptEditor::setEditorFont(QFont font) +{ + ui->textEdit->setFont(font); +} + +QFont ScriptEditor::editorFont() +{ + return ui->textEdit->font(); +} + QString ScriptEditor::toPlainText() { return ui->textEdit->toPlainText(); diff --git a/limereport/scripteditor/lrscripteditor.h b/limereport/scripteditor/lrscripteditor.h index eedb66d..fc7d7be 100644 --- a/limereport/scripteditor/lrscripteditor.h +++ b/limereport/scripteditor/lrscripteditor.h @@ -11,6 +11,8 @@ namespace LimeReport{ class ReportEnginePrivate; class BaseDesignIntf; +class PageDesignIntf; +class BandDesignIntf; namespace Ui { class ScriptEditor; @@ -23,21 +25,26 @@ public: explicit ScriptEditor(QWidget *parent = 0); ~ScriptEditor(); void setReportEngine(ReportEnginePrivate* reportEngine); + void setReportPage(PageDesignIntf* page); + void setPageBand(BandDesignIntf* band); void initCompleter(); QByteArray saveState(); void restoreState(QByteArray state); void setPlainText(const QString &text); + void setEditorFont(QFont font); + QFont editorFont(); QString toPlainText(); +signals: + void splitterMoved(int, int); private slots: void on_twData_doubleClicked(const QModelIndex &index); - void on_twScriptEngine_doubleClicked(const QModelIndex &index); - private: void addItemToCompleater(const QString& pageName, BaseDesignIntf* item, QStringList& dataWords); private: Ui::ScriptEditor *ui; ReportEnginePrivate* m_reportEngine; + PageDesignIntf* m_page; QCompleter* m_completer; };