SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
arginfogui.cpp
Go to the documentation of this file.
1 // Copyright (C) 2018 Edouard Griffiths, F4EXB //
3 // //
4 // This program is free software; you can redistribute it and/or modify //
5 // it under the terms of the GNU General Public License as published by //
6 // the Free Software Foundation as version 3 of the License, or //
7 // (at your option) any later version. //
8 // //
9 // This program is distributed in the hope that it will be useful, //
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
12 // GNU General Public License V3 for more details. //
13 // //
14 // You should have received a copy of the GNU General Public License //
15 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
17 
18 #include <math.h>
19 #include <QHBoxLayout>
20 
21 #include "ui_arginfogui.h"
22 #include "arginfogui.h"
23 
24 ArgInfoGUI::ArgInfoGUI(ArgInfoType type, ArgInfoValueType valueType, QWidget *parent) :
25  QWidget(parent),
26  ui(new Ui::ArgInfoGUI),
27  m_type(type),
28  m_valueType(valueType),
29  m_boolValue(false),
30  m_intValue(0),
31  m_floatValue(0.0),
32  m_hasRange(false),
33  m_minValue(0.0),
34  m_maxValue(0.0)
35 {
36  ui->setupUi(this);
37  QHBoxLayout *layout = ui->argLayout;
38 
40  {
41  if (m_type == ArgInfoContinuous) {
42  ui->argEdit->setAlignment(Qt::AlignRight);
43  } else if (m_type == ArgInfoDiscrete) {
44  ui->argCombo->setLayoutDirection(Qt::RightToLeft);
45  }
46  }
47 
48  if (m_type != ArgInfoBinary)
49  {
50  layout->removeWidget(ui->argCheck);
51  delete ui->argCheck;
52  }
53 
55  {
56  layout->removeWidget(ui->argEdit);
57  delete ui->argEdit;
58  }
59 
60  if (m_type != ArgInfoDiscrete)
61  {
62  layout->removeWidget(ui->argCombo);
63  delete ui->argCombo;
64  }
65 }
66 
68 {
69  delete ui;
70 }
71 
72 void ArgInfoGUI::setRange(double min, double max)
73 {
74  m_hasRange = true;
75  m_minValue = min;
76  m_maxValue = max;
77 }
78 
79 void ArgInfoGUI::setLabel(const QString& text)
80 {
81  ui->argLabel->setText(text);
82 }
83 
84 void ArgInfoGUI::setToolTip(const QString& text)
85 {
86  if (m_type == ArgInfoBinary) {
87  ui->argCheck->setToolTip(text);
88  } else if (m_type == ArgInfoContinuous) {
89  ui->argEdit->setToolTip(text);
90  } else if (m_type == ArgInfoDiscrete) {
91  ui->argCombo->setToolTip(text);
92  }
93 }
94 
95 void ArgInfoGUI::setUnits(const QString& units)
96 {
97  ui->argUnits->setText(units);
98 }
99 
100 void ArgInfoGUI::setBoolValue(bool value)
101 {
103  {
104  m_boolValue = value;
106  }
107  else if (m_valueType == ArgInfoValueInt)
108  {
109  m_intValue = setIntegerValue(value ? 1 : 0);
110  updateUIFromInt();
111  }
112  else if (m_valueType == ArgInfoValueFloat)
113  {
114  m_floatValue = setDoubleValue(value ? 1.0 : 0.0);
116  }
117  else if (m_valueType == ArgInfoValueString)
118  {
119  m_stringValue = QString(value ? "true" : "false");
121  }
122 }
123 
124 void ArgInfoGUI::addBoolValue(const QString& text, bool value)
125 {
126  if (m_type == ArgInfoDiscrete) {
127  ui->argCombo->addItem(text, QVariant(value));
128  }
129 }
130 
131 void ArgInfoGUI::setIntValue(int value)
132 {
134  {
135  m_boolValue = (value != 0);
137  }
138  else if (m_valueType == ArgInfoValueInt)
139  {
140  m_intValue = setIntegerValue(value);
141  updateUIFromInt();
142  }
143  else if (m_valueType == ArgInfoValueFloat)
144  {
145  m_floatValue = setDoubleValue(value);
147  }
148  else if (m_valueType == ArgInfoValueString)
149  {
150  m_stringValue = QString("%1").arg(value);
152  }
153 }
154 
155 void ArgInfoGUI::addIntValue(const QString& text, int value)
156 {
157  if (m_type == ArgInfoDiscrete) {
158  ui->argCombo->addItem(text, QVariant(setIntegerValue(value)));
159  }
160 }
161 
162 void ArgInfoGUI::setFloatValue(double value)
163 {
165  {
166  m_boolValue = (value != 0.0);
168  }
169  else if (m_valueType == ArgInfoValueInt)
170  {
171  m_intValue = setIntegerValue(value);
172  updateUIFromInt();
173  }
174  else if (m_valueType == ArgInfoValueFloat)
175  {
176  m_floatValue = setDoubleValue(value);
178  }
179  else if (m_valueType == ArgInfoValueString)
180  {
181  m_stringValue = QString("%1").arg(value);
183  }
184 }
185 
186 void ArgInfoGUI::addFloatValue(const QString& text, double value)
187 {
188  if (m_type == ArgInfoDiscrete) {
189  ui->argCombo->addItem(text, QVariant(setDoubleValue(value)));
190  }
191 }
192 
193 void ArgInfoGUI::setStringValue(const QString& value)
194 {
196  {
197  m_boolValue = (value == "true");
199  }
200  else if (m_valueType == ArgInfoValueInt)
201  {
202  int intValue = atoi(value.toStdString().c_str());
203  m_intValue = setIntegerValue(intValue);
204  updateUIFromInt();
205  }
206  else if (m_valueType == ArgInfoValueFloat)
207  {
208  double doubleValue = atof(value.toStdString().c_str());
209  m_floatValue = setDoubleValue(doubleValue);
211  }
212  else if (m_valueType == ArgInfoValueString)
213  {
214  m_stringValue = value;
216  }
217 }
218 
219 void ArgInfoGUI::addStringValue(const QString& text, const QString& value)
220 {
221  if (m_type == ArgInfoDiscrete) {
222  ui->argCombo->addItem(text, QVariant(value));
223  }
224 }
225 
227 {
228  if (m_hasRange) {
229  return value < round(m_minValue) ? round(m_minValue) : value > round(m_maxValue) ? round(m_maxValue) : value;
230  } else {
231  return value;
232  }
233 }
234 
235 double ArgInfoGUI::setDoubleValue(double value)
236 {
237  if (m_hasRange) {
238  return value < m_minValue ? m_minValue : value > m_maxValue ? m_maxValue : value;
239  } else {
240  return value;
241  }
242 }
243 
245 {
246  if (m_type == ArgInfoBinary)
247  {
248  ui->argCheck->blockSignals(true);
249  ui->argCheck->setChecked(m_boolValue);
250  ui->argCheck->blockSignals(false);
251  }
252  else if (m_type == ArgInfoContinuous)
253  {
254  ui->argEdit->blockSignals(true);
255  ui->argEdit->setText(QString(m_boolValue ? "true" : "false"));
256  ui->argEdit->blockSignals(false);
257  }
258  else if (m_type == ArgInfoDiscrete)
259  {
260  if (ui->argCombo->count() > 1)
261  {
262  ui->argCombo->blockSignals(true);
263  ui->argCombo->setCurrentIndex(m_boolValue ? 0 : 1);
264  ui->argCombo->blockSignals(false);
265  }
266  }
267 }
268 
270 {
271  if (m_type == ArgInfoBinary)
272  {
273  ui->argCheck->blockSignals(true);
274  ui->argCheck->setChecked(m_intValue == 0 ? false : true);
275  ui->argCheck->blockSignals(false);
276  }
277  else if (m_type == ArgInfoContinuous)
278  {
279  ui->argEdit->blockSignals(true);
280  ui->argEdit->setText(QString("%1").arg(m_intValue));
281  ui->argEdit->blockSignals(false);
282  }
283  else if (m_type == ArgInfoDiscrete)
284  {
285  for (int i = 0; i < ui->argCombo->count(); i++)
286  {
287  if (ui->argCombo->itemData(i).type() == QVariant::Int)
288  {
289  bool ok = false;
290  QVariant v = ui->argCombo->itemData(i);
291 
292  if (m_intValue >= v.toInt(&ok) && ok)
293  {
294  ui->argCombo->blockSignals(true);
295  ui->argCombo->setCurrentIndex(i);
296  ui->argCombo->blockSignals(false);
297  break;
298  }
299  }
300  }
301  }
302 }
303 
305 {
306  if (m_type == ArgInfoBinary)
307  {
308  ui->argCheck->blockSignals(true);
309  ui->argCheck->setChecked(m_floatValue == 0.0 ? false : true);
310  ui->argCheck->blockSignals(false);
311  }
312  else if (m_type == ArgInfoContinuous)
313  {
314  ui->argEdit->blockSignals(true);
315  ui->argEdit->setText(QString("%1").arg(m_floatValue));
316  ui->argEdit->blockSignals(false);
317  }
318  else if (m_type == ArgInfoDiscrete)
319  {
320  for (int i = 0; i < ui->argCombo->count(); i++)
321  {
322  if (ui->argCombo->itemData(i).type() == QVariant::Double)
323  {
324  bool ok = false;
325  QVariant v = ui->argCombo->itemData(i);
326 
327  if (m_floatValue >= v.toDouble(&ok) && ok)
328  {
329  ui->argCombo->blockSignals(true);
330  ui->argCombo->setCurrentIndex(i);
331  ui->argCombo->blockSignals(false);
332  break;
333  }
334  }
335  }
336  }
337 }
338 
340 {
341  if (m_type == ArgInfoBinary)
342  {
343  ui->argCheck->blockSignals(true);
344  ui->argCheck->setChecked(m_stringValue == "true" ? true : false);
345  ui->argCheck->blockSignals(false);
346  }
347  else if (m_type == ArgInfoContinuous)
348  {
349  ui->argEdit->blockSignals(true);
350  ui->argEdit->setText(m_stringValue);
351  ui->argEdit->blockSignals(false);
352  }
353  else if (m_type == ArgInfoDiscrete)
354  {
355  for (int i = 0; i < ui->argCombo->count(); i++)
356  {
357  if (ui->argCombo->itemData(i).type() == QVariant::String)
358  {
359  QVariant v = ui->argCombo->itemData(i);
360 
361  if (m_stringValue == v.toString())
362  {
363  ui->argCombo->blockSignals(true);
364  ui->argCombo->setCurrentIndex(i);
365  ui->argCombo->blockSignals(false);
366  break;
367  }
368  }
369  }
370  }
371 }
372 
374 {
375  setBoolValue(checked);
376  emit valueChanged();
377 }
378 
380 {
381  setStringValue(ui->argEdit->text());
382  emit valueChanged();
383 }
384 
386 {
387  (void) index;
388  QVariant v = ui->argCombo->currentData();
389  bool ok = false;
390 
391  if (v.type() == QVariant::Bool)
392  {
393  setBoolValue(v.toBool());
394  emit valueChanged();
395  }
396  else if (v.type() == QVariant::Int)
397  {
398  setIntValue(v.toInt(&ok));
399 
400  if (ok) {
401  emit valueChanged();
402  }
403  }
404  else if (v.type() == QVariant::Double)
405  {
406  setFloatValue(v.toDouble(&ok));
407 
408  if (ok) {
409  emit valueChanged();
410  }
411  }
412  else if (v.type() ==QVariant::String)
413  {
414  setStringValue(v.toString());
415  emit valueChanged();
416  }
417 }
418 
void on_argCheck_toggled(bool checked)
Definition: arginfogui.cpp:373
int m_intValue
Definition: arginfogui.h:96
void setUnits(const QString &units)
Definition: arginfogui.cpp:95
ArgInfoValueType m_valueType
Definition: arginfogui.h:94
double m_maxValue
Definition: arginfogui.h:101
void setRange(double min, double max)
Definition: arginfogui.cpp:72
void updateUIFromString()
Definition: arginfogui.cpp:339
ArgInfoType m_type
Definition: arginfogui.h:93
double setDoubleValue(double value)
Definition: arginfogui.cpp:235
Fixed< IntType, IntBits > arg(const std::complex< Fixed< IntType, IntBits > > &val)
Definition: fixed.h:2401
ArgInfoGUI(ArgInfoType type, ArgInfoValueType valueType, QWidget *parent=0)
Definition: arginfogui.cpp:24
QString m_stringValue
Definition: arginfogui.h:98
bool m_boolValue
Definition: arginfogui.h:95
double m_minValue
Definition: arginfogui.h:100
void on_argCombo_currentIndexChanged(int index)
Definition: arginfogui.cpp:385
int setIntegerValue(int value)
Definition: arginfogui.cpp:226
Ui::ArgInfoGUI * ui
Definition: arginfogui.h:92
void setIntValue(int value)
Definition: arginfogui.cpp:131
int32_t i
Definition: decimators.h:244
void updateUIFromFloat()
Definition: arginfogui.cpp:304
void valueChanged()
void addIntValue(const QString &text, int value)
Definition: arginfogui.cpp:155
void updateUIFromInt()
Definition: arginfogui.cpp:269
bool m_hasRange
Definition: arginfogui.h:99
void on_argEdit_editingFinished()
Definition: arginfogui.cpp:379
void addFloatValue(const QString &text, double value)
Definition: arginfogui.cpp:186
void setToolTip(const QString &text)
Definition: arginfogui.cpp:84
void updateUIFromBool()
Definition: arginfogui.cpp:244
void setBoolValue(bool value)
Definition: arginfogui.cpp:100
void addBoolValue(const QString &text, bool value)
Definition: arginfogui.cpp:124
void setFloatValue(double value)
Definition: arginfogui.cpp:162
double m_floatValue
Definition: arginfogui.h:97
void setLabel(const QString &text)
Definition: arginfogui.cpp:79
T max(const T &x, const T &y)
Definition: framework.h:446
void addStringValue(const QString &text, const QString &value)
Definition: arginfogui.cpp:219
void setStringValue(const QString &value)
Definition: arginfogui.cpp:193
T min(const T &x, const T &y)
Definition: framework.h:440