From 9967bbe297e1cc0c946fbcf653c75659ed6b028f Mon Sep 17 00:00:00 2001 From: Yorik van Havre Date: Thu, 23 Apr 2015 23:13:58 -0300 Subject: [PATCH] Drawing: Symbols can now contain editable texts --- src/Mod/Drawing/App/FeatureViewSymbol.cpp | 58 ++++++++++++++++++++++- src/Mod/Drawing/App/FeatureViewSymbol.h | 4 ++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/Mod/Drawing/App/FeatureViewSymbol.cpp b/src/Mod/Drawing/App/FeatureViewSymbol.cpp index 36f5b25ab..95f8d9c47 100644 --- a/src/Mod/Drawing/App/FeatureViewSymbol.cpp +++ b/src/Mod/Drawing/App/FeatureViewSymbol.cpp @@ -28,6 +28,7 @@ #endif #include +#include #include #include @@ -50,6 +51,7 @@ FeatureViewSymbol::FeatureViewSymbol(void) static const char *vgroup = "Drawing view"; ADD_PROPERTY_TYPE(Symbol,(""),vgroup,App::Prop_Hidden,"The SVG code defining this symbol"); + ADD_PROPERTY_TYPE(EditableTexts,(""),vgroup,App::Prop_None,"Substitution values for the editable strings in this symbol"); } @@ -57,13 +59,67 @@ FeatureViewSymbol::~FeatureViewSymbol() { } +/// get called by the container when a Property was changed +void FeatureViewSymbol::onChanged(const App::Property* prop) +{ + if (prop == &Symbol) { + if (!this->isRestoring()) { + std::vector eds; + std::string svg = Symbol.getValue(); + if (!svg.empty()) { + boost::regex e ("(.*?)"); + std::string::const_iterator tbegin, tend; + tbegin = svg.begin(); + tend = svg.end(); + boost::match_results twhat; + while (boost::regex_search(tbegin, tend, twhat, e)) { + eds.push_back(twhat[2]); + tbegin = twhat[0].second; + } + EditableTexts.setValues(eds); + } + } + } + Drawing::FeatureView::onChanged(prop); +} + App::DocumentObjectExecReturn *FeatureViewSymbol::execute(void) { + std::string svg = Symbol.getValue(); + const std::vector& editText = EditableTexts.getValues(); + + if (!editText.empty()) { + boost::regex e1 ("(.*?)"); + string::const_iterator begin, end; + begin = svg.begin(); + end = svg.end(); + boost::match_results what; + std::size_t count = 0; + std::string newsvg; + newsvg.reserve(svg.size()); + + while (boost::regex_search(begin, end, what, e1)) { + if (count < editText.size()) { + // change values of editable texts. Also strip the "freecad:editable" + // attribute so it isn't detected by the page + boost::regex e2 ("((.*?)()"); + boost::re_detail::string_out_iterator out(newsvg); + boost::regex_replace(out, begin, what[0].second, e2, "$1$3>"+editText[count]+"$5"); + } + count++; + begin = what[0].second; + } + + // now copy the rest + newsvg.insert(newsvg.end(), begin, end); + svg = newsvg; + } + std::stringstream result; result << "" << endl - << Symbol.getValue() << endl + << svg << endl << "" << endl; // Apply the resulting fragment diff --git a/src/Mod/Drawing/App/FeatureViewSymbol.h b/src/Mod/Drawing/App/FeatureViewSymbol.h index 36877b656..2377d53bc 100644 --- a/src/Mod/Drawing/App/FeatureViewSymbol.h +++ b/src/Mod/Drawing/App/FeatureViewSymbol.h @@ -49,6 +49,7 @@ public: virtual ~FeatureViewSymbol(); App::PropertyString Symbol; + App::PropertyStringList EditableTexts; /** @name methods overide Feature */ //@{ @@ -60,6 +61,9 @@ public: virtual const char* getViewProviderName(void) const { return "DrawingGui::ViewProviderDrawingView"; } + +protected: + void onChanged(const App::Property* prop); }; typedef App::FeaturePythonT FeatureViewSymbolPython;