COPASI API  4.16.103
CLGraphicalPrimitive1D.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/layout/CLGraphicalPrimitive1D.cpp,v $
3 // $Revision: 1.4 $
4 // $Name: $
5 // $Author: shoops $
6 // $Date: 2012/04/23 15:44:51 $
7 // End CVS Header
8 
9 // Copyright (C) 2012 - 2010 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., University of Heidelberg, and The University
11 // of Manchester.
12 // All rights reserved.
13 
14 #include <limits>
15 #include <sstream>
16 
17 #include <sbml/packages/render/sbml/GraphicalPrimitive1D.h>
18 
19 #include "CLGraphicalPrimitive1D.h"
20 
21 /**
22  * Constructor.
23  */
26  mStroke(""),
27  mStrokeWidth(std::numeric_limits<double>::quiet_NaN())
28 {
29 }
30 
31 /**
32  * Constructor to generate object from the corresponding SBML object.
33  */
34 CLGraphicalPrimitive1D::CLGraphicalPrimitive1D(const GraphicalPrimitive1D& source):
35  CLTransformation2D(source),
36  mStroke(source.getStroke()),
37  mStrokeWidth(source.getStrokeWidth()),
38  mStrokeDashArray(source.getDashArray())
39 {
40 }
41 
42 /**
43  * Sets the stroke color.
44  */
45 void CLGraphicalPrimitive1D::setStroke(const std::string& key)
46 {
47  this->mStroke = key;
48 }
49 
50 /**
51  * Sets the stroke width.
52  */
54 {
55  this->mStrokeWidth = (width > 0 || width != width) ? width : 0.0;
56 }
57 
58 /**
59  * Sets the dasharray.
60  */
61 void CLGraphicalPrimitive1D::setDashArray(const std::vector<unsigned int>& array)
62 {
63  this->mStrokeDashArray = array;
64 }
65 
66 /**
67  * Returns the stroke color.
68  */
69 const std::string& CLGraphicalPrimitive1D::getStroke() const
70 {
71  return this->mStroke;
72 }
73 
74 /**
75  * Returns the stroke width.
76  */
78 {
79  return this->mStrokeWidth;
80 }
81 
82 /**
83  * Returns the stroke color.
84  */
85 const std::vector<unsigned int>& CLGraphicalPrimitive1D::getDashArray() const
86 {
87  return this->mStrokeDashArray;
88 }
89 
90 /**
91  * Returns true is the stroke width has been set or false otherwise.
92  */
94 {
95  return (mStrokeWidth == mStrokeWidth);
96 }
97 
98 /**
99  * Returns true is the stroke has been set or false otherwise.
100  */
102 {
103  return (!this->mStroke.empty() && this->mStroke != "none");
104 }
105 
106 /**
107  * Returns true is the dash array has been set or false otherwise.
108  */
110 {
111  bool result = true;
112 
113  if (mStrokeDashArray.size() < 2 || mStrokeDashArray[0] != mStrokeDashArray[0])
114  {
115  result = false;
116  }
117 
118  return result;
119 }
120 
121 void CLGraphicalPrimitive1D::parseDashArray(const std::string& s)
122 {
123  this->mStrokeDashArray.clear();
124 
125  if (!s.empty())
126  {
127  std::istringstream is(s);
128  size_t size = s.size() + 1;
129  char* tmp = new char[size];
130  char* tmp2 = tmp;
131  char* tmp3;
132  char** endptr;
133  long value;
134  is.getline(tmp2, size, ',');
135 
136  // continue until the string is empty or the stream is
137  // no longer usable
138  while (tmp2[0] != 0 && is.good())
139  {
140  endptr = &tmp2;
141  tmp3 = tmp2;
142  value = strtol(tmp2, endptr, 10);
143 
144  if (value < 0 || *endptr == tmp3 || (**endptr != '\0' && **endptr != ' ' && **endptr != '\t'))
145  {
146  this->mStrokeDashArray.clear();
147  }
148  else
149  {
150  this->mStrokeDashArray.push_back((unsigned int)value);
151  }
152 
153  // read next element
154  is.getline(tmp2, size, ',');
155  }
156 
157  // check if the string was parsed to the end
158  if (is.eof())
159  {
160  // we have to parse the current tmp2
161  // and check if that was ok
162  endptr = &tmp2;
163  tmp3 = tmp2;
164  value = strtol(tmp2, endptr, 10);
165 
166  if (value < 0 || *endptr == tmp3 || (**endptr != '\0' && **endptr != ' ' && **endptr != '\t'))
167  {
168  this->mStrokeDashArray.clear();
169  }
170  else
171  {
172  this->mStrokeDashArray.push_back((unsigned int)value);
173  }
174  }
175  else
176  {
177  // since we did not parse to the end,
178  // there most have been an error
179  this->mStrokeDashArray.clear();
180  }
181 
182  delete[] tmp;
183  }
184 }
185 
186 /**
187  * Converts this object to the corresponding SBML object.
188  */
189 void CLGraphicalPrimitive1D::addSBMLAttributes(GraphicalPrimitive1D* pPrimitive) const
190 {
191  this->CLTransformation2D::addSBMLAttributes(pPrimitive);
192  pPrimitive->setStroke(this->mStroke);
193  pPrimitive->setStrokeWidth(this->mStrokeWidth);
194  pPrimitive->setDashArray(this->mStrokeDashArray);
195 }
virtual void addSBMLAttributes(Transformation2D *pTransformation) const
void parseDashArray(const std::string &s)
const std::string & getStroke() const
void setDashArray(const std::vector< unsigned int > &array)
void setStroke(const std::string &key)
std::vector< unsigned int > mStrokeDashArray
void setStrokeWidth(double width)
const std::vector< unsigned int > & getDashArray() const
virtual void addSBMLAttributes(GraphicalPrimitive1D *pPrimitive) const