Developer documentation | Axl-2.5.1

axlConeReader.cpp
Go to the documentation of this file.
1 /* axlConeReader.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 "axlConeReader.h"
21 
22 #include <axlCore/axlCone.h>
23 #include <axlCore/axlPoint.h>
27 
28 #include <dtkCoreSupport/dtkAbstractData.h>
29 #include <dtkCoreSupport/dtkAbstractDataFactory.h>
30 
31 // /////////////////////////////////////////////////////////////////
32 // axlConeReader
33 // /////////////////////////////////////////////////////////////////
34 
36 {
37  this->setObjectName(this->description());
38 }
39 
41 {
42 
43 }
44 
45 QString axlConeReader::identifier(void) const
46 {
47  return "axlConeReader";
48 }
49 
50 QString axlConeReader::description(void) const
51 {
52  return "axlConeReader";
53 }
54 
55 QStringList axlConeReader::handled(void) const
56 {
57  return QStringList() << "axlCone";
58 }
59 
61 {
62  return dtkAbstractDataFactory::instance()->registerDataReaderType("axlConeReader", QStringList(), createaxlConeReader);
63 }
64 
65 bool axlConeReader::accept(const QDomNode& node)
66 {
67  QDomElement element = node.toElement();
68 
69  if(element.tagName() != "cone")
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 axlConeReader::reject(const QDomNode& node)
82 {
83  return !this->accept(node);
84 }
85 
86 axlAbstractData *axlConeReader::read(const QDomNode& node)
87 {
88  QDomElement element = node.toElement();
89 
90  axlCone *currentCone = new axlCone();
91 
92  QString name = element.attribute("name");
93  if(!name.isEmpty())
94  {
95  currentCone->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  currentCone->setColor(QColor(colorList.at(0).toInt(), colorList.at(1).toInt(), colorList.at(2).toInt()));
104  if(colorList.size() == 4)
105  currentCone->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  currentCone->setShader(dirShader);
124  }
125 
126  QString size = element.attribute("size");
127  if(!size.isEmpty())
128  currentCone->setSize(size.toFloat());
129 
130  //Apex
131  QDomNodeList nodelistApex = element.elementsByTagName("point") ;
132  QDomElement elementApex = nodelistApex.item(0).toElement() ;
133 
134  QStringList coordinates = elementApex.text().simplified().split(QRegExp("\\s+"));
135  if(coordinates.size() == 3)
136  {
137  currentCone->setApex(new axlPoint(coordinates[0].toDouble(), coordinates[1].toDouble(), coordinates[2].toDouble()));
138  }
139  else
140  qDebug()<<"Cone data cannot be read correctly : coordinates";
141 
142  //BasePoint
143  QDomNodeList nodelistBasePoint = element.elementsByTagName("point") ;
144  QDomElement elementBasePoint = nodelistBasePoint.item(1).toElement() ;
145 
146  coordinates = elementBasePoint.text().simplified().split(QRegExp("\\s+"));
147  if(coordinates.size() == 3)
148  {
149  currentCone->setBasePoint(new axlPoint(coordinates[0].toDouble(), coordinates[1].toDouble(), coordinates[2].toDouble()));
150 
151  }
152  else
153  qDebug()<<"Cone 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  currentCone->setRadius(radius[0].toDouble());
163  }
164  else
165  qDebug()<<"Cone 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 = currentCone->changeFieldName(fieldToAdd->name());
179  fieldToAdd->setObjectName(newName);
180  currentCone->addField(fieldToAdd);
181  }
182  }
183  }
184  }
185 
186  return currentCone;
187 
188 }
189 
190 dtkAbstractDataReader *createaxlConeReader(void)
191 {
192  return new axlConeReader;
193 }
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
QString identifier(void) const
bool accept(const QDomNode &node)
bool reject(const QDomNode &node)
QStringList handled(void) const
axlAbstractData * read(const QDomNode &node)
void setApex(axlPoint *apex)
Change apex of this cone.
Definition: axlCone.cpp:185
void setShader(const QString &shader)
QString changeFieldName(QString fieldName)
void setRadius(double radius)
Change radius of this cone.
Definition: axlCone.cpp:221
dtkAbstractDataReader * create(const QString &interface_name)
dtkAbstractDataReader * createaxlConeReader(void)
virtual axlAbstractData * read(const QDomNode &node)=0
Class axlAbstractField defines an API for arrays of numeric data.
QString description(void) const
static bool registered(void)
bool hasChildNode(QDomElement element, const QString &tag)
Definition: axlFormat.h:20
void setOpacity(const double &opacity)
Class axlCone defines 3D cones.
Definition: axlCone.h:34
static axlFieldReadersFactory * instance(void)
void addField(axlAbstractField *field)
Add a field to the field list of the object.
void setBasePoint(axlPoint *basePoint)
Change base point of this cone.
Definition: axlCone.cpp:203
void setSize(const double &size)
void setColor(double r, double g, double b)
Class axlAbstractData defines an API for all type of axel data.