Developer documentation | Axl-2.5.1

axlSphereReader.cpp
Go to the documentation of this file.
1 /* axlSphereReader.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 "axlSphereReader.h"
21 
22 #include <axlCore/axlSphere.h>
24 
25 #include <dtkCoreSupport/dtkAbstractData.h>
26 #include <dtkCoreSupport/dtkAbstractDataFactory.h>
29 
30 // /////////////////////////////////////////////////////////////////
31 // axlSphereReader
32 // /////////////////////////////////////////////////////////////////
33 
35 {
36  this->setObjectName(this->description());
37 }
38 
40 {
41 
42 }
43 
44 QString axlSphereReader::identifier(void) const
45 {
46  return "axlSphereReader";
47 }
48 
49 QString axlSphereReader::description(void) const
50 {
51  return "axlSphereReader";
52 }
53 
54 QStringList axlSphereReader::handled(void) const
55 {
56  return QStringList() << "axlSphere";
57 }
58 
60 {
61  return dtkAbstractDataFactory::instance()->registerDataReaderType("axlSphereReader", QStringList(), createaxlSphereReader);
62 }
63 
64 bool axlSphereReader::accept(const QDomNode& node)
65 {
66  QDomElement element = node.toElement();
67 
68  if(element.tagName() != "sphere")
69  return false;
70 
71  if(!hasChildNode(element, "center"))
72  return false;
73 
74  if(!hasChildNode(element, "radius"))
75  return false;
76 
77 
78  return true;
79 }
80 
81 bool axlSphereReader::reject(const QDomNode& node)
82 {
83  return !this->accept(node);
84 }
85 
86 axlAbstractData *axlSphereReader::read(const QDomNode& node)
87 {
88  QDomElement element = node.toElement();
89 
90  axlSphere *currentSphere = new axlSphere();
91 
92  QString name = element.attribute("name");
93  if(!name.isEmpty())
94  {
95  currentSphere->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  currentSphere->setColor(QColor(colorList.at(0).toInt(), colorList.at(1).toInt(), colorList.at(2).toInt()));
104  if(colorList.size() == 4)
105  currentSphere->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  currentSphere->setShader(dirShader);
124  }
125 
126  QString size = element.attribute("size");
127  if(!size.isEmpty())
128  currentSphere->setSize(size.toFloat());
129 
130  //Center
131  QDomNodeList nodelistcenter = element.elementsByTagName("center") ;
132  QDomElement elementCenter = nodelistcenter.item(0).toElement() ;
133 
134  QStringList coordinates = elementCenter.text().simplified().split(QRegExp("\\s+"));
135  if(coordinates.size() == 3)
136  {
137 
138  currentSphere->coordinates()[0] = coordinates[0].toFloat();
139  currentSphere->coordinates()[1] = coordinates[1].toFloat();
140  currentSphere->coordinates()[2] = coordinates[2].toFloat();
141  }
142  else
143  qDebug()<<"Sphere data cannot be read correctly : coordinates";
144 
145  //radius
146  QDomNodeList nodelistradius = element.elementsByTagName("radius") ;
147  QDomElement elementRadius = nodelistradius.item(0).toElement() ;
148  QStringList radius = elementRadius.text().simplified().split(QRegExp("\\s+"));
149  if(radius.size() == 1)
150  {
151  currentSphere->setRadius(radius[0].toFloat());
152  }
153  else
154  qDebug()<<"Sphere data cannot be read correctly : radius";
155 
156  //if there are some field, read them thanks to the factory.
157  QDomNodeList nodeListField = element.elementsByTagName("field");
158  if(!nodeListField.isEmpty()){
159  for(int i =0; i < nodeListField.size(); i++){
160  QDomElement fieldElement = nodeListField.at(i).toElement();
161  QString fieldType = fieldElement.attribute("type");
162  if(!fieldType.isEmpty()){
163  axlAbstractDataReader *field_reader = dynamic_cast<axlAbstractDataReader *>(axlFieldReadersFactory::instance()->create(fieldType));
164  axlAbstractField * fieldToAdd = dynamic_cast<axlAbstractField *>(field_reader->read(fieldElement));
165  if(fieldToAdd){
166  QString newName = currentSphere->changeFieldName(fieldToAdd->name());
167  fieldToAdd->setObjectName(newName);
168  currentSphere->addField(fieldToAdd);
169  }
170  }
171  }
172  }
173 
174  return currentSphere;
175 
176 }
177 
178 dtkAbstractDataReader *createaxlSphereReader(void)
179 {
180  return new axlSphereReader;
181 }
QStringList handled(void) const
QString description(void) const
void setShader(const QString &shader)
static bool registered(void)
QString changeFieldName(QString fieldName)
dtkAbstractDataReader * create(const QString &interface_name)
double * coordinates(void) const
Returns coordinates of this sphere.
Definition: axlSphere.cpp:325
virtual axlAbstractData * read(const QDomNode &node)=0
void setRadius(double radius)
Definition: axlSphere.cpp:262
Class axlSphere defines 3D spheres.
Definition: axlSphere.h:33
dtkAbstractDataReader * createaxlSphereReader(void)
bool reject(const QDomNode &node)
Class axlAbstractField defines an API for arrays of numeric data.
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)
axlAbstractData * read(const QDomNode &node)
Class axlAbstractData defines an API for all type of axel data.
QString identifier(void) const
bool accept(const QDomNode &node)