Bridges-C++  3.4.5-dev1-6-g935685a
Bridges(C++ API)
Text.h
Go to the documentation of this file.
1 
2 #ifndef LABEL_H
3 
4 #define LABEL_H
5 
6 #include "DataStructure.h"
7 #include "Symbol.h"
8 #include <vector>
9 #include <cmath>
10 
11 using namespace std;
12 
16 namespace bridges {
17  namespace datastructure {
26  class Text : public Symbol {
27  private:
28  // label anchor location
29  double origin[2];
30 
31  // label attributes
32  double fontSize = 12;
33  string *anchorType = nullptr,
34  *anchorAlignmentLR = nullptr,
35  *anchorAlignmentTB = nullptr;
36 
37  string label_text = string();
38 
39  public:
40 
44  Text() :
45  anchorType(nullptr),
46  anchorAlignmentLR(nullptr),
47  anchorAlignmentTB(nullptr) {
48 
49  origin[0] = origin[1] = 0.0f;
50  }
51 
52  Text(const Text& t):
53  Symbol(t) {
54  this->label_text = t.label_text;
55  this->origin[0] = t.origin[0];
56  this->origin[1] = t.origin[1];
57  this->fontSize = t.fontSize;
58 
59  if (t.anchorType)
60  this->anchorType = new string(*(t.anchorType));
61  if (t.anchorAlignmentTB)
62  this->anchorAlignmentTB = new string(*(t.anchorAlignmentTB));
63  if (t.anchorAlignmentLR)
64  this->anchorAlignmentLR = new string(*(t.anchorAlignmentLR));
65  }
66 
67  Text& operator= (const Text& t) {
68  Symbol::operator=(t);
69 
70  this->label_text = t.label_text;
71  this->origin[0] = t.origin[0];
72  this->origin[1] = t.origin[1];
73 
74  if (anchorType) {
75  delete anchorType;
76  anchorType = nullptr;
77  }
78  if (anchorAlignmentLR) {
79  delete anchorAlignmentLR;
80  anchorAlignmentLR = nullptr;
81  }
82  if (anchorAlignmentTB) {
83  delete anchorAlignmentTB;
84  anchorAlignmentTB = nullptr;
85  }
86 
87  this->fontSize = t.fontSize;
88  if (t.anchorType)
89  this->anchorType = new string(*(t.anchorType));
90  if (t.anchorAlignmentTB)
91  this->anchorAlignmentTB = new string(*(t.anchorAlignmentTB));
92  if (t.anchorAlignmentLR)
93  this->anchorAlignmentLR = new string(*(t.anchorAlignmentLR));
94 
95  return *this;
96  }
97 
103  Text (string l): Text() {
104  label_text = l;
105  }
106 
107  ~Text() {
108  if (anchorType)
109  delete anchorType;
110  if (anchorAlignmentLR)
111  delete anchorAlignmentLR;
112  if (anchorAlignmentTB)
113  delete anchorAlignmentTB;
114  }
115 
116  /*
117  * @brief Get Data Structure name
118  * @return name of data type
119  */
120  virtual string getShapeType() const override {
121  return "text";
122  }
128  Text& setText(string lbl) {
129  label_text = lbl;
130 
131  return *this;
132  }
133 
139  string getText() const {
140  return label_text;
141  }
142 
149  Text& setAnchorLocation(float *loc) {
150  origin[0] = loc[0];
151  origin[1] = loc[1];
152 
153  return *this;
154  }
155 
163  Text& setAnchorLocation(float locx, float locy) {
164  origin[0] = locx;
165  origin[1] = locy;
166 
167  return *this;
168  }
169 
179  Text& setAnchorAlignment(string typeLR, string typeTB) {
180  if (!anchorAlignmentLR)
181  anchorAlignmentLR = new string;
182  if (!anchorAlignmentTB)
183  anchorAlignmentTB = new string;
184 
185  *anchorAlignmentLR = typeLR;
186  *anchorAlignmentTB = typeTB;
187 
188  return *this;
189  }
190 
198  return origin[0];
199  }
200 
208  return origin[1];
209  }
210 
218  Symbol& setAnchorType(string type) {
219  if (!anchorType)
220  anchorType = new string;
221 
222  *anchorType = type;
223 
224  return *this;
225  }
226 
233  string getAnchorType() {
234  if (!anchorType)
235  throw "Anchor type not set!";
236 
237  return *anchorType;
238  }
239 
247  Symbol& setFontSize(double sz) {
248  fontSize = sz;
249 
250  return *this;
251  }
252 
259  double getFontSize() {
260  return fontSize;
261  }
262 
268  const string getSymbolRepresentation() const override {
269 
270  string shape_json = getSymbolAttributeRepresentation();
271  if (anchorType)
272  shape_json += QUOTE + "anchorType" + QUOTE + COLON +
273  QUOTE + *anchorType + QUOTE + COMMA;
274 
275  if (anchorAlignmentLR)
276  shape_json += QUOTE + "anchor-alignmentLR" + QUOTE + COLON +
277  QUOTE + *anchorAlignmentLR + QUOTE + COMMA;
278 
279  if (anchorAlignmentTB)
280  shape_json += QUOTE + "anchor-alignmentTB" + QUOTE + COLON +
281  QUOTE + *anchorAlignmentTB + QUOTE + COMMA;
282 
283  shape_json += QUOTE + "font-size" + QUOTE + COLON +
284  to_string(fontSize) + COMMA;
285 
286  shape_json +=
287  QUOTE + "anchor-location" + QUOTE + COLON +
288  OPEN_BOX +
289  to_string(origin[0]) + COMMA + to_string(origin[1]) +
290  CLOSE_BOX + COMMA +
291  QUOTE + "text" + QUOTE + COLON + QUOTE + label_text + QUOTE +
292  CLOSE_CURLY;
293 
294  return shape_json;
295  }
296  };
297  }
298 } // namespace bridges
299 
300 #endif
This is an abstract class for deriving a number of Symbol shape objects, for use in a SymbolCollectio...
Definition: Symbol.h:31
This is a label object and used for defining text labels as part of the symbol collection.
Definition: Text.h:26
virtual string getShapeType() const override
Definition: Text.h:120
~Text()
Definition: Text.h:107
string getText() const
Get the symbol label.
Definition: Text.h:139
string getAnchorType()
This method gets the label's anchor type.
Definition: Text.h:233
Text & setText(string lbl)
Set the label text.
Definition: Text.h:128
double getFontSize()
This method gets the font size.
Definition: Text.h:259
double getAnchorLocationX()
This method gets the label anchor location.
Definition: Text.h:197
Text & setAnchorAlignment(string typeLR, string typeTB)
Definition: Text.h:179
Text(string l)
Definition: Text.h:103
double getAnchorLocationY()
This method gets the label anchor location.
Definition: Text.h:207
Text()
Definition: Text.h:44
Symbol & setFontSize(double sz)
This method sets the font size.
Definition: Text.h:247
const string getSymbolRepresentation() const override
This method returns the JSON representation of the shape.
Definition: Text.h:268
Text & setAnchorLocation(float locx, float locy)
This method sets the label origin;.
Definition: Text.h:163
Symbol & setAnchorType(string type)
This method sets the label's anchor type.
Definition: Text.h:218
Text & setAnchorLocation(float *loc)
This method sets the label origin;.
Definition: Text.h:149
Text(const Text &t)
Definition: Text.h:52
Support for drawing Bar charts.
Definition: alltypes.h:4
const string COLON
Definition: DataStructure.h:52
const string OPEN_BOX
Definition: DataStructure.h:55
const string COMMA
Definition: DataStructure.h:51
const string CLOSE_BOX
Definition: DataStructure.h:56
const string CLOSE_CURLY
Definition: DataStructure.h:54
const string QUOTE
Definition: DataStructure.h:50