QtXmlElement.cxx
Go to the documentation of this file.
1 
12 #include "QtXmlElement.h"
13 
14 #include "pattern/string_convert.h"
15 
16 #include <qstring.h>
17 
18 #include <cassert>
19 
20 using std::list;
21 using std::string;
22 
23 using namespace hippodraw;
24 
26  : XmlNode (),
27  XmlElement (),
28  QtXmlNode ( element )
29 {
30 #if QT_VERSION < 0x040000
31  const QDomElement & elem
32  = dynamic_cast < const QDomElement & > ( element.m_node );
33 #else
34  const QDomElement & elem
35  = reinterpret_cast < const QDomElement & > ( element.m_node );
36 #endif
37 
38  // make copy of pointer so we don't have to cast in the implementation.
39  m_element = elem;
40  m_node = m_element;
41 }
42 
47  : QtXmlNode ( )
48 {
49  // make copy of pointer so we don't have to cast in the implementation.
50  m_element = element;
51  m_node = m_element;
52 }
53 
56 {
57 }
58 
59 string
61 {
62  QString tagname = m_element.tagName ();
63 
64 #if QT_VERSION < 0x040000
65  return string ( tagname.latin1() );
66 #else
67  return tagname.toStdString ();
68 #endif
69 }
70 
71 int QtXmlElement::getID () const
72 {
73  QString value = m_element.attribute ( "id" );
74  if ( value == QString::null ) return 0;
75 
76  bool ok;
77  int id = value.toInt ( &ok );
78  if ( ok == false ) id = 0;
79 
80  return id;
81 }
82 
83 #ifdef CLONE_DEFECT
84 XmlElement *
85 #else
87 #endif
89 getNode ( const std::string & tagName ) const
90 {
91 
92  QString tagname ( tagName.c_str () );
93  QDomNode node = m_element.namedItem ( tagname );
94  if ( node.isNull () ) return 0;
95 
96  QDomElement element = node.toElement ();
97  assert ( element.isNull () == false );
98  return new QtXmlElement ( element );
99 }
100 
103 void
105 fillNodeList ( const std::string & tagName,
106  std::list < XmlElement * > & nodeList ) const
107 {
108  QString tag_name ( tagName.c_str() );
109 
110 
111  QDomNodeList nodelist = m_element.elementsByTagName ( tag_name );
112  unsigned int size ( nodelist.count () );
113  for ( unsigned int i = 0; i < size; i++ ) {
114  QDomNode node = nodelist.item ( i );
115  QDomElement element = node.toElement ();
116  nodeList.push_back ( new QtXmlElement ( element ) );
117  }
118 
119 }
120 
121 void
123 setAttribute ( const std::string & name, int value )
124 {
125  assert ( ! name.empty () );
126  m_element.setAttribute ( name.c_str(), value );
127 }
128 
129 
130 void
132 setAttribute ( const std::string & name, bool yes )
133 {
134  assert ( ! name.empty () );
135 
136  unsigned int value = yes ? 1 : 0;
137  m_element.setAttribute ( name.c_str(), value );
138 }
139 
140 void
142 setAttribute ( const std::string & name, unsigned int value )
143 {
144  assert ( ! name.empty () );
145  m_element.setAttribute ( name.c_str(), value );
146 }
147 
148 void
150 setAttribute ( const std::string & name, float value )
151 {
152  assert ( ! name.empty () );
153  double v = value; // Qt only supports double converstion to string
154 
155  setAttribute ( name, v );
156 }
157 
158 void
160 setAttribute ( const std::string & name, double value )
161 {
162  assert ( ! name.empty () );
163  // The following lead to a double 36. to be converted to "35.:" for
164  // a ViewBase X coordinate.
165  // m_element.setAttribute ( name.c_str(), value );
166  string vtext = String::convert ( value );
167 #if QT_VERSION < 0x040000
168  m_element.setAttribute ( name.c_str(), vtext.c_str() );
169 #else
170  const QString qname ( name.c_str() );
171  const string text ( vtext.c_str() );
172  const QString qtext ( text.c_str() );
173  m_element.setAttribute ( qname, qtext );
174 #endif
175 }
176 
177 void
179 setAttribute ( const std::string & name,
180  const std::string & value )
181 {
182  assert ( ! name.empty () );
183  m_element.setAttribute ( name.c_str(), value.c_str() );
184 }
185 
186 bool
188 attribute ( const std::string & name, int & value ) const
189 {
190  QString rstring = m_element.attribute ( name.c_str() );
191  if ( rstring == QString::null ) return false;
192 
193  bool ok;
194  int val = rstring.toInt ( & ok );
195  if ( ! ok ) return false;
196 
197  value = val;
198 
199  return true;
200 }
201 
202 bool
204 attribute ( const std::string & name, bool & value ) const
205 {
206  QString rstring = m_element.attribute ( name.c_str() );
207  if ( rstring == QString::null ) return false;
208 
209  bool ok;
210  int val = rstring.toInt ( & ok );
211  if ( ! ok ) return false;
212 
213  value = ( val = 1 ) ? true : false;
214 
215  return true;
216 }
217 
218 bool
220 attribute ( const std::string & name, unsigned int & value ) const
221 {
222  QString rstring = m_element.attribute ( name.c_str() );
223  if ( rstring == QString::null ) return false;
224 
225  bool ok;
226  int val = rstring.toUInt ( & ok );
227  if ( ! ok ) return false;
228 
229  value = val;
230 
231  return true;
232 }
233 
234 bool
236 attribute ( const std::string & name, float & value ) const
237 {
238  QString rstring = m_element.attribute ( name.c_str() );
239  if ( rstring == QString::null ) return false;
240 
241  bool ok;
242  float val = rstring.toFloat ( & ok );
243  if ( ! ok ) return false;
244 
245  value = val;
246 
247  return true;
248 }
249 
250 bool
252 attribute ( const std::string & name, double & value ) const
253 {
254  QString rstring = m_element.attribute ( name.c_str() );
255  if ( rstring == QString::null ) return false;
256 
257  bool ok;
258  float val = rstring.toDouble ( & ok );
259  if ( ! ok ) return false;
260 
261  value = val;
262 
263  return true;
264 }
265 
266 bool
268 attribute ( const std::string & name, std::string & value ) const
269 {
270  QString val = m_element.attribute ( name.c_str() );
271  if ( val == QString::null ) return false;
272 
273 #if QT_VERSION < 0x040000
274  value = val.latin1 ();
275 #else
276  value = val.toStdString ();
277 #endif
278  return true;
279 }
280 
281 const string &
283 getText () const
284 {
285  static string text; // don't initialize it.
286  QString t = m_element.text ();
287 #if QT_VERSION < 0x040000
288  text = t.latin1 ();
289 #else
290  text = t.toStdString ();
291 #endif
292 
293  return text;
294 }

Generated for HippoDraw Class Library by doxygen