Developer documentation | Axl-2.5.1

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