Developer documentation | Axl-2.5.1

axlLineReader.cpp
Go to the documentation of this file.
1 /* axlLineReader.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 "axlLineReader.h"
21 
22 #include <axlCore/axlLine.h>
23 #include <axlCore/axlPoint.h>
25 
26 #include <dtkCoreSupport/dtkAbstractData.h>
27 #include <dtkCoreSupport/dtkAbstractDataFactory.h>
30 
31 // /////////////////////////////////////////////////////////////////
32 // axlLineReader
33 // /////////////////////////////////////////////////////////////////
34 
36  this->setObjectName(this->description());
37 }
38 
40 }
41 
42 QString axlLineReader::identifier(void) const {
43  return "axlLineReader";
44 }
45 
46 QString axlLineReader::description(void) const {
47  return "axlLineReader";
48 }
49 
50 QStringList axlLineReader::handled(void) const {
51  return QStringList() << "axlLine";
52 }
53 
55  return dtkAbstractDataFactory::instance()->registerDataReaderType("axlLineReader", QStringList(), createaxlLineReader);
56 }
57 
58 bool axlLineReader::accept(const QDomNode& node) {
59  QDomElement element = node.toElement();
60 
61  if (element.tagName() != "line")
62  return false;
63 
64  if (!hasChildNode(element, "point"))
65  return false;
66 
67  return true;
68 }
69 
70 bool axlLineReader::reject(const QDomNode& node) {
71  return !this->accept(node);
72 }
73 
74 axlAbstractData *axlLineReader::read(const QDomNode& node) {
75  QDomElement element = node.toElement();
76 
77  // Create line to fill and return
78  axlLine *currentLine = new axlLine();
79 
80  // Get name
81  QString name = element.attribute("name");
82  if (!name.isEmpty()) {
83  currentLine->setObjectName(name);
84  }
85 
86  // Get color
87  QString color = element.attribute("color");
88  if (!color.isEmpty()) {
89  QStringList colorList = color.split(" ");
90  if(colorList.size() > 2) // rgb components
91  currentLine->setColor(QColor(colorList.at(0).toInt(), colorList.at(1).toInt(), colorList.at(2).toInt()));
92  if(colorList.size() == 4)
93  currentLine->setOpacity(colorList.at(3).toFloat());
94  }
95 
96  // Get shader
97  QString shader = element.attribute("shader");
98  QString dirShader;
99  if (!shader.isEmpty()) {
100  // try to read from axelShader.qrc
101  dirShader = ":axlShader/shader/"+shader;
102  if(!QFile::exists(dirShader)) {
103  QSettings settings("inria", "dtk");
104  QString defaultPath;
105  settings.beginGroup("shader");
106  dirShader = settings.value("path", defaultPath).toString();
107 // dirShader = this->file().left(this->file().lastIndexOf("axel-data") + 9); // to Remove later
108  dirShader.append("/"+shader);
109  }
110  currentLine->setShader(dirShader);
111  }
112 
113  // Get size
114  QString size = element.attribute("size");
115  if(!size.isEmpty())
116  currentLine->setSize(size.toFloat());
117 
118  // Get first point
119  QDomNodeList nodelistfirstPoint = element.elementsByTagName("point") ;
120  QDomElement elementfirstPoint = nodelistfirstPoint.item(0).toElement() ;
121 
122  QStringList coordinates = elementfirstPoint.text().simplified().split(QRegExp("\\s+"));
123  if(coordinates.size() == 3)
124  {
125  currentLine->setFirstPoint(new axlPoint(coordinates[0].toDouble(), coordinates[1].toDouble(), coordinates[2].toDouble()));
126  } else
127  dtkWarn() << "Error within axlLineReader::read, line first point data cannot be read correctly: coordinates";
128 
129  // Get second point
130  QDomNodeList nodelistsecondPoint = element.elementsByTagName("point") ;
131  QDomElement elementsecondPoint = nodelistsecondPoint.item(1).toElement() ;
132 
133  coordinates = elementsecondPoint.text().simplified().split(QRegExp("\\s+"));
134  if (coordinates.size() == 3) {
135  currentLine->setSecondPoint(new axlPoint(coordinates[0].toDouble(), coordinates[1].toDouble(), coordinates[2].toDouble()));
136 
137  } else
138  dtkWarn() << "Error within axlLineReader::read, line second point data cannot be read correctly: coordinates";
139 
140  // If there are some field, read them thanks to the factory.
141  QDomNodeList nodeListField = element.elementsByTagName("field");
142  if (!nodeListField.isEmpty()) {
143  for (int i = 0; i < nodeListField.size(); i++) {
144  QDomElement fieldElement = nodeListField.at(i).toElement();
145  QString fieldType = fieldElement.attribute("type");
146  if (!fieldType.isEmpty()) {
147  axlAbstractDataReader *field_reader = dynamic_cast<axlAbstractDataReader *>(axlFieldReadersFactory::instance()->create(fieldType));
148  axlAbstractField * fieldToAdd = dynamic_cast<axlAbstractField *>(field_reader->read(fieldElement));
149  if (fieldToAdd) {
150  QString newName = currentLine->changeFieldName(fieldToAdd->name());
151  fieldToAdd->setObjectName(newName);
152  currentLine->addField(fieldToAdd);
153  }
154  }
155  }
156  }
157 
158  return currentLine;
159 }
160 
161 dtkAbstractDataReader *createaxlLineReader(void)
162 {
163  return new axlLineReader;
164 }
QStringList handled(void) const
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
Class axlLine defines 3D lines.
Definition: axlLine.h:35
axlAbstractData * read(const QDomNode &node)
QString identifier(void) const
void setShader(const QString &shader)
QString changeFieldName(QString fieldName)
dtkAbstractDataReader * create(const QString &interface_name)
static bool registered(void)
QString description(void) const
virtual axlAbstractData * read(const QDomNode &node)=0
void setSecondPoint(axlPoint *p2)
Change second point of this line.
Definition: axlLine.cpp:192
bool reject(const QDomNode &node)
void setFirstPoint(axlPoint *p1)
Change first point of this line.
Definition: axlLine.cpp:182
Class axlAbstractField defines an API for arrays of numeric data.
dtkAbstractDataReader * createaxlLineReader(void)
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 accept(const QDomNode &node)
Class axlAbstractData defines an API for all type of axel data.