Fix remaining model editor sync issues with RawSource changes. (#5936)
[opentx.git] / companion / src / helpers.h
blob47cc12f7c43c42a855a6ce6eb8f140f843ea9350
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 void setWeight(int val);
71 signals:
72 void valueChanged();
74 protected slots:
75 void gvarCBChanged(int);
76 void valuesChanged();
78 protected:
79 QCheckBox *weightGV;
80 QAbstractSpinBox *weightSB;
81 QSpinBox *sb;
82 QDoubleSpinBox *dsb;
83 QComboBox *weightCB;
84 int & weight;
85 int deflt;
86 int mini;
87 int maxi;
88 double step;
89 bool lock;
92 #define HIDE_DIFF 0x01
93 #define HIDE_EXPO 0x02
94 #define HIDE_NEGATIVE_CURVES 0x04
96 class CurveGroup : public QObject {
98 Q_OBJECT
100 public:
101 CurveGroup(QComboBox *curveTypeCB, QCheckBox *curveGVarCB, QComboBox *curveValueCB, QSpinBox *curveValueSB, CurveReference & curve, const ModelData & model, unsigned int flags=0);
102 void update();
104 protected slots:
105 void gvarCBChanged(int);
106 void typeChanged(int);
107 void valuesChanged();
109 protected:
110 QComboBox *curveTypeCB;
111 QCheckBox *curveGVarCB;
112 QComboBox *curveValueCB;
113 QSpinBox *curveValueSB;
114 CurveReference & curve;
115 const ModelData & model;
116 unsigned int flags;
117 bool lock;
118 int lastType;
122 #define POPULATE_NONE (1<<0)
123 #define POPULATE_SOURCES (1<<1)
124 #define POPULATE_TRIMS (1<<2)
125 #define POPULATE_SWITCHES (1<<3)
126 #define POPULATE_GVARS (1<<4)
127 #define POPULATE_TELEMETRY (1<<5)
128 #define POPULATE_TELEMETRYEXT (1<<6)
129 #define POPULATE_VIRTUAL_INPUTS (1<<7)
130 #define POPULATE_SCRIPT_OUTPUTS (1<<8)
132 #define GVARS_VARIANT 0x0001
133 #define FRSKY_VARIANT 0x0002
135 namespace Helpers
137 void addRawSourceItems(QStandardItemModel * itemModel, const RawSourceType & type, int count, const GeneralSettings * const generalSettings = NULL,
138 const ModelData * const model = NULL, const int start = 0);
139 QStandardItemModel * getRawSourceItemModel(const GeneralSettings * const generalSettings = NULL, const ModelData * const model = NULL, unsigned int flags = 0);
141 void populateGvarUseCB(QComboBox *b, unsigned int phase);
142 void populateGVCB(QComboBox & b, int value, const ModelData & model);
143 QString getAdjustmentString(int16_t val, const ModelData * model = NULL, bool sign = false);
145 void populateFileComboBox(QComboBox * b, const QSet<QString> & set, const QString & current);
146 void getFileComboBoxValue(QComboBox * b, char * dest, int length);
147 } // namespace Helpers
149 // TODO : move to Helpers namespace
151 QString image2qstring(QImage image);
152 int findmult(float value, float base);
154 /* FrSky helpers */
155 QString getFrSkyAlarmType(int alarm);
156 QString getFrSkyUnits(int units);
158 void startSimulation(QWidget * parent, RadioData & radioData, int modelIdx);
160 // Format a pixmap to fit on the current firmware
161 QPixmap makePixMap(const QImage & image);
163 int version2index(const QString & version);
164 const QString index2version(int index);
166 class QTimeS : public QTime
168 public:
169 QTimeS(int s) { int h = s/3600; s %= 3600; int m = s/60; s %=60; setHMS(h, m, s); }
170 QTimeS(const QTime & q) : QTime(q) {}
171 int seconds() const { return hour()*3600 + minute()*60 + second(); }
174 bool qunlink(const QString & fileName);
176 QString generateProcessUniqueTempFileName(const QString & fileName);
177 bool isTempFileName(const QString & fileName);
179 QString getSoundsPath(const GeneralSettings &generalSettings);
180 QSet<QString> getFilesSet(const QString &path, const QStringList &filter, int maxLen);
182 bool caseInsensitiveLessThan(const QString &s1, const QString &s2);
185 class GpsCoord
187 public:
188 GpsCoord(): latitude(0), longitude(0) {}
189 double latitude; // Precede South latitudes and West longitudes with a minus sign. Latitudes range from -90 to 90.
190 double longitude; // Longitudes range from -180 to 180.
193 class GpsGlitchFilter
195 public:
196 GpsGlitchFilter() : lastValid(false), glitchCount(0) {}
197 bool isGlitch(GpsCoord coord);
199 private:
200 bool lastValid;
201 int glitchCount;
202 double lastLat;
203 double lastLon;
206 class GpsLatLonFilter
208 public:
209 GpsLatLonFilter() {}
210 bool isValid(GpsCoord coord);
212 private:
213 double lastLat;
214 double lastLon;
219 GpsCoord extractGpsCoordinates(const QString & position);
221 class TableLayout
223 public:
224 TableLayout(QWidget * parent, int rowCount, const QStringList & headerLabels);
225 // ~TableLayout() ;
227 void addWidget(int row, int column, QWidget * widget);
228 void addLayout(int row, int column, QLayout * layout);
230 void resizeColumnsToContents();
231 void setColumnWidth(int col, int width);
232 void pushRowsUp(int row);
234 private:
235 #if defined(TABLE_LAYOUT)
236 QTableWidget * tableWidget;
237 #else
238 QGridLayout * gridWidget;
239 #endif
243 class Stopwatch
245 public:
246 Stopwatch(const QString & name) :
247 name(name), total(0) {
248 timer.start();
250 ~Stopwatch() {}
252 void restart() {
253 total = 0;
254 timer.restart();
257 void report() {
258 qint64 elapsed = timer.restart();
259 total += elapsed;
260 qDebug() << name << QString("%1 ms [%2 ms]").arg(elapsed).arg(total);
263 void report(const QString & text) {
264 qint64 elapsed = timer.restart();
265 total += elapsed;
266 qDebug() << name << text << QString("%1 ms [%2 ms]").arg(elapsed).arg(total);
269 private:
270 QString name;
271 QElapsedTimer timer;
272 qint64 total;
275 extern Stopwatch gStopwatch;
277 #endif // _HELPERS_H_