summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean Claveau <jean.claveau@c277.fr>2014-10-04 15:52:12 +0200
committerJean Claveau <jean.claveau@c277.fr>2014-10-04 15:52:12 +0200
commitb41f4f97c0cdb5a553d54fefcd5ca551603b16d9 (patch)
treedcf43908ffab16a0667facb25a5eafd4eac52fe6
parent9aee5414efb0075459e7a7d6d7ad9d2351ba4c32 (diff)
svg parser scripts in extension + removing cisms + cleaning
-rw-r--r--script/console/__init__.js12
-rw-r--r--script/svg/__init__.js59
-rw-r--r--src/skin/skincontext.cpp8
-rw-r--r--src/skin/skincontext.h1
-rw-r--r--src/skin/svgparser.cpp69
-rw-r--r--src/skin/svgparser.h1
6 files changed, 104 insertions, 46 deletions
diff --git a/script/console/__init__.js b/script/console/__init__.js
new file mode 100644
index 0000000000..a1d833b9a9
--- /dev/null
+++ b/script/console/__init__.js
@@ -0,0 +1,12 @@
+__setupPackage__(__extension__);
+
+console = {
+ log : function(){
+ var out = [],
+ i = 0;
+ for( ; i<arguments.length; i++ ){
+ out.push( JSON.stringify(arguments[i]) );
+ }
+ print(out.join(' '));
+ }
+}
diff --git a/script/svg/__init__.js b/script/svg/__init__.js
new file mode 100644
index 0000000000..c7a0c39334
--- /dev/null
+++ b/script/svg/__init__.js
@@ -0,0 +1,59 @@
+__setupPackage__(__extension__);
+
+svg.templateHooks = {};
+
+svg.regexpQuote = function (str, delimiter) {
+ return String(str).replace(
+ new RegExp(
+ '[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]',
+ 'g'
+ ),
+ '\\$&'
+ );
+}
+
+svg.hookNames = function(){
+ var hookNames = ['variable'],
+ that = this;
+ for( var i in this.templateHooks )
+ hookNames.push(i);
+
+ hookNames.toPattern = function(){
+ for( var i in this )
+ this[i] = that.regexpQuote(this[i]);
+ return this.join('|');
+ }
+
+ return hookNames;
+}
+
+global = this;
+svg.templateHooks.variable = variable = function( varName ){
+ // console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
+ // console.log(global[varName]);
+ if( varName in global ){
+ return global[varName];
+ }
+ return '';
+}
+
+svg.templateHooks.prop = prop = function( propName, varName ){
+ var out = '';
+
+ if( (varName in global) ){
+ var value = global[varName];
+
+ if( isNumber(value) ){
+ out = propName + ':' + value + ';';
+ } else if( value.length ) {
+ out = propName + ':' + value + ';';
+ }
+
+ } else {
+ // print( 'Unable to find ' + varName + ' for prop hook.' );
+ }
+
+ // print( varName + ' => ' out + ' | ' + (varName in global) );
+ return out;
+}
+
diff --git a/src/skin/skincontext.cpp b/src/skin/skincontext.cpp
index 546cd6562d..71461658dc 100644
--- a/src/skin/skincontext.cpp
+++ b/src/skin/skincontext.cpp
@@ -273,6 +273,14 @@ QScriptValue SkinContext::evaluateScript(QString expression) {
return m_scriptEngine.evaluate(expression);
}
+QScriptValue SkinContext::importScriptExtension(QString extensionName) {
+ QScriptValue out = m_scriptEngine.importExtension(extensionName);
+ if (m_scriptEngine.hasUncaughtException()) {
+ qDebug() << out.toString();
+ }
+ return out;
+}
+
const QScriptEngine& SkinContext::getScriptEngine() const {
return m_scriptEngine;
}
diff --git a/src/skin/skincontext.h b/src/skin/skincontext.h
index 85a953a51d..85cdd55b6d 100644
--- a/src/skin/skincontext.h
+++ b/src/skin/skincontext.h
@@ -66,6 +66,7 @@ class SkinContext {
PixmapSource getPixmapSource(const QDomNode& pixmapNode) const;
QScriptValue evaluateScript(QString expression);
+ QScriptValue importScriptExtension(QString extensionName);
const QScriptEngine& getScriptEngine() const;
private:
diff --git a/src/skin/svgparser.cpp b/src/skin/svgparser.cpp
index 8bf6d9c5bf..e05fee3090 100644
--- a/src/skin/svgparser.cpp
+++ b/src/skin/svgparser.cpp
@@ -10,13 +10,13 @@ SvgParser::SvgParser() {
SvgParser::SvgParser(const SkinContext& parent)
: m_context(parent) {
+ m_context.importScriptExtension("console");
+ m_context.importScriptExtension("svg");
}
SvgParser::~SvgParser() {
}
-// look for the document of a node
-
QDomNode SvgParser::parseSvgFile(const QString& svgFileName) const {
QFile* file = new QFile(svgFileName);
QDomNode out;
@@ -26,19 +26,14 @@ QDomNode SvgParser::parseSvgFile(const QString& svgFileName) const {
QDomNode svgNode = document.elementsByTagName("svg").item(0);
out = parseSvgTree(svgNode);
file->close();
- return out;
}
-
return out;
}
QDomNode SvgParser::parseSvgTree(const QDomNode& svgSkinNode) const {
-
// clone svg to don't alter xml input
QDomNode svgNode = svgSkinNode.cloneNode(true);
-
scanTree(svgNode, &SvgParser::parseElement);
-
return svgNode;
}
@@ -46,11 +41,9 @@ void SvgParser::scanTree(const QDomNode& node, void (SvgParser::*callback)(const
(this->*callback)(node);
QDomNodeList children = node.childNodes();
- QDomNode child;
- uint i;
- for (i=0; i<children.length(); i++) {
- child = children.at(i);
+ for (uint i=0; i<children.length(); i++) {
+ QDomNode child = children.at(i);
if (child.isElement()) {
scanTree( child, callback);
}
@@ -82,7 +75,7 @@ void SvgParser::parseElement(const QDomNode& node) const {
}
} else if (element.tagName() == "script"){
-
+ // Look for a filepath in the "src" attribute
QString scriptPath = node.toElement().attribute("src");
if (!scriptPath.isNull()) {
QFile scriptFile(m_context.getSkinPath(scriptPath));
@@ -94,7 +87,7 @@ void SvgParser::parseElement(const QDomNode& node) const {
<< "in" << scriptPath;
}
}
-
+ // Evaluates the content of the script element
QString expression = m_context.nodeToString(node);
QScriptValue result = m_context.evaluateScript(expression);
if (m_context.getScriptEngine().hasUncaughtException()) {
@@ -108,41 +101,33 @@ void SvgParser::parseAttributes(const QDomNode& node) const {
QDomNamedNodeMap attributes = node.attributes();
QDomElement element = node.toElement();
- uint i;
- int pos;
- QString varName, varValue, attributeValue, attributeName,
- propName, match, replacement;
- QStringList captured;
- QDomAttr attribute;
-
-
- QRegExp hookRx, nameRx;
QScriptValue global = m_context.getScriptEngine().globalObject();
- QScriptValue hookNames = global.property("hookNames").call(global);
+ QScriptValue hookNames = global.property("svg").property("hookNames")
+ .call(global.property("svg"));
+ // Preparing the pattern of hooks
+ QRegExp hookRx;
if (hookNames.toString().length()) {
QString hooksPattern = hookNames.property("toPattern")
.call(hookNames).toString();
-
// hook_name( arg1 [, arg2]... )
hookRx.setPattern("("+hooksPattern+")\\(([^\\(\\)]+)\\)\\s*;?");
// qDebug() << "hooksPattern : " << hooksPattern << "\n";
}
// expr-attribute_name="var_name";
- nameRx.setPattern("^expr-([^=\\s]+)$");
-
- for (i=0; i < attributes.length(); i++) {
+ QRegExp nameRx("^expr-([^=\\s]+)$");
+ for (uint i=0; i < attributes.length(); i++) {
- attribute = attributes.item(i).toAttr();
- attributeValue = attribute.value();
- attributeName = attribute.name();
+ QDomAttr attribute = attributes.item(i).toAttr();
+ QString attributeValue = attribute.value();
+ QString attributeName = attribute.name();
// searching variable attributes :
// expr-attribute_name="variable_name|expression"
if (nameRx.indexIn(attributeName) != -1) {
- varValue = evaluateTemplateExpression(attributeValue).toString();
+ QString varValue = evaluateTemplateExpression(attributeValue).toString();
if (varValue.length()) {
element.setAttribute(nameRx.cap(1), varValue);
}
@@ -151,13 +136,13 @@ void SvgParser::parseAttributes(const QDomNode& node) const {
if (!hookRx.isEmpty()) {
// searching hooks in the attribute value
- pos = 0;
+ int pos = 0;
while ((pos = hookRx.indexIn(attributeValue, pos)) != -1) {
- captured = hookRx.capturedTexts();
- match = hookRx.cap(0);
- QString tmp = "templateHooks." + match;
+ QStringList captured = hookRx.capturedTexts();
+ QString match = hookRx.cap(0);
+ QString tmp = "svg.templateHooks." + match;
// qDebug() << "expression : " << tmp << "\n";
- replacement = evaluateTemplateExpression(tmp).toString();
+ QString replacement = evaluateTemplateExpression(tmp).toString();
attributeValue.replace(pos, match.length(), replacement);
pos += replacement.length();
}
@@ -170,26 +155,22 @@ void SvgParser::parseAttributes(const QDomNode& node) const {
QString SvgParser::saveToTempFile(const QDomNode& svgNode) const {
-
// Save the new svg in a temp file to use it with setPixmap
QTemporaryFile svgFile;
svgFile.setFileTemplate(QDir::temp().filePath("qt_temp.XXXXXX.svg"));
-
// the file will be removed before being parsed in skin if set to true
svgFile.setAutoRemove(false);
- QString svgTempFileName;
if (svgFile.open()) {
// qWarning() << "SVG : Temp filename" << svgFile.fileName() << " \n";
QTextStream out(&svgFile);
svgNode.save(out, 2);
svgFile.close();
- svgTempFileName = svgFile.fileName();
} else {
qDebug() << "Unable to open temp file for inline svg \n";
}
- return svgTempFileName;
+ return svgFile.fileName();
}
QByteArray SvgParser::saveToQByteArray(const QDomNode& svgNode) const {
@@ -217,18 +198,16 @@ QScriptValue SvgParser::evaluateTemplateExpression(QString expression) const {
/**
* Retrieves the document of a node.
* This method is required to replace a node by another : the document element
- * is th only one able to do createTextNode ).
+ * is th only one able to do createTextNode.
*/
QDomDocument SvgParser::getDocument(const QDomNode& node) const {
-
- QDomDocument document;
QDomNode parentNode = node;
+ QDomDocument document;
while (!parentNode.isNull()) {
if (parentNode.isDocument()) {
document = parentNode.toDocument();
}
parentNode = parentNode.parentNode();
}
-
return document;
}
diff --git a/src/skin/svgparser.h b/src/skin/svgparser.h
index d9abd9bc61..110d7a71c1 100644
--- a/src/skin/svgparser.h
+++ b/src/skin/svgparser.h
@@ -20,7 +20,6 @@ class SvgParser {
QDomDocument getDocument(const QDomNode& node) const;
void scanTree(const QDomNode& node, void (SvgParser::*callback)(const QDomNode& node)const) const;
- // QDomNode parseSvgTree(const QDomNode& svgSkinNode) const;
QDomNode parseSvgTree(const QDomNode& svgSkinNode) const;
QDomNode parseSvgFile(const QString& svgFileName) const;
QString saveToTempFile(const QDomNode& svgNode) const;