diff --git a/CMakeLists.txt b/CMakeLists.txt index a99f45c1681c3a0ae064616d286cdd957a939bc3..b8224ffefe6c0f7bec19d39adbd502949f95b8bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,4 @@ include (plugin) openflipper_plugin (PYTHONINTERFACE DIRS config parser scene types types/objectId - INSTALLDATA Icons VsiMetadata - QT5ONLY True) + INSTALLDATA Icons VsiMetadata) diff --git a/baseWidget.cc b/baseWidget.cc index 52ef8f6fbc9f2fb793697c1ad8e4133f2a25333a..b62f1ae354dfe307d23de9f2f77cf8867219e9c1 100644 --- a/baseWidget.cc +++ b/baseWidget.cc @@ -53,13 +53,9 @@ #include <QApplication> #include <QClipboard> -#include <QScriptEngine> #include <QDomDocument> -#include <QXmlQuery> -#include <QXmlResultItems> - #include <OpenFlipper/common/GlobalOptions.hh> #include "baseWidget.hh" @@ -278,25 +274,17 @@ void VSI::BaseWidget::load() scenes_.pop (); views_->setCurrentWidget (mainScene_->graphicsView()); - QXmlQuery query; - query.setFocus (&f); - query.setQuery ("VisualScript"); + QDomDocument doc("OpenFlipper"); + if (!doc.setContent(&f)) { + return; + } - QXmlResultItems root; + // Iterate over all elements in the file (One Level below the OpenFlipper Tag + QDomElement docElem = doc.documentElement(); + mainScene_->loadFromXml (docElem); - if (query.isValid ()) - { - query.evaluateTo (&root); - QXmlItem item (root.next ()); - if (!item.isNull ()) - { - QXmlQuery q (query); - q.setFocus (item); - mainScene_->loadFromXml (q); - } - } changedContent_ = false; fileName_ = filename; diff --git a/parser/context.cc b/parser/context.cc index aba76ced41d6abf33702178f52481f30995fdaba..bf49d465dd34bcfb7ffecbaff612176353fddaa1 100644 --- a/parser/context.cc +++ b/parser/context.cc @@ -42,10 +42,6 @@ //== INCLUDES ================================================================= -#include <QBuffer> -#include <QXmlQuery> -#include <QXmlResultItems> - #include "context.hh" #include "input.hh" #include "output.hh" @@ -63,6 +59,8 @@ #include "types/objectId/objectId.hh" #include "types/any.hh" +#include <QRegularExpression> + #define DATA_NAME "DataFlow" //== NAMESPACES =============================================================== @@ -178,60 +176,53 @@ QStringList Context::categories () //------------------------------------------------------------------------------ -/// Parse xml content -void Context::parse (QByteArray _xml) +/// Parse xml file content +void Context::parse (QFile& _xml) { - QBuffer device (&_xml); - device.open(QIODevice::ReadOnly); - - QXmlQuery query; - query.setFocus (&device); - - // for each OpenFlipper/element - query.setQuery ("OpenFlipper//element"); - QXmlResultItems elements; - - if (query.isValid ()) - { - query.evaluateTo (&elements); - QXmlItem item (elements.next ()); - while (!item.isNull ()) - { - QXmlQuery q (query); - q.setFocus (item); - parseElement (q); - item = elements.next (); + QDomDocument doc("OpenFlipper"); + if (!doc.setContent(&_xml)) { + return; } - } + // Iterate over all elements in the file (One Level below the OpenFlipper Tag + QDomElement docElem = doc.documentElement(); + QDomNode n = docElem.firstChild(); + while(!n.isNull()) { + QDomElement e = n.toElement(); // try to convert the node to an element. + if(!e.isNull()) { + parseElement(e); + } + n = n.nextSibling(); + } } //------------------------------------------------------------------------------ -// parse element from xml -void Context::parseElement (QXmlQuery &_xml) +/// parse element from xml +void Context::parseElement (QDomElement &_domElement) { - if (getXmlString (_xml, "string(@name)").isEmpty ()) - return; - Element *e = new Element (this, getXmlString (_xml, "string(@name)")); + // If the element has no name, something is wrong here! + if ( !_domElement.hasAttribute("name")) + return; - elements_.append (e); + // Create element in the database with the new elements name + Element *e = new Element (this, _domElement.attribute("name")); + elements_.append (e); - e->category_ = getXmlString (_xml, "category/string(text())", "Undefined"); + e->category_ = getXmlString (_domElement, "category", "Undefined"); - e->shortDesc_ = getXmlString (_xml, "short/string(text())", e->name ()); - e->longDesc_ = getXmlString (_xml, "long/string(text())", e->shortDesc_); + e->shortDesc_ = getXmlString (_domElement, "short", e->name ()); + e->longDesc_ = getXmlString (_domElement, "long", e->shortDesc_); // scene graph in/output for scenegraph elements - if (strToBool (getXmlString (_xml, "dataflow/string(text())"))) + if (strToBool (getXmlString (_domElement, "dataflow"))) { - e->dataIn_ = new Input (e); e->dataIn_->name_ = "data"; - e->dataIn_->type_ = QVariant::Invalid; + e->dataIn_->type_ = "---"; e->dataIn_->shortDesc_ = DATA_NAME; e->dataIn_->longDesc_ = DATA_NAME; @@ -239,87 +230,72 @@ void Context::parseElement (QXmlQuery &_xml) e->dataOut_ = new Output (e); e->dataOut_->name_ = "data"; - e->dataOut_->type_ = QVariant::Invalid; + e->dataOut_->type_ = "---"; e->dataOut_->shortDesc_ = DATA_NAME; e->dataOut_->longDesc_ = DATA_NAME; } - // parse all inputs - _xml.setQuery ("inputs/input"); + // Search through all children of the current element if we have inputs, outputs or functions + for(QDomElement n = _domElement.firstChildElement(); !n.isNull(); n = n.nextSiblingElement() ) + { + // Found an input Tag! + if (n.tagName() == "inputs") { - QXmlResultItems inouts; + // Iterate over all inputs inside it + for(QDomElement inputElement = n.firstChildElement(); !inputElement.isNull(); inputElement = inputElement.nextSiblingElement() ) + { + Input* i =parseInput(inputElement,e); - if (_xml.isValid ()) - { - _xml.evaluateTo (&inouts); - QXmlItem item (inouts.next ()); - while (!item.isNull ()) - { - QXmlQuery q (_xml); - q.setFocus (item); - Input *i = parseInput (q, e); - if (i) - { - e->inputs_.append (i); - } - item = inouts.next (); - } + if (i) { + e->inputs_.append (i); + } + } + } - } + // Found an input Tag! + if (n.tagName() == "outputs") { - // parse all outputs - _xml.setQuery ("outputs/output"); + // Iterate over all outputs inside it + for(QDomElement outputElement = n.firstChildElement(); !outputElement.isNull(); outputElement = outputElement.nextSiblingElement() ) + { + Output *o = parseOutput(outputElement,e); - if (_xml.isValid ()) - { - _xml.evaluateTo (&inouts); - QXmlItem item (inouts.next ()); - while (!item.isNull ()) - { - QXmlQuery q (_xml); - q.setFocus (item); - Output *o = parseOutput (q, e); - if (o) - { - o->element_ = e; - e->outputs_.append (o); + if (o) { + o->element_ = e; + e->outputs_.append (o); + } + } } - item = inouts.next (); - } - } - // parse all functions - _xml.setQuery ("functions/function"); + // Found an input Tag! + if (n.tagName() == "functions") { + + // Iterate over all outputs inside it + for(QDomElement functionElement = n.firstChildElement(); !functionElement.isNull(); functionElement = functionElement.nextSiblingElement() ) + { + Function *f = parseFunction (functionElement, e); + if (f) + { + e->functions_.append (f); + } + } - if (_xml.isValid ()) - { - _xml.evaluateTo (&inouts); - QXmlItem item (inouts.next ()); - while (!item.isNull ()) - { - QXmlQuery q (_xml); - q.setFocus (item); - Function *f = parseFunction (q, e); - if (f) - { - e->functions_.append (f); } - item = inouts.next (); - } } + // get code & precode segment - e->precode_ = getXmlString (_xml, "precode/string(text())", ""); - e->code_ = getXmlString (_xml, "code/string(text())", ""); + e->precode_ = getXmlString (_domElement, "precode", ""); + e->code_ = getXmlString (_domElement, "code", ""); - if (e->precode_.contains(QRegExp("^\\s*\\t"))) { + if (e->precode_.contains(QRegularExpression("^\\s*\\t"))) { // precode contains tab symbol emit loggingInterface_->log(LOGWARN, "Precode block of "+e->name()+" contains tab identation"); } - if (e->code_.contains(QRegExp("^\\s*\\t"))) { + if (e->code_.contains(QRegularExpression("^\\s*\\t"))) { // precode contains tab symbol emit loggingInterface_->log(LOGWARN, "Code block of "+e->name()+" contains tab identation"); } @@ -330,56 +306,56 @@ void Context::parseElement (QXmlQuery &_xml) } -//------------------------------------------------------------------------------ +////------------------------------------------------------------------------------ -// parse element input from xml -Input* Context::parseInput (QXmlQuery &_xml, Element *_e) +//// parse element input from xml +Input* Context::parseInput(QDomElement& _domElement, Element *_e) { - Input *i = new Input (_e); + Input *i = new Input (_e); - // common part - if (!parseInOutBase (_xml, i)) - { - delete i; - return NULL; - } + // common part + if (!parseInOutBase (_domElement, i)) + { + delete i; + return NULL; + } - // input states - QString stateStr = getXmlString (_xml, "string(@external)"); - unsigned int state = 0; - - if (!stateStr.isEmpty () && !strToBool (stateStr)) - state |= Input::NoExternalInput; + // input states + QString stateStr = _domElement.attribute("external"); + unsigned int state = 0; - stateStr = getXmlString (_xml, "string(@user)"); + if (!stateStr.isEmpty () && !strToBool (stateStr)) + state |= Input::NoExternalInput; - if (!stateStr.isEmpty () && !strToBool (stateStr)) - state |= Input::NoUserInput; + stateStr = _domElement.attribute("user"); - stateStr = getXmlString (_xml, "string(@runtime)"); + if (!stateStr.isEmpty () && !strToBool (stateStr)) + state |= Input::NoUserInput; - if (!stateStr.isEmpty () && !strToBool (stateStr)) - state |= Input::NoRuntimeUserInput; + stateStr = _domElement.attribute("runtime"); - stateStr = getXmlString (_xml, "string(@optional)"); + if (!stateStr.isEmpty () && !strToBool (stateStr)) + state |= Input::NoRuntimeUserInput; - if (!stateStr.isEmpty () && strToBool (stateStr)) - state |= Input::Optional; + stateStr = _domElement.attribute("optional"); - i->state_ = state; + if (!stateStr.isEmpty () && strToBool (stateStr)) + state |= Input::Optional; - return i; + i->state_ = state; + + return i; } -//------------------------------------------------------------------------------ +////------------------------------------------------------------------------------ -// parse element output from xml -Output* Context::parseOutput (QXmlQuery &_xml, Element *_e) +//// parse element output from xml +Output* Context::parseOutput (QDomElement& _domElement, Element *_e) { Output *o = new Output (_e); // common part - if (!parseInOutBase (_xml, o)) + if (!parseInOutBase (_domElement, o)) { delete o; return NULL; @@ -388,29 +364,28 @@ Output* Context::parseOutput (QXmlQuery &_xml, Element *_e) return o; } -//------------------------------------------------------------------------------ +////------------------------------------------------------------------------------ -// parse common input and output parts from xml -bool Context::parseInOutBase (QXmlQuery &_xml, InOut *_io) +//// parse common input and output parts from xml +bool Context::parseInOutBase (QDomElement &_domElement, InOut *_io) { - QString type = getXmlString (_xml, "string(@type)"); + QString type = _domElement.attribute("type",""); - if (getXmlString (_xml, "string(@name)").isEmpty () || - type.isEmpty ()) - return false; + if ( !_domElement.hasAttribute("name") || type.isEmpty() ) + return false; - _io->name_ = getXmlString (_xml, "string(@name)"); - _io->type_ = type; + _io->name_ = _domElement.attribute("name"); + _io->type_ = type; - _io->shortDesc_ = getXmlString (_xml, "short/string(text())", _io->name ()); - _io->longDesc_ = getXmlString (_xml, "long/string(text())", _io->shortDesc_); + _io->shortDesc_ = getXmlString (_domElement,"short", _io->name ()); + _io->longDesc_ = getXmlString (_domElement, "long", _io->shortDesc_); // get type hints for supported types if (typeSupported (type)) { foreach (QString hint, supportedTypes_[type]->supportedHints ()) { - QString value = getXmlString (_xml, hint + "/string(text())", ""); + QString value = getXmlString (_domElement, hint , ""); if (!value.isEmpty ()) _io->hints_[hint] = value; } @@ -419,19 +394,19 @@ bool Context::parseInOutBase (QXmlQuery &_xml, InOut *_io) return true; } -//------------------------------------------------------------------------------ +////------------------------------------------------------------------------------ -// parse element function from xml -Function* Context::parseFunction (QXmlQuery &_xml, Element *_e) +//// parse element function from xml +Function* Context::parseFunction (QDomElement& _domElement, Element *_e) { - QString name = getXmlString (_xml, "string(@name)"); + QString name = _domElement.attribute("name"); if (name.isEmpty ()) return NULL; Function *f = new Function (_e, name); - f->shortDesc_ = getXmlString (_xml, "short/string(text())", f->name ()); - f->longDesc_ = getXmlString (_xml, "long/string(text())", f->shortDesc_); + f->shortDesc_ = getXmlString (_domElement, "short", f->name ()); + f->longDesc_ = getXmlString (_domElement, "long", f->shortDesc_); // add start element f->start_ = new Element (_e->context (), "start_" + _e->name () + "_" + name); @@ -471,53 +446,49 @@ Function* Context::parseFunction (QXmlQuery &_xml, Element *_e) elements_.append (f->end_); - // inputs - _xml.setQuery ("inputs/input"); - QXmlResultItems inouts; - if (_xml.isValid ()) + // Search through all children of the current element if we have inputs, outputs or functions + for(QDomElement n = _domElement.firstChildElement(); !n.isNull(); n = n.nextSiblingElement() ) { - _xml.evaluateTo (&inouts); - QXmlItem item (inouts.next ()); - while (!item.isNull ()) - { - QXmlQuery q (_xml); - q.setFocus (item); - Output *o = new Output (f->start_); - if (parseInOutBase(q, o)) - { - f->start_->outputs_.append (o); + // Found an input Tag! + if (n.tagName() == "inputs") { + + // Iterate over all inputs inside it + for(QDomElement inputElement = n.firstChildElement(); !inputElement.isNull(); inputElement = inputElement.nextSiblingElement() ) + { + Output *o = new Output (f->start_); + if (parseInOutBase(inputElement, o)) + { + f->start_->outputs_.append (o); + } + else + delete o; + } + + // Only one input element allowed + break; } - else - delete o; - item = inouts.next (); - } - - } - - // outputs - _xml.setQuery ("outputs/output"); - if (_xml.isValid ()) - { - _xml.evaluateTo (&inouts); - QXmlItem item (inouts.next ()); - while (!item.isNull ()) - { - QXmlQuery q (_xml); - q.setFocus (item); - Input *i = new Input (f->end_); - if (parseInOutBase(q, i)) - { - i->state_ = Input::NoUserInput | Input::NoRuntimeUserInput; - f->end_->inputs_.append (i); + // Found an input Tag! + if (n.tagName() == "outputs") { + + // Iterate over all outputs inside it + for(QDomElement outputElement = n.firstChildElement(); !outputElement.isNull(); outputElement = outputElement.nextSiblingElement() ) + { + Input *i = new Input (f->end_); + if (parseInOutBase(outputElement, i)) + { + i->state_ = Input::NoUserInput | Input::NoRuntimeUserInput; + f->end_->inputs_.append (i); + } + else + delete i; + } + + // Only one output element allowed + break; } - else - delete i; - item = inouts.next (); - } - } // add end node only if we have outputs @@ -539,17 +510,18 @@ Function* Context::parseFunction (QXmlQuery &_xml, Element *_e) //------------------------------------------------------------------------------ -/// Gets the string of a xml query -QString Context::getXmlString (QXmlQuery &_xml, QString _expr, QString _default) +/// Gets the string of a tag or returns the default +QString Context::getXmlString (QDomElement &_domElement, QString _tag, QString _default) { - QStringList sl; - _xml.setQuery (_expr); - - if (_xml.isValid () && _xml.evaluateTo (&sl) && sl.length () == 1) - return sl[0]; + // Secrh through all children of the current element if it matches the given tag + for(QDomElement n = _domElement.firstChildElement(); !n.isNull(); n = n.nextSiblingElement() ) + { + if (n.tagName() == _tag) + return n.text(); + } - return _default; + return _default; } //------------------------------------------------------------------------------ @@ -620,7 +592,7 @@ QString Context::removeCommonTrailingSpaces(QString in) { int commonTrailingSpaces = INF; for (int line=0;line<lines.length();line++) { - int lenWithoutTrailingSpaces = QString(lines[line]).replace(QRegExp ("^\\s*"), "").length(); + int lenWithoutTrailingSpaces = QString(lines[line]).replace(QRegularExpression ("^\\s*"), "").length(); if (lenWithoutTrailingSpaces > 0) { // line not empty @@ -649,8 +621,7 @@ QString Context::removeCommonTrailingSpaces(QString in) { lines[line] = QString(""); } else { // remove common trailing whitespaces - QStringRef ref = QStringRef(&lines[line], commonTrailingSpaces, subLength); - lines[line] = ref.toString(); + lines[line] = lines[line].right(subLength); } } diff --git a/parser/context.hh b/parser/context.hh index 0c527cd4405761c07aeeba1b65bf5a69ed2222b4..938246b52a860b5319861eab38ce40c45f3f20f3 100644 --- a/parser/context.hh +++ b/parser/context.hh @@ -47,9 +47,11 @@ //== INCLUDES ================================================================= #include <QVector> #include <QStringList> -#include <QXmlQuery> +#include <QDomDocument> +#include <QFile> #include <OpenFlipper/BasePlugin/LoggingInterface.hh> #include <OpenFlipper/BasePlugin/PythonInterface.hh> +#include <parser/type.hh> #include "element.hh" @@ -76,7 +78,7 @@ class Context { ~Context (); /// Parse xml content - void parse (QByteArray _xml); + void parse (QFile& _xml); /// Returns all available elements const QVector<Element *>& elements () const { return elements_; }; @@ -106,7 +108,7 @@ class Context { static bool strToBool (QString _str); /// Gets the string of a xml query - static QString getXmlString (QXmlQuery &_xml, QString _expr, QString _default = ""); + static QString getXmlString (QDomElement &_element, QString _tag, QString _default = ""); /// Removes trailing spaces from string which are present in each line - relative trailing spaces are not removed static QString removeCommonTrailingSpaces(QString in); @@ -120,19 +122,19 @@ class Context { private: // parse element from xml - void parseElement (QXmlQuery &_xml); + void parseElement (QDomElement &_element); // parse element input from xml - Input *parseInput (QXmlQuery &_xml, Element *_e); + Input *parseInput (QDomElement &_domElement, Element *_e); // parse element output from xml - Output *parseOutput (QXmlQuery &_xml, Element *_e); + Output *parseOutput (QDomElement& _domElement, Element *_e); // parse element function from xml - Function *parseFunction (QXmlQuery &_xml, Element *_e); + Function *parseFunction (QDomElement& _domElement, Element *_e); // parse common input and output parts from xml - bool parseInOutBase (QXmlQuery &_xml, InOut *_io); + bool parseInOutBase (QDomElement &_domElement, InOut *_io); private: QVector <Element *> elements_; diff --git a/scene/elementFunction.cc b/scene/elementFunction.cc index a726c9d932dce33d2813a7a38a9e02fb3a43cf15..b294f9407451998d2245dda0bf083c798d0d3216 100644 --- a/scene/elementFunction.cc +++ b/scene/elementFunction.cc @@ -92,9 +92,9 @@ void ElementFunction::saveToXml(QDomDocument & _doc, QDomElement & _root) //------------------------------------------------------------------------------ /// Load from xml -void ElementFunction::loadFromXml(QXmlQuery & _xml) +void ElementFunction::loadElementFunctionFromXml(QDomElement& _domElement) { - scene_->loadFromXml (_xml); + scene_->loadFromXml (_domElement); } //------------------------------------------------------------------------------ diff --git a/scene/elementFunction.hh b/scene/elementFunction.hh index 14afb479dec8060989e0d958ac80d129cf161109..447beffd7b2654abed73cf5834c8d423d96f0d3c 100644 --- a/scene/elementFunction.hh +++ b/scene/elementFunction.hh @@ -49,7 +49,7 @@ #include <QDomDocument> #include <QDomElement> -#include <QXmlQuery> +#include <QObject> //== NAMESPACES =============================================================== @@ -63,7 +63,7 @@ class GraphicsScene; /** Class that represents a function of an element. * It holds the VSI::GraphicsScene to edit the function */ -class ElementFunction : public QObject{ +class ElementFunction : public QObject { Q_OBJECT @@ -88,7 +88,7 @@ class ElementFunction : public QObject{ void saveToXml (QDomDocument &_doc, QDomElement &_root); /// Load from xml - void loadFromXml (QXmlQuery &_xml); + void loadElementFunctionFromXml (QDomElement& _domElement); public slots: diff --git a/scene/elementInput.cc b/scene/elementInput.cc index e97031aea400e3d9d06df46862d855091b4015e3..66674976629aa941116f084cbcb3d1787825b2eb 100644 --- a/scene/elementInput.cc +++ b/scene/elementInput.cc @@ -252,21 +252,21 @@ void ElementInput::saveToXml(QDomDocument & _doc, QDomElement & _root) //------------------------------------------------------------------------------ /// Load from xml -void ElementInput::loadFromXml(QXmlQuery & _xml) +void ElementInput::loadElementInputFromXml(QDomElement& _domElement) { - QString val = Context::getXmlString (_xml, "is_set/string(text())"); + QString val = Context::getXmlString (_domElement, "is_set"); isSet_ = Context::strToBool (val); if (state () & Input::Optional) { - val = Context::getXmlString (_xml, "force_ask/string(text())"); + val = Context::getXmlString (_domElement, "force_ask"); forceAsk_ = Context::strToBool (val); } if (isSet_) { - value_ = Context::getXmlString (_xml, "value/string(text())"); + value_ = Context::getXmlString (_domElement, "value"); } if (isSet ()) diff --git a/scene/elementInput.hh b/scene/elementInput.hh index 3a25ca77dd17179d96127d4b92dae56ecbcd0c8e..f44d079601f15e4d045dd4c39563ae36e86946fc 100644 --- a/scene/elementInput.hh +++ b/scene/elementInput.hh @@ -49,7 +49,6 @@ #include <QDomDocument> #include <QDomElement> -#include <QXmlQuery> #include "elementInOut.hh" #include "../parser/input.hh" @@ -114,7 +113,7 @@ class ElementInput : public ElementInOut { void saveToXml (QDomDocument &_doc, QDomElement &_root); /// Load from xml - void loadFromXml (QXmlQuery &_xml); + void loadElementInputFromXml (QDomElement& _domElement); private: Input *in_; diff --git a/scene/graphicsScene.cc b/scene/graphicsScene.cc index b12924848780e986b1bf5f0ebbc73f4dd4072fd8..815e515c053e2d97f6aadeaa709e177c45adcc56 100644 --- a/scene/graphicsScene.cc +++ b/scene/graphicsScene.cc @@ -62,8 +62,6 @@ #include <QPaintEngine> #include <QMimeData> -#include <QXmlResultItems> - #include "../parser/context.hh" #include "../parser/output.hh" #include "../parser/function.hh" @@ -686,13 +684,14 @@ QString GraphicsScene::generateCode (QString &errors, bool _codeOnly) } } - if (e->dataOut ()) + if (e->dataOut ()) { foreach (Connection *c, e->dataOut ()->connections ()) if (c->input ()) { c->input ()->setValid (true); break; } + } } QList<SceneElement *> fixE; @@ -778,13 +777,14 @@ QString GraphicsScene::updateConnections (SceneElement *_from, bool _isStart) } } - if (_from->dataOut ()) + if (_from->dataOut ()) { foreach (Connection *c, _from->dataOut ()->connections ()) if (c->input ()) { c->input ()->setValid (true); break; } + } return rv; } @@ -869,11 +869,11 @@ void GraphicsScene::saveToXml (QDomDocument &_doc, QDomElement &_root) //------------------------------------------------------------------------------ /// Load from xml -void VSI::GraphicsScene::loadFromXml (QXmlQuery &_xml) +void VSI::GraphicsScene::loadFromXml (QDomElement& _domElement) { clearScene (true); - QStringList sl = Context::getXmlString (_xml, "transform/string(text ())").split ("|"); + QStringList sl = Context::getXmlString (_domElement, "transform").split ("|"); qreal m[9]; @@ -894,45 +894,35 @@ void VSI::GraphicsScene::loadFromXml (QXmlQuery &_xml) double x, y; QString val; - val = Context::getXmlString (_xml, "x/string(text())"); + val = Context::getXmlString (_domElement, "x"); x = val.toDouble (&ok1); - val = Context::getXmlString (_xml, "y/string(text())"); + val = Context::getXmlString (_domElement, "y"); y = val.toDouble (&ok2); if (ok1 && ok2) elementArea_->setPos (x, y); - _xml.setQuery ("elements/element"); - QXmlResultItems el; + // To make parsing easier, we collect all connections, while we traverse the element. + std::vector<QString> connections; - if (_xml.isValid ()) + // Search through all children of the current element if we have inputs, outputs or functions + for(QDomElement n = _domElement.firstChildElement(); !n.isNull(); n = n.nextSiblingElement() ) { - _xml.evaluateTo (&el); - QXmlItem item (el.next ()); - while (!item.isNull ()) - { - QXmlQuery q (_xml); - q.setFocus (item); - loadElement (q); - item = el.next (); - } - } + // Found an input Tag! + if (n.tagName() == "elements") { - _xml.setQuery ("elements/element/inputs/input/connection"); + // Iterate over all elements inside it (e.g. this are the algorithm boxes. + for(QDomElement sceneElement = n.firstChildElement(); !sceneElement.isNull(); sceneElement = sceneElement.nextSiblingElement() ) + { + loadElement (sceneElement,connections); + } + } + } - if (_xml.isValid ()) - { - _xml.evaluateTo (&el); - QXmlItem item (el.next ()); - while (!item.isNull ()) - { - QXmlQuery q (_xml); - q.setFocus (item); - loadConnection (q); - item = el.next (); - } + for (auto connection : connections) { + loadConnection(connection); } QTimer::singleShot(0, this, SLOT (updateConnections())); @@ -941,82 +931,89 @@ void VSI::GraphicsScene::loadFromXml (QXmlQuery &_xml) //------------------------------------------------------------------------------ // load element from xml -void GraphicsScene::loadElement(QXmlQuery & _xml) +void GraphicsScene::loadElement(QDomElement & _domElement,std::vector<QString>& _connections) { - QString name = Context::getXmlString (_xml, "string(@name)"); - if (name.isEmpty () || !ctx_->element (name)) - return; + QString name = _domElement.attribute("name"); + if (name.isEmpty () || !ctx_->element (name)) + return; - SceneElement *e = new SceneElement (this, ctx_->element (name)); - addElement (e); - e->loadFromXml (_xml); + SceneElement *e = new SceneElement (this, ctx_->element (name)); + addElement (e); + + e->loadFromXml (_domElement,_connections); } //------------------------------------------------------------------------------ // load connection from xml -void GraphicsScene::loadConnection(QXmlQuery & _xml) +void GraphicsScene::loadConnection(QString& _connection) { - bool ok; - int inId, outId; + bool ok; + int inId, outId; - QString inEl = Context::getXmlString (_xml, "../../../string(@name)"); - QString inElId = Context::getXmlString (_xml, "../../../id/string(text ())"); - QString inIn = Context::getXmlString (_xml, "../string(@name)"); + QStringList connectionInfo = _connection.split(";"); - inId = inElId.toInt (&ok); - if (!ok) - return; + if (connectionInfo.size() != 6 ) { + return; + } - QString outEl = Context::getXmlString (_xml, "string(@element)"); - QString outElId = Context::getXmlString (_xml, "string(@element_id)"); - QString outOut = Context::getXmlString (_xml, "string(@output)"); + QString inEl = connectionInfo[0]; + QString inElId = connectionInfo[1]; + QString inIn = connectionInfo[2]; - outId = outElId.toInt (&ok); - if (!ok) - return; + QString outEl = connectionInfo[3]; + QString outElId = connectionInfo[4]; + QString outOut = connectionInfo[5]; - SceneElement *in = NULL, *out = NULL; + inId = inElId.toInt (&ok); + if (!ok) + return; - foreach (SceneElement *e, elements ()) - { - if (e->element ()->name () == inEl && e->id () == inId) - in = e; - else if (e->element ()->name () == outEl && e->id () == outId) - out = e; - } + outId = outElId.toInt (&ok); + if (!ok) + return; - if (!in || !out) - return; + SceneElement *in = NULL, *out = NULL; - ConnectionPoint *p1 = NULL, *p2 = NULL; + foreach (SceneElement *e, elements ()) + { + if (e->element ()->name () == inEl && e->id () == inId) + in = e; + else if (e->element ()->name () == outEl && e->id () == outId) + out = e; + } - if (in->dataIn () && in->dataIn ()->inOut ()->name () == inIn && - out->dataOut () && out->dataOut ()->inOut ()->name () == outOut) - { - p1 = in->dataIn ()->connectionPointItem (); - p2 = out->dataOut ()->connectionPointItem (); - } - else - { - foreach (ElementInput *i, in->inputs()) - if (i->inOut ()->name () == inIn) - { - p1 = i->connectionPointItem (); - break; - } + if (!in || !out) + return; - foreach (ElementOutput *o, out->outputs()) - if (o->inOut ()->name () == outOut) - { - p2 = o->connectionPointItem (); - break; - } - } + ConnectionPoint *p1 = NULL, *p2 = NULL; - if (p1 && p2) - new Connection (p1, p2, this); + if (in->dataIn () && in->dataIn ()->inOut ()->name () == inIn && + out->dataOut () && out->dataOut ()->inOut ()->name () == outOut) + { + p1 = in->dataIn ()->connectionPointItem (); + p2 = out->dataOut ()->connectionPointItem (); + } + else + { + foreach (ElementInput *i, in->inputs()) + if (i->inOut ()->name () == inIn) + { + p1 = i->connectionPointItem (); + break; + } + + foreach (ElementOutput *o, out->outputs()) + if (o->inOut ()->name () == outOut) + { + p2 = o->connectionPointItem (); + break; + } + } + + if (p1 && p2) + new Connection (p1, p2, this); } //------------------------------------------------------------------------------ @@ -1080,13 +1077,15 @@ void GraphicsScene::updateConnections() foreach (Connection *c, o->connections ()) c->updatePositions (); - if (e->dataIn ()) + if (e->dataIn ()) { foreach (Connection *c, e->dataIn ()->connections ()) c->updatePositions (); + } - if (e->dataOut ()) + if (e->dataOut ()) { foreach (Connection *c, e->dataOut ()->connections ()) c->updatePositions (); + } } } diff --git a/scene/graphicsScene.hh b/scene/graphicsScene.hh index faecbf0ef121148ebc15d5e6b6b0be01a144d904..1cf0419a1601bb05d4a66cab6b6999f63c4be3f1 100644 --- a/scene/graphicsScene.hh +++ b/scene/graphicsScene.hh @@ -50,7 +50,6 @@ #include <QPointF> #include <QDomDocument> #include <QDomElement> -#include <QXmlQuery> //== FORWARDDECLARATIONS ====================================================== class QGraphicsRectItem; @@ -135,7 +134,7 @@ class GraphicsScene : public QGraphicsScene void saveToXml (QDomDocument &_doc, QDomElement &_root); /// Load from xml - void loadFromXml (QXmlQuery &_xml); + void loadFromXml (QDomElement& _domElement); /// Associated function ElementFunction *function () { return function_; }; @@ -192,10 +191,10 @@ class GraphicsScene : public QGraphicsScene QPoint mimeDataPoint (const QMimeData *); // load element from xml - void loadElement (QXmlQuery &_xml); + void loadElement (QDomElement& _domElement,std::vector<QString>& _connections); - // load connection from xml - void loadConnection (QXmlQuery &_xml); + // load connections from xml + void loadConnection (QString& _connections); // returns all scene elements. Also all elements of sub-functions QList<SceneElement *> getAllElements (); diff --git a/scene/sceneElement.cc b/scene/sceneElement.cc index 4388c149ed800b78fd6a5ebbf9789f41d454913f..734f458e4de4234dc4895d6ed807f74f94528b86 100644 --- a/scene/sceneElement.cc +++ b/scene/sceneElement.cc @@ -52,9 +52,9 @@ #include <QGraphicsProxyWidget> #include <QGraphicsGridLayout> #include <QGraphicsView> +#include <QRegularExpression> #include <QDomText> -#include <QXmlResultItems> #include "sceneElement.hh" #include "graphicsScene.hh" @@ -84,7 +84,6 @@ #define SELECTED_BACKGROUND_BLUE 0x6f #define SELECTED_BACKGROUND_ALPHA 0xff - //== NAMESPACES =============================================================== namespace VSI { @@ -542,13 +541,15 @@ void SceneElement::moveEvent (QGraphicsSceneMoveEvent *_event) foreach (Connection *c, e->connections ()) c->updatePositions (); - if (dataIn_) + if (dataIn_) { foreach (Connection *c, dataIn_->connections ()) c->updatePositions (); + } - if (dataOut_) + if (dataOut_) { foreach (Connection *c, dataOut_->connections ()) c->updatePositions (); + } scene_->contentChange (); } @@ -572,7 +573,7 @@ void SceneElement::resetCodeGeneration() void SceneElement::replaceCodeBlock(QString _name, QString _id, QString _value) { QString regex = "\\[\\s*" + _name + "\\s*=\\s*\"" + _id + "\"\\s*\\]"; - code_ = code_.replace (QRegExp (regex), _value); + code_ = code_.replace (QRegularExpression (regex), _value); } //------------------------------------------------------------------------------ @@ -623,7 +624,7 @@ bool SceneElement::isBefore(SceneElement * _e) } } - if (dataOut ()) + if (dataOut ()) { foreach (Connection *c, dataOut ()->connections ()) { if (c->input ()->element () == _e) @@ -632,6 +633,7 @@ bool SceneElement::isBefore(SceneElement * _e) if (rv) return true; } + } return false; } @@ -652,7 +654,7 @@ bool SceneElement::isAfter(SceneElement * _e) } } - if (dataIn ()) + if (dataIn ()) { foreach (Connection *c, dataIn ()->connections ()) { if (c->output ()->element () == _e) @@ -661,6 +663,7 @@ bool SceneElement::isAfter(SceneElement * _e) if (rv) return true; } + } return false; } @@ -678,13 +681,15 @@ void VSI::SceneElement::invalidateConnections() foreach (Connection *c, e->connections ()) c->invalidate (); - if (dataIn_) + if (dataIn_) { foreach (Connection *c, dataIn_->connections ()) c->invalidate (); + } - if (dataOut_) + if (dataOut_) { foreach (Connection *c, dataOut_->connections ()) c->invalidate (); + } } //------------------------------------------------------------------------------ @@ -742,14 +747,14 @@ void SceneElement::saveToXml(QDomDocument & _doc, QDomElement & _root) //------------------------------------------------------------------------------ /// Load from xml -void SceneElement::loadFromXml(QXmlQuery & _xml) +void SceneElement::loadFromXml(QDomElement& _domElement,std::vector<QString>& _connections) { bool ok1, ok2; int id; double x, y; QString val; - val = Context::getXmlString (_xml, "visible_name/string(text())"); + val = Context::getXmlString (_domElement, "visible_name"); if (!val.isEmpty ()) name_->setText (val); @@ -761,7 +766,7 @@ void SceneElement::loadFromXml(QXmlQuery & _xml) nameLayout_->invalidate (); - val = Context::getXmlString (_xml, "id/string(text())"); + val = Context::getXmlString (_domElement, "id"); id = val.toUInt (&ok1); @@ -771,79 +776,79 @@ void SceneElement::loadFromXml(QXmlQuery & _xml) element_->setMinId(id + 1); } - val = Context::getXmlString (_xml, "x/string(text())"); + val = Context::getXmlString (_domElement, "x"); x = val.toDouble (&ok1); - val = Context::getXmlString (_xml, "y/string(text())"); + val = Context::getXmlString (_domElement, "y"); y = val.toDouble (&ok2); if (ok1 && ok2) setPos (x, y); - val = Context::getXmlString (_xml, "is_set/string(text())"); + val = Context::getXmlString (_domElement, "is_set"); - _xml.setQuery ("inputs/input"); - QXmlResultItems i; - - if (_xml.isValid ()) + // Search through all children of the current element if we have inputs, outputs or functions + for(QDomElement n = _domElement.firstChildElement(); !n.isNull(); n = n.nextSiblingElement() ) { - _xml.evaluateTo (&i); - QXmlItem item (i.next ()); - while (!item.isNull ()) - { - QXmlQuery q (_xml); - q.setFocus (item); + // Found an input Tag! + if (n.tagName() == "inputs") { - val = Context::getXmlString (q, "string(@name)"); - - if (!val.isEmpty()) - { - if (val == "data" && dataIn_) - dataIn_->loadFromXml (q); - else - { - foreach (ElementInput *i, inputs_) - if (i->inOut()->name () == val) - { - i->loadFromXml (q); - break; - } - } + // Iterate over all inputs inside it + for(QDomElement inputElement = n.firstChildElement(); !inputElement.isNull(); inputElement = inputElement.nextSiblingElement() ) + { + val = inputElement.attribute("name"); + + + if (!val.isEmpty()) + { + if (val == "data" && dataIn_) { + dataIn_->loadElementInputFromXml(inputElement); + } else + { + foreach (ElementInput *i, inputs_) + if (i->inOut()->name () == val) + { + i->loadElementInputFromXml(inputElement); + break; + } + } + + // Iterate over all inputs and read the connection info + for(QDomElement connectionElement = inputElement.firstChildElement(); !connectionElement.isNull(); connectionElement = connectionElement.nextSiblingElement() ) + { + if (connectionElement.tagName() == "connection") { + QString tmp = _domElement.attribute("name") + ";"+ Context::getXmlString (_domElement, "id") + ";" + inputElement.attribute("name") + ";" + connectionElement.attribute("element") + ";" + connectionElement.attribute("element_id") + ";" + connectionElement.attribute("output"); + _connections.push_back(tmp); + } + } + } + } } - item = i.next (); - } - - } - - _xml.setQuery ("functions/function"); + // Found an function Tag! + if (n.tagName() == "functions") { - if (_xml.isValid ()) - { - _xml.evaluateTo (&i); - QXmlItem item (i.next ()); - while (!item.isNull ()) - { - QXmlQuery q (_xml); - q.setFocus (item); - - val = Context::getXmlString (q, "string(@name)"); - - if (!val.isEmpty()) - { - foreach (ElementFunction *ef, functions_) - if (ef->function ()->name () == val) + // Iterate over all functions inside it + for(QDomElement functionElement = n.firstChildElement(); !functionElement.isNull(); functionElement = functionElement.nextSiblingElement() ) { - ef->loadFromXml (q); - break; + val = functionElement.attribute("name"); + + if (!val.isEmpty()) + { + foreach (ElementFunction *ef, functions_) + if (ef->function ()->name () == val) + { + ef->loadElementFunctionFromXml (functionElement); + break; + } + } } - } - item = i.next (); - } + } } + } //------------------------------------------------------------------------------ diff --git a/scene/sceneElement.hh b/scene/sceneElement.hh index f928110193dc8023f8859252b8b1e5432777df4f..22c56e05eaefc673f32fc5249e4cf1e9401fc79d 100644 --- a/scene/sceneElement.hh +++ b/scene/sceneElement.hh @@ -49,7 +49,6 @@ #include <QString> #include <QDomDocument> #include <QDomElement> -#include <QXmlQuery> //== FORWARDDECLARATIONS ====================================================== class QGraphicsLinearLayout; @@ -126,8 +125,14 @@ class SceneElement : public QGraphicsWidget /// Save to xml void saveToXml (QDomDocument &_doc, QDomElement &_root); - /// Load from xml - void loadFromXml (QXmlQuery &_xml); + /** \brief Load one scene Element from xml + * + * Load one scene element from an xml dom. + * + * @param _domElement The element that should be loaded + * @param _connections Info about the connections returned for this element. The function appends the information to the vector + */ + void loadFromXml (QDomElement& _domElement,std::vector<QString>& _connections); /// Scene GraphicsScene *graphicsScene () { return scene_; }; diff --git a/scene/trash.cc b/scene/trash.cc index 4a1858e5b9706159ba63774b4478469d4a44deb3..68c9696e30f7bfaef929e76e0435f3b16fb7c5a0 100644 --- a/scene/trash.cc +++ b/scene/trash.cc @@ -103,13 +103,14 @@ void Trash::hoverLeaveEvent (QGraphicsSceneHoverEvent * /*_event*/) // delete selected elements on mouse press void Trash::mousePressEvent (QGraphicsSceneMouseEvent * /*_event*/) { - if (!scene_->selectedItems ().isEmpty ()) + if (!scene_->selectedItems ().isEmpty ()) { foreach (QGraphicsItem *e, scene_->selectedItems ()) { SceneElement *se = dynamic_cast<SceneElement *> (e); if (se && scene_->removeElement (se)) delete se; } + } } //------------------------------------------------------------------------------ diff --git a/vsiPlugin.cc b/vsiPlugin.cc index 309681d4c83dad08b76f72dd84561e76cd97be2b..3b6cece612cf822454fce685d14ccd2de8e7cca9 100644 --- a/vsiPlugin.cc +++ b/vsiPlugin.cc @@ -149,11 +149,13 @@ void VsiPlugin::initContext() foreach (QString file, subdir.entryList (QStringList("*.xml"), QDir::Files)) { - QFile f (subdir.filePath (file)); - if (!f.open (QIODevice::ReadOnly)) - continue; + QFile f (subdir.filePath (file)); + if (!f.open (QIODevice::ReadOnly)) + continue; - context_->parse (f.readAll ()); + context_->parse(f); + + f.close(); } } @@ -199,7 +201,11 @@ QString VsiPlugin::askForInputs(QString _element, QString _inputs) QString result("{"); bool first = true; +#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0) foreach (QString s, _inputs.split (",", QString::SkipEmptyParts)) { +#else + foreach (QString s, _inputs.split (",", Qt::SkipEmptyParts)) { +#endif QString value(results[s]); if (!first) result += ","; // value.replace("\"", "\\\"");