dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
fieldtreeitem.h
Go to the documentation of this file.
1 
12 /*
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  * for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with this program; if not, see <http://www.gnu.org/licenses/>
25  */
26 
27 #ifndef FIELDTREEITEM_H
28 #define FIELDTREEITEM_H
29 
30 #include "treeitem.h"
31 #include <QtCore/QStringList>
32 #include <QWidget>
33 #include <QSpinBox>
34 #include <QDoubleSpinBox>
35 #include <qscispinbox/QScienceSpinBox.h>
36 #include <QComboBox>
37 #include <limits>
38 #include <utils/longlongspinbox.h>
39 
40 #define QINT8MIN std::numeric_limits<qint8>::min()
41 #define QINT8MAX std::numeric_limits<qint8>::max()
42 #define QUINTMIN std::numeric_limits<quint8>::min()
43 #define QUINT8MAX std::numeric_limits<quint8>::max()
44 #define QINT16MIN std::numeric_limits<qint16>::min()
45 #define QINT16MAX std::numeric_limits<qint16>::max()
46 #define QUINT16MAX std::numeric_limits<quint16>::max()
47 #define QINT32MIN std::numeric_limits<qint32>::min()
48 #define QINT32MAX std::numeric_limits<qint32>::max()
49 #define QUINT32MAX std::numeric_limits<quint32>::max()
50 
51 class FieldTreeItem : public TreeItem
52 {
53  Q_OBJECT
54 public:
55  FieldTreeItem(int index, const QList<QVariant> &data, TreeItem *parent = nullptr)
56  : TreeItem(data, parent)
57  , m_index(index)
58  , m_defaultValue(true)
59  {
60  }
61  FieldTreeItem(int index, const QVariant &data, TreeItem *parent = nullptr)
62  : TreeItem(data, parent)
63  , m_index(index)
64  , m_defaultValue(true)
65  {
66  }
67  inline virtual bool isEditable() override { return true; }
68  void setIsDefaultValue(bool isDefault) { m_defaultValue = isDefault; }
69  virtual bool isDefaultValue() const override { return m_defaultValue; }
70  virtual QWidget *createEditor(QWidget *parent) = 0;
71  virtual QVariant getEditorValue(QWidget *editor) = 0;
72  virtual void setEditorValue(QWidget *editor, QVariant value) = 0;
73  virtual void apply() override {}
74 protected:
75  int m_index;
77 };
78 
80 {
81  Q_OBJECT
82 public:
84  TreeItem *parent = nullptr)
85  : FieldTreeItem(index, data, parent)
86  , m_enumOptions(field->getOptions())
87  , m_field(field)
88  {
89  }
90  EnumFieldTreeItem(UAVObjectField *field, int index, const QVariant &data, TreeItem *parent = nullptr)
91  : FieldTreeItem(index, data, parent)
92  , m_enumOptions(field->getOptions())
93  , m_field(field)
94  {
95  }
96  void setData(QVariant value, int column)
97  {
98  QStringList options = m_field->getOptions();
99  QVariant tmpValue = m_field->getValue(m_index);
100  int tmpValIndex = options.indexOf(tmpValue.toString());
101  setChanged(tmpValIndex != value);
102  TreeItem::setData(value, column);
103  }
104  QString enumOptions(int index)
105  {
106  if ((index < 0) || (index >= m_enumOptions.length())) {
107  return QString("Invalid Value (") + QString().setNum(index) + QString(")");
108  }
109  return m_enumOptions.at(index);
110  }
111  void apply()
112  {
113  int value = data(dataColumn).toInt();
114  if (value == -1) {
115  qDebug() << "Warning, UAVO browser field is outside range. This should never happen!";
116  Q_ASSERT(0);
117  return;
118  }
119  QStringList options = m_field->getOptions();
120  m_field->setValue(options[value], m_index);
121  setChanged(false);
122  }
123  void update()
124  {
125  QStringList options = m_field->getOptions();
126  QVariant value = m_field->getValue(m_index);
127  int valIndex = options.indexOf(value.toString());
128  if (data() != valIndex || changed()) {
129  TreeItem::setData(valIndex);
130  setHighlight();
131  }
132  UAVDataObject *obj = qobject_cast<UAVDataObject *>(m_field->getObject());
133  if (obj && obj->isSettings())
135  }
136  QWidget *createEditor(QWidget *parent)
137  {
138  QComboBox *editor = new QComboBox(parent);
139  editor->setFocusPolicy(Qt::ClickFocus);
140  foreach (QString option, m_enumOptions)
141  editor->addItem(option);
142  return editor;
143  }
144 
145  QVariant getEditorValue(QWidget *editor)
146  {
147  QComboBox *comboBox = static_cast<QComboBox *>(editor);
148  return comboBox->currentIndex();
149  }
150 
151  void setEditorValue(QWidget *editor, QVariant value)
152  {
153  QComboBox *comboBox = static_cast<QComboBox *>(editor);
154  comboBox->setCurrentIndex(value.toInt());
155  }
156 
157 private:
158  QStringList m_enumOptions;
159  UAVObjectField *m_field;
160 };
161 
163 {
164  Q_OBJECT
165 public:
167  TreeItem *parent = nullptr)
168  : FieldTreeItem(index, data, parent)
169  , m_field(field)
170  {
171  setMinMaxValues();
172  }
173  IntFieldTreeItem(UAVObjectField *field, int index, const QVariant &data, TreeItem *parent = nullptr)
174  : FieldTreeItem(index, data, parent)
175  , m_field(field)
176  {
177  setMinMaxValues();
178  }
179 
181  {
182  switch (m_field->getType()) {
184  m_minValue = QINT8MIN;
185  m_maxValue = QINT8MAX;
186  break;
188  m_minValue = QINT16MIN;
189  m_maxValue = QINT16MAX;
190  break;
192  m_minValue = QINT32MIN;
193  m_maxValue = QINT32MAX;
194  break;
196  m_minValue = QUINTMIN;
197  m_maxValue = QUINT8MAX;
198  break;
200  m_minValue = QUINTMIN;
201  m_maxValue = QUINT16MAX;
202  break;
204  m_minValue = QUINTMIN;
205  m_maxValue = QUINT32MAX;
206  break;
207  default:
208  Q_ASSERT(false);
209  break;
210  }
211  }
212 
213  QWidget *createEditor(QWidget *parent)
214  {
215  switch (m_field->getType()) {
217  {
218  LongLongSpinBox *editor = new LongLongSpinBox(parent);
219  editor->setRange(m_minValue, m_maxValue);
220  editor->setDisplayIntegerBase(m_field->getDisplayIntegerBase());
221  editor->setPrefix(m_field->getDisplayPrefix());
222  return editor;
223  }
224  default:
225  {
226  QSpinBox *editor = new QSpinBox(parent);
227  editor->setRange(m_minValue, m_maxValue);
228  editor->setDisplayIntegerBase(m_field->getDisplayIntegerBase());
229  editor->setPrefix(m_field->getDisplayPrefix());
230  return editor;
231  }
232  }
233  }
234 
235  QVariant getEditorValue(QWidget *editor)
236  {
237  if (QSpinBox *spinBox = qobject_cast<QSpinBox *>(editor)) {
238  spinBox->interpretText();
239  return QVariant(spinBox->value());
240  } else if (LongLongSpinBox *spinBox = qobject_cast<LongLongSpinBox *>(editor)) {
241  spinBox->interpretText();
242  return QVariant(spinBox->value());
243  } else {
244  Q_ASSERT(false);
245  return QVariant();
246  }
247  }
248 
249  void setEditorValue(QWidget *editor, QVariant value)
250  {
251  switch (m_field->getType()) {
255  {
256  QSpinBox *spinBox = static_cast<QSpinBox *>(editor);
257  spinBox->setValue(value.toInt());
258  break;
259  }
262  {
263  QSpinBox *spinBox = static_cast<QSpinBox *>(editor);
264  spinBox->setValue(value.toUInt());
265  break;
266  }
268  {
269  LongLongSpinBox *spinBox = static_cast<LongLongSpinBox *>(editor);
270  spinBox->setValue(value.toUInt());
271  break;
272  }
273  default:
274  Q_ASSERT(false);
275  break;
276  }
277  }
278  void setData(QVariant value, int column)
279  {
280  setChanged(m_field->getValue(m_index) != value);
281  TreeItem::setData(value, column);
282  }
283  void apply()
284  {
285  switch (m_field->getType()) {
289  m_field->setValue(data(dataColumn).toInt(), m_index);
290  break;
294  m_field->setValue(data(dataColumn).toUInt(), m_index);
295  break;
296  default:
297  Q_ASSERT(false);
298  break;
299  }
300  setChanged(false);
301  }
302  void update()
303  {
304 
305  QVariant value = m_field->getValue(m_index);
306  if (data() != value || changed()) {
307  switch (m_field->getType()) {
311  TreeItem::setData(value.toInt());
312  break;
316  TreeItem::setData(value.toUInt());
317  break;
318  default:
319  Q_ASSERT(false);
320  break;
321  }
322  setHighlight();
323  }
324 
325  UAVDataObject *obj = qobject_cast<UAVDataObject *>(m_field->getObject());
326  if (obj && obj->isSettings())
328  }
329  QString formattedData()
330  {
331  QString formatted = m_field->getDisplayPrefix();
332  switch (m_field->getType()) {
336  formatted += QString::number(TreeItem::data().toInt(), m_field->getDisplayIntegerBase());
337  break;
341  formatted += QString::number(TreeItem::data().toUInt(), m_field->getDisplayIntegerBase());
342  break;
343  default:
344  Q_ASSERT(false);
345  break;
346  }
347  return formatted;
348  }
349 
350 private:
351  UAVObjectField *m_field;
352  qint64 m_minValue;
353  qint64 m_maxValue;
354 };
355 
357 {
358  Q_OBJECT
359 public:
361  bool scientific = false, TreeItem *parent = nullptr)
362  : FieldTreeItem(index, data, parent)
363  , m_field(field)
364  , m_useScientificNotation(scientific)
365  {
366  }
367  FloatFieldTreeItem(UAVObjectField *field, int index, const QVariant &data,
368  bool scientific = false, TreeItem *parent = nullptr)
369  : FieldTreeItem(index, data, parent)
370  , m_field(field)
371  , m_useScientificNotation(scientific)
372  {
373  }
374  void setData(QVariant value, int column)
375  {
376  setChanged(m_field->getValue(m_index) != value);
377  TreeItem::setData(value, column);
378  }
379  void apply()
380  {
381  m_field->setValue(data(dataColumn).toDouble(), m_index);
382  setChanged(false);
383  UAVDataObject *obj = qobject_cast<UAVDataObject *>(m_field->getObject());
384  if (obj && obj->isSettings())
386  }
387  void update()
388  {
389  double value = m_field->getValue(m_index).toDouble();
390  if (data() != value || changed()) {
391  TreeItem::setData(value);
392  setHighlight();
393  }
394  }
395 
396  QWidget *createEditor(QWidget *parent)
397  {
398  if (m_useScientificNotation) {
399  QScienceSpinBox *editor = new QScienceSpinBox(parent);
400  editor->setDecimals(6);
401  editor->setMinimum(-std::numeric_limits<float>::max());
402  editor->setMaximum(std::numeric_limits<float>::max());
403  return editor;
404  } else {
405  QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
406  editor->setDecimals(8);
407  editor->setMinimum(-std::numeric_limits<float>::max());
408  editor->setMaximum(std::numeric_limits<float>::max());
409  return editor;
410  }
411  }
412 
413  QVariant getEditorValue(QWidget *editor)
414  {
415  if (m_useScientificNotation) {
416  QScienceSpinBox *spinBox = static_cast<QScienceSpinBox *>(editor);
417  spinBox->interpretText();
418  return spinBox->value();
419  } else {
420  QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox *>(editor);
421  spinBox->interpretText();
422  return spinBox->value();
423  }
424  }
425 
426  void setEditorValue(QWidget *editor, QVariant value)
427  {
428 
429  if (m_useScientificNotation) {
430  QScienceSpinBox *spinBox = static_cast<QScienceSpinBox *>(editor);
431  spinBox->setValue(value.toDouble());
432  } else {
433  QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox *>(editor);
434  spinBox->setValue(value.toDouble());
435  }
436  }
437 
438 private:
439  UAVObjectField *m_field;
440  bool m_useScientificNotation;
441 };
442 
443 #endif // FIELDTREEITEM_H
void setPrefix(const QString &prefix)
bool isDefaultValue(int index=0)
Check if the element is set to default value.
void setDisplayIntegerBase(int base)
void setData(QVariant value, int column)
virtual QVariant getEditorValue(QWidget *editor)=0
UAVObject * getObject() const
void setRange(qint64 min, qint64 max)
void setData(QVariant value, int column)
Definition: fieldtreeitem.h:96
virtual void setEditorValue(QWidget *editor, QVariant value)=0
QString getDisplayPrefix() const
Get the prefix for the preferred display format.
virtual QWidget * createEditor(QWidget *parent)=0
QVariant getValue(int index=0) const
void setData(QVariant value, int column)
void setEditorValue(QWidget *editor, QVariant value)
TreeItem * parent()
Definition: treeitem.h:132
FloatFieldTreeItem(UAVObjectField *field, int index, const QList< QVariant > &data, bool scientific=false, TreeItem *parent=nullptr)
virtual bool isDefaultValue() const override
Definition: fieldtreeitem.h:69
QStringList getOptions() const
FloatFieldTreeItem(UAVObjectField *field, int index, const QVariant &data, bool scientific=false, TreeItem *parent=nullptr)
void setValue(qint64 val)
void setEditorValue(QWidget *editor, QVariant value)
QString formattedData()
void setValue(const QVariant &data, int index=0)
virtual bool isEditable() override
Definition: fieldtreeitem.h:67
EnumFieldTreeItem(UAVObjectField *field, int index, const QList< QVariant > &data, TreeItem *parent=nullptr)
Definition: fieldtreeitem.h:83
FieldTreeItem(int index, const QList< QVariant > &data, TreeItem *parent=nullptr)
Definition: fieldtreeitem.h:55
QWidget * createEditor(QWidget *parent)
FieldTreeItem(int index, const QVariant &data, TreeItem *parent=nullptr)
Definition: fieldtreeitem.h:61
virtual void apply() override
Definition: fieldtreeitem.h:73
IntFieldTreeItem(UAVObjectField *field, int index, const QList< QVariant > &data, TreeItem *parent=nullptr)
QWidget * createEditor(QWidget *parent)
virtual void setData(QVariant value, int column=1)
Definition: treeitem.cpp:173
QVariant getEditorValue(QWidget *editor)
IntFieldTreeItem(UAVObjectField *field, int index, const QVariant &data, TreeItem *parent=nullptr)
void setEditorValue(QWidget *editor, QVariant value)
QVariant data(int column=1) const
Definition: treeitem.cpp:168
void setIsDefaultValue(bool isDefault)
Definition: fieldtreeitem.h:68
int getDisplayIntegerBase() const
Get the preferred integer base for this field.
void setChanged(bool changed)
Definition: treeitem.h:151
void setHighlight()
Definition: treeitem.cpp:193
EnumFieldTreeItem(UAVObjectField *field, int index, const QVariant &data, TreeItem *parent=nullptr)
Definition: fieldtreeitem.h:90
QWidget * createEditor(QWidget *parent)
QString enumOptions(int index)
FieldType getType() const
QVariant getEditorValue(QWidget *editor)
static const int dataColumn
Definition: treeitem.h:202
bool changed()
Definition: treeitem.h:141
QVariant getEditorValue(QWidget *editor)