Skip to content

Commit

Permalink
FEM: improve Abaqus syntax highlighter
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Mar 30, 2015
1 parent ff77474 commit 30098b6
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 7 deletions.
118 changes: 113 additions & 5 deletions src/Mod/Fem/Gui/AbaqusHighlighter.cpp
Expand Up @@ -24,6 +24,7 @@
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QRegExp>
# include <QTextCharFormat>
#endif

#include "AbaqusHighlighter.h"
Expand All @@ -45,14 +46,121 @@ AbaqusHighlighter::~AbaqusHighlighter()

void AbaqusHighlighter::highlightBlock(const QString &text)
{
// Find a syntax file for the Abaqus format here
// http://notepad-plus.sourceforge.net/commun/userDefinedLang/userDefineLang_Abaqus.xml
//
enum { NormalState = -1, Definition, BeforeKey, InideKey, BeforeValue, InsideValue, Number };

int state = NormalState;
int start = 0;

QColor keywordColor(102,0,227); // this->colorByType(SyntaxHighlighter::Keyword)
QColor defnameColor(0,119,255); // this->colorByType(SyntaxHighlighter::Defname)
QColor operateColor(153,0,102); // this->colorByType(SyntaxHighlighter::Operator)
QColor valueColor(0,0,0); // this->colorByType(SyntaxHighlighter::String)
QColor numberColor(0,127,127); // this->colorByType(SyntaxHighlighter::Number)
QColor commentColor = this->colorByType(SyntaxHighlighter::Comment);

for (int i = 0; i < text.length(); ++i) {
if (text.mid(i, 2) == QLatin1String("**")) {
setFormat(i, text.length() - i, this->colorByType(SyntaxHighlighter::Comment));
break;
// highlight the supported operators
if (text[i] == QLatin1Char(',') || text[i] == QLatin1Char('=')) {
setFormat(i, 1, operateColor);
}

if (state == Definition) {
if (!text[i].isLetterOrNumber() && !text[i].isSpace()) {
QTextCharFormat keywordFormat;
keywordFormat.setForeground(keywordColor);
keywordFormat.setFontWeight(QFont::Bold);
setFormat(start, i - start, keywordFormat);

start = i;
state = BeforeKey;
}
}
else if (state == BeforeKey) {
if (text[i].isLetterOrNumber()) {
start = i;
state = InideKey;
}
}
else if (state == InideKey) {
if (!text[i].isLetterOrNumber() && !text[i].isSpace()) {
QTextCharFormat keyFormat;
keyFormat.setForeground(defnameColor);
setFormat(start, i - start, keyFormat);

start = i;
state = BeforeValue;
}
}
else if (state == BeforeValue) {
if (text[i].isLetterOrNumber()) {
start = i;
state = InsideValue;
}
}
else if (text.mid(i, 1) == QLatin1String("*")) {
setFormat(i, text.length() - i, this->colorByType(SyntaxHighlighter::Defname));
else if (state == InsideValue) {
if (!text[i].isLetterOrNumber() && !text[i].isSpace()) {
QTextCharFormat valueFormat;
valueFormat.setForeground(valueColor);
setFormat(start, i - start, valueFormat);

start = i;
state = BeforeKey;
}
}
// Number
else if (state == Number) {
if (!text[i].isNumber()) {
QTextCharFormat numberFormat;
numberFormat.setForeground(numberColor);
setFormat(start, i - start, numberFormat);

start = i;
state = NormalState;
}
}
else if (text[i].isNumber()) {
if (state == NormalState) {
start = i;
state = Number;
}
}
// Comment lines
else if (text.mid(i, 2) == QLatin1String("**")) {
QTextCharFormat commentFormat;
commentFormat.setForeground(commentColor);
commentFormat.setFontItalic(true);
setFormat(i, text.length() - i, commentFormat);
break;
}
// Definition
else if (text[i] == QLatin1Char('*')) {
start = i;
state = Definition;
}
}

if (state == Definition) {
QTextCharFormat keywordFormat;
keywordFormat.setForeground(keywordColor);
keywordFormat.setFontWeight(QFont::Bold);
setFormat(start, text.length() - start, keywordFormat);
}
else if (state == InideKey) {
QTextCharFormat keyFormat;
keyFormat.setForeground(defnameColor);
setFormat(start, text.length() - start, keyFormat);
}
else if (state == InsideValue) {
QTextCharFormat valueFormat;
valueFormat.setForeground(valueColor);
setFormat(start, text.length() - start, valueFormat);
}
else if (state == Number) {
QTextCharFormat numberFormat;
numberFormat.setForeground(numberColor);
setFormat(start, text.length() - start, numberFormat);
}
}
4 changes: 4 additions & 0 deletions src/Mod/Fem/Gui/AppFemGuiPy.cpp
Expand Up @@ -106,6 +106,10 @@ static PyObject * openEditor(PyObject *self, PyObject *args)
edit->open(fileName);
edit->resize(400, 300);
Gui::getMainWindow()->addWindow(edit);

QFont font = editor->font();
font.setFamily(QString::fromLatin1("Arial"));
editor->setFont(font);
}
} PY_CATCH;

Expand Down
8 changes: 6 additions & 2 deletions src/Mod/Fem/MechanicalAnalysis.py
Expand Up @@ -551,14 +551,18 @@ def editCalculixInputFile(self):
import FemGui
FemGui.openEditor(filename)
else:
import webbrowser
#import webbrowser
# If inp-file extension is assigned the os will use the appropriate binary
# (normaly an Editor) to open the file. Works perfectly on Windows if SciTE is installed.
# However using webbrower.open is not portable and not supported
# https://docs.python.org/3.4/library/webbrowser.html
# FIXME That code should be removed as soon as there is "Preferred editor" option
# added to Preferences, to allow existing SciTE users override built-in editor
webbrowser.open(filename)
#webbrowser.open(filename)
# The Abaqus synax highlighter works similarly to that one of SciTE so that our
# own editor can be used now
import FemGui
FemGui.openEditor(filename)

def runCalculix(self):
print 'runCalculix'
Expand Down

0 comments on commit 30098b6

Please sign in to comment.