dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
inputchannelform.cpp
Go to the documentation of this file.
1 #include "inputchannelform.h"
2 #include "ui_inputchannelform.h"
3 
4 #include "manualcontrolsettings.h"
5 #include "uavtalkreceiver.h"
6 
10 
11 inputChannelForm::inputChannelForm(QWidget *parent, bool showlegend, bool showSlider,
12  ChannelFunc chanType)
13  : ConfigTaskWidget(parent)
14  , ui(new Ui::InputChannelForm)
15  , m_chanType(chanType)
16 {
17  ui->setupUi(this);
18 
19  // The first time through the loop, keep the legend. All other times, delete it.
20  if (!showlegend) {
21  layout()->removeWidget(ui->legend0);
22  layout()->removeWidget(ui->legend1);
23  layout()->removeWidget(ui->legend2);
24  layout()->removeWidget(ui->legend3);
25  layout()->removeWidget(ui->legend4);
26  layout()->removeWidget(ui->legend5);
27  layout()->removeWidget(ui->legend6);
28  layout()->removeWidget(ui->lblReverse);
29  delete ui->legend0;
30  delete ui->legend1;
31  delete ui->legend2;
32  delete ui->legend3;
33  delete ui->legend4;
34  delete ui->legend5;
35  delete ui->legend6;
36  delete ui->lblReverse;
37  }
38 
39  if (!showSlider) {
40  ui->channelNeutral->setHidden(true);
41  }
42 
43  // setup a hidden widget to track channel value without too much pain
44  sbChannelCurrent = new QSpinBox(this);
45  sbChannelCurrent->setVisible(false);
46  sbChannelCurrent->setMaximum(65535);
47 
48  // Connect slots
49  connect(ui->channelMin, QOverload<int>::of(&QSpinBox::valueChanged), this,
50  &inputChannelForm::minMaxUpdated);
51  connect(ui->channelMax, QOverload<int>::of(&QSpinBox::valueChanged), this,
52  &inputChannelForm::minMaxUpdated);
53  connect(ui->channelGroup, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
54  &inputChannelForm::groupUpdated);
55  connect(sbChannelCurrent, QOverload<int>::of(&QSpinBox::valueChanged), ui->channelNeutral,
57  connect(ui->btnReverse, &QAbstractButton::released, this, &inputChannelForm::reverseChannel);
58 
59  // This is awkward but since we want the UI to be a dropdown but the field is not an enum
60  // so it breaks the UAUVObject widget relation of the task gadget. Running the data through
61  // a spin box fixes this
62  connect(ui->channelNumberDropdown, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
63  &inputChannelForm::channelDropdownUpdated);
64  connect(ui->channelNumber, QOverload<int>::of(&QSpinBox::valueChanged), this,
65  &inputChannelForm::channelNumberUpdated);
66 
68 }
69 
71 {
72  delete ui;
73 }
74 
75 void inputChannelForm::setName(QString &name)
76 {
77  ui->channelName->setText(name);
78  QFontMetrics metrics(ui->channelName->font());
79  int width = metrics.width(name) + 5;
80  foreach (inputChannelForm *form, parent()->findChildren<inputChannelForm *>()) {
81  if (form == this)
82  continue;
83  if (form->ui->channelName->minimumSize().width() < width)
84  form->ui->channelName->setMinimumSize(width, 0);
85  else
86  width = form->ui->channelName->minimumSize().width();
87  }
88  ui->channelName->setMinimumSize(width, 0);
89 }
90 
94 void inputChannelForm::minMaxUpdated()
95 {
96  if (ui->channelMin->value() != ui->channelMax->value()) {
97  bool reverse = (ui->channelMin->value() > ui->channelMax->value());
98  if (reverse) {
99  ui->channelNeutral->setMinimum(ui->channelMax->value());
100  ui->channelNeutral->setMaximum(ui->channelMin->value());
101 
102  // Set the QSlider groove colors so that the fill is on the side of the minimum value
103  ui->channelNeutral->setProperty("state", "inverted");
104  } else {
105  ui->channelNeutral->setMinimum(ui->channelMin->value());
106  ui->channelNeutral->setMaximum(ui->channelMax->value());
107 
108  // Set the QSlider groove colors so that the fill is on the side of the minimum value
109  ui->channelNeutral->setProperty("state", "normal");
110  }
111  ui->channelNeutral->setInvertedAppearance(reverse);
112 
113  // make sure slider is enabled (can be removed when "else" below is removed)
114  ui->channelNeutral->setEnabled(true);
115  } else {
116  // when the min and max is equal, disable this slider to prevent crash
117  // from Qt bug: https://bugreports.qt.io/browse/QTBUG-43398
118  ui->channelNeutral->setMinimum(ui->channelMin->value() - 1);
119  ui->channelNeutral->setMaximum(ui->channelMax->value() + 1);
120  ui->channelNeutral->setEnabled(false);
121  }
122 
123  // Force refresh of style sheet.
124  ui->channelNeutral->setStyle(QApplication::style());
125 }
126 
133 void inputChannelForm::groupUpdated()
134 {
135  int selected = ui->channelNumberDropdown->currentIndex();
136  ui->channelNumberDropdown->clear();
137  ui->channelNumberDropdown->addItem("Disabled");
138 
139  quint8 count = 0;
140 
141  switch (m_chanType) {
142 
144  switch (ui->channelGroup->currentIndex()) {
145  case -1: // Nothing selected
146  count = 0;
147  break;
148  case ManualControlSettings::CHANNELGROUPS_PWM:
149  count = 8; // Need to make this 6 for CC
150  break;
151  case ManualControlSettings::CHANNELGROUPS_PPM:
152  case ManualControlSettings::CHANNELGROUPS_DSM:
153  count = 12;
154  break;
155  case ManualControlSettings::CHANNELGROUPS_SBUS:
156  count = 18;
157  break;
158  case ManualControlSettings::CHANNELGROUPS_OPENLRS:
159  count = 8;
160  break;
161  case ManualControlSettings::CHANNELGROUPS_UAVTALK:
162  count = UAVTalkReceiver::CHANNEL_NUMELEM;
163  break;
164  case ManualControlSettings::CHANNELGROUPS_HOTTSUM:
165  count = 32;
166  break;
167  case ManualControlSettings::CHANNELGROUPS_SRXL:
168  count = 16;
169  break;
170  case ManualControlSettings::CHANNELGROUPS_IBUS:
171  count = 10;
172  break;
173  case ManualControlSettings::CHANNELGROUPS_TBSCROSSFIRE:
174  count = 12;
175  break;
176  case ManualControlSettings::CHANNELGROUPS_NONE:
177  count = 0;
178  break;
179  default:
180  Q_ASSERT(0);
181  }
182  } break;
183 
185  switch (ui->channelGroup->currentIndex()) {
186  case -1: // Nothing selected
187  count = 0;
188  break;
189  case ManualControlSettings::RSSITYPE_PWM:
190  count = 8;
191  break;
192  case ManualControlSettings::RSSITYPE_PPM:
193  count = 12;
194  break;
195  case ManualControlSettings::RSSITYPE_SBUS:
196  count = 18;
197  break;
198  case ManualControlSettings::RSSITYPE_HOTTSUM:
199  count = 32;
200  break;
201  case ManualControlSettings::RSSITYPE_ADC:
202  count = 9;
203  break;
204  case ManualControlSettings::RSSITYPE_OPENLRS:
205  count = 8;
206  break;
207  case ManualControlSettings::RSSITYPE_FRSKYPWM:
208  count = 1;
209  break;
210  case ManualControlSettings::RSSITYPE_TBSCROSSFIRE:
211  count = 12;
212  break;
213  case ManualControlSettings::RSSITYPE_IBUS:
214  count = 10;
215  break;
216  case ManualControlSettings::RSSITYPE_NONE:
217  count = 0;
218  break;
219  default:
220  Q_ASSERT(0);
221  }
222  } break;
223  }
224 
225  ui->channelNumber->setMaximum(count);
226  ui->channelNumber->setMinimum(0);
227 
228  if (m_chanType == inputChannelForm::CHANNELFUNC_RSSI
229  && ui->channelGroup->currentIndex() == ManualControlSettings::RSSITYPE_ADC) {
230  QStringList names;
232  if (board)
233  names = board->getAdcNames();
234 
235  for (int i = 0; i < count; i++) {
236  QString name;
237  if (i < names.length())
238  name = names[i] + QString(" (ADC%1)").arg(i);
239  else if (names.isEmpty())
240  name = QString("ADC%1").arg(i);
241  else
242  name = QString("N/A (ADC%1)").arg(i);
243 
244  ui->channelNumberDropdown->addItem(name);
245  }
246  } else {
247  for (int i = 0; i < count; i++)
248  ui->channelNumberDropdown->addItem(QString(tr("Chan %1").arg(i + 1)));
249  if (selected > 0 && selected <= count)
250  ui->channelNumberDropdown->setCurrentIndex(selected);
251  }
252 
253  Q_EMIT assignmentChanged();
254 }
255 
259 void inputChannelForm::channelDropdownUpdated(int newval)
260 {
261  ui->channelNumber->setValue(newval);
262 }
263 
267 void inputChannelForm::channelNumberUpdated(int newval)
268 {
269  ui->channelNumberDropdown->setCurrentIndex(newval);
270  Q_EMIT assignmentChanged();
271 }
272 
276 void inputChannelForm::reverseChannel()
277 {
278  int min = ui->channelMin->value();
279  int neutral = ui->channelNeutral->value();
280  int max = ui->channelMax->value();
281  ui->channelMax->setValue(min);
282  ui->channelMin->setValue(max);
283  if (ui->channelName->text() == "Throttle") // this is bad... but...
284  neutral = max - (neutral - min);
285  ui->channelNeutral->setValue(neutral);
286 }
287 
292 {
293  return ui->channelGroup->currentIndex() != ManualControlSettings::CHANNELGROUPS_NONE
294  && ui->channelNumber->value() > 0;
295 }
UAVObjectUtilManager * utilMngr
virtual QStringList getAdcNames()
Definition: iboardtype.h:237
for i
Definition: OPPlots.m:140
bool assigned()
Is an input assigned to this channel?
inputChannelForm(QWidget *parent=nullptr, bool showlegend=false, bool showSlider=true, ChannelFunc chanType=CHANNELFUNC_RC)
void assignmentChanged()
void setName(QString &name)
void setIndicatorValue(int us)
Core::IBoardType * getBoardType()
Get the IBoardType corresponding to the connected board.