Developer documentation | Axl-2.5.1

axlCylinderReader.cpp
Go to the documentation of this file.
1 /* axlCylinderReader.cpp ---
2  *
3  * Author: Meriadeg Perrinel
4  * Copyright (C) 2008-2011 - Meriadeg Perrinel, Inria.
5  * Created: Wed Sep 21 11:36:52 2011 (+0200)
6  * Version: $Id$
7  * Last-Updated: Wed Sep 21 12:11:29 2011 (+0200)
8  * By: Meriadeg Perrinel
9  * Update #: 88
10  */
11 
12 /* Commentary:
13  *
14  */
15 
16 /* Change log:
17  *
18  */
19 
20 #include "axlCylinderReader.h"
21 
22 #include <axlCore/axlCylinder.h>
23 #include <axlCore/axlPoint.h>
25 
26 #include <dtkCoreSupport/dtkAbstractData.h>
27 #include <dtkCoreSupport/dtkAbstractDataFactory.h>
30 
31 // /////////////////////////////////////////////////////////////////
32 // axlCylinderReader
33 // /////////////////////////////////////////////////////////////////
34 
36 {
37  this->setObjectName(this->description());
38 }
39 
41 {
42 
43 }
44 
45 QString axlCylinderReader::identifier(void) const
46 {
47  return "axlCylinderReader";
48 }
49 
50 QString axlCylinderReader::description(void) const
51 {
52  return "axlCylinderReader";
53 }
54 
55 QStringList axlCylinderReader::handled(void) const
56 {
57  return QStringList() << "axlCylinder";
58 }
59 
61 {
62  return dtkAbstractDataFactory::instance()->registerDataReaderType("axlCylinderReader", QStringList(), createaxlCylinderReader);
63 }
64 
65 bool axlCylinderReader::accept(const QDomNode& node)
66 {
67  QDomElement element = node.toElement();
68 
69  if(element.tagName() != "cylinder")
70  return false;
71 
72  if(!hasChildNode(element, "point"))
73  return false;
74 
75  if(!hasChildNode(element, "radius"))
76  return false;
77 
78  return true;
79 }
80 
81 bool axlCylinderReader::reject(const QDomNode& node)
82 {
83  return !this->accept(node);
84 }
85 
87 {
88  QDomElement element = node.toElement();
89 
90  axlCylinder *currentCylinder = new axlCylinder();
91 
92  QString name = element.attribute("name");
93  if(!name.isEmpty())
94  {
95  currentCylinder->setObjectName(name);
96  }
97 
98  QString color = element.attribute("color");
99  if(!color.isEmpty())
100  {
101  QStringList colorList = color.split(" ");
102  if(colorList.size() > 2) // rgb components
103  currentCylinder->setColor(QColor(colorList.at(0).toInt(), colorList.at(1).toInt(), colorList.at(2).toInt()));
104  if(colorList.size() == 4)
105  currentCylinder->setOpacity(colorList.at(3).toFloat());
106  }
107 
108  QString shader = element.attribute("shader");
109  QString dirShader;
110  if(!shader.isEmpty())
111  {
112  // try to read from axelShader.qrc
113  dirShader = ":axlShader/shader/"+shader;
114  if(!QFile::exists(dirShader))
115  {
116  QSettings settings("inria", "dtk");
117  QString defaultPath;
118  settings.beginGroup("shader");
119  dirShader = settings.value("path", defaultPath).toString();
120 // dirShader = this->file().left(this->file().lastIndexOf("axel-data") + 9); // to Remove later
121  dirShader.append("/"+shader);
122  }
123  currentCylinder->setShader(dirShader);
124  }
125 
126  QString size = element.attribute("size");
127  if(!size.isEmpty())
128  currentCylinder->setSize(size.toFloat());
129 
130  QDomNodeList nodelistPoint = element.elementsByTagName("point") ;
131 
132  //FirstPoint
133  QDomElement elementfirstPoint = nodelistPoint.item(0).toElement() ;
134 
135  QStringList coordinates = elementfirstPoint.text().simplified().split(QRegExp("\\s+"));
136  if(coordinates.size() == 3)
137  {
138  currentCylinder->setFirstPoint(new axlPoint(coordinates[0].toDouble(), coordinates[1].toDouble(), coordinates[2].toDouble()));
139  }
140  else
141  qDebug()<<"Cylinder data cannot be read correctly : coordinates";
142 
143  //SecondPOint
144  QDomElement elementsecondPoint = nodelistPoint.item(1).toElement() ;
145 
146  coordinates = elementsecondPoint.text().simplified().split(QRegExp("\\s+"));
147  if(coordinates.size() == 3)
148  {
149  currentCylinder->setSecondPoint(new axlPoint(coordinates[0].toDouble(), coordinates[1].toDouble(), coordinates[2].toDouble()));
150 
151  }
152  else
153  qDebug()<<"Cylinder data cannot be read correctly : coordinates";
154 
155  //radius
156  QDomNodeList nodelistradius = element.elementsByTagName("radius") ;
157  QDomElement elementradius = nodelistradius.item(0).toElement() ;
158 
159  QStringList radius = elementradius.text().simplified().split(QRegExp("\\s+"));
160  if(radius.size() == 1)
161  {
162  currentCylinder->setRadius(radius[0].toDouble());
163  }
164  else
165  qDebug()<<"Cylinder data cannot be read correctly : radius";
166 
167 
168  //if there are some field, read them thanks to the factory.
169  QDomNodeList nodeListField = element.elementsByTagName("field");
170  if(!nodeListField.isEmpty()){
171  for(int i =0; i < nodeListField.size(); i++){
172  QDomElement fieldElement = nodeListField.at(i).toElement();
173  QString fieldType = fieldElement.attribute("type");
174  if(!fieldType.isEmpty()){
175  axlAbstractDataReader *field_reader = dynamic_cast<axlAbstractDataReader *>(axlFieldReadersFactory::instance()->create(fieldType));
176  axlAbstractField * fieldToAdd = dynamic_cast<axlAbstractField *>(field_reader->read(fieldElement));
177  if(fieldToAdd){
178  QString newName = currentCylinder->changeFieldName(fieldToAdd->name());
179  fieldToAdd->setObjectName(newName);
180  currentCylinder->addField(fieldToAdd);
181  }
182  }
183  }
184  }
185 
186  return currentCylinder;
187 
188 }
189 
190 dtkAbstractDataReader *createaxlCylinderReader(void)
191 {
192  return new axlCylinderReader;
193 }
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
void setFirstPoint(axlPoint *p1)
Change first point of this cylinder.
void setRadius(double radius)
Change radius of this cylinder.
bool reject(const QDomNode &node)
QStringList handled(void) const
Class axlCylinder defines 3D cylinders.
Definition: axlCylinder.h:33
void setShader(const QString &shader)
void setSecondPoint(axlPoint *p2)
Change second point of this cylinder.
QString changeFieldName(QString fieldName)
dtkAbstractDataReader * createaxlCylinderReader(void)
dtkAbstractDataReader * create(const QString &interface_name)
axlAbstractData * read(const QDomNode &node)
virtual axlAbstractData * read(const QDomNode &node)=0
QString identifier(void) const
bool accept(const QDomNode &node)
Class axlAbstractField defines an API for arrays of numeric data.
QString description(void) const
bool hasChildNode(QDomElement element, const QString &tag)
Definition: axlFormat.h:20
void setOpacity(const double &opacity)
static axlFieldReadersFactory * instance(void)
void addField(axlAbstractField *field)
Add a field to the field list of the object.
void setSize(const double &size)
void setColor(double r, double g, double b)
Class axlAbstractData defines an API for all type of axel data.
static bool registered(void)