From e3da4288a1dcaa2a2acd9655b25d8d1bcf677ebb Mon Sep 17 00:00:00 2001 From: Yorik van Havre Date: Thu, 29 Dec 2011 19:02:58 -0200 Subject: [PATCH] Added editable texts to DrawingPage - Editable texts can be added to templates by adding a freecad:editable="textName" attribute to SVG tags - Added EditableTexts property (stringlist) to FeaturePage objects - Instead of writing directly the SVG fragments to the temp file, they are scanned for editable texts, and if needed those are changed, then the final temp file is written, allowing for editable texts anywhere, even in FeatureView objects - On changing the Template, editable texts are picked and stored in the EditableTexts property. --- src/Mod/Drawing/App/FeaturePage.cpp | 90 +++++++++++++++++++---------- src/Mod/Drawing/App/FeaturePage.h | 1 - 2 files changed, 58 insertions(+), 33 deletions(-) diff --git a/src/Mod/Drawing/App/FeaturePage.cpp b/src/Mod/Drawing/App/FeaturePage.cpp index 30559a857f..2779105e96 100644 --- a/src/Mod/Drawing/App/FeaturePage.cpp +++ b/src/Mod/Drawing/App/FeaturePage.cpp @@ -75,6 +75,36 @@ void FeaturePage::onChanged(const App::Property* prop) return; } } + if (prop == &Template) { + // getting editable texts from "freecad:editable" tags in SVG template + if (!this->isRestoring()) { + if (Template.getValue() != "") { + Base::FileInfo tfi(Template.getValue()); + if (tfi.isReadable()) { + string tline, tfrag; + ifstream tfile (tfi.filePath().c_str()); + while (!tfile.eof()) { + getline (tfile,tline); + tfrag += tline; + tfrag += "--endOfLine--"; + } + tfile.close(); + boost::regex e ("(.*?)"); + string::const_iterator tbegin, tend; + tbegin = tfrag.begin(); + tend = tfrag.end(); + boost::match_results twhat; + std::vector eds; + while (boost::regex_search(tbegin, tend, twhat, e)) { + printf(twhat[1].str().c_str()); + eds.push_back(twhat[2]); + tbegin = twhat[0].second; + } + EditableTexts.setValues(eds); + } + } + } + } App::DocumentObjectGroup::onChanged(prop); } @@ -104,7 +134,8 @@ App::DocumentObjectExecReturn *FeaturePage::execute(void) // make a temp file for FileIncluded Property string tempName = PageResult.getExchangeTempFile(); - ofstream ofile(tempName.c_str()); + ostringstream ofile; + string tempendl = "--endOfLine--"; while (!file.eof()) { @@ -112,7 +143,7 @@ App::DocumentObjectExecReturn *FeaturePage::execute(void) // check if the marker in the template is found if(line.find("") == string::npos) // if not - write through - ofile << line << endl; + ofile << line << tempendl; else { // get through the children and collect all the views @@ -121,46 +152,41 @@ App::DocumentObjectExecReturn *FeaturePage::execute(void) if ((*It)->getTypeId().isDerivedFrom(Drawing::FeatureView::getClassTypeId())) { Drawing::FeatureView *View = dynamic_cast(*It); ofile << View->ViewResult.getValue(); - ofile << endl << endl << endl; + ofile << tempendl << tempendl << tempendl; } } } } file.close(); - ofile.close(); - // checking for freecad editable texts - boost::regex e ("(.*?)",boost::regex_constants::icase); + // checking for freecad editable texts + string outfragment(ofile.str()); + if (EditableTexts.getSize() > 0) { + boost::regex e1 ("(.*?)"); + string::const_iterator begin, end; + begin = ofile.str().begin(); + end = ofile.str().end(); + boost::match_results what; + int count = 0; - // reading file contents - ifstream tfile (tempName.c_str()); - string tline; - string fragment; - - while (!tfile.eof()) { - getline (tfile,tline); - fragment += tline; - fragment += "--endOfLine--"; + while (boost::regex_search(begin, end, what, e1)) { + if (count < EditableTexts.getSize()) { + boost::regex e2 ("((.*?)()"); + outfragment = boost::regex_replace(outfragment, e2, "$1>"+EditableTexts.getValues()[count]+"$3"); + } + count ++; + begin = what[0].second; + } } - tfile.close(); - - //printf(fragment.c_str()); - - string::const_iterator start, end; - start = fragment.begin(); - end = fragment.end(); - //boost::smatch what; - boost::match_results what; - - cout << "Stored strings: " << EditableTexts.getValue(); - - while (boost::regex_search(start,end,what,e)) { - //cout << "match" << what.str() << endl; - cout << "new match:" << what[1] << " = " << what[2] << endl; - start = what[0].second; - } + // restoring linebreaks and saving the file + boost::regex e3 ("--endOfLine--"); + string fmt = "\\n"; + outfragment = boost::regex_replace(outfragment, e3, fmt); + ofstream outfinal(tempName.c_str()); + outfinal << outfragment; + outfinal.close(); PageResult.setValue(tempName.c_str()); diff --git a/src/Mod/Drawing/App/FeaturePage.h b/src/Mod/Drawing/App/FeaturePage.h index 885bef6b86..ed64c1b3b2 100644 --- a/src/Mod/Drawing/App/FeaturePage.h +++ b/src/Mod/Drawing/App/FeaturePage.h @@ -68,5 +68,4 @@ protected: } //namespace Drawing - #endif