Remoto - VFS: VFS_form.cpp Source File
Remoto - VFS
VFS_form.cpp
Go to the documentation of this file.
1 
2 #include <stdio.h>
3 
5 
6 #include "VFS_form.h"
7 //#include "VFS.h"
8 
9 #include "fields/breakField.h"
10 
27 //QJsonObject VFS_form::_noDiffTypes = { { "metadata", QJsonValue { false } } };
28 
33 VFS_form::VFS_form(QString baseForm)
34 : _index(1)
35 , _breakCount(1)
36 , _baseForm(baseForm)
37 {
38 
39 }
40 
42 {
43 
44 }
45 
51 void VFS_form::setBase(QString baseForm)
52 {
53  _baseForm = baseForm;
54 }
55 
61 void VFS_form::setHelp(QString helpText)
62 {
63  _helpText = helpText;
64 }
65 
78 QJsonValue VFS_form::cleanValue(QJsonValue v, bool isValue)
79 {
80  QJsonValue r = v;
81 
82  if (v.isObject())
83  {
84  QJsonObject o = v.toObject();
85  QString n;
86  QJsonValue c;
87 
88  for (QJsonObject::iterator i=o.begin();i!=o.end();++i)
89  {
90  n = i.key();
91  c = i.value();
92 
93  if (!isValue)
94  { if (n != "value")
95  {
96  o.remove(n);
97  --i; //arg!
98  }
99  else
100  o[n] = cleanValue(c,true);
101  }
102  else
103  {
104  o[n] = cleanValue(c);
105  }
106  }
107 
108  r = o;
109  }
110 
111  return r;
112 }
113 
147 QJsonDocument VFS_form::toJsonDocument(bool useAttributes)
148 {
149  if (useAttributes)
150  {
151  QJsonObject o;
152  o["attributes"] = _form;
153 
154  if (_helpText!="")
155  o["help"] = _helpText;
156 
157  return QJsonDocument(o);
158  }
159 
160  return QJsonDocument(_form);
161 }
162 
179 {
180  QJsonObject o;
181 
182  if (_baseForm=="") o["base"] = QJsonValue::Null;
183  else o["base"] = _baseForm;
184 
185  //should come from base...
186  //if (_helpText!="") o["help"] = _helpText;
187 
188  QJsonObject v;
189  QJsonObject::iterator i = _form.begin();
190 
191  while (i != _form.end())
192  {
193  QString var = i.key();
194  QJsonObject val = i.value().toObject();
195 
196  if (val.contains("value"))
197  { QJsonObject vv;
198  vv["value"] = cleanValue(val["value"],true);
199  v[var] = vv;
200  }
201  //else
202  // printf("Form field %s has no value!\n",qUtf8Printable(var));
203 
204  ++i;
205  }
206 
207  //o["attributes"] = _form;
208  o["attributes"] = v;
209 
210  //printf("SUBCLASS:\n%s\n",qUtf8Printable(QJsonDocument(o).toJson()));
211 
212  return QJsonDocument(o);
213 }
214 
222 void VFS_form::append(QString name, VFS_form_field field, int index)
223 {
224  if (_form.contains(name))
225  printf("Warning: form already contains field '%s'... it will be replaced.\n",qUtf8Printable(name));
226 
227  field.setLabel(name.left(1).toUpper()+name.mid(1),false);
228 
229  if (index == -1)
230  field.setIndex(_index++);
231  else
232  field.setIndex(index);
233 
234  _form[name] = field;
235 }
236 
244 void VFS_form::remove(QString name)
245 {
246  if (_form.contains(name))
247  _form.remove(name);
248 }
249 
261 {
262  if (_form.contains(name))
263  {
264  QJsonObject f = _form[name].toObject();
265  field.setIndex( f.value("index").toInt() );
266  field.setLabel( f.value("label").toString() );
267  _form[name] = field;
268  }
269  else
270  append(name,field);
271 }
272 
281 void VFS_form::removeIndexRange(int s, int e)
282 {
283  QStringList keys = _form.keys();
284 
285  int j;
286  for (int i=0;i<keys.length();i++)
287  { j = _form[keys[i]].toObject()["index"].toInt();
288  if (j>=s && j<=e)
289  _form.remove( keys[i] );
290  }
291 }
292 
299 bool VFS_form::contains(QString field)
300 {
301  return _form.contains(field);
302 }
303 
313 bool VFS_form::setValue(QString var, QJsonValue val)
314 {
315  //printf("DIFF: %s\n",qUtf8Printable(QJsonDocument(val.toObject()).toJson()));
316 
317  if (contains(var))
318  {
319  QJsonObject v = _form[var].toObject();
320 
321  if (val.isObject() && v.contains("value") && v["value"].isObject())
322  {
323  //printf("KEY: %s, case 1\n",qUtf8Printable(var));
324  QJsonObject vo = val.toObject();
325 
326  if (vo.contains("value") && vo["value"].isObject())
327  {
328  //printf("KEY: %s, case 1a\n",qUtf8Printable(var));
329  v["value"] = VFS_datastore::applyJsonDiff(v["value"].toObject(),vo["value"].toObject());
330  }
331  else
332  {
333  //printf("KEY: %s, case 1b\n",qUtf8Printable(var));
334  v["value"] = VFS_datastore::applyJsonDiff(v["value"].toObject(),vo);
335  }
336  }
337  else if (val.isObject())
338  {
339  //printf("KEY: %s, case 2\n",qUtf8Printable(var));
340  v = VFS_datastore::applyJsonDiff(v,val.toObject());
341  }
342  else
343  {
344  //printf("KEY: %s, case 3\n",qUtf8Printable(var));
345  v["value"] = val;
346  }
347 
348  _form[var] = v;
349  return true;
350  }
351 
352  return false;
353 }
354 
361 QJsonValue VFS_form::getValue(QString var)
362 {
363  if (contains(var))
364  {
365  QJsonObject o = _form[var].toObject();
366 
367  if (o.contains("value"))
368  return o["value"];
369 
370  if (o.contains("default"))
371  return o["default"];
372  }
373 
374  return QJsonValue::Null;
375 }
376 
385 bool VFS_form::setOption(QString var, QString option, QJsonValue o)
386 {
387  if (contains(var))
388  {
389  QJsonObject v = _form[var].toObject();
390  QJsonObject op = v["options"].toObject();
391  op[option] = o;
392  v["options"] = op;
393 
394  _form[var] = v;
395  return true;
396  }
397 
398  return false;
399 }
400 
408 QJsonValue VFS_form::getOption(QString var, QString option)
409 {
410  if (contains(var))
411  {
412  QJsonObject o = _form[var].toObject()["options"].toObject();
413 
414  if (o.contains(option))
415  return o[option];
416  }
417 
418  return QJsonValue::Null;
419 }
420 
426 void VFS_form::addBreak(int index)
427 {
428  append(QString("break%1").arg(_breakCount++),breakField(),index);
429 }
430 
431 /*
432 void VFS_form::submit(QJsonObject values)
433 {
434  Q_UNUSED(values);
435 
436  printf("WOULD SUBMIT HERE!\n");
437 }
438 */
439 
448 void VFS_form::applyDiff(QJsonObject diff, bool useAttributes)
449 {
450  if (useAttributes && diff.contains("attributes"))
451  diff = diff["attributes"].toObject();
452 
453  QJsonObject::iterator i = diff.begin();
454  while (i != diff.end())
455  {
456  if (_form.contains(i.key()))
457  {
458  if (i.value().isNull())
459  remove(i.key());
460  else
461  setValue(i.key(),i.value());
462  }
463  //else
464  // create new field
465 
466  i++;
467  }
468 }
static QJsonObject applyJsonDiff(QJsonObject obj, QJsonObject diff, QString trace="", QString user="server")
Apply a json diff to a json object.
Base class for all form field types.
Definition: VFS_form_field.h:7
void setLabel(QString l, bool replace=true)
Set the label.
void setIndex(int i)
Set the field index for this widget.
QJsonDocument toJsonDocument(bool useAttributes=false)
Return a form document as a QJsonDocument.
Definition: VFS_form.cpp:147
QString _baseForm
The base json 'class' that this form will augment. Leave blank for no base.
Definition: VFS_form.h:44
QString _helpText
The help text that will appear in a menu or modal for this form.
Definition: VFS_form.h:45
void setHelp(QString helpText)
Set the helptext for this form.
Definition: VFS_form.cpp:61
void removeIndexRange(int s, int e)
Remove fields whose index is within a range.
Definition: VFS_form.cpp:281
QJsonDocument toSubclassDocument()
Return the form as a QJsonDocument, and include reference to its base.
Definition: VFS_form.cpp:178
QJsonValue cleanValue(QJsonValue o, bool isValue=false)
Normalize a form value recursively.
Definition: VFS_form.cpp:78
int _breakCount
The break count, because all fields must have unique names, and addBreak will use this.
Definition: VFS_form.h:42
bool contains(QString field)
Check if a field is part of a form.
Definition: VFS_form.cpp:299
void remove(QString name)
Remove a field from the form.
Definition: VFS_form.cpp:244
void append(QString name, VFS_form_field field, int index=-1)
Append a field to the form.
Definition: VFS_form.cpp:222
QJsonObject _form
The form itself, which is just a json object.
Definition: VFS_form.h:46
bool setOption(QString var, QString option, QJsonValue o)
Set an option on a form field if it exists.
Definition: VFS_form.cpp:385
QJsonValue getOption(QString var, QString option)
Fetch an option value from a form field if it exists.
Definition: VFS_form.cpp:408
virtual ~VFS_form()
Definition: VFS_form.cpp:41
int _index
The index count used for autoindexing.
Definition: VFS_form.h:41
void addBreak(int index=-1)
Add a break field to a form.
Definition: VFS_form.cpp:426
QJsonValue getValue(QString var)
Fetch the value of a form field.
Definition: VFS_form.cpp:361
VFS_form(QString base="")
Definition: VFS_form.cpp:33
bool setValue(QString var, QJsonValue val)
Set the value of a form field if it exists.
Definition: VFS_form.cpp:313
void replace(QString name, VFS_form_field field)
Replace or append a field to the form.
Definition: VFS_form.cpp:260
void applyDiff(QJsonObject diff, bool useAttributes=false)
Apply a diff to this form.
Definition: VFS_form.cpp:448
void setBase(QString baseForm)
Set the 'class' that this form inherits from.
Definition: VFS_form.cpp:51
The breakField class adds a space to a form field. Useful for visually separating groups of fields.
Definition: breakField.h:7
setter name
a setter DOCME