dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
configosdwidget.cpp
Go to the documentation of this file.
1 
11 /*
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20  * for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, see <http://www.gnu.org/licenses/>
24  */
25 
26 #include "configosdwidget.h"
27 
30 
31 #include "onscreendisplaysettings.h"
32 #include "onscreendisplaypagesettings.h"
33 
34 #include "manualcontrolcommand.h"
35 #include "manualcontrolsettings.h"
36 
37 #include "ui_osdpage.h"
38 
40  : ConfigTaskWidget(parent)
41 {
42  ui = new Ui::Osd();
43  ui->setupUi(this);
44 
45  osdSettingsObj = OnScreenDisplaySettings::GetInstance(getObjectManager());
46  manualCommandObj = ManualControlCommand::GetInstance(getObjectManager());
47  manualSettingsObj = ManualControlSettings::GetInstance(getObjectManager());
48 
49  // connect signals to set/get custom OSD text
50  connect(ui->applyButton, &QAbstractButton::clicked, this, &ConfigOsdWidget::setCustomText);
51  connect(ui->saveButton, &QAbstractButton::clicked, this, &ConfigOsdWidget::setCustomText);
52  connect(ui->reloadButton, &QAbstractButton::clicked, this, &ConfigOsdWidget::getCustomText);
53  connect(osdSettingsObj, &OnScreenDisplaySettings::CustomText_0Changed, this,
54  &ConfigOsdWidget::getCustomText);
55 
56  // setup the OSD widgets
57  connect(ManualControlCommand::GetInstance(getObjectManager()), &UAVObject::objectUpdated, this,
58  &ConfigOsdWidget::movePageSlider);
59  connect(OnScreenDisplaySettings::GetInstance(getObjectManager()), &UAVObject::objectUpdated,
60  this, &ConfigOsdWidget::updatePositionSlider);
61 
62  // Setup OSD pages
63  pages[0] = ui->osdPage1;
64  pages[1] = ui->osdPage2;
65  pages[2] = ui->osdPage3;
66  pages[3] = ui->osdPage4;
67 
68  ui_pages[0] = new Ui::OsdPage();
69  setupOsdPage(ui_pages[0], ui->osdPage1, QStringLiteral("OnScreenDisplayPageSettings"));
70 
71  ui_pages[0]->copyButton1->setText("Copy Page 2");
72  ui_pages[0]->copyButton2->setText("Copy Page 3");
73  ui_pages[0]->copyButton3->setText("Copy Page 4");
74 
75  connect(ui_pages[0]->copyButton1, &QAbstractButton::released, this, [this]() {
76  copyOsdPage(0, 1);
77  });
78  connect(ui_pages[0]->copyButton2, &QAbstractButton::released, this, [this]() {
79  copyOsdPage(0, 2);
80  });
81  connect(ui_pages[0]->copyButton3, &QAbstractButton::released, this, [this]() {
82  copyOsdPage(0, 3);
83  });
84 
85  ui_pages[1] = new Ui::OsdPage();
86  setupOsdPage(ui_pages[1], ui->osdPage2, QStringLiteral("OnScreenDisplayPageSettings2"));
87 
88  ui_pages[1]->copyButton1->setText("Copy Page 1");
89  ui_pages[1]->copyButton2->setText("Copy Page 3");
90  ui_pages[1]->copyButton3->setText("Copy Page 4");
91 
92  connect(ui_pages[1]->copyButton1, &QAbstractButton::released, this, [this]() {
93  copyOsdPage(1, 0);
94  });
95  connect(ui_pages[1]->copyButton2, &QAbstractButton::released, this, [this]() {
96  copyOsdPage(1, 2);
97  });
98  connect(ui_pages[1]->copyButton3, &QAbstractButton::released, this, [this]() {
99  copyOsdPage(1, 3);
100  });
101 
102  ui_pages[2] = new Ui::OsdPage();
103  setupOsdPage(ui_pages[2], ui->osdPage3, QStringLiteral("OnScreenDisplayPageSettings3"));
104 
105  ui_pages[2]->copyButton1->setText("Copy Page 1");
106  ui_pages[2]->copyButton2->setText("Copy Page 2");
107  ui_pages[2]->copyButton3->setText("Copy Page 4");
108 
109  connect(ui_pages[2]->copyButton1, &QAbstractButton::released, this, [this]() {
110  copyOsdPage(2, 0);
111  });
112  connect(ui_pages[2]->copyButton2, &QAbstractButton::released, this, [this]() {
113  copyOsdPage(2, 1);
114  });
115  connect(ui_pages[2]->copyButton3, &QAbstractButton::released, this, [this]() {
116  copyOsdPage(2, 3);
117  });
118 
119  ui_pages[3] = new Ui::OsdPage();
120  setupOsdPage(ui_pages[3], ui->osdPage4, QStringLiteral("OnScreenDisplayPageSettings4"));
121 
122  ui_pages[3]->copyButton1->setText("Copy Page 1");
123  ui_pages[3]->copyButton2->setText("Copy Page 2");
124  ui_pages[3]->copyButton3->setText("Copy Page 3");
125 
126  connect(ui_pages[3]->copyButton1, &QAbstractButton::released, this, [this]() {
127  copyOsdPage(3, 0);
128  });
129  connect(ui_pages[3]->copyButton2, &QAbstractButton::released, this, [this]() {
130  copyOsdPage(3, 1);
131  });
132  connect(ui_pages[3]->copyButton3, &QAbstractButton::released, this, [this]() {
133  copyOsdPage(3, 2);
134  });
135 
136  // Load UAVObjects to widget relations from UI file
137  // using objrelation dynamic property
138  autoLoadWidgets();
139 
140  // Refresh widget contents
142 
143  // Prevent mouse wheel from changing values
145 }
146 
148 {
149  for (auto p : ui_pages)
150  delete p;
151  delete ui;
152 }
153 
160 quint8 ConfigOsdWidget::scaleSwitchChannel(quint8 channelNumber, quint8 switchPositions)
161 {
162  if (channelNumber > (ManualControlSettings::CHANNELMIN_NUMELEM - 1))
163  return 0;
164  ManualControlSettings::DataFields manualSettingsDataPriv = manualSettingsObj->getData();
165  ManualControlCommand::DataFields manualCommandDataPriv = manualCommandObj->getData();
166 
167  float valueScaled;
168  int chMin = manualSettingsDataPriv.ChannelMin[channelNumber];
169  int chMax = manualSettingsDataPriv.ChannelMax[channelNumber];
170  int chNeutral = manualSettingsDataPriv.ChannelNeutral[channelNumber];
171 
172  int value = manualCommandDataPriv.Channel[channelNumber];
173  if ((chMax > chMin && value >= chNeutral) || (chMin > chMax && value <= chNeutral)) {
174  if (chMax != chNeutral)
175  valueScaled = (float)(value - chNeutral) / (float)(chMax - chNeutral);
176  else
177  valueScaled = 0;
178  } else {
179  if (chMin != chNeutral)
180  valueScaled = (float)(value - chNeutral) / (float)(chNeutral - chMin);
181  else
182  valueScaled = 0;
183  }
184 
185  if (valueScaled < -1.0)
186  valueScaled = -1.0;
187  else if (valueScaled > 1.0)
188  valueScaled = 1.0;
189 
190  // Convert channel value into the switch position in the range [0..N-1]
191  // This uses the same optimized computation as flight code to be consistent
192  quint8 pos = ((qint16)(valueScaled * 256) + 256) * switchPositions >> 9;
193  if (pos >= switchPositions)
194  pos = switchPositions - 1;
195 
196  return pos;
197 }
198 
199 void ConfigOsdWidget::movePageSlider()
200 {
201  OnScreenDisplaySettings::DataFields onScreenDisplaySettingsDataPriv = osdSettingsObj->getData();
202 
203  switch (onScreenDisplaySettingsDataPriv.PageSwitch) {
204  case OnScreenDisplaySettings::PAGESWITCH_ACCESSORY0:
205  ui->osdPageSlider->setValue(scaleSwitchChannel(ManualControlSettings::CHANNELMIN_ACCESSORY0,
206  onScreenDisplaySettingsDataPriv.NumPages));
207  break;
208  case OnScreenDisplaySettings::PAGESWITCH_ACCESSORY1:
209  ui->osdPageSlider->setValue(scaleSwitchChannel(ManualControlSettings::CHANNELMIN_ACCESSORY1,
210  onScreenDisplaySettingsDataPriv.NumPages));
211  break;
212  case OnScreenDisplaySettings::PAGESWITCH_ACCESSORY2:
213  ui->osdPageSlider->setValue(scaleSwitchChannel(ManualControlSettings::CHANNELMIN_ACCESSORY2,
214  onScreenDisplaySettingsDataPriv.NumPages));
215  break;
216  }
217 }
218 
219 void ConfigOsdWidget::updatePositionSlider()
220 {
221  OnScreenDisplaySettings::DataFields onScreenDisplaySettingsDataPriv = osdSettingsObj->getData();
222 
223  switch (onScreenDisplaySettingsDataPriv.NumPages) {
224  default:
225  case 6:
226  ui->osdPagePos6->setEnabled(true);
227  Q_FALLTHROUGH();
228  case 5:
229  ui->osdPagePos5->setEnabled(true);
230  Q_FALLTHROUGH();
231  case 4:
232  ui->osdPagePos4->setEnabled(true);
233  Q_FALLTHROUGH();
234  case 3:
235  ui->osdPagePos3->setEnabled(true);
236  Q_FALLTHROUGH();
237  case 2:
238  ui->osdPagePos2->setEnabled(true);
239  Q_FALLTHROUGH();
240  case 1:
241  ui->osdPagePos1->setEnabled(true);
242  Q_FALLTHROUGH();
243  case 0:
244  break;
245  }
246 
247  switch (onScreenDisplaySettingsDataPriv.NumPages) {
248  case 0:
249  ui->osdPagePos1->setEnabled(false);
250  Q_FALLTHROUGH();
251  case 1:
252  ui->osdPagePos2->setEnabled(false);
253  Q_FALLTHROUGH();
254  case 2:
255  ui->osdPagePos3->setEnabled(false);
256  Q_FALLTHROUGH();
257  case 3:
258  ui->osdPagePos4->setEnabled(false);
259  Q_FALLTHROUGH();
260  case 4:
261  ui->osdPagePos5->setEnabled(false);
262  Q_FALLTHROUGH();
263  case 5:
264  ui->osdPagePos6->setEnabled(false);
265  Q_FALLTHROUGH();
266  case 6:
267  default:
268  break;
269  }
270 }
271 
272 void ConfigOsdWidget::setCustomText()
273 {
274  const QString text = ui->le_custom_text->displayText();
275  unsigned int n_string = text.size();
276 
277  for (unsigned int i = 0; i < OnScreenDisplaySettings::CUSTOMTEXT_NUMELEM; ++i) {
278  if (i < n_string) {
279  osdSettingsObj->setCustomText(i, (quint8)(text.data()[i].toLatin1()));
280  } else {
281  osdSettingsObj->setCustomText(i, 0);
282  }
283  }
284 }
285 
286 void ConfigOsdWidget::getCustomText()
287 {
288  char text[OnScreenDisplaySettings::CUSTOMTEXT_NUMELEM];
289 
290  for (unsigned int i = 0; i < OnScreenDisplaySettings::CUSTOMTEXT_NUMELEM; ++i) {
291  text[i] = osdSettingsObj->getCustomText(i);
292  }
293  QString q_text = QString::fromLatin1(text, OnScreenDisplaySettings::CUSTOMTEXT_NUMELEM);
294  ui->le_custom_text->setText(q_text);
295 }
296 
297 void ConfigOsdWidget::setupOsdPage(Ui::OsdPage *page, QWidget *page_widget, const QString &objName)
298 {
299  page->setupUi(page_widget);
300  const QString nameRel = QStringLiteral("objname:%0").arg(objName);
301 
302  for (const auto wid : page_widget->findChildren<QWidget *>()) {
303  const auto rel = wid->property("objrelation");
304  if (!rel.isValid())
305  continue;
306  auto relList = rel.toStringList();
307  for (const auto &s : qAsConst(relList)) {
308  if (s.startsWith(QStringLiteral("fieldname:"))) {
309  relList.append(nameRel);
310  wid->setProperty("objrelation", relList);
311  break;
312  }
313  }
314  }
315 }
316 
317 void ConfigOsdWidget::copyOsdPage(int to, int from)
318 {
319  for (const auto checkbox : pages[from]->findChildren<QCheckBox *>()) {
320  auto cb_to = pages[to]->findChild<QCheckBox *>(checkbox->objectName());
321  if (cb_to) {
322  cb_to->setChecked(checkbox->checkState());
323  }
324  }
325 
326  for (const auto spinbox : pages[from]->findChildren<QSpinBox *>()) {
327  auto sb_to = pages[to]->findChild<QSpinBox *>(spinbox->objectName());
328  if (sb_to) {
329  sb_to->setValue(spinbox->value());
330  }
331  }
332 
333  for (const auto combobox : pages[from]->findChildren<QComboBox *>()) {
334  auto cmb_to = pages[to]->findChild<QComboBox *>(combobox->objectName());
335  if (cmb_to) {
336  cmb_to->setCurrentIndex(combobox->currentIndex());
337  }
338  }
339 }
340 
341 void ConfigOsdWidget::resizeEvent(QResizeEvent *event)
342 {
343  QWidget::resizeEvent(event);
344 }
345 
347 {
348  Q_UNUSED(enable);
349 }
350 
for i
Definition: OPPlots.m:140
void objectUpdated(UAVObject *obj)
Signal sent whenever any field of the object is updated.
virtual void enableControls(bool enable)
Configure the OSD.
void resizeEvent(QResizeEvent *event)
virtual void refreshWidgetsValues(UAVObject *obj=NULL)
UAVObjectManager * getObjectManager()
ConfigTaskWidget::getObjectManager Utility function to get a pointer to the object manager...
ConfigOsdWidget(QWidget *parent=nullptr)