dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
mixercurve.cpp
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 #include <QWidget>
28 #include <QResizeEvent>
29 
31 
32 #include "mixercurve.h"
33 #include "dblspindelegate.h"
34 
35 MixerCurve::MixerCurve(QWidget *parent)
36  : QFrame(parent)
37  , m_mixerUI(new Ui::MixerCurvePlot)
38 {
39  m_mixerUI->setupUi(this);
40 
41  // setup some convienence pointers
42  m_curve = m_mixerUI->CurveWidget;
43  m_settings = m_mixerUI->CurveSettings;
44 
45  // by default mixcurve isn't used for curve2source
46  m_mixerUI->CBCurve2Source->hide();
47 
48  m_mixerUI->SettingsGroup->hide();
49  m_curve->showCommands(false);
50  m_curve->showCommand("Reset", false);
51  m_curve->showCommand("Popup", false);
52  m_curve->showCommand("Commands", false);
53 
54  // create our spin delegate
55  m_spinDelegate = new DoubleSpinDelegate();
56 
57  // set the default mixer type
59 
60  // setup and turn off advanced mode
61  CommandActivated();
62 
63  // paint the ui
64  UpdateCurveUI();
65 
66  // wire up our signals
67 
68  connect(m_mixerUI->CurveType, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
69  &MixerCurve::CurveTypeChanged);
70  connect(m_mixerUI->ResetCurve, &QAbstractButton::clicked, this, &MixerCurve::ResetCurve);
71  connect(m_mixerUI->PopupCurve, &QAbstractButton::clicked, this, &MixerCurve::PopupCurve);
72  connect(m_mixerUI->GenerateCurve, &QAbstractButton::clicked, this, &MixerCurve::GenerateCurve);
74  connect(m_curve, &MixerCurveWidget::commandActivated, this, &MixerCurve::CommandActivated);
75  connect(m_settings, &QTableWidget::cellChanged, this, &MixerCurve::SettingsTableChanged);
76  connect(m_mixerUI->CurveMin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
77  &MixerCurve::CurveMinChanged);
78  connect(m_mixerUI->CurveMax, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
79  &MixerCurve::CurveMaxChanged);
80  connect(m_mixerUI->CurveStep, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
82 }
83 
85 {
86  delete m_mixerUI;
87  delete m_spinDelegate;
88 }
89 
90 void MixerCurve::setMixerType(MixerCurveType curveType, bool isCurve1)
91 {
92  m_curveType = curveType;
93 
94  m_mixerUI->CurveMin->setMaximum(1.0);
95  m_mixerUI->CurveMax->setMaximum(1.0);
96 
97  switch (m_curveType) {
99  m_mixerUI->SettingsGroup->setTitle("Throttle Curve");
100  m_curve->setRange(0.0, 1.0);
101  m_mixerUI->CurveMin->setMinimum(0.0);
102  m_mixerUI->CurveMax->setMinimum(0.0);
103  break;
104  }
106  m_mixerUI->SettingsGroup->setTitle("Pitch Curve");
107  m_curve->setRange(-1.0, 1.0);
108  m_curve->setPositiveColor("#0000aa", "#0000aa");
109  m_mixerUI->CurveMin->setMinimum(-1.0);
110  m_mixerUI->CurveMax->setMinimum(-1.0);
111  break;
112  }
113  }
114 
115  if (!isCurve1) {
116  m_mixerUI->CBCurve2Source->show();
117  m_mixerUI->inputLabelCurve2Source->setText("Input:");
118  } else {
119  m_mixerUI->inputLabelCurve2Source->setText("Input: Throttle");
120  }
121 
122  m_spinDelegate->setRange(m_mixerUI->CurveMin->minimum(), m_mixerUI->CurveMax->maximum());
123  for (int i = 0; i < MixerCurveWidget::NODE_NUMELEM; i++) {
124  m_settings->setItemDelegateForRow(i, m_spinDelegate);
125  }
126 
127  ResetCurve();
128 }
129 
131 {
132  m_mixerUI->CurveMin->setValue(m_mixerUI->CurveMin->minimum());
133  m_mixerUI->CurveMax->setValue(m_mixerUI->CurveMax->maximum());
134  m_mixerUI->CurveType->setCurrentIndex(m_mixerUI->CurveType->findText("Linear"));
135 
137 
138  m_curve->activateCommand("Linear");
139 
141 }
142 
144 {
145  if (!m_curve->isCommandActive("Popup")) {
146 
147  m_mixerUI->SettingsGroup->show();
148  m_mixerUI->PopupCurve->hide();
149 
150  PopupWidget *popup = new PopupWidget();
151  popup->popUp(this);
152 
153  m_mixerUI->SettingsGroup->hide();
154  m_mixerUI->PopupCurve->show();
155  m_curve->showCommands(false);
156  }
157 }
158 void MixerCurve::UpdateCurveUI()
159 {
160  // get the user settings
161  QString curveType = m_mixerUI->CurveType->currentText();
162 
163  m_curve->activateCommand(curveType);
164  bool cmdsActive = m_curve->isCommandActive("Commands");
165 
166  m_curve->showCommand("StepPlus", cmdsActive && curveType != "Linear");
167  m_curve->showCommand("StepMinus", cmdsActive && curveType != "Linear");
168  m_curve->showCommand("StepValue", cmdsActive && curveType != "Linear");
169 
170  m_mixerUI->CurveStep->setMinimum(0.0);
171  m_mixerUI->CurveStep->setMaximum(100.0);
172  m_mixerUI->CurveStep->setSingleStep(1.00);
173 
174  // set default visible
175  m_mixerUI->minLabel->setVisible(true);
176  m_mixerUI->CurveMin->setVisible(true);
177  m_mixerUI->maxLabel->setVisible(false);
178  m_mixerUI->CurveMax->setVisible(false);
179  m_mixerUI->stepLabel->setVisible(false);
180  m_mixerUI->CurveStep->setVisible(false);
181 
182  if (curveType.compare("Flat") == 0) {
183  m_mixerUI->minLabel->setVisible(false);
184  m_mixerUI->CurveMin->setVisible(false);
185  m_mixerUI->stepLabel->setVisible(true);
186  m_mixerUI->CurveStep->setVisible(true);
187  m_mixerUI->CurveStep->setMinimum(m_mixerUI->CurveMin->minimum());
188  m_mixerUI->CurveStep->setMaximum(m_mixerUI->CurveMax->maximum());
189  m_mixerUI->CurveStep->setSingleStep(0.01);
190  m_mixerUI->CurveStep->setValue(m_mixerUI->CurveMax->value() / 2);
191  }
192  if (curveType.compare("Linear") == 0) {
193  m_mixerUI->maxLabel->setVisible(true);
194  m_mixerUI->CurveMax->setVisible(true);
195  }
196  if (curveType.compare("Step") == 0) {
197  m_mixerUI->maxLabel->setVisible(true);
198  m_mixerUI->CurveMax->setVisible(true);
199  m_mixerUI->stepLabel->setText("Step at");
200  m_mixerUI->stepLabel->setVisible(true);
201  m_mixerUI->CurveStep->setVisible(true);
202 
203  m_mixerUI->CurveStep->setMinimum(1.0);
204  }
205  if (curveType.compare("Exp") == 0) {
206  m_mixerUI->maxLabel->setVisible(true);
207  m_mixerUI->CurveMax->setVisible(true);
208  m_mixerUI->stepLabel->setText("Power");
209  m_mixerUI->stepLabel->setVisible(true);
210  m_mixerUI->CurveStep->setVisible(true);
211 
212  m_mixerUI->CurveStep->setMinimum(1.0);
213  }
214  if (curveType.compare("Log") == 0) {
215  m_mixerUI->maxLabel->setVisible(true);
216  m_mixerUI->CurveMax->setVisible(true);
217  m_mixerUI->stepLabel->setText("Power");
218  m_mixerUI->stepLabel->setVisible(true);
219  m_mixerUI->CurveStep->setVisible(true);
220  m_mixerUI->CurveStep->setMinimum(1.0);
221  }
222 
223  GenerateCurve();
224 }
225 
227 {
228  double scale;
229  double newValue;
230 
231  // get the user settings
232  double value1 = getCurveMin();
233  double value2 = getCurveMax();
234  double value3 = getCurveStep();
235 
236  m_curve->setCommandText("StepValue", QString("%0").arg(value3));
237 
238  QString CurveType = m_mixerUI->CurveType->currentText();
239 
240  QList<double> points;
241 
242  for (int i = 0; i < MixerCurveWidget::NODE_NUMELEM; i++) {
243  scale = ((double)i / (double)(MixerCurveWidget::NODE_NUMELEM - 1));
244 
245  if (CurveType.compare("Flat") == 0) {
246  points.append(value3);
247  }
248  if (CurveType.compare("Linear") == 0) {
249  newValue = value1 + (scale * (value2 - value1));
250  points.append(newValue);
251  }
252  if (CurveType.compare("Step") == 0) {
253  if (scale * 100 < value3) {
254  points.append(value1);
255  } else {
256  points.append(value2);
257  }
258  }
259  if (CurveType.compare("Exp") == 0) {
260  newValue = value1 + (((exp(scale * (value3 / 10)) - 1)) / (exp((value3 / 10)) - 1)
261  * (value2 - value1));
262  points.append(newValue);
263  }
264  if (CurveType.compare("Log") == 0) {
265  newValue = value1
266  + (((log(scale * (value3 * 2) + 1)) / (log(1 + (value3 * 2)))) * (value2 - value1));
267  points.append(newValue);
268  }
269  }
270 
271  setCurve(&points);
272 }
273 
278 {
279  m_curve->setCurve(points);
281 }
283 {
284  return m_curve->getCurve();
285 }
286 void MixerCurve::initLinearCurve(int numPoints, double maxValue, double minValue)
287 {
288  setMin(minValue);
289  setMax(maxValue);
290 
291  m_curve->initLinearCurve(numPoints, maxValue, minValue);
292 
293  if (m_spinDelegate)
294  m_spinDelegate->setRange(minValue, maxValue);
295 }
297 {
298  m_curve->setCurve(points);
300 }
301 void MixerCurve::setMin(double value)
302 {
303  // m_curve->setMin(value);
304  m_mixerUI->CurveMin->setMinimum(value);
305 }
307 {
308  return m_curve->getMin();
309 }
310 void MixerCurve::setMax(double value)
311 {
312  // m_curve->setMax(value);
313  m_mixerUI->CurveMax->setMaximum(value);
314 }
316 {
317  return m_curve->getMax();
318 }
319 double MixerCurve::setRange(double min, double max)
320 {
321  return m_curve->setRange(min, max);
322 }
323 
325 {
326  return m_mixerUI->CurveMin->value();
327 }
329 {
330  return m_mixerUI->CurveMax->value();
331 }
332 
334 {
335  return m_mixerUI->CurveStep->value();
336 }
337 
339 {
340  QList<double> points = m_curve->getCurve();
341  int ptCnt = points.count();
342 
343  for (int i = 0; i < ptCnt; i++) {
344  QTableWidgetItem *item = m_settings->item(i, 0);
345  if (item)
346  item->setText(QString().sprintf("%.2f", points.at((ptCnt - 1) - i)));
347  }
348 }
349 
350 void MixerCurve::SettingsTableChanged()
351 {
352  QList<double> points;
353 
354  for (int i = 0; i < m_settings->rowCount(); i++) {
355  QTableWidgetItem *item = m_settings->item(i, 0);
356 
357  if (item)
358  points.push_front(item->text().toDouble());
359  }
360 
361  m_mixerUI->CurveMin->setValue(points.first());
362  m_mixerUI->CurveMax->setValue(points.last());
363 
364  m_curve->setCurve(&points);
365 }
366 
367 void MixerCurve::CommandActivated(MixerNode *node)
368 {
369  QString name = (node) ? node->getName() : "Reset";
370 
371  if (name == "Reset") {
372  ResetCurve();
373  m_curve->showCommands(false);
374  } else if (name == "Commands") {
375 
376  } else if (name == "Popup") {
377  PopupCurve();
378  } else if (name == "Linear") {
379  m_mixerUI->CurveType->setCurrentIndex(m_mixerUI->CurveType->findText("Linear"));
380  } else if (name == "Log") {
381  m_mixerUI->CurveType->setCurrentIndex(m_mixerUI->CurveType->findText("Log"));
382  } else if (name == "Exp") {
383  m_mixerUI->CurveType->setCurrentIndex(m_mixerUI->CurveType->findText("Exp"));
384  } else if (name == "Flat") {
385  m_mixerUI->CurveType->setCurrentIndex(m_mixerUI->CurveType->findText("Flat"));
386  } else if (name == "Step") {
387  m_mixerUI->CurveType->setCurrentIndex(m_mixerUI->CurveType->findText("Step"));
388  } else if (name == "MinPlus") {
389  m_mixerUI->CurveMin->stepUp();
390  } else if (name == "MinMinus") {
391  m_mixerUI->CurveMin->stepDown();
392  } else if (name == "MaxPlus") {
393  m_mixerUI->CurveMax->stepUp();
394  } else if (name == "MaxMinus") {
395  m_mixerUI->CurveMax->stepDown();
396  } else if (name == "StepPlus") {
397  m_mixerUI->CurveStep->stepUp();
398  m_curve->setCommandText("StepValue", QString("%0").arg(getCurveStep()));
399  } else if (name == "StepMinus") {
400  m_mixerUI->CurveStep->stepDown();
401  m_curve->setCommandText("StepValue", QString("%0").arg(getCurveStep()));
402  }
403 
404  GenerateCurve();
405 }
406 
407 void MixerCurve::CurveTypeChanged()
408 {
409  // setup the ui for this curvetype
410  UpdateCurveUI();
411 }
412 
413 void MixerCurve::CurveMinChanged(double value)
414 {
415  QList<double> points = m_curve->getCurve();
416  points.removeFirst();
417  points.push_front(value);
418  setCurve(&points);
419 }
420 
421 void MixerCurve::CurveMaxChanged(double value)
422 {
423  // the max changed so redraw the curve
424  // mixercurvewidget::setCurve will trim any points above max
425  QList<double> points = m_curve->getCurve();
426  points.removeLast();
427  points.append(value);
428  setCurve(&points);
429 }
430 
431 void MixerCurve::showEvent(QShowEvent *event)
432 {
433  Q_UNUSED(event);
434 
435  m_settings->resizeColumnsToContents();
436  m_settings->setColumnWidth(0, (m_settings->width() - m_settings->verticalHeader()->width()));
437 
438  int h =
439  (m_settings->height() - m_settings->horizontalHeader()->height()) / m_settings->rowCount();
440  for (int i = 0; i < m_settings->rowCount(); i++)
441  m_settings->setRowHeight(i, h);
442 
443  m_curve->showEvent(event);
444 }
445 
446 void MixerCurve::resizeEvent(QResizeEvent *event)
447 {
448  m_settings->resizeColumnsToContents();
449  m_settings->setColumnWidth(0, (m_settings->width() - m_settings->verticalHeader()->width()));
450 
451  int h =
452  (m_settings->height() - m_settings->horizontalHeader()->height()) / m_settings->rowCount();
453  for (int i = 0; i < m_settings->rowCount(); i++)
454  m_settings->setRowHeight(i, h);
455 
456  m_curve->resizeEvent(event);
457 }
void setMin(double value)
Definition: mixercurve.cpp:301
void UpdateSettingsTable()
Definition: mixercurve.cpp:338
void initLinearCurve(int numPoints, double maxValue=1, double minValue=0)
Definition: mixercurve.cpp:286
void ResetCurve()
Definition: mixercurve.cpp:130
QList< double > getCurve()
void setMax(double value)
Definition: mixercurve.cpp:310
static const int NODE_NUMELEM
void showCommands(bool show)
void showEvent(QShowEvent *event)
Definition: mixercurve.cpp:431
void setCurve(const QList< double > *points)
double getMax()
Definition: mixercurve.cpp:315
for i
Definition: OPPlots.m:140
void activateCommand(const QString &name)
double getMin()
Definition: mixercurve.cpp:306
void showCommand(const QString &name, bool show)
double getCurveStep()
Definition: mixercurve.cpp:333
double getCurveMin()
Definition: mixercurve.cpp:324
double getCurveMax()
Definition: mixercurve.cpp:328
void PopupCurve()
Definition: mixercurve.cpp:143
void showEvent(QShowEvent *event)
void resizeEvent(QResizeEvent *event)
void initCurve(const QList< double > *points)
Definition: mixercurve.cpp:277
void setCurve(const QList< double > *points)
Definition: mixercurve.cpp:296
double setRange(double min, double max)
Definition: mixercurve.cpp:319
void initLinearCurve(int numPoints, double maxValue=1, double minValue=0)
double setRange(double min, double max)
void setCommandText(const QString &name, const QString &text)
void resizeEvent(QResizeEvent *event)
Definition: mixercurve.cpp:446
QList< double > getCurve()
Definition: mixercurve.cpp:282
MixerCurve(QWidget *parent=nullptr)
Definition: mixercurve.cpp:35
void setRange(double min, double max)
void setMixerType(MixerCurveType curveType, bool isCurve1=true)
Definition: mixercurve.cpp:90
void popUp(QWidget *widget=nullptr)
Definition: popupwidget.cpp:31
bool isCommandActive(const QString &name)
void commandActivated(MixerNode *node)
void GenerateCurve()
Definition: mixercurve.cpp:226