0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-24 00:33:02 +03:00

TextItem editor has been refactored

This commit is contained in:
Arin Alexander 2017-09-21 00:44:09 +03:00
parent 2b708b38f5
commit fcf165df66
5 changed files with 135 additions and 350 deletions

View File

@ -33,6 +33,7 @@
#include "lrdatasourcemanager.h"
#include "lrscriptenginemanager.h"
#include "lrdatadesignintf.h"
#include "lrscripteditor.h"
#include <QMenu>
#include <QScrollBar>
@ -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<<dsName+"."+field;
}
}
} else {
ui->tabWidget->setVisible(false);
ScriptEditor* scriptEditor = dynamic_cast<ScriptEditor*>(ui->codeEditor);
if (scriptEditor){
scriptEditor->setReportPage(m_page);
scriptEditor->setPageBand(findParentBand());
scriptEditor->setPlainText(m_textItem->content());
}
foreach (LimeReport::ScriptFunctionDesc functionDesc, se.functionsDescribers()) {
dataWords<<functionDesc.name;
}
m_completer->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<<dsName;
}
return new QStringListModel(dataSources, m_completer);
}
QStringListModel *TextItemEditor::getPrefixes()
{
QStringList prefixes;
prefixes<<"D{"<<"S{";
return new QStringListModel(prefixes, m_completer);
}
QStringListModel *TextItemEditor::getColumns(QString datasource)
{
QStringList fields;
LimeReport::DataSourceManager* dm = m_page->datasourceManager();
foreach(QString field, dm->fieldNames(datasource)){
fields<<field;
}
return new QStringListModel(fields, m_completer);
}
void TextItemEditor::on_pbCancel_clicked()
@ -192,12 +137,6 @@ void TextItemEditor::on_pbCancel_clicked()
close();
}
void TextItemEditor::slotFieldSelected()
{
QAction* action = dynamic_cast<QAction*>(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<LimeReport::DataNode*>(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<LimeReport::ScriptEngineNode*>(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<LimeReport::ScriptEngineNode*>(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<LimeReport::ScriptEngineNode*>(to.internalPointer());
if (node->type()==LimeReport::ScriptEngineNode::Function){
ui->lblDescription->setText(node->description());
}
writeSetting();
}
BandDesignIntf *TextItemEditor::findParentBand()

View File

@ -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;

View File

@ -9,15 +9,15 @@
<rect>
<x>0</x>
<y>0</y>
<width>539</width>
<height>436</height>
<width>457</width>
<height>366</height>
</rect>
</property>
<property name="windowTitle">
<string>Text Item Editor</string>
</property>
<property name="windowIcon">
<iconset resource="items.qrc">
<iconset>
<normaloff>:/items/images/insert-text_3.png</normaloff>:/items/images/insert-text_3.png</iconset>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
@ -33,9 +33,6 @@
<string>Content</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>2</number>
</property>
<property name="leftMargin">
<number>2</number>
</property>
@ -49,84 +46,13 @@
<number>2</number>
</property>
<item>
<widget class="QSplitter" name="splitter">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
<widget class="LimeReport::ScriptEditor" name="codeEditor" native="true">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="CompleaterTextEditor" name="textEdit">
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
</widget>
<widget class="QTabWidget" name="tabWidget">
<property name="tabPosition">
<enum>QTabWidget::South</enum>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Data</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="spacing">
<number>3</number>
</property>
<property name="leftMargin">
<number>3</number>
</property>
<property name="topMargin">
<number>3</number>
</property>
<property name="rightMargin">
<number>3</number>
</property>
<property name="bottomMargin">
<number>3</number>
</property>
<item>
<widget class="QTreeView" name="twData">
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Functions</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QSplitter" name="splitter_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<widget class="QTreeView" name="twScriptEngine">
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
</widget>
<widget class="QLabel" name="lblDescription">
<property name="text">
<string/>
</property>
</widget>
</widget>
</item>
</layout>
</widget>
</widget>
</widget>
</item>
</layout>
@ -183,7 +109,7 @@
<string>...</string>
</property>
<property name="icon">
<iconset resource="items.qrc">
<iconset>
<normaloff>:/items/images/settings.png</normaloff>:/items/images/settings.png</iconset>
</property>
<property name="checkable">
@ -233,17 +159,16 @@
</widget>
<customwidgets>
<customwidget>
<class>CompleaterTextEditor</class>
<extends>QTextEdit</extends>
<header>lrtextitemeditor.h</header>
<class>LimeReport::ScriptEditor</class>
<extends>QWidget</extends>
<header>lrscripteditor.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>pbOk</tabstop>
<tabstop>pbCancel</tabstop>
</tabstops>
<resources>
<include location="items.qrc"/>
</resources>
<resources/>
<connections/>
</ui>

View File

@ -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();

View File

@ -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;
};