KSeExpr  4.0.4.0
Editable.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2011-2019 Disney Enterprises, Inc.
2 // SPDX-License-Identifier: LicenseRef-Apache-2.0
3 // SPDX-FileCopyrightText: 2020 L. E. Segovia <amy@amyspark.me>
4 // SPDX-License-Identifier: GPL-3.0-or-later
5 /*
6  * @file Editable.h
7  * @author Andrew Selle, L. E. Segovia
8  */
9 
10 #include <algorithm>
11 #include <cmath>
12 #include <cstdio>
13 #include <utility>
14 
15 #include "Debug.h"
16 #include "Editable.h"
17 #include "Utils.h"
18 
19 Editable::Editable(const std::string &name, int startPos, int endPos)
20  : name(name)
21  , startPos(startPos)
22  , endPos(endPos)
23 {
24 }
25 
27 {
28  startPos = other.startPos;
29  endPos = other.endPos;
30 }
31 
32 std::string Editable::str() const
33 {
34  return std::string("<unknown>");
35 }
36 
37 NumberEditable::NumberEditable(const std::string &name, int startPos, int endPos, double val)
38  : Editable(name, startPos, endPos)
39  , v(val)
40  , min(0)
41  , max(1)
42  , isInt(false)
43 {
44 }
45 
46 bool NumberEditable::parseComment(const std::string &comment)
47 {
48  if (comment.find('.') != std::string::npos || comment.find('e') != std::string::npos) {
49  float fmin = NAN;
50  float fmax = NAN;
51  if (KSeExpr::Utils::parseRangeComment(comment, fmin, fmax)) {
52  min = fmin;
53  max = fmax;
54  isInt = false;
55  }
56  } else {
57  int32_t imin = 0;
58  int32_t imax = 0;
59  if (KSeExpr::Utils::parseRangeComment(comment, imin, imax)) {
60  min = imin;
61  max = imax;
62  isInt = true;
63  }
64  }
65  return true;
66 }
67 
68 std::string NumberEditable::str() const
69 {
70  std::stringstream s;
71  s << name << " " << v << " in [" << min << "," << max << "] subset " << (isInt ? "Integers" : "Reals");
72  return s.str();
73 }
74 
75 void NumberEditable::appendString(std::stringstream &stream) const
76 {
77  stream << v;
78 }
79 
80 bool NumberEditable::controlsMatch(const Editable &other) const
81 {
82  if (const auto *o = dynamic_cast<const NumberEditable *>(&other)) {
83  return min == o->min && max == o->max && v == o->v && isInt == o->isInt && name == o->name;
84  } else
85  return false;
86 }
87 
88 VectorEditable::VectorEditable(const std::string &name, int startPos, int endPos, const KSeExpr::Vec3d &val)
89  : Editable(name, startPos, endPos)
90  , v(val)
91  , min(0)
92  , max(1)
93  , isColor(true)
94 {
95 }
96 
97 bool VectorEditable::parseComment(const std::string &comment)
98 {
99  float fmin = NAN;
100  float fmax = NAN;
101  bool parsed = KSeExpr::Utils::parseRangeComment(comment, fmin, fmax);
102  if (parsed) {
103  if (fmin < 0.0 || fmax > 1.0) {
104  isColor = false;
105  }
106  min = fmin;
107  max = fmax;
108  }
109  return true;
110 }
111 std::string VectorEditable::str() const
112 {
113  std::stringstream s;
114  s << name << " " << v << " in [" << min << "," << max << "]";
115  return s.str();
116 }
117 
118 void VectorEditable::appendString(std::stringstream &stream) const
119 {
120  printVal(stream, v);
121 }
122 
123 bool VectorEditable::controlsMatch(const Editable &other) const
124 {
125  if (const auto *o = dynamic_cast<const VectorEditable *>(&other)) {
126  return min == o->min && max == o->max && v == o->v && name == o->name;
127  } else
128  return false;
129 }
130 
131 StringEditable::StringEditable(int startPos, int endPos, const std::string &val)
132  : Editable("unknown", startPos, endPos)
133  , v(val)
134 {
135 }
136 
137 bool StringEditable::parseComment(const std::string &comment)
138 {
139  std::string namebuf {};
140  std::string typebuf {};
141  bool parsed = KSeExpr::Utils::parseTypeNameComment(comment, namebuf, typebuf);
142  if (parsed) {
143  name = namebuf;
144  type = typebuf;
145  return true;
146  } else {
147  return false;
148  }
149 }
150 
151 void StringEditable::appendString(std::stringstream &stream) const
152 {
153  // TODO: escape strs
154  stream << "\"" << v << "\"";
155 }
156 
157 std::string StringEditable::str() const
158 {
159  std::stringstream s;
160  s << name << " " << type << " = " << v;
161  return s.str();
162 }
163 
164 bool StringEditable::controlsMatch(const Editable &other) const
165 {
166  if (const auto *o = dynamic_cast<const StringEditable *>(&other)) {
167  return v == o->v && type == o->type && name == o->name;
168  } else
169  return false;
170 }
171 
172 ColorSwatchEditable::ColorSwatchEditable(const std::string &name, int startPos, int endPos)
173  : Editable(name, startPos, endPos)
174 {
175 }
176 
177 bool ColorSwatchEditable::parseComment(const std::string &comment)
178 {
179  std::string labelbuf {};
180  bool parsed = KSeExpr::Utils::parseLabelComment(comment, labelbuf);
181  if (parsed) {
182  labelType = labelbuf;
183  }
184  return true;
185 }
186 
187 std::string ColorSwatchEditable::str() const
188 {
189  std::stringstream s;
190  s << name << " swatch";
191  return s.str();
192 }
193 
194 void ColorSwatchEditable::appendString(std::stringstream &stream) const
195 {
196  for (const auto &color : colors) {
197  stream << ",";
198  printVal(stream, color);
199  }
200 }
201 
203 {
204  if (const auto *o = dynamic_cast<const ColorSwatchEditable *>(&other)) {
205  return this->labelType == o->labelType
206  && o->colors.size() == this->colors.size()
207  && std::equal(o->colors.begin(), o->colors.end(), this->colors.begin());
208  } else
209  return false;
210 }
211 
213 {
214  colors.push_back(value);
215 }
216 
217 void ColorSwatchEditable::change(int index, const KSeExpr::Vec3d &value)
218 {
219  colors[index] = value;
220 }
221 
223 {
224  colors.erase(colors.begin() + index);
225 }
226 
228 {
229  dbgSeExpr << "\nColorSwatchEditable:\n";
230  for (const auto &color : colors) {
231  dbgSeExpr << color[0] << ", " << color[1] << ", " << color[2];
232  }
233 }
#define dbgSeExpr
Definition: Debug.h:17
void printVal(std::stringstream &stream, double v)
Definition: Editable.h:17
void remove(int index)
Definition: Editable.cpp:222
bool controlsMatch(const Editable &other) const override
Definition: Editable.cpp:202
void change(int index, const KSeExpr::Vec3d &value)
Definition: Editable.cpp:217
void add(const KSeExpr::Vec3d &value)
Definition: Editable.cpp:212
std::string str() const override
Definition: Editable.cpp:187
std::string labelType
Definition: Editable.h:149
ColorSwatchEditable(const std::string &name, int startPos, int endPos)
Definition: Editable.cpp:172
void appendString(std::stringstream &stream) const override
Definition: Editable.cpp:194
bool parseComment(const std::string &comment) override
parses a comment. if false is returned then delete the control from the editable
Definition: Editable.cpp:177
std::vector< KSeExpr::Vec3d > colors
Definition: Editable.h:148
virtual std::string str() const
Definition: Editable.cpp:32
void updatePositions(const Editable &other)
Definition: Editable.cpp:26
int endPos
Definition: Editable.h:34
Editable(const std::string &name, int startPos, int endPos)
Definition: Editable.cpp:19
int startPos
Definition: Editable.h:34
std::string name
Definition: Editable.h:33
double min
Definition: Editable.h:57
bool parseComment(const std::string &comment) override
parses a comment. if false is returned then delete the control from the editable
Definition: Editable.cpp:46
bool controlsMatch(const Editable &other) const override
Definition: Editable.cpp:80
double max
Definition: Editable.h:57
double v
Definition: Editable.h:56
NumberEditable(const std::string &name, int startPos, int endPos, double val)
Definition: Editable.cpp:37
void appendString(std::stringstream &stream) const override
Definition: Editable.cpp:75
std::string str() const override
Definition: Editable.cpp:68
bool parseComment(const std::string &comment) override
parses a comment. if false is returned then delete the control from the editable
Definition: Editable.cpp:137
std::string v
Definition: Editable.h:84
std::string str() const override
Definition: Editable.cpp:157
bool controlsMatch(const Editable &other) const override
Definition: Editable.cpp:164
std::string type
Definition: Editable.h:85
StringEditable(int startPos, int endPos, const std::string &val)
Definition: Editable.cpp:131
void appendString(std::stringstream &stream) const override
Definition: Editable.cpp:151
double min
Definition: Editable.h:71
void appendString(std::stringstream &stream) const override
Definition: Editable.cpp:118
KSeExpr::Vec3d v
Definition: Editable.h:70
std::string str() const override
Definition: Editable.cpp:111
double max
Definition: Editable.h:71
bool isColor
Definition: Editable.h:72
bool controlsMatch(const Editable &other) const override
Definition: Editable.cpp:123
bool parseComment(const std::string &comment) override
parses a comment. if false is returned then delete the control from the editable
Definition: Editable.cpp:97
VectorEditable(const std::string &name, int startPos, int endPos, const KSeExpr::Vec3d &val)
Definition: Editable.cpp:88
bool parseRangeComment(const std::string &comment, double &from, double &to)
Definition: Utils.cpp:7
bool parseLabelComment(const std::string &comment, std::string &label)
Definition: Utils.cpp:124
bool parseTypeNameComment(const std::string &comment, std::string &type, std::string &name)
Definition: Utils.cpp:99
double max(double x, double y)
Definition: ExprBuiltins.h:74
double min(double x, double y)
Definition: ExprBuiltins.h:78