[Simulator] Dyanamic Simulator virtual hardware generation and many other improvements (#4260)

* Move simulator widgets to subfolder (no code changes).

* [Simulator] Major Simulator re-structuring to reduce code redundancies and allow for fully dynamic simulated hardware setup:
    SimulatorDialog is no longer sub-classed by individual radio UIs, instead a new class is added which handles just the "skin," LCD, and navigation controls (also makes setting up new models much quicker);
    All radio switches/knobs/sliders are populated dynamically based on loaded hardware configuration data (eeprom/bin), including control types and names (in case of no hardware config data, reasonable "factory defaults" are loaded);
    Starting Horus sim with alternate path for RADIO and MODEL data folders is now supported (.otx file is WIP);
    6-way switch now works properly and various other inconsistencies addressed (eg. some Horus analogs were backwards).

* Add missing OpenTX headers to new files.

* [Simulator] Fix loading of Horus hardware settings from Companion simulation.

* Update Travis CI build environment to Qt 5.7.

* [Simulator] Fix 9X missing colored side LCD images, an #include fix, and some cleanup.

* [TravisCI] Set up for parallel Travis builds and use Qt5.7 (#4263)

* Fix starting Simulator with non-existing file/folder structure (now properly creates new ones as needed, including Horus directories with default radio/model);  Temporarily remove option for using .otx file with Horus (until that feature is added).

* Load correct simulator skin for X7 when launching from Companion.

* X7 hardware defaults changed to match production hardware

* Fix minor compiler warning in mdichild.cpp, and some cleanup in eeprominterface.cpp.

* [Simulator] Centralize radio UI flavor determination.

* [Simulator] Updates:
    Add X7 UI flavor;
    Centralize LCD settings;
    Simplify drawing curved UI widgets;
    LCD color can be changed for any 1/4-bit display radio;
    Slight change on default LCD "off" color;
    Refactor LcdWidget to not use globals.

* [Companion] X7 EEPROM import fix

+ cosmetics
+ compilation error fixed on Manjaro

* [Companion] 9XR-PRO EEPROM import fix

+ cosmetics
+ compilation error fixed on Manjaro

* Navigation fix
French translations

* [X7] Main view cosmetics

* Fix switch/ana custom name import

* [Simulator] Add white border above/below X7 bezel, and use white-colored padding on sides; Adjust X7 default LCD color.

* Does this work? (#4269)

Read an Horus SD card produces a temporary .otx file now

* Bsongis/companion warning before automatic conversions (#4271)

* Does this work?

* Warning before automatic conversions

* Force different file name after conversion (#4272)

* Force different file name after conversion

* More detailed instructions

* [Companion] Files automatically named during XXX => Horus conversions
This commit is contained in:
Max Paperno
2017-01-24 07:54:12 +01:00
committed by Bertrand Songis
parent b3f4870da7
commit 17a03e6c18
43 changed files with 3561 additions and 5934 deletions
@@ -0,0 +1,139 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef _BUTTONSWIDGET_H_
#define _BUTTONSWIDGET_H_
#include "radiouiaction.h"
#include <QWidget>
#include <QtGui>
#include <QStyleOption>
class ButtonsWidget : public QWidget
{
Q_OBJECT
public:
explicit ButtonsWidget(QWidget * parent):
QWidget(parent)
{
}
virtual void setStyleSheet(const QString & sheet)
{
defaultStyleSheet = sheet;
QWidget::setStyleSheet(sheet);
}
void addArea(int x1, int y1, int x2, int y2, const char * image, RadioUiAction * action = NULL)
{
QPolygon polygon;
polygon.setPoints(4, x1, y1, x1, y2, x2, y2, x2, y1);
addArea(polygon, image, action);
}
void addArea(const QPolygon & polygon, const char * image, RadioUiAction * action = NULL)
{
areas.push_back(Area(polygon, image, action));
}
protected:
class Area
{
public:
Area(const QPolygon & polygon, const QString &image, RadioUiAction * action = NULL):
polygon(polygon),
imgFile(image),
action(action)
{
}
bool contains(int x, int y)
{
return polygon.containsPoint(QPoint(x, y), Qt::OddEvenFill);
}
QPolygon polygon;
QString imgFile;
RadioUiAction * action;
};
void setBitmap(QString bitmap)
{
if (!bitmap.isEmpty()) {
QWidget::setStyleSheet(QString("background:url(:/images/simulator/%1);").arg(bitmap));
}
else {
QWidget::setStyleSheet(defaultStyleSheet);
}
}
void onMouseButtonEvent(bool press, QMouseEvent * event)
{
bool trig;
QString img = "";
int x = event->x();
int y = event->y();
foreach(Area area, areas) {
trig = false;
if (press && event->button() == Qt::LeftButton && area.contains(x, y)) {
img = area.imgFile;
setFocus();
trig = true;
}
if (area.action)
area.action->trigger(trig);
}
setBitmap(img);
}
virtual void mousePressEvent(QMouseEvent * event)
{
onMouseButtonEvent(true, event);
}
virtual void mouseReleaseEvent(QMouseEvent * event)
{
onMouseButtonEvent(false, event);
}
virtual void mouseLeaveEvent(QMouseEvent * event)
{
onMouseButtonEvent(false, event);
}
void paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
protected:
QList<Area> areas;
QString defaultStyleSheet;
};
#endif // _BUTTONSWIDGET_H_
@@ -0,0 +1,207 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef _LCDWIDGET_H_
#define _LCDWIDGET_H_
#include <QApplication>
#include <QWidget>
#include <QPainter>
#include <QClipboard>
#include <QDir>
#include <QDebug>
#include "appdata.h"
class LcdWidget : public QWidget
{
Q_OBJECT
public:
LcdWidget(QWidget * parent = 0):
QWidget(parent),
lcdBuf(NULL),
previousBuf(NULL),
lightEnable(false),
bgDefaultColor(QColor(198, 208, 199))
{
}
~LcdWidget()
{
if (previousBuf) {
free(previousBuf);
}
}
void setData(unsigned char *buf, int width, int height, int depth=1)
{
lcdBuf = buf;
lcdWidth = width;
lcdHeight = height;
lcdDepth = depth;
if (depth >= 8)
lcdSize = (width * height) * ((depth+7) / 8);
else
lcdSize = (width * ((height+7)/8)) * depth;
previousBuf = (unsigned char *)malloc(lcdSize);
memset(previousBuf, 0, lcdSize);
}
void setBgDefaultColor(QColor color)
{
bgDefaultColor = color;
}
void setBackgroundColor(QColor color)
{
_r = color.red();
_g = color.green();
_b = color.blue();
}
void makeScreenshot(const QString & fileName)
{
int width, height;
if (lcdDepth < 12) {
width = 2 * lcdWidth;
height = 2 * lcdHeight;
}
else {
width = lcdWidth;
height = lcdHeight;
}
QPixmap buffer(width, height);
QPainter p(&buffer);
doPaint(p);
if (fileName.isEmpty()) {
qDebug() << "Screenshot saved to clipboard";
QApplication::clipboard()->setPixmap( buffer );
}
else {
qDebug() << "Screenshot" << fileName;
buffer.toImage().save(fileName);
}
}
void onLcdChanged(bool light)
{
if (light != lightEnable || memcmp(previousBuf, lcdBuf, lcdSize)) {
lightEnable = light;
memcpy(previousBuf, lcdBuf, lcdSize);
update();
}
}
protected:
int lcdWidth;
int lcdHeight;
int lcdDepth;
int lcdSize;
unsigned char *lcdBuf;
unsigned char *previousBuf;
bool lightEnable;
int _r, _g, _b;
QColor bgDefaultColor;
inline void doPaint(QPainter & p)
{
QRgb rgb;
if (lcdDepth == 16) {
for (int x=0; x<lcdWidth; x++) {
for (int y=0; y<lcdHeight; y++) {
uint16_t z = ((uint16_t *)lcdBuf)[y * lcdWidth + x];
rgb = qRgb(255*((z&0xF800)>>11)/0x1F, 255*((z&0x07E0)>>5)/0x3F, 255*(z&0x001F)/0x1F);
p.setPen(rgb);
p.drawPoint(x, y);
}
}
}
else if (lcdDepth == 12) {
for (int x=0; x<lcdWidth; x++) {
for (int y=0; y<lcdHeight; y++) {
uint16_t z = ((uint16_t *)lcdBuf)[y * lcdWidth + x];
rgb = qRgb(255*((z&0xF00)>>8)/0x0f, 255*((z&0x0F0)>>4)/0x0f, 255*(z&0x00F)/0x0f);
p.setPen(rgb);
p.drawPoint(x, y);
}
}
}
else {
if (lightEnable)
rgb = qRgb(_r, _g, _b);
else
rgb = bgDefaultColor.rgba();
p.setBackground(QBrush(rgb));
p.eraseRect(0, 0, 2*lcdWidth, 2*lcdHeight);
if (lcdBuf) {
if (lcdDepth == 1) {
rgb = qRgb(0, 0, 0);
p.setPen(rgb);
p.setBrush(QBrush(rgb));
}
unsigned int previousDepth = 0xFF;
for (int y=0; y<lcdHeight; y++) {
unsigned int idx = (y*lcdDepth/8)*lcdWidth;
unsigned int mask = (1 << (y%8));
for (int x=0; x<lcdWidth; x++, idx++) {
if (lcdDepth == 1) {
if (lcdBuf[idx] & mask) {
p.drawRect(2*x, 2*y, 1, 1);
}
}
else {
unsigned int z = (y & 1) ? (lcdBuf[idx] >> 4) : (lcdBuf[idx] & 0x0F);
if (z) {
if (z != previousDepth) {
previousDepth = z;
if (lightEnable)
rgb = qRgb(_r-(z*_r)/15, _g-(z*_g)/15, _b-(z*_b)/15);
else
rgb = qRgb(161-(z*161)/15, 161-(z*161)/15, 161-(z*161)/15);
p.setPen(rgb);
p.setBrush(QBrush(rgb));
}
p.drawRect(2*x, 2*y, 1, 1);
}
}
}
}
}
}
}
void paintEvent(QPaintEvent*)
{
QPainter p(this);
doPaint(p);
}
};
#endif // _LCDWIDGET_H_
@@ -0,0 +1,69 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef _RADIOFADERWIDGET_H_
#define _RADIOFADERWIDGET_H_
#include "radiowidget.h"
#include "sliderwidget.h"
class RadioFaderWidget : public RadioWidget
{
Q_OBJECT
public:
explicit RadioFaderWidget(QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
RadioWidget(parent, f)
{
init();
}
explicit RadioFaderWidget(const QString &labelText, int value = 0, QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
RadioWidget(labelText, value, parent, f)
{
init();
}
void init()
{
m_type = RADIO_WIDGET_FADER;
SliderWidget * sl = new SliderWidget(this);
sl->setOrientation(Qt::Vertical);
sl->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
sl->setMinimumHeight(75);
//sl->setMaximumHeight(75);
sl->setTickPosition(QSlider::TicksBothSides);
sl->setPageStep(128);
sl->setMinimum(-1024);
sl->setMaximum(1024);
sl->setTickInterval(65);
connect(sl, &SliderWidget::valueChanged, this, &RadioWidget::setValue);
connect(this, &RadioWidget::valueChanged, sl, &QSlider::setValue);
setWidget(sl);
}
};
#endif // _RADIOFADERWIDGET_H_
@@ -0,0 +1,127 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef _RADIOKNOBWIDGET_H_
#define _RADIOKNOBWIDGET_H_
#include "radiowidget.h"
#include "radiodata.h"
#include <QDial>
#include <QMouseEvent>
#include <QDebug>
#include <math.h>
class RadioKnobWidget : public RadioWidget
{
Q_OBJECT
public:
using potType_t = GeneralSettings::PotConfig;
explicit RadioKnobWidget(potType_t type = potType_t::POT_WITH_DETENT, QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
RadioWidget(parent, f),
m_potType(type)
{
init();
}
explicit RadioKnobWidget(potType_t type, const QString &labelText, int value = 0, QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
RadioWidget(labelText, value, parent, f),
m_potType(type)
{
init();
}
void init()
{
m_type = RADIO_WIDGET_KNOB;
KnobWidget * pot = new KnobWidget(m_potType, this);
pot->setValue(m_value);
connect(pot, &KnobWidget::valueChanged, this, &RadioWidget::setValue);
connect(this, &RadioWidget::valueChanged, pot, &KnobWidget::setValue);
setWidget(pot);
}
private:
potType_t m_potType;
class KnobWidget : public QDial
{
friend class RadioKnobWidget;
using potType_t = RadioKnobWidget::potType_t;
public:
explicit KnobWidget(potType_t type, QWidget * parent = 0):
QDial(parent),
m_type(type),
m_positions(type == potType_t::POT_MULTIPOS_SWITCH ? 5 : 0)
{
setToolTip(tr("Right-double-click to reset to center."));
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
setFixedSize(QSize(42, 42));
setMinimum(-1024);
setMaximum(1024);
setNotchesVisible(true);
if (m_positions) {
// this is a bit of a hack to get the notch markers to display correctly
// the actual notches/value are constrained in setValue()
setSingleStep(2048 / m_positions / 10);
setPageStep(2048 / m_positions);
setNotchTarget(5.7);
}
else {
setPageStep(128);
setNotchTarget(64);
}
}
void setValue(int value)
{
if (m_positions) {
value = ((int)roundf((value + 1024) / (2048.0f / m_positions)) * (2048.0f / m_positions) - 1024);
}
QDial::setValue(value);
}
void mousePressEvent(QMouseEvent * event)
{
if (event->button() == Qt::RightButton && event->type() == QEvent::MouseButtonDblClick) {
setValue(0);
event->accept();
return;
}
QDial::mousePressEvent(event);
}
potType_t m_type;
quint8 m_positions;
};
};
#endif // _RADIOKNOBWIDGET_H_
@@ -0,0 +1,80 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef _RADIOSWITCHWIDGET_H_
#define _RADIOSWITCHWIDGET_H_
#include "radiowidget.h"
#include "radiodata.h"
#include <QSlider>
#include <QDebug>
class RadioSwitchWidget : public RadioWidget
{
Q_OBJECT
public:
typedef GeneralSettings::SwitchConfig swType_t;
explicit RadioSwitchWidget(swType_t type = swType_t::SWITCH_3POS, QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
RadioWidget(parent, f),
swType(type)
{
init();
}
explicit RadioSwitchWidget(swType_t type, const QString &labelText, int value = 0, QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
RadioWidget(labelText, value, parent, f),
swType(type)
{
init();
}
void init()
{
m_type = RADIO_WIDGET_SWITCH;
QSlider * sl = new QSlider();
sl->setOrientation(Qt::Vertical);
sl->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
sl->setFixedHeight(50);
sl->setInvertedAppearance(true);
sl->setInvertedControls(true);
sl->setTickPosition(QSlider::TicksBothSides);
sl->setPageStep(1);
sl->setMinimum((swType == swType_t::SWITCH_3POS ? -1 : 0));
sl->setMaximum(1);
sl->setTickInterval(1);
sl->setValue(m_value);
connect(sl, &QSlider::valueChanged, this, &RadioWidget::setValue);
connect(this, &RadioWidget::valueChanged, sl, &QSlider::setValue);
setWidget(sl);
}
private:
swType_t swType;
};
#endif // _RADIOSWITCHWIDGET_H_
@@ -0,0 +1,166 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef _RADIOWIDGET_H_
#define _RADIOWIDGET_H_
#include <QWidget>
#include <QLabel>
#include <QGridLayout>
class RadioWidget : public QWidget
{
Q_OBJECT
public:
enum RadioWidgetType { RADIO_WIDGET_NONE, RADIO_WIDGET_SWITCH, RADIO_WIDGET_KNOB, RADIO_WIDGET_FADER };
explicit RadioWidget(QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
QWidget(parent, f),
m_value(0),
m_invertValue(false),
m_showLabel(false),
m_labelText(""),
m_type(RADIO_WIDGET_NONE)
{
init();
}
explicit RadioWidget(const QString &labelText, int value = 0, QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags()) :
QWidget(parent, f),
m_value(value),
m_invertValue(false),
m_showLabel(true),
m_labelText(labelText),
m_type(RADIO_WIDGET_NONE)
{
init();
}
void setIndex(int index) { m_index = index; }
void setInvertValue(bool invertValue) { m_invertValue = invertValue; }
void setValue(int value)
{
if (value != m_value) {
m_value = value;
emit valueChanged(m_value);
}
}
void setShowLabel(bool show)
{
if (show != m_showLabel) {
m_showLabel = show;
addLabel();
}
}
void setLabelText(const QString &labelText, bool showLabel = true)
{
m_labelText = labelText;
m_showLabel = showLabel;
addLabel();
}
void changeVisibility(bool visible)
{
static QSizePolicy oldSP = sizePolicy();
setVisible(visible);
if (visible) {
setSizePolicy(oldSP);
}
else {
oldSP = sizePolicy();
setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
}
}
int getValue() const { return m_value * (m_invertValue ? -1 : 1); }
int getIndex() const { return m_index; }
int getType() const { return m_type; }
protected:
void init()
{
setIndex(0);
m_controlWidget = NULL;
m_nameLabel = NULL;
m_gridLayout= new QGridLayout(this);
m_gridLayout->setContentsMargins(0, 0, 0, 0);
m_gridLayout->setVerticalSpacing(4);
m_gridLayout->setHorizontalSpacing(0);
addLabel();
}
void addLabel()
{
if (m_nameLabel) {
m_gridLayout->removeWidget(m_nameLabel);
m_nameLabel->deleteLater();
}
if (m_showLabel && !m_labelText.isEmpty()) {
m_nameLabel = new QLabel(m_labelText, this);
m_nameLabel->setAlignment(Qt::AlignCenter);
m_nameLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
m_gridLayout->addWidget(m_nameLabel, 1, 0, 1, 1, Qt::AlignTop);
m_gridLayout->setRowStretch(1, 0);
}
}
void setWidget(QWidget * widget = NULL)
{
if (m_controlWidget) {
m_gridLayout->removeWidget(m_controlWidget);
m_controlWidget->deleteLater();
}
m_controlWidget = widget;
if (widget) {
m_gridLayout->addWidget(widget, 0, 0, 1, 1, Qt::AlignHCenter);
m_gridLayout->setRowStretch(0, 1);
}
}
int m_value;
int m_index;
bool m_invertValue;
bool m_showLabel;
QString m_labelText;
RadioWidgetType m_type;
private:
QGridLayout * m_gridLayout;
QWidget * m_controlWidget;
QLabel * m_nameLabel;
signals:
void valueChanged(int m_value);
};
#endif // _RADIOWIDGET_H_
@@ -0,0 +1,51 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef _SLIDERWIDGET_H_
#define _SLIDERWIDGET_H_
#include <QtGui>
#include <QSlider>
class SliderWidget : public QSlider
{
Q_OBJECT
public:
explicit SliderWidget(QWidget * parent = 0):
QSlider(parent)
{
setToolTip(tr("Right-double-click to reset to center."));
}
protected:
void mousePressEvent(QMouseEvent * event)
{
if (event->button() == Qt::RightButton && event->type() == QEvent::MouseButtonDblClick) {
setValue(0);
event->accept();
}
QSlider::mousePressEvent(event);
}
};
#endif // _SLIDERWIDGET_H_
@@ -0,0 +1,496 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "virtualjoystickwidget.h"
#include "constants.h"
#include "sliderwidget.h"
#include "modeledit/node.h"
#include "helpers.h"
VirtualJoystickWidget::VirtualJoystickWidget(QWidget *parent, QChar side, bool showTrims, bool showBtns, bool showValues, QSize size) :
QWidget(parent),
stickSide(side),
prefSize(size),
hTrimSlider(NULL),
vTrimSlider(NULL),
btnHoldX(NULL),
btnHoldY(NULL),
btnFixX(NULL),
btnFixY(NULL),
nodeLabelX(NULL),
nodeLabelY(NULL)
{
ar = (float)size.width() / size.height();
extraSize = QSize(0, 0);
// 5 col x 4 rows
layout = new QGridLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
setSizePolicy(sizePolicy);
gv = new QGraphicsView(this);
gv->setSizePolicy(sizePolicy);
gv->setMinimumSize(QSize(150, 150));
gv->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
gv->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scene = new QGraphicsScene(gv);
scene->setItemIndexMethod(QGraphicsScene::NoIndex);
gv->setScene(scene);
node = new Node();
node->setPos(-GBALL_SIZE / 2, -GBALL_SIZE / 2);
node->setBallSize(GBALL_SIZE);
scene->addItem(node);
int colvt, colbb, colvx, colvy;
if (stickSide == 'L') {
colvt = 3;
colbb = 1;
colvx = 1;
colvy = 3;
}
else {
colvt = 1;
colbb = 3;
colvx = 3;
colvy = 1;
}
if (showTrims) {
QWidget * hTrimWidget = createTrimWidget('H');
QWidget * vTrimWidget = createTrimWidget('V');
layout->addWidget(vTrimWidget, 1, colvt, 1, 1);
layout->addWidget(hTrimWidget, 2, 2, 1, 1);
hTrimSlider = hTrimWidget->findChild<SliderWidget *>();
vTrimSlider = vTrimWidget->findChild<SliderWidget *>();
extraSize += QSize(vTrimWidget->sizeHint().width(), hTrimWidget->sizeHint().height());
}
if (showBtns) {
QVBoxLayout * btnbox = new QVBoxLayout();
btnbox->setContentsMargins(9, 9, 9, 9);
btnbox->setSpacing(2);
btnbox->addWidget(btnHoldY = createButtonWidget(HOLD_Y));
btnbox->addWidget(btnFixY = createButtonWidget(FIX_Y));
btnbox->addWidget(btnFixX = createButtonWidget(FIX_X));
btnbox->addWidget(btnHoldX = createButtonWidget(HOLD_X));
layout->addLayout(btnbox, 1, colbb, 1, 1);
extraSize.setWidth(extraSize.width() + btnbox->sizeHint().width());
}
if (showValues) {
QLayout * valX = createNodeValueLayout('X', nodeLabelX);
QLayout * valY = createNodeValueLayout('Y', nodeLabelY);
layout->addLayout(valX, 2, colvx, 1, 1);
layout->addLayout(valY, 2, colvy, 1, 1);
}
layout->addItem(new QSpacerItem(0, 0), 0, 0, 1, 5); // r0 c0-4: top v spacer
layout->addItem(new QSpacerItem(0, 0), 1, 0, 2, 1); // r1-2 c0: left h spacer
layout->addWidget(gv, 1, 2, 1, 1); // r1 c2: stick widget
layout->addItem(new QSpacerItem(0, 0), 1, 4, 2, 1); // r1-2 c4: right h spacer
layout->addItem(new QSpacerItem(0, 0), 3, 0, 1, 5); // r3 c0-4: bot v spacer
connect(node, SIGNAL(xChanged()), this, SLOT(updateNodeValueLabels()));
connect(node, SIGNAL(yChanged()), this, SLOT(updateNodeValueLabels()));
setSize(prefSize);
}
void VirtualJoystickWidget::setStickX(qreal x)
{
node->setX(x);
}
void VirtualJoystickWidget::setStickY(qreal y)
{
node->setY(y);
}
void VirtualJoystickWidget::setStickPos(QPointF xy)
{
node->setPos(xy);
}
void VirtualJoystickWidget::centerStick()
{
node->stepToCenter();
}
qreal VirtualJoystickWidget::getStickX()
{
return node->getX();
}
qreal VirtualJoystickWidget::getStickY()
{
return node->getY();
}
QPointF VirtualJoystickWidget::getStickPos()
{
return QPointF(node->getX(), node->getY());
}
void VirtualJoystickWidget::setTrimValue(int which, int value)
{
SliderWidget * slider = getTrimSlider(which);
if (slider) {
slider->setValue(value);
}
}
void VirtualJoystickWidget::setTrimRange(int which, int min, int max)
{
SliderWidget * slider = getTrimSlider(which);
if (slider) {
slider->setRange(min, max);
}
}
int VirtualJoystickWidget::getTrimValue(int which)
{
SliderWidget * slider = getTrimSlider(which);
if (slider) {
return slider->value();
}
return 0;
}
void VirtualJoystickWidget::setStickConstraint(int which, bool active)
{
if (btnHoldX == NULL)
return; // no buttons
switch (which) {
case HOLD_X:
btnHoldX->setChecked(active);
break;
case HOLD_Y:
btnHoldY->setChecked(active);
break;
case FIX_X:
btnFixX->setChecked(active);
break;
case FIX_Y:
btnFixY->setChecked(active);
break;
default:
break;
}
}
void VirtualJoystickWidget::setSize(QSize size)
{
float thisAspectRatio = (float)size.width() / size.height();
float newGvSz, spacerSz;
bool sized = false;
if (thisAspectRatio > ar) {
// constrain width
newGvSz = (height() - extraSize.height()) * ar; // new width
spacerSz = (width() - newGvSz - extraSize.width()) / 2; // + 0.5;
if (spacerSz >= 0.0f) {
layout->setRowStretch(0, 0);
layout->setColumnStretch(0, spacerSz);
layout->setColumnStretch(4, spacerSz);
layout->setRowStretch(3, 0);
sized = true;
}
}
if (!sized) {
// constrain height
newGvSz = (width() - extraSize.width()) * ar; // new height
spacerSz = (height() - newGvSz - extraSize.height()) / 2; // + 0.5;
spacerSz = qMax(spacerSz, 0.0f);
layout->setRowStretch(0, spacerSz);
layout->setColumnStretch(0, 0);
layout->setColumnStretch(4, 0);
layout->setRowStretch(3, spacerSz);
}
layout->setColumnStretch(2, newGvSz);
layout->setRowStretch(1, newGvSz);
prefSize = QSize(newGvSz + extraSize.width(), newGvSz + extraSize.height());
gv->resize(newGvSz, newGvSz);
gv->updateGeometry();
repositionNode();
//qDebug() << thisAspectRatio << size << newGvSz << spacerSz << extraSize << gv->geometry() << gv->contentsRect() << gv->frameRect() << getStickPos();
}
void VirtualJoystickWidget::repositionNode()
{
QRect qr = gv->contentsRect();
qreal w = (qreal)qr.width() - GBALL_SIZE;
qreal h = (qreal)qr.height() - GBALL_SIZE;
qreal cx = (qreal)qr.width()/2;
qreal cy = (qreal)qr.height()/2;
scene->setSceneRect(-cx,-cy,w,h);
QPointF p = node->pos();
p.setX(qMin(cx, qMax(p.x(), -cx)));
p.setY(qMin(cy, qMax(p.y(), -cy)));
node->setPos(p);
updateNodeValueLabels();
}
QWidget *VirtualJoystickWidget::createTrimWidget(QChar type)
{
QSizePolicy sp;
QString btnAlabel, btnBlabel;
QString btnAname = QString("%1TrimBtnA_%2").arg(type.toLower()).arg(stickSide);
QString btnBname = QString("%1TrimBtnB_%2").arg(type.toLower()).arg(stickSide);
QString sliderName = QString("%1TrimAdj_%2").arg(type.toLower()).arg(stickSide);
QWidget * trimWidget = new QWidget(this);
QBoxLayout * trimLayout = new QVBoxLayout(trimWidget);
SliderWidget * trimSlider = new SliderWidget(trimWidget);
QPushButton * trimBtnA = new QPushButton(trimWidget);
QPushButton * trimBtnB = new QPushButton(trimWidget);
if (type == 'H') {
sp = QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
trimLayout->setDirection(QBoxLayout::LeftToRight);
trimSlider->setFixedHeight(23);
trimSlider->setOrientation(Qt::Horizontal);
trimBtnA->setText(ARROW_LEFT);
trimBtnB->setText(ARROW_RIGHT);
}
else {
sp = QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
trimLayout->setDirection(QBoxLayout::TopToBottom);
trimSlider->setFixedWidth(23);
trimSlider->setOrientation(Qt::Vertical);
trimBtnA->setText(ARROW_UP);
trimBtnB->setText(ARROW_DOWN);
}
trimWidget->setSizePolicy(sp);
trimSlider->setObjectName(sliderName);
trimSlider->setProperty("trimType", getTrimSliderType(type));
trimSlider->setSizePolicy(sp);
trimSlider->setMinimum(-125);
trimSlider->setMaximum(125);
trimBtnA->setObjectName(btnAname);
trimBtnA->setProperty("btnType", getTrimButtonType(type, 0));
trimBtnA->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
trimBtnA->setMaximumSize(QSize(23, 23));
trimBtnA->setAutoDefault(false);
trimBtnB->setObjectName(btnBname);
trimBtnB->setProperty("btnType", getTrimButtonType(type, 1));
trimBtnB->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
trimBtnB->setMaximumSize(QSize(23, 23));
trimBtnB->setAutoDefault(false);
trimLayout->setSpacing(6);
trimLayout->setContentsMargins(8, 9, 8, 9);
trimLayout->addWidget(trimBtnA);
trimLayout->addWidget(trimSlider);
trimLayout->addWidget(trimBtnB);
connect(trimBtnA, SIGNAL(pressed()), SLOT(onTrimPressed()));
connect(trimBtnB, SIGNAL(pressed()), SLOT(onTrimPressed()));
connect(trimBtnA, SIGNAL(released()), SIGNAL(trimButtonReleased()));
connect(trimBtnB, SIGNAL(released()), SIGNAL(trimButtonReleased()));
connect(trimSlider, SIGNAL(valueChanged(int)), SLOT(onSliderChange(int)));
return trimWidget;
}
QPushButton * VirtualJoystickWidget::createButtonWidget(int type)
{
QString btnRole, btnLabel;
switch (type) {
case HOLD_Y:
btnLabel = tr("Hold Y");
break;
case FIX_Y:
btnLabel = tr("Fix Y");
break;
case FIX_X:
btnLabel = tr("Fix X");
break;
case HOLD_X:
default:
btnLabel = tr("Hold X");
break;
}
QPushButton * btn = new QPushButton(this);
btn->setObjectName(QString("%1_%2").arg(btnLabel.replace(" ", "_")).arg(stickSide));
btn->setProperty("btnType", type);
btn->setText(btnLabel);
QFont font;
font.setPointSize(8);
btn->setFont(font);
btn->setStyleSheet(QLatin1String( \
"QPushButton {"
" background-color: #EEEEEE;"
" border-style: outset;"
" border-width: 1px;"
" border-radius: 4px;"
" border-color: black;"
" padding: 2px;"
"}"
"QPushButton:checked {\n"
" background-color: #4CC417;\n"
" border-style: inset;\n"
"}" ));
btn->setCheckable(true);
connect(btn, SIGNAL(toggled(bool)), SLOT(onButtonChange(bool)));
return btn;
}
QLayout *VirtualJoystickWidget::createNodeValueLayout(QChar type, QLabel *& valLabel)
{
QLabel * lbl = new QLabel(QString("%1 %").arg(type), this);
lbl->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
lbl->setAlignment(Qt::AlignCenter);
QLabel * val = new QLabel("0");
val->setObjectName(QString("val_%1").arg(type));
val->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
val->setAlignment(Qt::AlignCenter);
QVBoxLayout * layout = new QVBoxLayout();
layout->setContentsMargins(2, 2, 2, 2);
layout->setSpacing(2);
layout->addWidget(lbl);
layout->addWidget(val);
valLabel = val;
return layout;
}
int VirtualJoystickWidget::getTrimSliderType(QChar type)
{
if (stickSide == 'L') {
if (type == 'H')
return TRIM_AXIS_L_X;
else
return TRIM_AXIS_L_Y;
}
else {
if (type == 'H')
return TRIM_AXIS_R_X;
else
return TRIM_AXIS_R_Y;
}
}
int VirtualJoystickWidget::getTrimButtonType(QChar type, int pos)
{
if (stickSide == 'L') {
if (type == 'H') {
if (pos == 0)
return TRIM_LH_L;
else
return TRIM_LH_R;
}
else {
if (pos == 0)
return TRIM_LV_UP;
else
return TRIM_LV_DN;
}
}
// right side
else {
if (type == 'H') {
if (pos == 0)
return TRIM_RH_L;
else
return TRIM_RH_R;
}
else {
if (pos == 0)
return TRIM_RV_UP;
else
return TRIM_RV_DN;
}
}
}
SliderWidget *VirtualJoystickWidget::getTrimSlider(int which)
{
if (which == TRIM_AXIS_L_X || which == TRIM_AXIS_R_X)
return hTrimSlider;
else
return vTrimSlider;
}
void VirtualJoystickWidget::onTrimPressed()
{
if (!sender() || !sender()->property("btnType").isValid())
return;
emit trimButtonPressed(sender()->property("btnType").toInt());
}
void VirtualJoystickWidget::onSliderChange(int value)
{
if (!sender() || !sender()->property("trimType").isValid())
return;
emit trimSliderMoved(sender()->property("trimType").toInt(), value);
updateNodeValueLabels();
}
void VirtualJoystickWidget::onButtonChange(bool checked)
{
if (!sender() || !sender()->property("btnType").isValid())
return;
switch (sender()->property("btnType").toInt()) {
case HOLD_Y:
node->setCenteringY(!checked);
break;
case FIX_Y:
node->setFixedY(checked);
break;
case FIX_X:
node->setFixedX(checked);
break;
case HOLD_X:
node->setCenteringX(!checked);
break;
}
}
void VirtualJoystickWidget::updateNodeValueLabels()
{
if (nodeLabelX)
nodeLabelX->setText(QString("%1").arg((qreal)node->getX() * 100 + getTrimValue(0) / 5, 2, 'f', 0));
if (nodeLabelY)
nodeLabelY->setText(QString("%1").arg((qreal)node->getY() * -100 + getTrimValue(1) / 5, 2, 'f', 0));
}
@@ -0,0 +1,121 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef VIRTUALJOYSTICKWIDGET_H
#define VIRTUALJOYSTICKWIDGET_H
#include <QWidget>
#include <QResizeEvent>
#include <QBoxLayout>
#include <QGridLayout>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QPushButton>
#include <QLabel>
class Node;
class SliderWidget;
#define GBALL_SIZE 20
#define RESX 1024
class VirtualJoystickWidget : public QWidget
{
Q_OBJECT
public:
enum TrimAxes {
TRIM_AXIS_L_X = 0,
TRIM_AXIS_L_Y,
TRIM_AXIS_R_Y,
TRIM_AXIS_R_X,
};
enum ConstraintTypes {
HOLD_X = 0,
HOLD_Y,
FIX_X,
FIX_Y
};
explicit VirtualJoystickWidget(QWidget * parent = NULL, QChar side = 'L', bool showTrims = true, bool showBtns = true, bool showValues = true, QSize size = QSize(245, 245));
void setStickX(qreal x);
void setStickY(qreal y);
void setStickPos(QPointF xy);
void centerStick();
qreal getStickX();
qreal getStickY();
QPointF getStickPos();
void setTrimValue(int which, int value);
void setTrimRange(int which, int min, int max);
int getTrimValue(int which);
void setStickConstraint(int which, bool active);
virtual QSize sizeHint() const
{
return prefSize;
}
virtual void resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
setSize(event->size());
}
signals:
void trimButtonPressed(int which);
void trimButtonReleased();
void trimSliderMoved(int which, int value);
protected slots:
void onTrimPressed();
void onSliderChange(int value);
void onButtonChange(bool checked);
void updateNodeValueLabels();
protected:
void setSize(QSize size);
void repositionNode();
QWidget * createTrimWidget(QChar type);
QPushButton * createButtonWidget(int type);
QLayout * createNodeValueLayout(QChar type, QLabel *& valLabel);
int getTrimSliderType(QChar type);
int getTrimButtonType(QChar type, int pos);
SliderWidget * getTrimSlider(int which);
QChar stickSide;
QSize prefSize;
QGridLayout * layout;
QGraphicsView * gv;
QGraphicsScene * scene;
Node * node;
SliderWidget * hTrimSlider, * vTrimSlider;
QPushButton * btnHoldX, * btnHoldY;
QPushButton * btnFixX, * btnFixY;
QLabel * nodeLabelX, * nodeLabelY;
QSize extraSize;
float ar; // aspect ratio
};
#endif // VIRTUALJOYSTICKWIDGET_H