forked from jasp-stats/jasp-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabel.cpp
282 lines (224 loc) · 6.24 KB
/
label.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "label.h"
#include "column.h"
#include <cassert>
#include "timers.h"
#include "databaseinterface.h"
const int Label::DOUBLE_LABEL_VALUE = -1;
Label::Label(Column * column)
: DataSetBaseNode(dataSetBaseNodeType::label, column), _column(column)
{
_intsId = EmptyValues::missingValueInteger;
}
Label::Label(Column * column, int value)
: DataSetBaseNode(dataSetBaseNodeType::label, column), _column(column)
{
setIntsId(value);
setOriginalValue(value);
setLabel(originalValueAsString());
}
Label::Label(Column * column, const std::string &label, int value, bool filterAllows, const std::string & description, const Json::Value & originalValue, int order, int id)
: DataSetBaseNode(dataSetBaseNodeType::label, column), _column(column)
{
_label = label;
_intsId = value;
_filterAllows = filterAllows;
_description = description;//description != "" || label.size() < MAX_LABEL_DISPLAY_LENGTH ? description : label; //Use description given if filled otherwise use label if the label won't be displayed entirely
_originalValue = originalValue;
_order = order;
if(id == -1) dbCreate();
else _dbId = id;
}
void Label::dbDelete()
{
if(_column->batchedLabelDepth())
return;
assert(_dbId != -1);
db().labelDelete(_dbId);
_dbId = -1;
}
void Label::dbCreate()
{
JASPTIMER_SCOPE(Label::dbCreate);
if(_column->batchedLabelDepth())
return;
assert(_dbId == -1);
_dbId = db().labelAdd(_column->id(), _intsId, _label, _filterAllows, _description, _originalValue.toStyledString());
}
void Label::dbLoad(int labelId)
{
if(_column->batchedLabelDepth())
return;
assert(_dbId != -1 || labelId != -1);
if(labelId != -1)
_dbId = labelId;
int columnId;
std::string origValJsonStr;
db().labelLoad(labelId, columnId, _intsId, _label, _filterAllows, _description, origValJsonStr, _order);
_originalValue = Json::nullValue;
Json::Reader().parse(origValJsonStr, _originalValue);
}
void Label::dbUpdate()
{
JASPTIMER_SCOPE(Label::dbUpdate);
if(_column->batchedLabelDepth())
return;
if(_dbId == -1)
dbCreate();
else
{
db().labelSet(_dbId, _column->id(), _intsId, _label, _filterAllows, _description, _originalValue.toStyledString());
_column->incRevision();
}
}
void Label::setInformation(Column * column, int id, int order, const std::string &label, int value, bool filterAllows, const std::string & description, const Json::Value & originalValue)
{
assert(_column == column);
_dbId = id;
_order = order;
_label = label;
_intsId = value;
_filterAllows = filterAllows;
_description = description;
_originalValue = originalValue;
}
Json::Value Label::serialize() const
{
Json::Value json(Json::objectValue);
json["id"] = _dbId;
json["order"] = _order;
json["label"] = _label;
json["intsId"] = _intsId;
json["filterAllows"] = _filterAllows;
json["description"] = _description;
json["originalValue"] = _originalValue;
return json;
}
void Label::setIntsId(int value)
{
_intsId = value;
dbUpdate();
}
void Label::setOrder(int order)
{
_order = order;
//We'll let Column handle the order changes
}
bool Label::setLabel(const std::string & label)
{
if(_label != label)
{
std::string oldLabel = _label;
_label = label.empty() ? originalValueAsString() : label;
_column->labelDisplayChanged(this, oldLabel);
dbUpdate();
return true;
}
return false;
}
bool Label::setOriginalValue(const Json::Value & originalValue)
{
if(_originalValue != originalValue)
{
Json::Value previous = _originalValue;
_originalValue = originalValue;
dbUpdate();
_column->labelValueChanged(this, previous);
return true;
}
return false;
}
bool Label::setOrigValLabel(const Json::Value &originalValue)
{
std::string oldLabel = _label,
newLabel = !originalValue.isDouble() ? originalValue.asString() : _column->doubleToDisplayString(originalValue.asDouble(), false, false);
Json::Value previous = _originalValue;
bool labelChange = _label != newLabel,
valChange = _originalValue != originalValue,
aChange = labelChange || valChange;
if(labelChange)
_label = newLabel;
if(valChange)
_originalValue = originalValue;
if(aChange)
{
dbUpdate();
_column->labelValDisplayChanged(this, oldLabel, previous);
return true;
}
return false;
}
bool Label::setDescription(const std::string &description)
{
if(_description != description)
{
_description = description;
dbUpdate();
return true;
}
return false;
}
bool Label::setFilterAllows(bool allowFilter)
{
JASPTIMER_SCOPE(Label::setFilterAllows);
if(_filterAllows != allowFilter)
{
_filterAllows = allowFilter;
dbUpdate();
return true;
}
return false;
}
DatabaseInterface & Label::db()
{
return _column->db();
}
const DatabaseInterface & Label::db() const
{
return _column->db();
}
Label &Label::operator=(const Label &label)
{
this->_originalValue = label._originalValue;
this->_filterAllows = label._filterAllows;
this->_description = label._description;
this->_intsId = label._intsId;
this->_label = label._label;
this->_order = label._order;
this->_dbId = label._dbId;
this->_column = label._column;
return *this;
}
std::string Label::labelDisplay() const
{
return isEmptyValue() ? EmptyValues::displayString() : label();
}
std::string Label::labelIgnoreEmpty() const
{
return label();
}
bool Label::isEmptyValue() const
{
return _column->isEmptyValue(originalValueAsString(false)) || _column->isEmptyValue(label());
}
std::string Label::originalValueAsString(bool fancyEmptyValue) const
{
return originalValueAsString(_column, _originalValue, fancyEmptyValue);
}
std::string Label::originalValueAsString(const Column * column, const Json::Value & originalValue, bool fancyEmptyValue)
{
switch(originalValue.type())
{
default:
return fancyEmptyValue ? EmptyValues::displayString() : "";
case Json::intValue:
return std::to_string(originalValue.asInt());
case Json::realValue:
return column->doubleToDisplayString(originalValue.asDouble(), fancyEmptyValue);
case Json::stringValue:
return originalValue.asString();
}
}
std::string Label::str() const
{
return "Label of column '" + _column->name() + "' has display: '" + label() + "' for value " + std::to_string(intsId()) + ", order " + std::to_string(order()) + " and " + ( isEmptyValue() ? "considers itself to be " : "is not ") + "a missing value!";
}