diff options
-rw-r--r-- | script/svg/__init__.js | 85 | ||||
-rw-r--r-- | src/skin/skincontext.cpp | 54 | ||||
-rw-r--r-- | src/skin/skincontext.h | 7 | ||||
-rw-r--r-- | src/skin/svgparser.cpp | 13 |
4 files changed, 100 insertions, 59 deletions
diff --git a/script/svg/__init__.js b/script/svg/__init__.js index 3da56904f0..673b0d169f 100644 --- a/script/svg/__init__.js +++ b/script/svg/__init__.js @@ -3,55 +3,58 @@ __setupPackage__(__extension__); * This extension provides the api to add hooks into attributes in * the SVG parser. */ -svg.templateHooks = {}; +(function(){ -svg.regexpQuote = function (str, delimiter) { - return String(str).replace( - new RegExp( - '[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', - 'g' - ), - '\\$&' - ); -} + svg.templateHooks = {}; -svg.getHooksPattern = function(){ - var hookNames = [], - that = this; - for( var i in this.templateHooks ) - hookNames.push(i); - - // hook_name( arg1 [, arg2]... ) - if( hookNames.length ){ - var pattern = "("+hookNames.join('|')+")\\(([^\\(\\)]+)\\)\\s*;?"; - return pattern; + svg.regexpQuote = function (str, delimiter) { + return String(str).replace( + new RegExp( + '[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', + 'g' + ), + '\\$&' + ); } -} -global = this; -svg.templateHooks.variable = function( varName ){ - if( varName in global ){ - return global[varName]; + svg.getHooksPattern = function(){ + var hookNames = [], + that = this; + for( var i in this.templateHooks ) + hookNames.push(i); + + // hook_name( arg1 [, arg2]... ) + if( hookNames.length ){ + var pattern = "("+hookNames.join('|')+")\\(([^\\(\\)]+)\\)\\s*;?"; + return pattern; + } + } + + var global = this; + svg.templateHooks.variable = function( varName ){ + if( varName in global ){ + return global[varName]; + } + return ''; } - return ''; -} -svg.templateHooks.prop = function( propName, varName ){ - var out = ''; - - if( (varName in global) ){ - var value = global[varName]; + svg.templateHooks.prop = function( propName, varName ){ + var out = ''; - if( isNumber(value) ){ - out = propName + ':' + value + ';'; - } else if( value.length ) { - out = propName + ':' + value + ';'; + if( (varName in global) ){ + var value = global[varName]; + + if( isNumber(value) ){ + out = propName + ':' + value + ';'; + } else if( value.length ) { + out = propName + ':' + value + ';'; + } + + } else { + console.log( 'Unable to find ' + varName + ' for prop hook.' ); } - } else { - console.log( 'Unable to find ' + varName + ' for prop hook.' ); + return out; } - - return out; -} +})(); diff --git a/src/skin/skincontext.cpp b/src/skin/skincontext.cpp index a265a39f7f..837739bed7 100644 --- a/src/skin/skincontext.cpp +++ b/src/skin/skincontext.cpp @@ -17,10 +17,7 @@ SkinContext::SkinContext(ConfigObject<ConfigValue>* pConfig, m_debugger(QSharedPointer<QScriptEngineDebugger>( new QScriptEngineDebugger())), m_xmlPath(xmlPath) { - if (CmdlineArgs::Instance().getDeveloper() && m_pConfig->getValueString( - ConfigKey("[ScriptDebugger]", "Enabled")) == "1") { - enableDebugger(true); - } + enableDebugger(true); // m_scriptEngine->installTranslatorFunctions(); } @@ -31,25 +28,37 @@ SkinContext::SkinContext(const SkinContext& parent) m_scriptEngine(parent.m_scriptEngine), m_debugger(parent.m_debugger), m_xmlPath(parent.m_xmlPath) { + // m_scriptEngine = parent.m_scriptEngine; + // m_scriptContext = QSharedPointer<QScriptContext>( + // m_scriptEngine->pushContext()); QScriptValue context = m_scriptEngine->pushContext()->activationObject(); + // QScriptValue context = m_scriptEngine->currentContext()->activationObject(); + + // m_parentGlobal = m_scriptEngine->globalObject(); + // QScriptValue newGlobal = m_scriptEngine->newObject(); + // newGlobal.setPrototype(m_parentGlobal); + // m_scriptEngine->setGlobalObject(context); for (QHash<QString, QString>::const_iterator it = m_variables.begin(); it != m_variables.end(); ++it) { - if(it.key() == "color") - qDebug() << "!!!!!!!!!!!!!!!!!!!!!!!! Setting variable : " << it.key() << " => " << it.value(); - else - qDebug() << "Setting variable : " << it.key() << " => " << it.value(); + qDebug() << "Setting variable : " << it.key() << " => " << it.value(); context.setProperty(it.key(), it.value()); } + + // QScriptContext* currentScriptContext = m_scriptEngine->currentContext(); + // qDebug() << "!!!!!!!!!!!!!!!!!!!!!! : " << (&m_scriptContext == ¤tScriptContext); + // qDebug() << "!!!!!!!!!!!!!!!!!!!!!! : " << m_context.toString() ; } SkinContext::~SkinContext() { + // m_scriptEngine->setGlobalObject(m_parentGlobal); m_scriptEngine->popContext(); } SkinContext& SkinContext::operator=(const SkinContext& other) { m_variables = other.variables(); QScriptValue context = m_scriptEngine->currentContext()->activationObject(); + // QScriptValue context = m_scriptContext->activationObject(); for (QHash<QString, QString>::const_iterator it = m_variables.begin(); it != m_variables.end(); ++it) { context.setProperty(it.key(), it.value()); @@ -64,8 +73,9 @@ QString SkinContext::variable(const QString& name) const { void SkinContext::setVariable(const QString& name, const QString& value) { m_variables[name] = value; QScriptValue context = m_scriptEngine->currentContext()->activationObject(); + // QScriptValue context = m_scriptContext->activationObject(); context.setProperty(name, value); - qDebug() << "!!!!!!!!!!!!!!!!!----- Setting variable : " << name << " => " << value; + // qDebug() << "!!!!!!!!!!!!!!!!!----- Setting variable : " << name << " => " << value; } void SkinContext::setXmlPath(const QString& xmlPath) { @@ -275,6 +285,19 @@ PixmapSource SkinContext::getPixmapSource(const QDomNode& pixmapNode) const { QScriptValue SkinContext::evaluateScript(const QString& expression, const QString& filename/*=QString()*/, int lineNumber/*=1*/) { + + /** / + QString programId = filename + QString::number(lineNumber); + qDebug() << "!!!!!!!!!!!!!!!!!! : " << programId; + + + if(!m_scriptPrograms.contains(programId)){ + QScriptProgram program(expression, filename, lineNumber); + m_scriptPrograms[programId] = program; + } + + return m_scriptEngine->evaluate(m_scriptPrograms[programId]); + /**/ return m_scriptEngine->evaluate(expression, filename, lineNumber); } @@ -291,9 +314,14 @@ const QScriptEngine* SkinContext::getScriptEngine() const { } void SkinContext::enableDebugger(bool state) const { - if( state ) - m_debugger->attachTo(m_scriptEngine.data()); - else - m_debugger->detach(); + if (CmdlineArgs::Instance().getDeveloper() && m_pConfig->getValueString( + ConfigKey("[ScriptDebugger]", "Enabled")) == "1") { + if( state ) { + m_debugger->attachTo(m_scriptEngine.data()); + m_debugger->action(QScriptEngineDebugger::StepOutAction); + } else { + m_debugger->detach(); + } + } } diff --git a/src/skin/skincontext.h b/src/skin/skincontext.h index 127b732745..945e1f6cdd 100644 --- a/src/skin/skincontext.h +++ b/src/skin/skincontext.h @@ -81,10 +81,15 @@ class SkinContext { QHash<QString, QString> m_variables; QString m_skinBasePath; ConfigObject<ConfigValue>* m_pConfig; - mutable QSharedPointer<QScriptEngine> m_scriptEngine; // todo m_pScriptEngine + // mutable QSharedPointer<QScriptEngine> m_scriptEngine; // todo m_pScriptEngine + QSharedPointer<QScriptEngine> m_scriptEngine; // todo m_pScriptEngine QSharedPointer<QScriptEngineDebugger> m_debugger; // idem + QSharedPointer<QScriptContext> m_scriptContext; QString m_xmlPath; + QScriptValue m_context; + QHash<QString, QScriptProgram> m_scriptPrograms; + QScriptValue m_parentGlobal; }; #endif /* SKINCONTEXT_H */ diff --git a/src/skin/svgparser.cpp b/src/skin/svgparser.cpp index 5cca4899d1..dec4e3faeb 100644 --- a/src/skin/svgparser.cpp +++ b/src/skin/svgparser.cpp @@ -98,7 +98,8 @@ void SvgParser::parseElement(QDomNode* node) const { } else if (element.tagName() == "script") { // Look for a filepath in the "src" attribute - QString scriptPath = node->toElement().attribute("src"); + // QString scriptPath = node->toElement().attribute("src"); + QString scriptPath = element.attribute("src"); if (!scriptPath.isNull()) { QFile scriptFile(m_context.getSkinPath(scriptPath)); scriptFile.open(QIODevice::ReadOnly|QIODevice::Text); @@ -107,9 +108,13 @@ void SvgParser::parseElement(QDomNode* node) const { scriptPath); } // Evaluates the content of the script element - QString expression = m_context.nodeToString(*node); - QScriptValue result = m_context.evaluateScript( - expression, m_currentFile, node->lineNumber()); + // QString expression = m_context.nodeToString(*node); + QString expression = element.text(); + QString result = evaluateTemplateExpression( + expression, node->lineNumber()).toString(); + // qDebug() << expression; + // QScriptValue result = m_context.evaluateScript( + // expression, m_currentFile, node->lineNumber()); } } |