Files
edgetx-gx12/companion/src/simulation/widgets/radiowidget.cpp
T
Max PapernoandBertrand Songis 57dc0159d6 [Simulator] Asynchronous SimulatorInterface & a few new features. (#4738)
* [Simulator] Create RadioKeyWidget class for UI buttons & refactor ButtonsWidget; Refactor SimulatedUIWidget (and subtypes) to use new RadioKeyWidgets/ButtonsWidget; Centralize help text for key mappings and get creative with some icons; Simplify some radio UI setups with rectangular buttons.

* [Simulator] Convert all simulator data I/O to signals/slots mechanism:
    * SimulatorInterface/OpenTxSimulator:
       - Now inherits from QObject to allow signal/slot interface;
       - Allows data exchange on a per-item basis (eg. each I/O value is treated separately instead of sending whole arrays or structs of data);
       - Checks for data changes and only emits signals when change is detected (GUI can now assume only new values are being sent);
       - Manages its own 10ms timer (doesn't rely on GUI to do that);
       - Sends "heartbeat" signals @ 1Hz for status monitoring;
    * Simulator GUI:
       - All data is exchanged between GUI elements as well as SimulatorInterface via signals/slots using standardized methods;
       - Data is sent immediately, and only, when actually changed (eg. a control is moved) instead of in bulk at specific time intervals;
       - Similarly, an asynchronous method is used for reading incoming data, w/out timers or loops;
       - Improve VirtualJoystickWidget to be more encapsulated and configurable;
       - Pause telemetry simulator if window is hidden;

* [Simulator] Move SimulatorInterface instance to separate thread, ensure safe asynchronous operations & proper timer interactions; Protect/remove some functions, & reorganize the order (cosmetics).

* [Simulator] Traces are now delivered to OpenTxSimulator and one or more QIODevice(s) can be added as recipient(s); Add SimulatorInterface::getCapability() for compile-time settings; Remove reversed POT1/SLIDER1 mixer exception (Taranis) requirement for SIMU; Fix plus/minus key delay on wheel event w/out encoder.

* [Simulator] Add current knob/slider/trim input value in tool-tips (KnobWidget and SliderWidget).

* [Simulator] Fix trims widget internal value not properly updating, and remove trim influence on virtual joystick X/Y value display (closes #4671).

* [SimulatorInterface] Add handling of transmitter input voltage, including a rough conversion of volts to ADC value for different boards, and default battery volts lookup function; Clear analogs array before starting.

* [Simulator] Add SimulatorInterface::init() method to separate pre-startup tasks; Report actual trim range, not just extended on/off; Change how radio widget states are restored; VirtualJoystickWidget: Connect trim changes directly from simulator, connect joystick events directly, report stick mode directly instead of setting values/constraints externally.

* [Simulator] Calculate default Tx V input based on configured range in radio settings (or warning V+2 for radios which don't support a range).

* [Simulator] Add functional aux. trims for Horus (closes #4699).

* [Companion] Remove problematic QMessageLogContext from AppDebugMessageHandler::messageOutput().

* [Simulator] Prevent trim change via slider if disabled for flight mode (closes #4600).

* [OpenTxSimulator] Fixes for Qt < 5.4.

* [OpenTxSimulator] Fix slot name.
2017-04-02 12:17:37 +02:00

243 lines
5.4 KiB
C++

/*
* 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 "radiouiaction.h"
#include "radiowidget.h"
RadioWidget::RadioWidget(QWidget * parent, Qt::WindowFlags f) :
QWidget(parent, f),
m_gridLayout(NULL),
m_controlWidget(NULL),
m_nameLabel(NULL),
m_action(NULL),
m_value(0),
m_flags(0),
m_valueReset(false),
m_showLabel(false),
m_labelText("")
{
qRegisterMetaType<RadioWidgetState>();
setIndex(-1);
setType(RADIO_WIDGET_NONE);
}
RadioWidget::RadioWidget(RadioUiAction * action, QWidget * parent, Qt::WindowFlags f) :
RadioWidget(parent, f)
{
setAction(action);
}
RadioWidget::RadioWidget(const QString & labelText, int value, QWidget * parent, Qt::WindowFlags f) :
RadioWidget(parent, f)
{
setValue(value);
setLabelText(labelText);
}
void RadioWidget::setIndex(const int & index)
{
m_index = index;
}
void RadioWidget::setType(const RadioWidgetType & type)
{
m_type = type;
}
void RadioWidget::setValue(const int & value)
{
if (value != m_value || m_valueReset) {
m_value = value;
m_valueReset = false;
emit valueChanged(value);
emit valueChange(m_type, m_index, value);
}
}
void RadioWidget::setValueQual(const RadioWidget::RadioWidgetType & type, const int & index, const int & value)
{
if (type == m_type && index == m_index)
setValue(value);
}
void RadioWidget::setFlags(const quint16 & flags)
{
if (flags != m_flags) {
m_flags = flags;
emit flagsChanged(flags);
}
}
void RadioWidget::setShowLabel(const bool show)
{
if (show != m_showLabel) {
m_showLabel = show;
addLabel();
}
}
void RadioWidget::setLabelText(const QString & labelText, bool showLabel)
{
m_labelText = labelText;
m_showLabel = showLabel;
addLabel();
}
void RadioWidget::setStateData(const RadioWidgetState & state)
{
if (state.type != m_type || state.index != m_index)
return;
m_valueReset = true;
setValue(state.value);
setFlags(state.flags);
}
void RadioWidget::changeVisibility(bool visible)
{
static QSizePolicy oldSP = sizePolicy();
setVisible(visible);
if (visible) {
setSizePolicy(oldSP);
}
else {
oldSP = sizePolicy();
setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
}
}
void RadioWidget::setAction(RadioUiAction * action)
{
if (m_action) {
disconnect(m_action, 0, this, 0);
delete m_action;
m_action = NULL;
}
m_action = action;
if (m_action) {
if (m_action->getIndex() > -1)
setIndex(m_action->getIndex());
connect(m_action, &RadioUiAction::toggled, this, &RadioWidget::onActionToggled);
}
}
int RadioWidget::getValue() const
{
return m_value;
}
int RadioWidget::getIndex() const
{
return m_index;
}
int RadioWidget::getType() const
{
return m_type;
}
QByteArray RadioWidget::getStateData() const
{
QByteArray ba;
QDataStream stream(&ba, QIODevice::WriteOnly);
stream << getState();
return ba;
}
RadioWidget::RadioWidgetState RadioWidget::getState() const
{
return RadioWidgetState(quint8(m_type), qint8(m_index), qint16(m_value), quint16(m_flags));
}
RadioUiAction * RadioWidget::getAction() const
{
return m_action;
}
void RadioWidget::onActionToggled(int index, bool active)
{
emit valueChange(m_type, index, getValue());
}
void RadioWidget::addLayout()
{
if (m_gridLayout)
return;
m_gridLayout = new QGridLayout(this);
m_gridLayout->setContentsMargins(0, 0, 0, 0);
m_gridLayout->setVerticalSpacing(4);
m_gridLayout->setHorizontalSpacing(0);
}
void RadioWidget::addLabel()
{
addLayout();
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 RadioWidget::setWidget(QWidget * widget, Qt::Alignment align)
{
addLayout();
if (m_controlWidget) {
m_gridLayout->removeWidget(m_controlWidget);
m_controlWidget->deleteLater();
}
m_controlWidget = widget;
if (widget) {
m_gridLayout->addWidget(widget, 0, 0, 1, 1, align);
m_gridLayout->setRowStretch(0, 1);
}
}
QDataStream & operator << (QDataStream &out, const RadioWidget::RadioWidgetState & o)
{
out << o._version << o.type << o.index << o.value << o.flags;
return out;
}
QDataStream & operator >> (QDataStream &in, RadioWidget::RadioWidgetState & o)
{
if (o._version <= RADIO_WIDGET_STATE_VERSION)
in >> o._version >> o.type >> o.index >> o.value >> o.flags;
return in;
}
QDebug operator << (QDebug d, const RadioWidget::RadioWidgetState & o)
{
QDebugStateSaver saver(d);
d << "RadioWidget::RadioWidgetState: type=" << o.type << "; index=" << o.index
<< "; value=" << o.value << "; flags=0x" << hex << o.flags;
return d;
}