00001
00012
00013 #ifdef _MSC_VER
00014 #include "msdevstudio/MSconfig.h"
00015 #endif
00016
00017 #include "AxisModelXML.h"
00018
00019 #include "AxisTickXML.h"
00020 #include "XmlElement.h"
00021
00022 #include "axes//AxisModelBase.h"
00023 #include "axes//AxisTick.h"
00024
00025 #include <cassert>
00026
00027 using namespace hippodraw;
00028
00029 using std::string;
00030 using std::vector;
00031
00032 AxisModelXML::AxisModelXML ( XmlController * controller )
00033 : BaseXML ( "AxisModel", controller ),
00034 m_autorange ( "autorange" ),
00035 m_low ( "low" ),
00036 m_high ( "high" ),
00037 m_scale_factor ( "scale_factor" ),
00038 m_log ( "log" ),
00039 m_auto_tick ( "autotick" )
00040 {
00041 m_axistick_xml = new AxisTickXML ( controller );
00042 }
00043
00044 void AxisModelXML::setAttributes ( XmlElement & tag,
00045 const AxisModelBase & model )
00046 {
00047 bool yes = model.isAutoRanging ();
00048 if ( yes ) {
00049 tag.setAttribute ( m_autorange, 1 );
00050 }
00051 else {
00052 tag.setAttribute ( m_autorange, 0 );
00053 }
00054 const Range & range = model.getRange ( false );
00055 tag.setAttribute ( m_low, range.low() );
00056 tag.setAttribute ( m_high, range.high() );
00057
00058 tag.setAttribute ( m_scale_factor, model.getScaleFactor () );
00059
00060 if ( model.isLog () == true ) {
00061 tag.setAttribute ( m_log, 1 );
00062 }
00063
00064 yes = model.isAutoTicks ();
00065 if ( yes ) {
00066 tag.setAttribute ( m_auto_tick, 1 );
00067 }
00068 else {
00069 tag.setAttribute ( m_auto_tick, 0 );
00070 createChildren ( tag, model );
00071 }
00072 }
00073
00074 void
00075 AxisModelXML::
00076 createChildren ( XmlElement & tag, const AxisModelBase & model )
00077 {
00078 const vector < AxisTick > & ticks = model.getTicks ();
00079 unsigned int size = ticks.size ();
00080 for ( unsigned int i = 0; i < size; i++ ) {
00081 const AxisTick & tick = ticks [ i ];
00082 XmlElement * element = m_axistick_xml -> createElement ();
00083 m_axistick_xml -> setAttributes ( *element, tick );
00084 tag.appendChild ( *element );
00085 }
00086 }
00087
00088 Axes::Type
00089 AxisModelXML::
00090 getAxis ( const XmlElement * element,
00091 const std::string & tagname )
00092 {
00093 string value;
00094 bool ok = element->attribute ( tagname, value );
00095 assert ( ok );
00096
00097 return Axes::convert ( value );
00098 }
00099
00100 bool AxisModelXML::isLog ( const XmlElement * element )
00101 {
00102 int value;
00103 bool ok = element->attribute ( m_log, value );
00104 if ( ok && value != 0 ) return true;
00105
00106 return false;
00107 }
00108
00109 void AxisModelXML::setAttributes ( AxisModelBase * model,
00110 const XmlElement * element )
00111
00112 {
00113 int value;
00114 bool ok = element->attribute ( m_autorange, value );
00115 if ( ok && ( value == 0 ) ) model->setAutoRanging ( false );
00116
00117 double low = 0.0;
00118 ok = element->attribute ( m_low, low );
00119 double high = 0.0;
00120 ok = element->attribute ( m_high, high );
00121
00122 Range range ( low, high );
00123
00124 model->setRange ( range, false );
00125
00126 double scale_factor;
00127 ok = element->attribute ( m_scale_factor, scale_factor );
00128 if ( ok ) model->setScaleFactor ( scale_factor );
00129
00130 ok = element -> attribute ( m_auto_tick, value );
00131 if ( ok ) {
00132 bool yes = value != 0;
00133 model -> setAutoTicks ( yes );
00134 if ( ! yes ) {
00135 createChildren ( element, model );
00136 }
00137 }
00138 }
00139
00140 void
00141 AxisModelXML::
00142 createChildren ( const XmlElement * element, AxisModelBase * model )
00143 {
00144 vector < AxisTick > ticks;
00145 AxisTick tick;
00146
00147 NodeList_t nodelist;
00148 m_axistick_xml -> fillNodeList ( element, nodelist );
00149 if ( nodelist.empty () == false ) {
00150 NodeList_t :: const_iterator first = nodelist.begin ();
00151 while ( first != nodelist.end() ) {
00152 XmlElement * element = *first++;
00153 m_axistick_xml -> setAttributes ( & tick, element );
00154 ticks.push_back ( tick );
00155 }
00156 model -> setTicks ( ticks );
00157 }
00158 }