00001
00012
00013 #ifdef _MSC_VER
00014 #include "msdevstudio/MSconfig.h"
00015 #endif
00016
00017 #include "PlotterBaseXML.h"
00018
00019 #include "AxisModelXML.h"
00020 #include "DataRepXML.h"
00021 #include "FontXML.h"
00022 #include "PointRepXML.h"
00023 #include "TransformXML.h"
00024 #include "TupleCutXML.h"
00025 #include "XmlController.h"
00026 #include "XmlDocument.h"
00027 #include "XmlElement.h"
00028
00029 #include "axes/AxisModelLog.h"
00030 #include "controllers/DisplayController.h"
00031 #include "controllers/FunctionController.h"
00032 #include "datareps/DataRepException.h"
00033 #include "datareps/FunctionRep.h"
00034 #include "datasrcs/TupleCut.h"
00035
00036 #include "plotters/Cut1DPlotter.h"
00037 #include "plotters/Cut2DPlotter.h"
00038 #include "plotters/PlotterFactory.h"
00039 #include "plotters/TextPlotter.h"
00040
00041 #include <typeinfo>
00042
00043 #include <cassert>
00044
00045 using namespace hippodraw;
00046
00047 using std::list;
00048 using std::string;
00049
00050 PlotterBaseXML::PlotterBaseXML ( XmlController * controller )
00051 : BaseXML ( "PlotterBase", controller ),
00052 m_axis ( "axis" ),
00053 m_title ( "title" ),
00054 m_x_label ( "xlabel" ),
00055 m_y_label ( "ylabel" ),
00056 m_z_label ( "zlabel" ),
00057 m_pindex ( "pindex" )
00058 {
00059 m_axismodel = new AxisModelXML ( controller );
00060 m_datarep = new DataRepXML ( controller );
00061 m_font = new FontXML ( controller );
00062 m_pointrep = new PointRepXML ( controller );
00063 m_transform_xml = new TransformXML ( controller );
00064 m_tuple_cut_xml = new TupleCutXML ( controller );
00065 }
00066
00067 PlotterBaseXML::
00068 PlotterBaseXML ( const std::string & name,
00069 XmlController * controller )
00070 : BaseXML ( name, controller ),
00071 m_axis ( "axis" )
00072 {
00073 m_axismodel = new AxisModelXML ( controller );
00074 m_datarep = new DataRepXML ( controller );
00075 m_font = new FontXML ( controller );
00076 }
00077
00078 bool PlotterBaseXML::areDataSourcesSaved ( const PlotterBase & plotter )
00079 {
00080 DisplayController * controller = DisplayController::instance ();
00081
00082 return controller->areDataSourcesSaved ( & plotter );
00083 }
00084
00085 void PlotterBaseXML::createChildren ( XmlElement &tag,
00086 const PlotterBase & plotter )
00087 {
00088 createAxisModel ( tag, plotter, Axes::X );
00089 createAxisModel ( tag, plotter, Axes::Y );
00090 if ( plotter.hasAxis ( Axes::Z ) ) createAxisModel ( tag, plotter, Axes::Z );
00091
00092 try {
00093 const XyPlotter & xyplotter
00094 = dynamic_cast < const XyPlotter & > ( plotter );
00095 createFontElements ( tag, xyplotter );
00096 }
00097 catch ( ... ) {
00098
00099 }
00100
00101 TransformBase * transform = plotter.getTransform ();
00102 if ( transform != 0 ) {
00103 XmlElement * element = m_transform_xml->createElement ( *transform );
00104 tag.appendChild ( *element );
00105 }
00106
00107 int number = plotter.getNumDataReps ();
00108 for ( int i = 0; i < number; i++ ) {
00109 DataRep * rep = plotter.getDataRep ( i );
00110
00111 XmlElement * element = m_datarep->createElement ( *rep );
00112
00113 tag.appendChild ( *element );
00114 }
00115
00116 if ( typeid ( plotter ) == typeid ( Cut1DPlotter ) ||
00117 typeid ( plotter ) == typeid ( Cut2DPlotter ) ) {
00118 try {
00119 const CutPlotter & cut_plotter
00120 = dynamic_cast < const CutPlotter & > ( plotter );
00121
00122 createCutChildren ( tag, cut_plotter );
00123 }
00124 catch ( ... ) {
00125
00126 }
00127 }
00128
00129 if ( plotter.name() == "TextPlotter" ) {
00130 try {
00131 const TextPlotter & text_plotter
00132 = dynamic_cast < const TextPlotter & > ( plotter );
00133 createTextChildren ( tag, text_plotter );
00134 }
00135 catch ( ... ) {
00136
00137 }
00138 }
00139 return;
00140 }
00141
00142 void PlotterBaseXML::createCutChildren ( XmlElement & tag,
00143 const CutPlotter & plotter )
00144 {
00145 const vector < TupleCut > & cuts = plotter.getCuts ();
00146 for ( unsigned int i = 0; i < cuts.size (); i++ ) {
00147 XmlElement * element = m_tuple_cut_xml -> createElement ( i, cuts[i] );
00148 tag.appendChild ( *element );
00149 }
00150
00151 const list < DataRep * > & targets = plotter.getCutTargets ();
00152 #ifdef ITERATOR_MEMBER_DEFECT
00153 std::
00154 #endif
00155 list < DataRep * >::const_iterator first = targets.begin();
00156
00157 for ( ; first != targets.end(); ++first ) {
00158 DataRep * rep = *first;
00159 const void * addr = reinterpret_cast < const void * > ( & rep );
00160 int id = m_controller -> getId ( addr );
00161 XmlElement * element
00162 = XmlController::m_xml_doc->createElement ( "CutTarget" );
00163 element->setAttribute ( "id", id );
00164 tag.appendChild ( *element );
00165 }
00166 }
00167
00168 void
00169 PlotterBaseXML::
00170 createTextChildren ( XmlElement & tag,
00171 const TextPlotter & plotter )
00172 {
00173 const DataRep * rep = plotter.getParentDataRep ();
00174 const void * addr = reinterpret_cast < const void * > ( & rep );
00175 int id = m_controller -> getId ( addr );
00176 XmlElement * element
00177 = XmlController::m_xml_doc->createElement ( "TextTarget" );
00178 element->setAttribute ( "id", id );
00179
00180 tag.appendChild ( *element );
00181 }
00182
00183 void
00184 PlotterBaseXML::
00185 createFontElements ( XmlElement & tag,
00186 const XyPlotter & plotter )
00187 {
00188 const FontBase * font = plotter.titleFont ();
00189 if ( font != 0 ) {
00190 XmlElement * element = m_font -> createElement ();
00191 const string t ( "t" );
00192 element -> setAttribute ( m_axis, t );
00193 m_font -> setAttributes ( *element, *font );
00194 tag.appendChild ( *element );
00195 }
00196
00197 for ( unsigned int i = 0; i < 3; i++ ) {
00198 Axes::Type type = Axes::convert ( i );
00199 createFontElement ( tag, plotter, type );
00200 }
00201 }
00202
00203 void
00204 PlotterBaseXML::
00205 createFontElement ( XmlElement & tag,
00206 const XyPlotter & plotter,
00207 hippodraw::Axes::Type axis )
00208 {
00209 const FontBase * font = plotter.labelFont ( axis );
00210 if ( font != 0 ) {
00211 XmlElement * element = m_font -> createElement ();
00212 string s;
00213 switch ( axis )
00214 {
00215 case Axes::X :
00216 s = "x";
00217 break;
00218 case Axes::Y :
00219 s = "y";
00220 break;
00221 case Axes::Z :
00222 s = "z";
00223 break;
00224 default:
00225 assert ( false );
00226 break;
00227 }
00228 element -> setAttribute ( m_axis, s );
00229 m_font -> setAttributes ( *element, *font );
00230
00231 tag.appendChild ( *element );
00232 }
00233 }
00234
00235 void
00236 PlotterBaseXML::
00237 createAxisModel ( XmlElement & tag,
00238 const PlotterBase & plotter,
00239 hippodraw::Axes::Type axis )
00240 {
00241 const AxisModelBase * model = plotter.getAxisModel ( axis );
00242 if ( model == 0 ) return;
00243
00244 XmlElement * element = m_axismodel->createElement ();
00245 string tmp;
00246 if ( axis == Axes::X ) {
00247 tmp = "x";
00248 }
00249 else if ( axis == Axes::Y ) {
00250 tmp = "y";
00251 }
00252 else if ( axis == Axes::Z ) {
00253 tmp = "z";
00254 }
00255 element -> setAttribute ( m_axis, tmp );
00256 m_axismodel->setAttributes ( *element, *model );
00257 tag.appendChild ( *element );
00258 }
00259
00260 XmlElement * PlotterBaseXML::createElement ( const PlotterBase & plotter )
00261 {
00262 XmlElement * tag = BaseXML::createElement ();
00263 const void * addr = reinterpret_cast < const void * > ( & plotter );
00264 int id = m_controller -> getId ( addr );
00265 setId ( *tag, id );
00266
00267 tag->setAttribute ( m_type, plotter.name () );
00268
00269 const string & title = plotter.getInternalTitle ();
00270 const string & x_label = plotter.getInternalLabel ( Axes::X );
00271 const string & y_label = plotter.getInternalLabel ( Axes::Y );
00272 const string & z_label = plotter.getInternalLabel ( Axes::Z );
00273
00274 tag -> setAttribute ( m_title, title );
00275 tag -> setAttribute ( m_x_label, x_label );
00276 tag -> setAttribute ( m_y_label, y_label );
00277 tag -> setAttribute ( m_z_label, z_label );
00278
00279 PlotterBase * parent = plotter.getParentPlotter ();
00280 if ( parent != 0 ) {
00281 const void * addr = reinterpret_cast < const void * > ( parent );
00282 int ref = m_controller -> getId ( addr );
00283 tag -> setAttribute ( "ref", ref );
00284 int index = plotter.getParentDataRepIndex ( );
00285 tag -> setAttribute ( m_pindex, index );
00286 }
00287
00288 createChildren ( *tag, plotter );
00289
00290 return tag;
00291 }
00292
00293 PlotterBase * PlotterBaseXML::getObject ( const XmlElement * plot_element )
00294 {
00295 PlotterBase * plotter = createPlotter ( plot_element );
00296
00297 if ( plotter == 0 ) return 0;
00298
00299 const XmlElement * element = m_transform_xml->getNode ( plot_element );
00300 if ( element != 0 ) {
00301 TransformBase * transform = m_transform_xml->createObject ( element );
00302 plotter->setTransform ( transform );
00303 }
00304
00305 createAxisModels ( plot_element, plotter );
00306 createFontObjects ( plot_element, plotter );
00307
00308 list < XmlElement * > nodelist;
00309 m_datarep->fillNodeList ( plot_element, nodelist );
00310
00311 FunctionController * controller = FunctionController::instance();
00312
00313 #ifdef ITERATOR_MEMBER_DEFECT
00314 std::
00315 #endif
00316 list < XmlElement * > ::const_iterator first = nodelist.begin ();
00317 for ( ; first != nodelist.end(); ++first ) {
00318 XmlElement * element = *first;
00319 int id = element->getID ();
00320 DataRep * rep = m_controller->getDataRep ( id );
00321 if ( rep == 0 ) {
00322 string what ( "Unable to find data representation" );
00323 throw DataRepException ( what );
00324 }
00325
00326 assert ( rep );
00327 handleFunction ( element, rep );
00328 controller->addDataRep ( plotter, rep );
00329 }
00330
00331 CutPlotter * cutplotter = dynamic_cast < CutPlotter * > ( plotter );
00332 if ( cutplotter != 0 ) {
00333 handleCutPlotter ( plot_element, cutplotter );
00334 }
00335
00336 TextPlotter * textplotter = dynamic_cast < TextPlotter * > ( plotter );
00337 if ( textplotter != 0 ) {
00338 if ( ! ( handleTextPlotter ( plot_element, textplotter ) ) ){
00339 return 0;
00340 }
00341 }
00342
00343 return plotter;
00344 }
00345
00346 void
00347 PlotterBaseXML::
00348 createFontObjects ( const XmlElement * plot_element, PlotterBase * plotter )
00349 {
00350 list < XmlElement * > nodelist;
00351 m_font -> fillNodeList ( plot_element, nodelist );
00352 if ( nodelist.empty () == false ) {
00353 XyPlotter * xypl = dynamic_cast < XyPlotter * > ( plotter );
00354 assert ( xypl );
00355
00356 list < XmlElement * > ::const_iterator first = nodelist.begin ();
00357
00358 while ( first != nodelist.end () ) {
00359 XmlElement * element = *first++;
00360 FontBase * font = m_controller -> createFont ();
00361 m_font -> setAttributes ( element, font );
00362 Axes::Type axis = m_font -> getAxis ( element, m_axis );
00363 if ( axis == Axes::T ) {
00364 xypl -> setTitleFont ( font );
00365 }
00366 else {
00367 xypl -> setLabelFont ( font, axis );
00368 }
00369 }
00370 }
00371 }
00372
00373 void
00374 PlotterBaseXML::
00375 createAxisModels ( const XmlElement * pl_element, PlotterBase * plotter )
00376 {
00377 list < XmlElement * > nodelist;
00378 m_axismodel->fillNodeList ( pl_element, nodelist );
00379 if ( nodelist.empty () == false ) {
00380
00381 #ifdef ITERATOR_MEMBER_DEFECT
00382 std::
00383 #endif
00384 list < XmlElement * > :: const_iterator first = nodelist.begin();
00385 for ( ; first != nodelist.end (); ++first ) {
00386 XmlElement * element = *first;
00387 Axes::Type axis = m_axismodel->getAxis ( element, m_axis );
00388 if ( axis == Axes::Z ) plotter -> setEnableZ ( true );
00389 AxisModelBase * model = plotter->getAxisModel ( axis );
00390
00391 if ( m_axismodel->isLog ( element ) == true ) {
00392 AxisLoc label = model->getLabelLocation();
00393 AxisLoc scale = model->getScaleLocation ();
00394 AxisModelBase * tmp = new AxisModelLog ( label, scale );
00395 std::swap ( tmp, model );
00396 delete tmp;
00397 plotter->setAxisModel ( model, axis );
00398 }
00399
00400 m_axismodel->setAttributes ( model, element );
00401 }
00402 }
00403 }
00404
00405 PlotterBase * PlotterBaseXML::createPlotter ( const XmlElement * element )
00406 {
00407 string type;
00408 bool ok = element->attribute ( m_type, type );
00409 assert ( ok );
00410
00411 bool has_Z = type == "XYColorPlotter";
00412 if ( type == "XYPlotter" ||
00413 type == "XYColorPlotter" ) {
00414 type = "XyPlotter";
00415 }
00416 PlotterBase * plotter = 0;
00417 PlotterFactory * factory = PlotterFactory::instance ();
00418 try {
00419 plotter = factory->create ( type );
00420 if ( has_Z ) plotter ->setEnableZ ();
00421 int id = element -> getID ();
00422 m_controller -> registerPlotter ( id, plotter );
00423
00424 string value;
00425 ok = element -> attribute ( m_title, value );
00426 plotter -> setTitle ( value );
00427 ok = element -> attribute ( m_x_label, value );
00428 plotter -> setLabel ( Axes::X, value );
00429 ok = element -> attribute ( m_y_label, value );
00430 plotter -> setLabel ( Axes::Y, value );
00431 ok = element -> attribute ( m_z_label, value );
00432 plotter -> setLabel ( Axes::Z, value );
00433
00434 int index;
00435 ok = element -> attribute ( m_pindex, index );
00436 if ( ok ) {
00437 plotter -> setParentDataRepIndex ( index );
00438 }
00439 }
00440 catch ( const FactoryException & ) {
00441 assert ( false );
00442 }
00443
00444 return plotter;
00445 }
00446
00447 void
00448 PlotterBaseXML::
00449 getCutTargets ( const XmlElement * plot_element, CutPlotter * plotter )
00450 {
00451 list < XmlElement * > nodelist;
00452 plot_element->fillNodeList ( "CutTarget", nodelist );
00453 if ( nodelist.empty() ) return;
00454
00455 #ifdef ITERATOR_MEMBER_DEFECT
00456 std::
00457 #endif
00458 list < XmlElement * > :: const_iterator first = nodelist.begin();
00459 for ( ; first != nodelist.end(); ++first ) {
00460 XmlElement * element = *first;
00461 int ref = element->getID ();
00462 DataRep * target = m_controller->getDataRep ( ref );
00463 if ( target != 0 ) {
00464 plotter->addCutTarget ( target );
00465 }
00466 }
00467 }
00468
00471 void PlotterBaseXML::handleFunction ( const XmlElement * dr_element,
00472 DataRep * rep )
00473 {
00474 FunctionRep * frep = dynamic_cast < FunctionRep * > ( rep );
00475 if ( frep == 0 ) return;
00476
00477 XmlElement * element = dr_element->getNode ( "FunctionTarget" );
00478 int id = element->getID ();
00479 DataRep * target = m_controller->getDataRep ( id );
00480 assert ( target );
00481 frep->setTarget ( target );
00482 }
00483
00487 void
00488 PlotterBaseXML::
00489 handleCutPlotter ( const XmlElement * plot_element, CutPlotter * plotter )
00490 {
00491 vector < const TupleCut * > cuts;
00492
00493 const XmlElement * element = m_tuple_cut_xml->getNode ( plot_element );
00494 int id = element->getID ();
00495 const TupleCut * tuplecut = m_controller->getTupleCut ( id );
00496 cuts.push_back ( tuplecut );
00497 tuplecut = m_controller -> getTupleCut ( -id );
00498 if ( tuplecut != 0 ) {
00499 cuts.push_back ( tuplecut );
00500 }
00501 plotter->setCuts ( cuts );
00502
00503 element = m_pointrep->getNode ( plot_element );
00504
00505
00506
00507
00508 getCutTargets ( plot_element, plotter );
00509 }
00510
00511 int
00512 PlotterBaseXML::
00513 handleTextPlotter ( const XmlElement * plot_element, TextPlotter * plotter )
00514 {
00515
00516 XmlElement * element = plot_element->getNode ( "TextTarget" );
00517 int id = element->getID ();
00518 DataRep * target = m_controller->getDataRep ( id );
00519
00520 if ( !target ){
00521 return 0;
00522 }
00523
00524 assert ( target );
00525 plotter->setParentDataRep ( target );
00526
00527 return 1;
00528 }