Developer documentation | Axl-2.5.1

axlCircleArcReader.cpp
Go to the documentation of this file.
1 /* axlCircleArcReader.cpp ---
2  *
3  * Author: Valentin Michelet
4  * Copyright (C) 2008 - Valentin Michelet, Inria.
5  * Created: Tue Nov 9 16:58:59 2010 (+0100)
6  * Version: $Id$
7  * Last-Updated: Tue Nov 9 17:09:38 2010 (+0100)
8  * By: Valentin Michelet
9  * Update #: 19
10  */
11 
12 /* Commentary:
13  *
14  */
15 
16 /* Change log:
17  *
18  */
19 
20 #include "axlCircleArcReader.h"
21 
22 #include <axlCore/axlCircleArc.h>
23 #include <axlCore/axlPoint.h>
27 
28 #include <dtkCoreSupport/dtkAbstractData.h>
29 #include <dtkCoreSupport/dtkAbstractDataFactory.h>
30 
31 // /////////////////////////////////////////////////////////////////
32 // axlCircleArcReader
33 // /////////////////////////////////////////////////////////////////
34 
36  this->setObjectName(this->description());
37 }
38 
40 }
41 
42 QString axlCircleArcReader::identifier(void) const {
43  return "axlCircleArcReader";
44 }
45 
46 QString axlCircleArcReader::description(void) const {
47  return "axlCircleArcReader";
48 }
49 
50 QStringList axlCircleArcReader::handled(void) const {
51  return QStringList() << "axlCircleArc";
52 }
53 
55  return dtkAbstractDataFactory::instance()->registerDataReaderType("axlCircleArcReader", QStringList(), createaxlCircleArcReader);
56 }
57 
58 bool axlCircleArcReader::accept(const QDomNode& node) {
59  QDomElement element = node.toElement();
60 
61  if (element.tagName() != "arc")
62  return false;
63 
64  if (!hasChildNode(element, "point1"))
65  return false;
66 
67  if (!hasChildNode(element, "point2"))
68  return false;
69 
70  if (!hasChildNode(element, "center"))
71  return false;
72 
73  if (!hasChildNode(element, "normal"))
74  return false;
75 
76  if (!hasChildNode(element, "ccw"))
77  return false;
78 
79  return true;
80 }
81 
82 bool axlCircleArcReader::reject(const QDomNode& node) {
83  return !this->accept(node);
84 }
85 
86 axlAbstractData* axlCircleArcReader::read(const QDomNode& node) {
87  QDomElement element = node.toElement();
88 
89  // Create circle arc pointer to fill and return
90  axlCircleArc* currentArc = new axlCircleArc;
91 
92  // Get name
93  QString name = element.attribute("name");
94  if (!name.isEmpty()) {
95  currentArc->setObjectName(name);
96  }
97 
98  // Get color
99  QString color = element.attribute("color");
100  if (!color.isEmpty()) {
101  QStringList colorList = color.split(" ");
102  if (colorList.size() > 2) // rgb components
103  currentArc->setColor(QColor(colorList.at(0).toInt(), colorList.at(1).toInt(), colorList.at(2).toInt()));
104  if (colorList.size() == 4)
105  currentArc->setOpacity(colorList.at(3).toFloat());
106  }
107 
108  // Get shader
109  QString shader = element.attribute("shader");
110  QString dirShader;
111  if (!shader.isEmpty()) {
112  // try to read from axelShader.qrc
113  dirShader = ":axlShader/shader/" + shader;
114  if (!QFile::exists(dirShader)) {
115  QSettings settings("inria", "dtk");
116  QString defaultPath;
117  settings.beginGroup("shader");
118  dirShader = settings.value("path", defaultPath).toString();
119  dirShader.append("/" + shader);
120  }
121  currentArc->setShader(dirShader);
122  }
123 
124  // Get size
125  QString size = element.attribute("size");
126  if (!size.isEmpty())
127  currentArc->setSize(size.toFloat());
128 
129  // Get first point
130  QDomNodeList nodeListPoint1 = element.elementsByTagName("point1");
131  QDomElement elementPoint1 = nodeListPoint1.item(0).toElement();
132  QStringList coordPoint1 = elementPoint1.text().simplified().split(QRegExp("\\s+"));
133  if (coordPoint1.size() == 3)
134  currentArc->setPoint1(new axlPoint(coordPoint1[0].toDouble(), coordPoint1[1].toDouble(), coordPoint1[2].toDouble()));
135  else
136  dtkWarn() << "Error within axlCircleArcReader::read, arc data cannot be read correctly: point1 coordinates";
137 
138  // Get second point
139  QDomNodeList nodeListPoint2 = element.elementsByTagName("point2");
140  QDomElement elementPoint2 = nodeListPoint2.item(0).toElement();
141  QStringList coordPoint2 = elementPoint2.text().simplified().split(QRegExp("\\s+"));
142  if (coordPoint2.size() == 3)
143  currentArc->setPoint2(new axlPoint(coordPoint2[0].toDouble(), coordPoint2[1].toDouble(), coordPoint2[2].toDouble()));
144  else
145  dtkWarn() << "Error within axlCircleArcReader::read, arc data cannot be read correctly: point2 coordinates";
146 
147  // Get center
148  QDomNodeList nodeListCenter = element.elementsByTagName("center");
149  QDomElement elementCenter = nodeListCenter.item(0).toElement();
150  QStringList coordCenter = elementCenter.text().simplified().split(QRegExp("\\s+"));
151  if (coordCenter.size() == 3)
152  currentArc->setCenter(new axlPoint(coordCenter[0].toDouble(), coordCenter[1].toDouble(), coordCenter[2].toDouble()));
153  else
154  dtkWarn() << "Error within axlCircleArcReader::read, arc data cannot be read correctly: radius coordinates";
155 
156  // Get normal
157  QDomNodeList nodeListNormal = element.elementsByTagName("normal");
158  QDomElement elementNormal = nodeListNormal.item(0).toElement();
159  QStringList coordNormal = elementNormal.text().simplified().split(QRegExp("\\s+"));
160  if (coordNormal.size() == 3)
161  currentArc->setNormal(new axlPoint(coordNormal[0].toDouble(), coordNormal[1].toDouble(), coordNormal[2].toDouble()));
162  else
163  dtkWarn() << "Error within axlCircleArcReader::read, arc data cannot be read correctly: normal coordinates";
164 
165  // Get counter clockwise bool
166  QDomNodeList nodeListCCW = element.elementsByTagName("ccw");
167  QDomElement elementCCW = nodeListCCW.item(0).toElement();
168  QString coordCCW = elementCCW.text().simplified();
169  bool ok;
170  currentArc->setDirect(coordCCW.toInt(&ok));
171  if (!ok) {
172  dtkWarn() << "Error within axlCircleArcReader::read, arc data cannot be read correctly: ccw bool value";
173  }
174 
175  // If there are some field, read them thanks to the factory.
176  QDomNodeList nodeListField = element.elementsByTagName("field");
177  if(!nodeListField.isEmpty()){
178  for(int i =0; i < nodeListField.size(); i++){
179  QDomElement fieldElement = nodeListField.at(i).toElement();
180  QString fieldType = fieldElement.attribute("type");
181  if(!fieldType.isEmpty()){
182  axlAbstractDataReader *field_reader = dynamic_cast<axlAbstractDataReader *>(axlFieldReadersFactory::instance()->create(fieldType));
183  axlAbstractField * fieldToAdd = dynamic_cast<axlAbstractField *>(field_reader->read(fieldElement));
184  if(fieldToAdd){
185  QString newName = currentArc->changeFieldName(fieldToAdd->name());
186  fieldToAdd->setObjectName(newName);
187  currentArc->addField(fieldToAdd);
188  }
189  }
190  }
191  }
192 
193  return currentArc;
194 }
195 
196 dtkAbstractDataReader *createaxlCircleArcReader(void) {
197  return new axlCircleArcReader;
198 }
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
bool reject(const QDomNode &node)
void setCenter(const axlPoint &center)
void setDirect(bool direct)
void setShader(const QString &shader)
QString changeFieldName(QString fieldName)
dtkAbstractDataReader * create(const QString &interface_name)
QString identifier(void) const
bool accept(const QDomNode &node)
virtual axlAbstractData * read(const QDomNode &node)=0
QString description(void) const
virtual ~axlCircleArcReader(void)
Class axlAbstractField defines an API for arrays of numeric data.
void setPoint1(const axlPoint &point1)
static bool registered(void)
bool hasChildNode(QDomElement element, const QString &tag)
Definition: axlFormat.h:20
void setOpacity(const double &opacity)
void setPoint2(const axlPoint &point2)
static axlFieldReadersFactory * instance(void)
void addField(axlAbstractField *field)
Add a field to the field list of the object.
dtkAbstractDataReader * createaxlCircleArcReader(void)
void setSize(const double &size)
void setColor(double r, double g, double b)
QStringList handled(void) const
axlAbstractData * read(const QDomNode &node)
Class axlAbstractData defines an API for all type of axel data.
void setNormal(const axlPoint &normal)