diff --git a/SessionCreator.cpp b/SessionCreator.cpp index dd8d93b..d6ca8ee 100644 --- a/SessionCreator.cpp +++ b/SessionCreator.cpp @@ -117,6 +117,13 @@ void SessionCreator::loadConfig(XMLElement *viewsNode) } } +SessionLoader::SessionLoader(): Visitor(), + session_(nullptr), xmlCurrent_(nullptr), recursion_(0) +{ + // impose C locale + setlocale(LC_ALL, "C"); +} + SessionLoader::SessionLoader(Session *session, int recursion): Visitor(), session_(session), xmlCurrent_(nullptr), recursion_(recursion) { @@ -269,12 +276,10 @@ void SessionLoader::load(XMLElement *sessionNode) } } } - } - } -Source *SessionLoader::createSource(tinyxml2::XMLElement *sourceNode, bool clone_duplicates) +Source *SessionLoader::createSource(tinyxml2::XMLElement *sourceNode, Mode mode) { xmlCurrent_ = sourceNode; @@ -284,13 +289,13 @@ Source *SessionLoader::createSource(tinyxml2::XMLElement *sourceNode, bool clone SourceList::iterator sit = session_->end(); // check if a source with the given id exists in the session - if (clone_duplicates) { + if (mode == CLONE) { uint64_t id__ = 0; xmlCurrent_->QueryUnsigned64Attribute("id", &id__); sit = session_->find(id__); } - // no source with this id exists + // no source with this id exists or Mode DUPLICATE if ( sit == session_->end() ) { // create a new source depending on type const char *pType = xmlCurrent_->Attribute("type"); @@ -346,6 +351,109 @@ Source *SessionLoader::createSource(tinyxml2::XMLElement *sourceNode, bool clone return load_source; } +Source *SessionLoader::createDummy(tinyxml2::XMLElement *sourceNode) +{ + SessionLoader loader; + loader.xmlCurrent_ = sourceNode; + + DummySource *dum = new DummySource; + dum->accept(loader); + + return dum; +} + +bool SessionLoader::isClipboard(std::string clipboard) +{ + if (clipboard.size() > 6 && clipboard.substr(0, 6) == "<" APP_NAME ) + return true; + + return false; +} + +tinyxml2::XMLElement* SessionLoader::firstSourceElement(std::string clipboard, XMLDocument &xmlDoc) +{ + tinyxml2::XMLElement* sourceNode = nullptr; + + if ( !isClipboard(clipboard) ) + return sourceNode; + + // header + tinyxml2::XMLError eResult = xmlDoc.Parse(clipboard.c_str()); + if ( XMLResultError(eResult)) + return sourceNode; + + tinyxml2::XMLElement *root = xmlDoc.FirstChildElement(APP_NAME); + if ( root == nullptr ) + return sourceNode; + + // find node + sourceNode = root->FirstChildElement("Source"); + return sourceNode; +} + +void SessionLoader::applyImageProcessing(const Source &s, std::string clipboard) +{ + if ( !isClipboard(clipboard) ) + return; + + // header + tinyxml2::XMLDocument xmlDoc; + tinyxml2::XMLError eResult = xmlDoc.Parse(clipboard.c_str()); + if ( XMLResultError(eResult)) + return; + + tinyxml2::XMLElement *root = xmlDoc.FirstChildElement(APP_NAME); + if ( root == nullptr ) + return; + + // find node + tinyxml2::XMLElement* imgprocNode = nullptr; + tinyxml2::XMLElement* sourceNode = root->FirstChildElement("Source"); + if (sourceNode == nullptr) + imgprocNode = root->FirstChildElement("ImageProcessing"); + else + imgprocNode = sourceNode->FirstChildElement("ImageProcessing"); + + if (imgprocNode == nullptr) + return; + + // create session visitor and browse + SessionLoader loader; + loader.xmlCurrent_ = imgprocNode; + s.processingShader()->accept(loader); +} + +//void SessionLoader::applyMask(const Source &s, std::string clipboard) +//{ +// if ( !isClipboard(clipboard) ) +// return; + +// // header +// tinyxml2::XMLDocument xmlDoc; +// tinyxml2::XMLError eResult = xmlDoc.Parse(clipboard.c_str()); +// if ( XMLResultError(eResult)) +// return; + +// tinyxml2::XMLElement *root = xmlDoc.FirstChildElement(APP_NAME); +// if ( root == nullptr ) +// return; + +// // find node +// tinyxml2::XMLElement* naskNode = nullptr; +// tinyxml2::XMLElement* sourceNode = root->FirstChildElement("Source"); +// if (sourceNode == nullptr) +// naskNode = root->FirstChildElement("Mask"); +// else +// naskNode = sourceNode->FirstChildElement("ImageProcessing"); + +// if (naskNode == nullptr) +// return; + +// // create session visitor and browse +// SessionLoader loader; +// loader.xmlCurrent_ = naskNode; +//// s.processingShader()->accept(loader); +//} void SessionLoader::XMLToNode(tinyxml2::XMLElement *xml, Node &n) { diff --git a/SessionCreator.h b/SessionCreator.h index 9ce0684..2ec2778 100644 --- a/SessionCreator.h +++ b/SessionCreator.h @@ -12,6 +12,8 @@ class Session; class SessionLoader : public Visitor { + SessionLoader(); + public: SessionLoader(Session *session, int recursion = 0); @@ -21,7 +23,16 @@ public: std::map< uint64_t, Source* > getSources() const; std::list< SourceList > getMixingGroups() const; - Source *createSource(tinyxml2::XMLElement *sourceNode, bool clone_duplicates = true); + typedef enum { + CLONE, + DUPLICATE + } Mode; + Source *createSource(tinyxml2::XMLElement *sourceNode, Mode mode = CLONE); + + static bool isClipboard(std::string clipboard); + static tinyxml2::XMLElement* firstSourceElement(std::string clipboard, tinyxml2::XMLDocument &xmlDoc); + static void applyImageProcessing(const Source &s, std::string clipboard); + //TODO static void applyMask(const Source &s, std::string clipboard); // Elements of Scene void visit (Node& n) override; @@ -60,6 +71,7 @@ protected: std::list< SourceIdList > groups_sources_id_; static void XMLToNode(tinyxml2::XMLElement *xml, Node &n); + static Source *createDummy(tinyxml2::XMLElement *sourceNode); }; class SessionCreator : public SessionLoader { diff --git a/SessionVisitor.cpp b/SessionVisitor.cpp index ae1206e..56c6f96 100644 --- a/SessionVisitor.cpp +++ b/SessionVisitor.cpp @@ -540,3 +540,90 @@ void SessionVisitor::visit (MixingGroup& g) xmlCurrent_->InsertEndChild(sour); } } + +std::string SessionVisitor::getClipboard(SourceList list) +{ + std::string x = ""; + + if (!list.empty()) { + + // create xml doc and root node + tinyxml2::XMLDocument xmlDoc; + tinyxml2::XMLElement *selectionNode = xmlDoc.NewElement(APP_NAME); + selectionNode->SetAttribute("size", (int) list.size()); + xmlDoc.InsertEndChild(selectionNode); + + // fill doc by visiting sources + SourceList selection_clones_; + SessionVisitor sv(&xmlDoc, selectionNode); + for (auto iter = list.begin(); iter != list.end(); iter++, sv.setRoot(selectionNode) ){ + // start with clones + CloneSource *clone = dynamic_cast(*iter); + if (clone) + (*iter)->accept(sv); + else + selection_clones_.push_back(*iter); + } + // add others in front + for (auto iter = selection_clones_.begin(); iter != selection_clones_.end(); iter++, sv.setRoot(selectionNode) ){ + (*iter)->accept(sv); + } + + // get compact string + tinyxml2::XMLPrinter xmlPrint(0, true); + xmlDoc.Print( &xmlPrint ); + x = xmlPrint.CStr(); + } + + return x; +} + +std::string SessionVisitor::getClipboard(Source *s) +{ + std::string x = ""; + + if (s != nullptr) { + // create xml doc and root node + tinyxml2::XMLDocument xmlDoc; + tinyxml2::XMLElement *selectionNode = xmlDoc.NewElement(APP_NAME); + selectionNode->SetAttribute("size", 1); + xmlDoc.InsertEndChild(selectionNode); + + // visit source + SessionVisitor sv(&xmlDoc, selectionNode); + s->accept(sv); + + // get compact string + tinyxml2::XMLPrinter xmlPrint(0, true); + xmlDoc.Print( &xmlPrint ); + x = xmlPrint.CStr(); + } + + return x; +} + +std::string SessionVisitor::getClipboard(ImageProcessingShader *s) +{ + std::string x = ""; + + if (s != nullptr) { + // create xml doc and root node + tinyxml2::XMLDocument xmlDoc; + tinyxml2::XMLElement *selectionNode = xmlDoc.NewElement(APP_NAME); + xmlDoc.InsertEndChild(selectionNode); + + tinyxml2::XMLElement *imgprocNode = xmlDoc.NewElement( "ImageProcessing" ); + selectionNode->InsertEndChild(imgprocNode); + + // visit source + SessionVisitor sv(&xmlDoc, imgprocNode); + s->accept(sv); + + // get compact string + tinyxml2::XMLPrinter xmlPrint(0, true); + xmlDoc.Print( &xmlPrint ); + x = xmlPrint.CStr(); + } + + return x; +} diff --git a/SessionVisitor.h b/SessionVisitor.h index ae8005a..e4653e1 100644 --- a/SessionVisitor.h +++ b/SessionVisitor.h @@ -3,6 +3,7 @@ #include "Visitor.h" #include "tinyxml2Toolkit.h" +#include "SourceList.h" class Session; @@ -21,6 +22,10 @@ public: static bool saveSession(const std::string& filename, Session *session); + static std::string getClipboard(SourceList list); + static std::string getClipboard(Source *s); + static std::string getClipboard(ImageProcessingShader *s); + // Elements of Scene void visit(Scene& n) override; void visit(Node& n) override;