PlotterBaseXML.cxx
Go to the documentation of this file.
1 
12 // for iterator member defect
13 #ifdef _MSC_VER
14 #include "msdevstudio/MSconfig.h"
15 #endif
16 
17 #include "PlotterBaseXML.h"
18 
19 #include "AxisModelXML.h"
20 #include "DataRepXML.h"
21 #include "FontXML.h"
22 #include "PointRepXML.h"
23 #include "TransformXML.h"
24 #include "TupleCutXML.h"
25 #include "XmlController.h"
26 #include "XmlDocument.h"
27 #include "XmlElement.h"
28 
29 #include "axes/AxisModelLog.h"
33 #include "datareps/FunctionRep.h"
34 #include "datasrcs/TupleCut.h"
35 
36 #include "plotters/Cut1DPlotter.h"
37 #include "plotters/Cut2DPlotter.h"
39 #include "plotters/TextPlotter.h"
40 #include "plotters/XyPlotter.h"
41 
43 
44 #include "pattern/string_convert.h"
45 
46 #include <typeinfo>
47 
48 #include <cassert>
49 
50 using std::list;
51 using std::string;
52 
53 using namespace hippodraw;
54 
56  : BaseXML ( "PlotterBase", controller ),
57  m_axis ( "axis" ),
58  m_title ( "title" ),
59  m_x_label ( "xlabel" ),
60  m_y_label ( "ylabel" ),
61  m_z_label ( "zlabel" ),
62  m_pindex ( "pindex" )
63 {
64  m_axismodel = new AxisModelXML ( controller );
65  m_datarep = new DataRepXML ( controller );
66  m_font = new FontXML ( controller );
67  m_pointrep = new PointRepXML ( controller );
68  m_transform_xml = new TransformXML ( controller );
69  m_tuple_cut_xml = new TupleCutXML ( controller );
70 }
71 
73 PlotterBaseXML ( const std::string & name,
74  XmlController * controller )
75  : BaseXML ( name, controller ),
76  m_axis ( "axis" )
77 {
78  m_axismodel = new AxisModelXML ( controller );
79  m_datarep = new DataRepXML ( controller );
80  m_font = new FontXML ( controller );
81 }
82 
85 {
86  delete m_axismodel;
87  delete m_datarep;
88  delete m_font;
89  delete m_pointrep;
90  delete m_transform_xml;
91  delete m_tuple_cut_xml;
92 }
93 
95 {
97 
98  return controller->areDataSourcesSaved ( & plotter );
99 }
100 
102  const PlotterBase & plotter )
103 {
104  createAxisModel ( tag, plotter, Axes::X );
105  createAxisModel ( tag, plotter, Axes::Y );
106  if ( plotter.hasAxis ( Axes::Z ) ) createAxisModel ( tag, plotter, Axes::Z );
107 
108  try {
109  const XyPlotter & xyplotter
110  = dynamic_cast < const XyPlotter & > ( plotter );
111  createFontElements ( tag, xyplotter );
112  }
113  catch ( ... ) {
114  // do nothing
115  }
116 
117  TransformBase * transform = plotter.getTransform ();
118  if ( transform != 0 ) {
119  XmlElement * element = m_transform_xml->createElement ( *transform );
120  tag.appendChild ( *element );
121  delete element;
122  }
123 
124  int number = plotter.getNumDataReps ();
125  for ( int i = 0; i < number; i++ ) {
126  DataRep * rep = plotter.getDataRep ( i );
127 
128  XmlElement * element = m_datarep->createElement ( *rep );
129 
130  tag.appendChild ( *element );
131  delete element;
132  }
133 
134  if ( typeid ( plotter ) == typeid ( Cut1DPlotter ) ||
135  typeid ( plotter ) == typeid ( Cut2DPlotter ) ) {
136  try {
137  const CutPlotter & cut_plotter
138  = dynamic_cast < const CutPlotter & > ( plotter );
139 
140  createCutChildren ( tag, cut_plotter );
141  }
142  catch ( ... ) {
143  // not a cut, so do nothing
144  }
145  }
146 
147  if ( plotter.name() == "TextPlotter" ) {
148  try {
149  const TextPlotter & text_plotter
150  = dynamic_cast < const TextPlotter & > ( plotter );
151  createTextChildren ( tag, text_plotter );
152  }
153  catch ( ... ) {
154  // not a text plotter, so do nothing
155  }
156  }
157  return;
158 }
159 
161  const CutPlotter & plotter )
162 {
163  const vector < TupleCut > & cuts = plotter.getCuts ();
164  for ( unsigned int i = 0; i < cuts.size (); i++ ) {
165  XmlElement * element = m_tuple_cut_xml -> createElement ( i, cuts[i] );
166  tag.appendChild ( *element ); // cut properties
167  delete element;
168  }
169 
170  const list < DataRep * > & targets = plotter.getCutTargets ();
171 #ifdef ITERATOR_MEMBER_DEFECT
172  std::
173 #endif
174  list < DataRep * >::const_iterator first = targets.begin();
175 
176  for ( ; first != targets.end(); ++first ) {
177  DataRep * rep = *first;
178  const void * addr = reinterpret_cast < const void * > ( rep );
179  int id = m_controller -> getId ( addr );
180  XmlElement * element
181  = XmlController::m_xml_doc->createElement ( "CutTarget" );
182  element->setAttribute ( "id", id );
183  tag.appendChild ( *element );
184  delete element;
185  }
186 }
187 
188 void
191  const TextPlotter & plotter )
192 {
193  const DataRep * rep = plotter.getParentDataRep ();
194 
195  const void * addr = reinterpret_cast < const void * > ( rep );
196  int id = m_controller -> getId ( addr );
197  XmlElement * element
198  = XmlController::m_xml_doc->createElement ( "TextTarget" );
199  element->setAttribute ( "id", id );
200 
201  tag.appendChild ( *element );
202  delete element;
203 }
204 
205 void
208  const XyPlotter & plotter )
209 {
210  const FontBase * font = plotter.titleFont ();
211  if ( font != 0 ) {
212  XmlElement * element = m_font -> createElement ();
213  const string t ( "t" );
214  element -> setAttribute ( m_axis, t );
215  m_font -> setAttributes ( *element, *font );
216  tag.appendChild ( *element );
217  delete element;
218  }
219 
220  for ( unsigned int i = 0; i < 3; i++ ) {
221  Axes::Type type = Axes::convert ( i );
222  createFontElement ( tag, plotter, type );
223  }
224 }
225 
226 void
229  const XyPlotter & plotter,
230  hippodraw::Axes::Type axis )
231 {
232  const FontBase * font = plotter.labelFont ( axis );
233  if ( font != 0 ) {
234  XmlElement * element = m_font -> createElement ();
235  string s;
236  switch ( axis )
237  {
238  case Axes::X :
239  s = "x";
240  break;
241  case Axes::Y :
242  s = "y";
243  break;
244  case Axes::Z :
245  s = "z";
246  break;
247  default:
248  assert ( false );
249  break;
250  }
251  element -> setAttribute ( m_axis, s );
252  m_font -> setAttributes ( *element, *font );
253 
254  tag.appendChild ( *element );
255  delete element;
256  }
257 }
258 
259 void
262  const PlotterBase & plotter,
263  hippodraw::Axes::Type axis )
264 {
265  const AxisModelBase * model = plotter.getAxisModel ( axis );
266  if ( model == 0 ) return;
267 
268  XmlElement * element = m_axismodel->createElement ();
269  string tmp;
270  if ( axis == Axes::X ) {
271  tmp = "x";
272  }
273  else if ( axis == Axes::Y ) {
274  tmp = "y";
275  }
276  else if ( axis == Axes::Z ) {
277  tmp = "z";
278  }
279  element -> setAttribute ( m_axis, tmp );
280  m_axismodel->setAttributes ( *element, *model );
281  tag.appendChild ( *element );
282  delete element;
283 }
284 
286 {
288  const void * addr = reinterpret_cast < const void * > ( & plotter );
289  int id = m_controller -> getId ( addr );
290  setId ( *tag, id );
291 
292  tag->setAttribute ( m_type, plotter.name () );
293 
294  const string & title = plotter.getInternalTitle ();
295  const string & x_label = plotter.getInternalLabel ( Axes::X );
296  const string & y_label = plotter.getInternalLabel ( Axes::Y );
297  const string & z_label = plotter.getInternalLabel ( Axes::Z );
298 
299  tag -> setAttribute ( m_title, title );
300  tag -> setAttribute ( m_x_label, x_label );
301  tag -> setAttribute ( m_y_label, y_label );
302  tag -> setAttribute ( m_z_label, z_label );
303 
304  PlotterBase * parent = plotter.getParentPlotter ();
305  if ( parent != 0 ) {
306  const void * addr = reinterpret_cast < const void * > ( parent );
307  int ref = m_controller -> getId ( addr );
308  tag -> setAttribute ( "ref", ref );
309  int index = plotter.getParentDataRepIndex ( );
310  tag -> setAttribute ( m_pindex, index );
311  }
312 
313  createChildren ( *tag, plotter );
314 
315  return tag;
316 }
317 
319 {
320  PlotterBase * plotter = createPlotter ( plot_element );
321 
322  if ( plotter == 0 ) return 0;
323 
324  const XmlElement * element = m_transform_xml->getNode ( plot_element );
325  if ( element != 0 ) {
326  TransformBase * transform = m_transform_xml->createObject ( element );
327  plotter->setTransform ( transform );
328  }
329 
330  createAxisModels ( plot_element, plotter );
331  createFontObjects ( plot_element, plotter );
332 
333  list < XmlElement * > nodelist;
334  m_datarep->fillNodeList ( plot_element, nodelist );
335 
337  DataRep * rep = 0;
338 
339 #ifdef ITERATOR_MEMBER_DEFECT
340  std::
341 #endif
342  list < XmlElement * > ::const_iterator first = nodelist.begin ();
343  for ( ; first != nodelist.end(); ++first ) {
344  XmlElement * element = *first;
345  int id = element->getID ();
346  rep = m_controller->getDataRep ( id );
347  if ( rep == 0 ) {
348  string what ( "Unable to find data representation" );
349  throw DataRepException ( what );
350  }
351 
352  assert ( rep );
353  handleFunction ( element, rep );
354  controller->addDataRep ( plotter, rep );
355  }
356 
357  CutPlotter * cutplotter = dynamic_cast < CutPlotter * > ( plotter );
358  if ( cutplotter != 0 ) {
359  cutplotter -> addTupleCut ( rep );
360  handleCutPlotter ( plot_element, cutplotter );
361  }
362 
363  TextPlotter * textplotter = dynamic_cast < TextPlotter * > ( plotter );
364  if ( textplotter != 0 ) {
365  if ( ! ( handleTextPlotter ( plot_element, textplotter ) ) ){
366  return 0;
367  }
368  }
369 
370  return plotter;
371 }
372 
373 void
375 createFontObjects ( const XmlElement * plot_element, PlotterBase * plotter )
376 {
377  list < XmlElement * > nodelist;
378  m_font -> fillNodeList ( plot_element, nodelist );
379  if ( nodelist.empty () == false ) {
380  XyPlotter * xypl = dynamic_cast < XyPlotter * > ( plotter );
381  assert ( xypl );
382 
383  list < XmlElement * > ::const_iterator first = nodelist.begin ();
384 
385  while ( first != nodelist.end () ) {
386  XmlElement * element = *first++;
387  FontBase * font = m_controller -> createFont ();
388  m_font -> setAttributes ( element, font );
389  Axes::Type axis = m_font -> getAxis ( element, m_axis );
390  if ( axis == Axes::T ) {
391  xypl -> setTitleFont ( font );
392  }
393  else {
394  xypl -> setLabelFont ( font, axis );
395  }
396  }
397  }
398 }
399 
400 void
402 createAxisModels ( const XmlElement * pl_element, PlotterBase * plotter )
403 {
404  list < XmlElement * > nodelist;
405  m_axismodel->fillNodeList ( pl_element, nodelist );
406  if ( nodelist.empty () == false ) {
407 
408 #ifdef ITERATOR_MEMBER_DEFECT
409  std::
410 #endif
411  list < XmlElement * > :: const_iterator first = nodelist.begin();
412  for ( ; first != nodelist.end (); ++first ) {
413  XmlElement * element = *first;
414  Axes::Type axis = m_axismodel->getAxis ( element, m_axis );
415  if ( axis == Axes::Z ) plotter -> setEnableZ ( true );
416  AxisModelBase * model = plotter->getAxisModel ( axis );
417 
418  if ( m_axismodel->isLog ( element ) == true ) {
419  AxisLoc label = model->getLabelLocation();
420  AxisLoc scale = model->getScaleLocation ();
421  AxisModelBase * tmp = new AxisModelLog ( label, scale );
422  std::swap ( tmp, model );
423  delete tmp;
424  plotter->setAxisModel ( model, axis );
425  } // log case
426 
427  m_axismodel->setAttributes ( model, element );
428  }
429  }
430 }
431 
433 {
434  string type;
435  bool ok = element->attribute ( m_type, type );
436  assert ( ok );
437 
438  bool has_Z = type == "XYColorPlotter";
439  if ( type == "XYPlotter" ||
440  type == "XYColorPlotter" ) { // old names of current class
441  type = "XyPlotter";
442  }
443  PlotterBase * plotter = 0;
445  try {
446  plotter = factory->create ( type );
447  if ( has_Z ) plotter ->setEnableZ ();
448  int id = element -> getID ();
449  m_controller -> registerPlotter ( id, plotter );
450 
451  string value;
452  bool needMargin;
453 
454  ok = element -> attribute ( m_title, value );
455  plotter -> setTitle ( value );
456  needMargin = String::ci_find(value, "tex:")==0;
457  plotter->setTopMargin(needMargin?10.0:0.0);
458 
459  ok = element -> attribute ( m_x_label, value );
460  plotter -> setLabel ( Axes::X, value );
461  needMargin = String::ci_find(value, "tex:")==0;
462  plotter->setBottomMargin(needMargin?8.0:0.0);
463 
464  ok = element -> attribute ( m_y_label, value );
465  plotter -> setLabel ( Axes::Y, value );
466  needMargin = String::ci_find(value, "tex:")==0;
467  plotter->setLeftMargin(needMargin?0.0:0.0);
468 
469  ok = element -> attribute ( m_z_label, value );
470  plotter -> setLabel ( Axes::Z, value );
471  needMargin = String::ci_find(value, "tex:")==0;
472  plotter->setZMargin(needMargin?7.0:0.0);
473 
474  int index;
475  ok = element -> attribute ( m_pindex, index );
476  if ( ok ) {
477  plotter -> setParentDataRepIndex ( index );
478  }
479  }
480  catch ( const FactoryException & ) {
481  assert ( false );
482  }
483 
484  return plotter;
485 }
486 
487 void
489 getCutTargets ( const XmlElement * plot_element, CutPlotter * plotter )
490 {
491  list < XmlElement * > nodelist;
492  plot_element->fillNodeList ( "CutTarget", nodelist );
493  if ( nodelist.empty() ) return;
494 
495 #ifdef ITERATOR_MEMBER_DEFECT
496  std::
497 #endif
498  list < XmlElement * > :: const_iterator first = nodelist.begin();
499  for ( ; first != nodelist.end(); ++first ) {
500  XmlElement * element = *first;
501  int ref = element->getID ();
502  DataRep * target = m_controller->getDataRep ( ref );
503  if ( target != 0 ) { // may not be part of copy/paste
504  ProjectorBase * pb = target -> getProjector ();
505  NTupleProjector * projector
506  = dynamic_cast < NTupleProjector * > ( pb );
507 
508  const vector < TupleCut > & cuts = plotter -> getCuts ();
509  for ( unsigned int i = 0; i < cuts.size(); i++ ) {
510  projector -> addCut ( &cuts[i] );
511  }
512  target -> setDirty ( true );
513  plotter->addCutTarget ( target );
514  }
515  }
516 }
517 
520 void PlotterBaseXML::handleFunction ( const XmlElement * dr_element,
521  DataRep * rep )
522 {
523  FunctionRep * frep = dynamic_cast < FunctionRep * > ( rep );
524  if ( frep == 0 ) return;
525 
526  XmlElement * element = dr_element->getNode ( "FunctionTarget" );
527  int id = element->getID ();
528  DataRep * target = m_controller->getDataRep ( id );
529  assert ( target );
530  frep->setTarget ( target );
531 }
532 
536 void
538 handleCutPlotter ( const XmlElement * plot_element, CutPlotter * plotter )
539 {
540  vector < const TupleCut * > cuts;
541 // const XmlElement * element = m_tuple_cut_xml->getNode ( plot_element );
542 
543  list < XmlElement * > node_list;
544  m_tuple_cut_xml -> fillNodeList ( plot_element, node_list );
545  list < XmlElement * >::const_iterator first = node_list.begin();
546 
547  while ( first != node_list.end() ) {
548  const XmlElement * element = *first++;
549  unsigned int a = 0;
550  // bool ok =
551  element -> attribute ( "axis", a );
552  Axes::Type axis = Axes::convert ( a );
553  const string & label = plotter -> getLabel ( axis );
554 
555  int id = element->getID ();
556  const TupleCut * tuplecut = m_controller->getTupleCut ( id );
557  TupleCut * tc = const_cast < TupleCut * > ( tuplecut );
558  tc -> setLabel ( label );
559  cuts.push_back ( tuplecut );
560  tuplecut = m_controller -> getTupleCut ( -id ); // old multiDim
561  if ( tuplecut != 0 ) {
562  cuts.push_back ( tuplecut );
563  }
564  }
565  plotter->setCuts ( cuts );
566 
567  getCutTargets ( plot_element, plotter );
568 }
569 
570 int
572 handleTextPlotter ( const XmlElement * plot_element, TextPlotter * plotter )
573 {
574 
575  XmlElement * element = plot_element->getNode ( "TextTarget" );
576  int id = element->getID ();
577  DataRep * target = m_controller->getDataRep ( id );
578 
579  if ( !target ){
580  return 0;
581  }
582 
583  assert ( target );
584  plotter->setParentDataRep ( target );
585 
586  return 1;
587 }

Generated for HippoDraw Class Library by doxygen