dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
expocurve.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 "qwt/src/qwt_painter.h"
27 #include "expocurve.h"
28 
29 #include <QFrame>
30 
31 ExpoCurve::ExpoCurve(QWidget *parent)
32  : QwtPlot(parent)
33 {
34  QPalette palette = parent->palette();
35 
36  palette.setColor(QPalette::Foreground, palette.color(QPalette::Dark));
37  palette.setColor(QPalette::Background, palette.color(QPalette::Light));
38 
39  QwtPainter::setPolylineSplitting(false);
40  QwtPainter::setRoundingAlignment(false);
41  setMouseTracking(true);
42 
43  setMinimumSize(64, 64);
44  setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
45  setContentsMargins(6, 0, 0, 0);
46 
47  QFrame *f = qobject_cast<QFrame *>(canvas());
48  if (f) {
49  f->setFrameStyle(QFrame::Box | QFrame::Plain);
50  f->setLineWidth(1);
51  f->setPalette(palette);
52  }
53 
54  // Add grid lines
55  QwtPlotGrid *grid = new QwtPlotGrid;
56  grid->setMajorPen(QPen(Qt::gray, 0, Qt::DashLine));
57  grid->setMinorPen(QPen(Qt::lightGray, 0, Qt::DotLine));
58  grid->setPen(QPen(Qt::darkGray, 1, Qt::DotLine));
59  grid->attach(this);
60 
61  roll_elements.Curve.setRenderHint(QwtPlotCurve::RenderAntialiased);
62  roll_elements.Curve.setPen(QPen(QColor(41, 128, 185), 1.25, Qt::SolidLine, Qt::FlatCap));
63  roll_elements.Curve.attach(this);
64 
65  pitch_elements.Curve.setRenderHint(QwtPlotCurve::RenderAntialiased);
66  pitch_elements.Curve.setPen(QPen(QColor(192, 57, 43), 1.25, Qt::SolidLine, Qt::FlatCap));
67  pitch_elements.Curve.attach(this);
68 
69  yaw_elements.Curve.setRenderHint(QwtPlotCurve::RenderAntialiased);
70  yaw_elements.Curve.setPen(QPen(QColor(39, 174, 96), 1.25, Qt::SolidLine, Qt::FlatCap));
71  yaw_elements.Curve.attach(this);
72 
73  // legend
74  // Show a legend at the top
75  QwtLegend *m_legend = new QwtLegend(this);
76  m_legend->setDefaultItemMode(QwtLegendData::Checkable);
77  // m_legend->setFrameStyle(QFrame::Box | QFrame::Sunken);
78  m_legend->setToolTip(tr("Click legend to show/hide expo curve"));
79 
80  // connect signal when clicked on legend entry to function that shows/hides the curve
81  connect(m_legend, &QwtLegend::checked, this, &ExpoCurve::showCurve);
82 
83  QPalette pal = m_legend->palette();
84  pal.setColor(m_legend->backgroundRole(), QColor(100, 100, 100)); // background colour
85  pal.setColor(QPalette::Text, QColor(0, 0, 0)); // text colour
86  m_legend->setPalette(pal);
87  m_legend->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
88 
89  insertLegend(m_legend, QwtPlot::RightLegend);
90 
91  steps = 150;
92  x_data = new double[steps];
93  y_data = new double[steps];
94 
95  // double step = 2 * 1.0 / (steps - 1);
96  double step = 1.0 / (steps - 1);
97  for (int i = 0; i < steps; i++) {
98  // x_data[i] = (i * step) - 1.0;
99  x_data[i] = (i * step);
100  }
101 
102  // setup of the axis title
103  QwtText axis_title;
104 
105  // get the default font
106  QFont axis_title_font = this->axisTitle(yLeft).font();
107  // and only change the font size
108  axis_title_font.setPointSize(10);
109  axis_title.setFont(axis_title_font);
110 
111  axis_title.setText(tr("normalized stick input"));
112  this->setAxisTitle(QwtPlot::xBottom, axis_title);
113 
114  roll_elements.Curve.setTitle(tr("Roll"));
115  pitch_elements.Curve.setTitle(tr("Pitch"));
116  yaw_elements.Curve.setTitle(tr("Yaw"));
117 
118  axis_title.setText(tr("rate (deg/s)"));
119  this->setAxisTitle(QwtPlot::yLeft, axis_title);
120 
121  this->axisWidget(QwtPlot::xBottom)->setPalette(palette);
122  this->axisWidget(QwtPlot::yLeft)->setPalette(palette);
123 
124  plotDataRoll(50, 720, 50);
125  plotDataPitch(50, 720, 50);
126  plotDataYaw(50, 720, 30);
127 }
128 
139 void ExpoCurve::plotData(int value, int max, int exponent, ExpoPlotElements_t &plot_elements)
140 {
141  for (int i = 0; i < steps; i++) {
142  y_data[i] = max * (x_data[i] * ((100 - value) / 100.0)
143  + pow(x_data[i], exponent / 10.0f) * (value / 100.0));
144  }
145 
146  plot_elements.Curve.setSamples(x_data, y_data, steps);
147  plot_elements.Curve.show();
148 
149  // Surely there is a less costly way to do this!
150  setAxisAutoScale(yLeft, true);
151  updateAxes();
152  const QwtScaleDiv &div = axisScaleDiv(yLeft);
153  setAxisScale(yLeft, 0, div.upperBound(), 180);
154 
155  this->replot();
156 }
157 
163 void ExpoCurve::plotDataRoll(int value, int max, int exponent)
164 {
165  plotData(value, max, exponent, this->roll_elements);
166 }
167 
173 void ExpoCurve::plotDataPitch(int value, int max, int exponent)
174 {
175  plotData(value, max, exponent, this->pitch_elements);
176 }
177 
183 void ExpoCurve::plotDataYaw(int value, int max, int exponent)
184 {
185  plotData(value, max, exponent, this->yaw_elements);
186 }
187 
196 void ExpoCurve::showCurve(const QVariant &itemInfo, bool on, int index)
197 {
198  Q_UNUSED(index);
199  QwtPlotItem *item = QwtPlot::infoToItem(itemInfo);
200  if (item) {
201  item->setVisible(!on);
202  }
203 
204  replot();
205 }
void showCurve(const QVariant &itemInfo, bool on, int index)
Show/Hide a expo curve and markers.
Definition: expocurve.cpp:196
void plotData(int value, int max, int exponent, ExpoPlotElements_t &plot_elements)
Show expo data for one of the stick channels.
Definition: expocurve.cpp:139
void plotDataYaw(int value, int max, int exponent)
Show expo data for yaw.
Definition: expocurve.cpp:183
for i
Definition: OPPlots.m:140
void plotDataPitch(int value, int max, int exponent)
Show expo data for pitch.
Definition: expocurve.cpp:173
ExpoCurve(QWidget *parent=nullptr)
Definition: expocurve.cpp:31
void plotDataRoll(int value, int max, int exponent)
Show expo data for roll.
Definition: expocurve.cpp:163