From 3a745e603e77a89eae40a7e11bd45e43dee3ff78 Mon Sep 17 00:00:00 2001 From: Guido Ranzuglia granzuglia Date: Thu, 24 Nov 2011 18:12:50 +0000 Subject: [PATCH] added a lot of new functionalities to the editor --- src/meshlab/additionalgui.cpp | 130 +++++++++++++++++++++++++++++++- src/meshlab/additionalgui.h | 39 +++++++++- src/meshlab/xmlgeneratorgui.cpp | 6 +- src/meshlab/xmlgeneratorgui.h | 2 +- 4 files changed, 171 insertions(+), 6 deletions(-) diff --git a/src/meshlab/additionalgui.cpp b/src/meshlab/additionalgui.cpp index f8b591795..b906ae1e0 100644 --- a/src/meshlab/additionalgui.cpp +++ b/src/meshlab/additionalgui.cpp @@ -328,10 +328,16 @@ void TreeWidgetWithMenu::insertInMenu(const QString& st,const QVariant& data) } MLScriptEditor::MLScriptEditor( QWidget* par /*= NULL*/ ) -:QPlainTextEdit(par) +:QPlainTextEdit(par),regexps() { + QTextDocument* mydoc = new QTextDocument(this); + QPlainTextDocumentLayout* ld = new QPlainTextDocumentLayout(mydoc); + mydoc->setDocumentLayout(ld); + setDocument(mydoc); + narea = new MLNumberArea(this); connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int))); + //connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateCursorPos(int))); connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int))); connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine())); @@ -349,6 +355,7 @@ void MLScriptEditor::lineNumberAreaPaintEvent( QPaintEvent *event,const QColor& QPainter painter(narea); painter.fillRect(event->rect(),col); QTextBlock block = firstVisibleBlock(); + int indent = block.blockFormat().indent(); int blockNumber = block.blockNumber(); int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top(); int bottom = top + (int) blockBoundingRect(block).height(); @@ -390,7 +397,7 @@ void MLScriptEditor::resizeEvent( QResizeEvent* e) narea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height())); } -void MLScriptEditor::updateLineNumberAreaWidth( int /*newBlockCount*/ ) +void MLScriptEditor::updateLineNumberAreaWidth( int newBlockCount) { setViewportMargins(lineNumberAreaWidth(), 0, 0, 0); } @@ -425,6 +432,41 @@ void MLScriptEditor::updateLineNumberArea( const QRect & r, int dy) updateLineNumberAreaWidth(0); } +void MLScriptEditor::keyPressEvent( QKeyEvent * e ) +{ + switch(e->key()) + { + case (Qt::Key_Return): + case (Qt::Key_Enter): + { + QTextBlock b = textCursor().block(); + QRegExp tab("(\\t)+\\w"); + bool tabfound = (b.text().indexOf(tab) == 0); + textCursor().insertText("\n"); + if (tabfound) + { + QString cap = tab.cap(); + int tabcount = cap.lastIndexOf(QRegExp("\\t")) + 1; + QString tabst; + for(int ii = 0;ii < tabcount;++ii) + tabst += '\t'; + textCursor().insertText(tabst); + } + return; + break; + } + + } + QPlainTextEdit::keyPressEvent(e); +} + +void MLScriptEditor::setSyntaxHighlighter( MLSyntaxHighlighter* high ) +{ + slh = high; + if (slh != NULL) + slh->setDocument(document()); +} + MLNumberArea::MLNumberArea( MLScriptEditor* editor ) : QWidget(editor) { mledit = editor; @@ -439,3 +481,87 @@ void MLNumberArea::paintEvent(QPaintEvent* e) { mledit->lineNumberAreaPaintEvent(e,UsefulGUIFunctions::editorMagicColor()); } + +MLSyntaxHighlighter::MLSyntaxHighlighter(const QString& pluginvar,const QStringList& namespacelist,const QStringList& filterlist,QObject* parent ) +:QSyntaxHighlighter(parent),reserved(),langfuncs(),vcgbridgefun(),vcgbridgetype(),mlfun(),mlnmspace() +{ + HighlightingRule pvar; + pvar.format.setForeground(Qt::red); + pvar.pattern = QRegExp(pluginvar); + highlightingRules << pvar; + foreach(const QString& s,namespacelist) + { + //pvar.format.setForeground(Qt::magenta); + pvar.pattern = QRegExp(s); + highlightingRules << pvar; + } + foreach(const QString& s,filterlist) + { + pvar.pattern = QRegExp(s); + highlightingRules << pvar; + } + +} + +void MLSyntaxHighlighter::highlightBlock( const QString& text ) +{ + foreach (const HighlightingRule &rule, highlightingRules) + { + QRegExp expression(rule.pattern); + int index = expression.indexIn(text); + while (index >= 0) + { + int length = expression.matchedLength(); + setFormat(index, length, rule.format); + index = expression.indexIn(text, index + length); + } + } + setCurrentBlockState(0); +} + +QString MLSyntaxHighlighter::addIDBoundary( const QString& st ) +{ + return "\\b" + st + "\\b"; +} + +JavaScriptSyntaxHighLighter::JavaScriptSyntaxHighLighter(const QString& pluginvar,const QStringList& namespacelist,const QStringList& filterlist,QObject* parent ) +:MLSyntaxHighlighter(pluginvar,namespacelist,filterlist,parent) +{ + HighlightingRule res; + res.format.setForeground(Qt::darkBlue); + res.format.setFontWeight(QFont::Bold); + reserved << addIDBoundary("break"); + reserved << addIDBoundary("case"); + reserved << addIDBoundary("catch"); + reserved << addIDBoundary("continue"); + reserved << addIDBoundary("default"); + reserved << addIDBoundary("delete"); + reserved << addIDBoundary("do"); + reserved << addIDBoundary("else"); + reserved << addIDBoundary("finally"); + reserved << addIDBoundary("for"); + reserved << addIDBoundary("function"); + reserved << addIDBoundary("if"); + reserved << addIDBoundary("in"); + reserved << addIDBoundary("instanceof"); + reserved << addIDBoundary("new"); + reserved << addIDBoundary("return"); + reserved << addIDBoundary("switch"); + reserved << addIDBoundary("this"); + reserved << addIDBoundary("throw"); + reserved << addIDBoundary("try"); + reserved << addIDBoundary("typeof"); + reserved << addIDBoundary("var"); + reserved << addIDBoundary("void"); + reserved << addIDBoundary("while"); + reserved << addIDBoundary("with"); + reserved << addIDBoundary("true"); + reserved << addIDBoundary("false"); + reserved << addIDBoundary("null"); + foreach(QString st,reserved) + { + res.pattern = QRegExp(st); + highlightingRules << res; + } +} + diff --git a/src/meshlab/additionalgui.h b/src/meshlab/additionalgui.h index 7df9dbdd0..b29b30751 100644 --- a/src/meshlab/additionalgui.h +++ b/src/meshlab/additionalgui.h @@ -124,25 +124,62 @@ private: MLScriptEditor* mledit; }; +class MLSyntaxHighlighter : public QSyntaxHighlighter +{ +protected: + void highlightBlock (const QString& text); + + struct HighlightingRule + { + QRegExp pattern; + QTextCharFormat format; + }; + + QList highlightingRules; + + static QString addIDBoundary(const QString& st); + MLSyntaxHighlighter(const QString& pluginvar,const QStringList& namespacelist,const QStringList& filterlist, QObject* parent); + + QStringList reserved; + QStringList langfuncs; + QStringList mlnmspace; + QStringList mlfun; + QStringList vcgbridgetype; + QStringList vcgbridgefun; + + QString plugvar; +}; + +class JavaScriptSyntaxHighLighter : public MLSyntaxHighlighter +{ +public: + JavaScriptSyntaxHighLighter(const QString& pluginvar,const QStringList& namespacelist,const QStringList& filterlist,QObject* parent); + +}; + class MLScriptEditor : public QPlainTextEdit { Q_OBJECT public: MLScriptEditor(QWidget* par = NULL); ~MLScriptEditor(); - void lineNumberAreaPaintEvent(QPaintEvent *event,const QColor& col); int lineNumberAreaWidth(); + void setSyntaxHighlighter(MLSyntaxHighlighter* high); protected: void resizeEvent(QResizeEvent *event); + void keyPressEvent(QKeyEvent * e); private slots: void updateLineNumberAreaWidth(int newBlockCount); + //void updateCursorPos(int newBlockCount); void highlightCurrentLine(); void updateLineNumberArea(const QRect& r, int dy); private: MLNumberArea* narea; + QStringList regexps; + MLSyntaxHighlighter* slh; }; #endif // CHECKBOXLIST_H diff --git a/src/meshlab/xmlgeneratorgui.cpp b/src/meshlab/xmlgeneratorgui.cpp index d55bc2535..0f6ddb265 100644 --- a/src/meshlab/xmlgeneratorgui.cpp +++ b/src/meshlab/xmlgeneratorgui.cpp @@ -416,7 +416,7 @@ void FilterGeneratorGUI::importInfo( const MLXMLFilterSubTree& tree ) addParam(tree.params[ii]); } -FilterGeneratorTab::FilterGeneratorTab(const QString& filtername,QWidget* parent /*= NULL*/ ) +FilterGeneratorTab::FilterGeneratorTab(const QString& filtername,QWidget* parent /*= NULL*/,const QString& pluginvar /*= QString()*/,const QStringList& namespacelist /*= QStringList()*/,const QStringList& filterlist /*= QStringList()*/) :QFrame(parent),jsexp(true),guiexp(false) { //filtereditor = new FilterEditorFrame(this); @@ -438,6 +438,8 @@ FilterGeneratorTab::FilterGeneratorTab(const QString& filtername,QWidget* parent connect(ui->guiframe,SIGNAL(filterNameUpdated(const QString&,QWidget*)),this,SIGNAL(filterNameUpdated(const QString&,QWidget*))); connect(ui->guiframe,SIGNAL(validateFilterName(const QString&,FilterGeneratorGUI*)),this,SIGNAL(validateFilterName(const QString&,FilterGeneratorGUI*))); //disconnect(this,SIGNAL(itemExpanded(QTreeWidgetItem*)),this,SLOT(expandItem(QTreeWidgetItem*))); + JavaScriptSyntaxHighLighter* js = new JavaScriptSyntaxHighLighter(pluginvar,namespacelist,filterlist,this); + ui->jscode->setSyntaxHighlighter(js); } FilterGeneratorTab::~FilterGeneratorTab() @@ -712,7 +714,7 @@ void PluginGeneratorGUI::addNewFilter() for(int ii = 0;ii < tabs->count();++ii) namelist.push_back(tabs->tabText(ii)); QString tmpname = UsefulGUIFunctions::generateUniqueDefaultName("Filter",namelist); - FilterGeneratorTab* tb = new FilterGeneratorTab(tmpname,this); + FilterGeneratorTab* tb = new FilterGeneratorTab(tmpname,this,PM.pluginNameSpace(),PM.pluginnamespaces,PM.filterscriptnames); //tb->initLibInEnv(PM); tabs->addTab(tb,tmpname); connect(tb,SIGNAL(filterNameUpdated(const QString&,QWidget*)),this,SLOT(updateTabTitle(const QString&,QWidget*))); diff --git a/src/meshlab/xmlgeneratorgui.h b/src/meshlab/xmlgeneratorgui.h index 51c346e45..3caaa5f03 100644 --- a/src/meshlab/xmlgeneratorgui.h +++ b/src/meshlab/xmlgeneratorgui.h @@ -118,7 +118,7 @@ class FilterGeneratorTab : public QFrame { Q_OBJECT public: - FilterGeneratorTab(const QString& filtername,QWidget* parent = NULL); + FilterGeneratorTab(const QString& filtername,QWidget* parent = NULL,const QString& pluginvar = QString(),const QStringList& namespacelist = QStringList(),const QStringList& filterlist = QStringList()); ~FilterGeneratorTab(); void collectInfo(MLXMLFilterSubTree& filter);