[companion] Adjust GVAR not possible in global functions (fix #5425)
[opentx.git] / companion / src / helpers.h
blob18554287c7fe66e200d8348b1e5d70eb18b8d360
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #ifndef _HELPERS_H_
22 #define _HELPERS_H_
24 #include "eeprominterface.h"
25 #include <QCheckBox>
26 #include <QSpinBox>
27 #include <QTableWidget>
28 #include <QGridLayout>
29 #include <QDebug>
30 #include <QTime>
31 #include <QElapsedTimer>
32 #include <QStandardItemModel>
34 extern const QColor colors[CPN_MAX_CURVES];
36 #define TMR_NUM_OPTION (TMRMODE_COUNT+2*9+2*getCurrentFirmware()->getCapability(LogicalSwitches)-1)
38 //convert from mode 1 to mode generalSettings.stickMode
39 //NOTICE! => 1..4 -> 1..4
40 #define CONVERT_MODE(x) (((x)<=4) ? modn12x3[generalSettings.stickMode][((x)-1)] : (x))
42 #define CURVE_BASE 7
43 #define CH(x) (SRC_CH1+(x)-1-(SRC_SWC-SRC_3POS))
44 #define CV(x) (CURVE_BASE+(x)-1)
46 #define CURVE5(x) ((x)-1)
47 #define CURVE9(x) (MAX_CURVE5+(x)-1)
49 #define TRIM_ON 0
50 #define TRIM_OFF 1
51 #define TRIM_OFFSET 2
53 #define TRIM_MODE_NONE 0x1F // 0b11111
55 QString getTheme();
57 class CompanionIcon: public QIcon {
58 public:
59 CompanionIcon(const QString &baseimage);
62 class GVarGroup: public QObject {
64 Q_OBJECT
66 public:
67 GVarGroup(QCheckBox * weightGV, QAbstractSpinBox * weightSB, QComboBox * weightCB, int & weight, const ModelData & model, const int deflt, const int mini, const int maxi, const double step=1.0, bool allowGVars=true);
69 signals:
70 void valueChanged();
72 protected slots:
73 void gvarCBChanged(int);
74 void valuesChanged();
76 protected:
77 QCheckBox *weightGV;
78 QAbstractSpinBox *weightSB;
79 QSpinBox *sb;
80 QDoubleSpinBox *dsb;
81 QComboBox *weightCB;
82 int & weight;
83 double step;
84 bool lock;
87 #define HIDE_DIFF 0x01
88 #define HIDE_EXPO 0x02
89 #define HIDE_NEGATIVE_CURVES 0x04
91 class CurveGroup : public QObject {
93 Q_OBJECT
95 public:
96 CurveGroup(QComboBox *curveTypeCB, QCheckBox *curveGVarCB, QComboBox *curveValueCB, QSpinBox *curveValueSB, CurveReference & curve, const ModelData & model, unsigned int flags=0);
97 void update();
99 protected slots:
100 void gvarCBChanged(int);
101 void typeChanged(int);
102 void valuesChanged();
104 protected:
105 QComboBox *curveTypeCB;
106 QCheckBox *curveGVarCB;
107 QComboBox *curveValueCB;
108 QSpinBox *curveValueSB;
109 CurveReference & curve;
110 const ModelData & model;
111 unsigned int flags;
112 bool lock;
113 int lastType;
117 #define POPULATE_NONE (1<<0)
118 #define POPULATE_SOURCES (1<<1)
119 #define POPULATE_TRIMS (1<<2)
120 #define POPULATE_SWITCHES (1<<3)
121 #define POPULATE_GVARS (1<<4)
122 #define POPULATE_TELEMETRY (1<<5)
123 #define POPULATE_TELEMETRYEXT (1<<6)
124 #define POPULATE_VIRTUAL_INPUTS (1<<7)
125 #define POPULATE_SCRIPT_OUTPUTS (1<<8)
127 #define GVARS_VARIANT 0x0001
128 #define FRSKY_VARIANT 0x0002
130 namespace Helpers
132 void addRawSourceItems(QStandardItemModel * itemModel, const RawSourceType & type, int count, const GeneralSettings * const generalSettings = NULL,
133 const ModelData * const model = NULL, const int start = 0, const QList<int> exclude = QList<int>());
134 QStandardItemModel * getRawSourceItemModel(const GeneralSettings * const generalSettings = NULL, const ModelData * const model = NULL, unsigned int flags = 0);
136 void populateGvarUseCB(QComboBox *b, unsigned int phase);
137 void populateGVCB(QComboBox & b, int value, const ModelData & model);
138 QString getAdjustmentString(int16_t val, const ModelData * model = NULL, bool sign = false);
140 void populateFileComboBox(QComboBox * b, const QSet<QString> & set, const QString & current);
141 void getFileComboBoxValue(QComboBox * b, char * dest, int length);
142 } // namespace Helpers
144 // TODO : move to Helpers namespace
146 QString image2qstring(QImage image);
147 int findmult(float value, float base);
149 /* FrSky helpers */
150 QString getFrSkyAlarmType(int alarm);
151 QString getFrSkyUnits(int units);
152 QString getFrSkyProtocol(int protocol);
153 QString getFrSkyMeasure(int units);
154 QString getFrSkySrc(int index);
156 void startSimulation(QWidget * parent, RadioData & radioData, int modelIdx);
158 // Format a pixmap to fit on the current firmware
159 QPixmap makePixMap(const QImage & image);
161 int version2index(const QString & version);
162 const QString index2version(int index);
164 class QTimeS : public QTime
166 public:
167 QTimeS(int s) { int h = s/3600; s %= 3600; int m = s/60; s %=60; setHMS(h, m, s); }
168 QTimeS(const QTime & q) : QTime(q) {}
169 int seconds() const { return hour()*3600 + minute()*60 + second(); }
172 bool qunlink(const QString & fileName);
174 QString generateProcessUniqueTempFileName(const QString & fileName);
175 bool isTempFileName(const QString & fileName);
177 QString getSoundsPath(const GeneralSettings &generalSettings);
178 QSet<QString> getFilesSet(const QString &path, const QStringList &filter, int maxLen);
180 bool caseInsensitiveLessThan(const QString &s1, const QString &s2);
183 class GpsCoord
185 public:
186 GpsCoord(): latitude(0), longitude(0) {}
187 double latitude; // Precede South latitudes and West longitudes with a minus sign. Latitudes range from -90 to 90.
188 double longitude; // Longitudes range from -180 to 180.
191 class GpsGlitchFilter
193 public:
194 GpsGlitchFilter() : lastValid(false), glitchCount(0) {}
195 bool isGlitch(GpsCoord coord);
197 private:
198 bool lastValid;
199 int glitchCount;
200 double lastLat;
201 double lastLon;
204 class GpsLatLonFilter
206 public:
207 GpsLatLonFilter() {}
208 bool isValid(GpsCoord coord);
210 private:
211 double lastLat;
212 double lastLon;
217 GpsCoord extractGpsCoordinates(const QString & position);
219 class TableLayout
221 public:
222 TableLayout(QWidget * parent, int rowCount, const QStringList & headerLabels);
223 // ~TableLayout() ;
225 void addWidget(int row, int column, QWidget * widget);
226 void addLayout(int row, int column, QLayout * layout);
228 void resizeColumnsToContents();
229 void setColumnWidth(int col, int width);
230 void pushRowsUp(int row);
232 private:
233 #if defined(TABLE_LAYOUT)
234 QTableWidget * tableWidget;
235 #else
236 QGridLayout * gridWidget;
237 #endif
241 class Stopwatch
243 public:
244 Stopwatch(const QString & name) :
245 name(name), total(0) {
246 timer.start();
248 ~Stopwatch() {}
250 void restart() {
251 total = 0;
252 timer.restart();
255 void report() {
256 qint64 elapsed = timer.restart();
257 total += elapsed;
258 qDebug() << name << QString("%1 ms [%2 ms]").arg(elapsed).arg(total);
261 void report(const QString & text) {
262 qint64 elapsed = timer.restart();
263 total += elapsed;
264 qDebug() << name << text << QString("%1 ms [%2 ms]").arg(elapsed).arg(total);
267 private:
268 QString name;
269 QElapsedTimer timer;
270 qint64 total;
273 extern Stopwatch gStopwatch;
275 #endif // _HELPERS_H_