Salome HOME
copy tag mergefrom_BR_V0_1_CC_Salome_04oct07
[modules/yacs.git] / src / gui / YACSGui_XMLDriver.cxx
1 #include <YACSGui_XMLDriver.h>
2 #include <YACSGui_Graph.h>
3 #include <YACSGui_Module.h>
4 #include <YACSPrs_ElementaryNode.h>
5 #include <YACSPrs_Link.h>
6
7 #include <QxGraph_Canvas.h>
8
9 #include <Node.hxx>
10 #include <InputPort.hxx>
11 #include <OutputPort.hxx>
12 #include <InputDataStreamPort.hxx>
13 #include <OutputDataStreamPort.hxx>
14
15 #include <qstring.h>
16 #include <qptrlist.h>
17
18 #include <set>
19
20 using namespace YACS::ENGINE;
21 using namespace std;
22
23 static canvastype_parser canvas_parser;
24 static presentationtype_parser presentation_parser;
25 static pointtype_parser point_parser;
26 static prslinktype_parser prslink_parser;
27
28 void prslinktype_parser::onStart(const XML_Char* el, const XML_Char** attr)
29 {
30   std::cerr << "prslinktype_parser::onStart: " << el << std::endl;
31   std::string element(el);
32   parser* pp=&main_parser;
33   if(element == "point") pp = &point_parser;
34   SetUserDataAndPush(pp);
35   pp->init();
36   pp->pre();
37   pp->buildAttr(attr);
38 }
39
40 YACSGui_VisitorSaveSchema::YACSGui_VisitorSaveSchema(YACSGui_Module* module,
41                                                      ComposedNode *root)
42   : VisitorSaveSchema(root), myModule(module)
43 {
44 }
45
46 YACSGui_VisitorSaveSchema::~YACSGui_VisitorSaveSchema()
47 {
48 }
49
50 void YACSGui_VisitorSaveSchema::visitProc(Proc *node)
51 {
52   this->VisitorSaveSchema::visitProc(node);
53   writePresentation(node);
54   writeLinks(node);
55 }
56
57 void YACSGui_VisitorSaveSchema::writePresentation(Proc *proc)
58 {
59   YACSGui_Graph* aGraph = myModule->getGraph(proc);
60   if ( !aGraph || !aGraph->getCanvas() )
61     return;
62
63   int depth = 1;
64   
65   _out << indent(depth) << "<canvas";
66   _out                  << " width=\""  << aGraph->getCanvas()->width() << "\"";
67   _out                  << " height=\"" << aGraph->getCanvas()->height() << "\"";
68   _out                  << "/>" << endl;
69
70   set<Node*> nodeSet = getAllNodes(proc);
71
72   for (set<Node*>::iterator iter = nodeSet.begin(); iter != nodeSet.end(); ++iter)
73   {
74     Node* aNode = *iter;
75     YACSPrs_ElementaryNode* aPrs = aGraph->getItem( aNode );
76     if ( !aPrs )
77       continue;
78
79     _out << indent(depth) << "<presentation";
80     _out                  << " name=\""  << proc->getChildName( aNode ) << "\"";
81     _out                  << " x=\""     << aPrs->x()                   << "\"";
82     _out                  << " y=\""     << aPrs->y()                   << "\"";
83     _out                  << " z=\""     << aPrs->z()                   << "\"";
84     _out                  << " width=\"" << aPrs->width()               << "\"";
85     _out                  << " height=\""<< aPrs->height()              << "\"";
86     _out                  << "/>" << endl;
87   }
88 }
89
90 void YACSGui_VisitorSaveSchema::writeLinks(YACS::ENGINE::Proc *proc)
91 {
92     YACSGui_Graph* aGraph = myModule->getGraph(proc);
93   if ( !aGraph || !aGraph->getCanvas() )
94     return;
95
96   int depth = 1;
97   
98   set<Node*> nodeSet = getAllNodes(proc);
99
100   for (set<Node*>::iterator iter = nodeSet.begin(); iter != nodeSet.end(); ++iter)
101   {
102     Node* aNode = *iter;
103     YACSPrs_ElementaryNode* aPrs = aGraph->getItem( aNode );
104     if ( !aPrs )
105       continue;
106
107     QPtrList<YACSPrs_Port> aPorts = aPrs->getPortList();
108     for (YACSPrs_Port* aPort = aPorts.first(); aPort; aPort = aPorts.next())
109     {
110       YACSPrs_InOutPort* anIOPort;
111       if ( ( anIOPort = dynamic_cast<YACSPrs_InOutPort*>( aPort ) ) && !anIOPort->isInput()
112            ||
113            dynamic_cast<YACSPrs_LabelPort*>( aPort ) )
114       { // this port is an output port => iterates on its links
115         list<YACSPrs_Link*> aLinks = aPort->getLinks();
116         for(list<YACSPrs_Link*>::iterator itL = aLinks.begin(); itL != aLinks.end(); itL++)
117         {
118           _out << indent(depth) << "<prslink";
119           
120           if ( YACSPrs_PortLink* aPL = dynamic_cast<YACSPrs_PortLink*>( *itL ) ) {
121             _out << " fromnode=\"" << proc->getChildName( aNode )                                       << "\"";
122             _out << " fromport=\"" << aPort->getName()                                                  << "\"";
123             _out << " tonode=\""   << proc->getChildName( aPL->getInputPort()->getEngine()->getNode() ) << "\"";
124             _out << " toport=\""   << aPL->getInputPort()->getName()                                    << "\"";
125           }
126           else if ( YACSPrs_LabelLink* aLL = dynamic_cast<YACSPrs_LabelLink*>( *itL ) ) {
127             _out << " tonode=\""   << proc->getChildName( aLL->getSlaveNode()->getEngine() )            << "\"";
128           }
129           
130           _out << ">" << endl;
131           
132           list<QPoint> aPoints = (*itL)->getPoints();
133           for(list<QPoint>::iterator itP = aPoints.begin(); itP != aPoints.end(); itP++) {
134             _out << indent(depth+1) << "<point";
135             _out                    << " x=\"" << (*itP).x() << "\"";
136             _out                    << " y=\"" << (*itP).y() << "\"";
137             _out                    << "/>" << endl;
138           }
139           
140           _out << indent(depth) << "</prslink>" << endl;
141         }
142       }
143     }
144   }
145 }
146
147 YACSGui_Loader::YACSGui_Loader()
148   : YACSLoader()
149 {
150   _defaultParsersMap.clear();
151
152   presentation_parser.collector_ = this;
153   prslink_parser.collector_ = this;
154
155   _defaultParsersMap.insert(make_pair("canvas",&canvas_parser));
156   _defaultParsersMap.insert(make_pair("presentation",&presentation_parser));
157   _defaultParsersMap.insert(make_pair("point",&point_parser));
158   _defaultParsersMap.insert(make_pair("prslink",&prslink_parser));
159 }
160
161 YACSGui_Loader::~YACSGui_Loader()
162 {
163 }
164
165 const YACSGui_Loader::PrsDataMap& YACSGui_Loader::getPrsData(Proc* proc, int& width, int& height)
166 {
167   myPrsMap.clear();
168
169   if ( _defaultParsersMap.empty()
170        ||
171        !_defaultParsersMap["canvas"] ) return myPrsMap;
172
173   // get information from canvastype_parser
174   width  = ((canvastype_parser*)_defaultParsersMap["canvas"])->width_;
175   height = ((canvastype_parser*)_defaultParsersMap["canvas"])->height_;
176
177   for ( InputMap::iterator it = myInputMap.begin(); it != myInputMap.end(); it++ )
178   {
179     Node* aNode = proc->getChildByName( (*it).first );
180     myPrsMap[aNode] = (*it).second;
181   }
182
183   return myPrsMap;
184 }
185
186 const YACSGui_Loader::PortLinkDataMap& YACSGui_Loader::getPortLinkData(YACS::ENGINE::Proc* proc)
187 {
188   myPortLinkMap.clear();
189
190   for ( InputPLList::iterator it = myInputPLList.begin(); it != myInputPLList.end(); it++ )
191   {
192     Node* aFromNode = proc->getChildByName((*it).fromnode);
193     Port* aFromPort = 0;
194     if ( (*it).fromport == "Gate" )
195       aFromPort = aFromNode->getOutGate();
196     else
197       aFromPort = (Port*)(aFromNode->getOutPort((*it).fromport));
198     
199     Node* aToNode = proc->getChildByName((*it).tonode);
200     Port* aToPort;
201     if ( (*it).toport == "Gate" )
202       aToPort = aToNode->getInGate();
203     else
204       aToPort = (Port*)(aToNode->getInPort((*it).toport));
205
206     std::pair<Port*,Port*> aPPair(aFromPort,aToPort);
207     myPortLinkMap[aPPair] = (*it).points;
208   }
209
210   return myPortLinkMap;
211 }
212
213 const YACSGui_Loader::LabelLinkDataMap& YACSGui_Loader::getLabelLinkData(YACS::ENGINE::Proc* proc)
214 {
215     myLabelLinkMap.clear();
216
217   for ( InputLLList::iterator it = myInputLLList.begin(); it != myInputLLList.end(); it++ )
218   {
219     Node* aSlaveNode = proc->getChildByName((*it).slavenode);
220     myLabelLinkMap[aSlaveNode] = (*it).points;
221   }
222
223   return myLabelLinkMap;
224 }
225
226 void YACSGui_Loader::process(std::string theElement, bool theNewLink)
227 {
228   if(theElement == "presentation")
229   {
230     if ( _defaultParsersMap["presentation"] )
231     {
232       presentationtype_parser* aP = (presentationtype_parser*)_defaultParsersMap["presentation"];
233       myInputMap[aP->name_] = PrsData(aP->x_,aP->y_,aP->z_,aP->width_,aP->height_);
234     }
235   }
236   else if(theElement == "prslink")
237   {
238     if ( _defaultParsersMap["prslink"] )
239     {
240       prslinktype_parser* aP = (prslinktype_parser*)_defaultParsersMap["prslink"];
241       if ( aP->type() == "portlink" )
242       {
243         if ( theNewLink ) {
244           PortLinkData aPLData(aP->fromnode_,aP->fromport_,aP->tonode_,aP->toport_);
245           //aPLData.fillPoints(aP->points_);
246           myInputPLList.push_back(aPLData);
247         }
248         else if ( !myInputPLList.empty() )
249           myInputPLList.back().appendPoint(aP->points_.back());
250       }
251       else if ( aP->type() == "labellink" )
252       {
253         if ( theNewLink ) {
254           LabelLinkData aLLData(aP->tonode_);
255           //aLLData.fillPoints(aP->points_);
256           myInputLLList.push_back(aLLData);
257         }
258         else if ( !myInputLLList.empty() )
259           myInputLLList.back().appendPoint(aP->points_.back());
260       }
261     }
262   }
263 }