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 std::string;
00028 using std::vector;
00029
00030 namespace hippodraw {
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 AxisModelXML::
00045 ~AxisModelXML ()
00046 {
00047 delete m_axistick_xml;
00048 }
00049
00050 void AxisModelXML::setAttributes ( XmlElement & tag,
00051 const AxisModelBase & model )
00052 {
00053 bool yes = model.isAutoRanging ();
00054 if ( yes ) {
00055 tag.setAttribute ( m_autorange, 1 );
00056 }
00057 else {
00058 tag.setAttribute ( m_autorange, 0 );
00059 }
00060 const Range & range = model.getRange ( false );
00061 tag.setAttribute ( m_low, range.low() );
00062 tag.setAttribute ( m_high, range.high() );
00063
00064 tag.setAttribute ( m_scale_factor, model.getScaleFactor () );
00065
00066 if ( model.isLog () == true ) {
00067 tag.setAttribute ( m_log, 1 );
00068 }
00069
00070 yes = model.isAutoTicks ();
00071 if ( yes ) {
00072 tag.setAttribute ( m_auto_tick, 1 );
00073 }
00074 else {
00075 tag.setAttribute ( m_auto_tick, 0 );
00076 createChildren ( tag, model );
00077 }
00078 }
00079
00080 void
00081 AxisModelXML::
00082 createChildren ( XmlElement & tag, const AxisModelBase & model )
00083 {
00084 const vector < AxisTick > & ticks = model.getTicks ();
00085 unsigned int size = ticks.size ();
00086 for ( unsigned int i = 0; i < size; i++ ) {
00087 const AxisTick & tick = ticks [ i ];
00088 XmlElement * element = m_axistick_xml -> createElement ();
00089 m_axistick_xml -> setAttributes ( *element, tick );
00090 tag.appendChild ( *element );
00091 delete element;
00092 }
00093 }
00094
00095 Axes::Type
00096 AxisModelXML::
00097 getAxis ( const XmlElement * element,
00098 const std::string & tagname )
00099 {
00100 string value;
00101 bool ok = element->attribute ( tagname, value );
00102 assert ( ok );
00103
00104 return Axes::convert ( value );
00105 }
00106
00107 bool AxisModelXML::isLog ( const XmlElement * element )
00108 {
00109 int value;
00110 bool ok = element->attribute ( m_log, value );
00111 if ( ok && value != 0 ) return true;
00112
00113 return false;
00114 }
00115
00116 void AxisModelXML::setAttributes ( AxisModelBase * model,
00117 const XmlElement * element )
00118
00119 {
00120 int value;
00121 bool ok = element->attribute ( m_autorange, value );
00122 if ( ok && ( value == 0 ) ) model->setAutoRanging ( false );
00123
00124 double low = 0.0;
00125 ok = element->attribute ( m_low, low );
00126 double high = 0.0;
00127 ok = element->attribute ( m_high, high );
00128
00129 Range range ( low, high );
00130
00131 model->setRange ( range, false );
00132
00133 double scale_factor;
00134 ok = element->attribute ( m_scale_factor, scale_factor );
00135 if ( ok ) model->setScaleFactor ( scale_factor );
00136
00137 ok = element -> attribute ( m_auto_tick, value );
00138 if ( ok ) {
00139 bool yes = value != 0;
00140 model -> setAutoTicks ( yes );
00141 if ( ! yes ) {
00142 createChildren ( element, model );
00143 }
00144 }
00145 }
00146
00147 void
00148 AxisModelXML::
00149 createChildren ( const XmlElement * element, AxisModelBase * model )
00150 {
00151 vector < AxisTick > ticks;
00152 AxisTick tick;
00153
00154 NodeList_t nodelist;
00155 m_axistick_xml -> fillNodeList ( element, nodelist );
00156 if ( nodelist.empty () == false ) {
00157 NodeList_t :: const_iterator first = nodelist.begin ();
00158 while ( first != nodelist.end() ) {
00159 XmlElement * element = *first++;
00160 m_axistick_xml -> setAttributes ( & tick, element );
00161 ticks.push_back ( tick );
00162 }
00163 model -> setTicks ( ticks );
00164 }
00165 }
00166
00167 }
00168