refactor(cpn): Support JSON radio hardware definitions (#4406)
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
project(shared)
|
||||
|
||||
set(${PROJECT_NAME}_NAMES
|
||||
autobitmappedcheckbox
|
||||
autobitmappedcombobox
|
||||
autobitsetcheckbox
|
||||
autocheckbox
|
||||
autocombobox
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* 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 "autobitmappedcheckbox.h"
|
||||
|
||||
AutoBitMappedCheckBox::AutoBitMappedCheckBox(QWidget * parent):
|
||||
QCheckBox(parent),
|
||||
AutoWidget(),
|
||||
m_field(nullptr),
|
||||
m_invert(false)
|
||||
{
|
||||
setBits();
|
||||
connect(this, &QCheckBox::toggled, this, &AutoBitMappedCheckBox::onToggled);
|
||||
}
|
||||
|
||||
AutoBitMappedCheckBox::~AutoBitMappedCheckBox()
|
||||
{
|
||||
}
|
||||
|
||||
void AutoBitMappedCheckBox::setField(int & field, GenericPanel * panel, bool invert)
|
||||
{
|
||||
m_field = &field;
|
||||
m_invert = invert;
|
||||
setPanel(panel);
|
||||
updateValue();
|
||||
}
|
||||
|
||||
void AutoBitMappedCheckBox::setField(unsigned int & field, GenericPanel * panel, bool invert)
|
||||
{
|
||||
m_field = (int *)&field;
|
||||
m_invert = invert;
|
||||
setPanel(panel);
|
||||
updateValue();
|
||||
}
|
||||
|
||||
void AutoBitMappedCheckBox::setInvert(bool invert)
|
||||
{
|
||||
m_invert = invert;
|
||||
updateValue();
|
||||
}
|
||||
|
||||
void AutoBitMappedCheckBox::setBits(const unsigned int numBits, const unsigned int offsetBits,
|
||||
const unsigned int index, const unsigned int indexBits)
|
||||
{
|
||||
m_bits = numBits;
|
||||
m_offsetBits = offsetBits;
|
||||
m_index = index;
|
||||
m_indexBits = indexBits;
|
||||
|
||||
if (m_offsetBits + m_bits > m_indexBits)
|
||||
m_indexBits = m_offsetBits + m_bits;
|
||||
}
|
||||
|
||||
void AutoBitMappedCheckBox::onToggled(bool checked)
|
||||
{
|
||||
if (m_field && !lock()) {
|
||||
const bool val = m_invert ? !checked : checked;
|
||||
unsigned int fieldmask = (bitmask() << shiftbits());
|
||||
*m_field = (*m_field & ~fieldmask) | (val << shiftbits());
|
||||
emit currentDataChanged(val);
|
||||
dataChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void AutoBitMappedCheckBox::updateValue()
|
||||
{
|
||||
if (m_field) {
|
||||
setLock(true);
|
||||
bool val = (bool)((*m_field >> shiftbits()) & bitmask());
|
||||
setChecked(m_invert ? !val : val);
|
||||
setLock(false);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int AutoBitMappedCheckBox::shiftbits()
|
||||
{
|
||||
return m_indexBits * m_index + m_offsetBits;
|
||||
}
|
||||
|
||||
unsigned int AutoBitMappedCheckBox::bitmask()
|
||||
{
|
||||
int mask = -1;
|
||||
mask = ~(mask << m_bits);
|
||||
return (unsigned int)mask;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "autowidget.h"
|
||||
|
||||
#include <QCheckBox>
|
||||
|
||||
class AutoBitMappedCheckBox : public QCheckBox, public AutoWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit AutoBitMappedCheckBox(QWidget * parent = nullptr);
|
||||
virtual ~AutoBitMappedCheckBox();
|
||||
|
||||
virtual void updateValue() override;
|
||||
|
||||
void setField(int & field, GenericPanel * panel = nullptr, bool invert = false);
|
||||
void setField(unsigned int & field, GenericPanel * panel = nullptr, bool invert = false);
|
||||
void setInvert(bool invert);
|
||||
void setBits(const unsigned int numBits = 1, const unsigned int offsetBits = 0, const unsigned int index = 0,
|
||||
const unsigned int indexBits = 1);
|
||||
|
||||
signals:
|
||||
void currentDataChanged(bool value);
|
||||
|
||||
protected slots:
|
||||
void onToggled(bool checked);
|
||||
|
||||
private:
|
||||
int *m_field;
|
||||
bool m_invert;
|
||||
unsigned int m_index;
|
||||
unsigned int m_indexBits;
|
||||
unsigned int m_bits;
|
||||
unsigned int m_offsetBits;
|
||||
|
||||
unsigned int shiftbits();
|
||||
unsigned int bitmask();
|
||||
};
|
||||
@@ -0,0 +1,206 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* 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 "autobitmappedcombobox.h"
|
||||
|
||||
AutoBitMappedComboBox::AutoBitMappedComboBox(QWidget * parent):
|
||||
QComboBox(parent),
|
||||
AutoWidget(),
|
||||
m_field(nullptr),
|
||||
m_next(0),
|
||||
m_hasModel(false),
|
||||
m_rawSource(nullptr),
|
||||
m_rawSwitch(nullptr)
|
||||
{
|
||||
setBits();
|
||||
connect(this, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &AutoBitMappedComboBox::onCurrentIndexChanged);
|
||||
}
|
||||
|
||||
AutoBitMappedComboBox::~AutoBitMappedComboBox()
|
||||
{
|
||||
}
|
||||
|
||||
void AutoBitMappedComboBox::clear()
|
||||
{
|
||||
if (m_hasModel)
|
||||
return;
|
||||
|
||||
setLock(true);
|
||||
QComboBox::clear();
|
||||
m_next = 0;
|
||||
setLock(false);
|
||||
}
|
||||
|
||||
void AutoBitMappedComboBox::insertItems(int index, const QStringList & items)
|
||||
{
|
||||
if (m_hasModel)
|
||||
return;
|
||||
|
||||
foreach(QString item, items) {
|
||||
addItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
void AutoBitMappedComboBox::addItem(const QString & item)
|
||||
{
|
||||
if (m_hasModel)
|
||||
return;
|
||||
|
||||
addItem(item, m_next++);
|
||||
}
|
||||
|
||||
void AutoBitMappedComboBox::addItem(const QString & item, int value)
|
||||
{
|
||||
if (m_hasModel)
|
||||
return;
|
||||
|
||||
setLock(true);
|
||||
QComboBox::addItem(item, value);
|
||||
setLock(false);
|
||||
updateValue();
|
||||
}
|
||||
|
||||
void AutoBitMappedComboBox::setField(unsigned int & field, GenericPanel * panel)
|
||||
{
|
||||
m_field = (int *)&field;
|
||||
m_rawSource = nullptr;
|
||||
m_rawSwitch = nullptr;
|
||||
setFieldInit(panel);
|
||||
}
|
||||
|
||||
void AutoBitMappedComboBox::setField(int & field, GenericPanel * panel)
|
||||
{
|
||||
m_field = &field;
|
||||
m_rawSource = nullptr;
|
||||
m_rawSwitch = nullptr;
|
||||
setFieldInit(panel);
|
||||
}
|
||||
|
||||
void AutoBitMappedComboBox::setField(RawSource & field, GenericPanel * panel)
|
||||
{
|
||||
m_rawSource = &field;
|
||||
m_rawSwitch = nullptr;
|
||||
m_field = nullptr;
|
||||
setFieldInit(panel);
|
||||
}
|
||||
|
||||
void AutoBitMappedComboBox::setField(RawSwitch & field, GenericPanel * panel)
|
||||
{
|
||||
m_rawSwitch = &field;
|
||||
m_rawSource = nullptr;
|
||||
m_field = nullptr;
|
||||
setFieldInit(panel);
|
||||
}
|
||||
|
||||
void AutoBitMappedComboBox::setFieldInit(GenericPanel * panel)
|
||||
{
|
||||
setPanel(panel);
|
||||
updateValue();
|
||||
}
|
||||
|
||||
void AutoBitMappedComboBox::setBits(const unsigned int numBits, const unsigned int offsetBits,
|
||||
const unsigned int index, const unsigned int indexBits)
|
||||
{
|
||||
m_bits = numBits;
|
||||
m_offsetBits = offsetBits;
|
||||
m_index = index;
|
||||
m_indexBits = indexBits;
|
||||
|
||||
if (m_offsetBits + m_bits > m_indexBits)
|
||||
m_indexBits = m_offsetBits + m_bits;
|
||||
}
|
||||
|
||||
void AutoBitMappedComboBox::setModel(QAbstractItemModel * model)
|
||||
{
|
||||
setLock(true);
|
||||
QComboBox::setModel(model);
|
||||
setLock(false);
|
||||
m_hasModel = true;
|
||||
updateValue();
|
||||
}
|
||||
|
||||
void AutoBitMappedComboBox::setAutoIndexes()
|
||||
{
|
||||
if (m_hasModel)
|
||||
return;
|
||||
|
||||
for (int i = 0; i < count(); ++i)
|
||||
setItemData(i, i);
|
||||
updateValue();
|
||||
}
|
||||
|
||||
void AutoBitMappedComboBox::updateValue()
|
||||
{
|
||||
if (!m_field && !m_rawSource && !m_rawSwitch)
|
||||
return;
|
||||
|
||||
setLock(true);
|
||||
|
||||
if (m_field) {
|
||||
int val = (*m_field >> shiftbits()) & bitmask();
|
||||
setCurrentIndex(findData(val));
|
||||
}
|
||||
else if (m_rawSource)
|
||||
setCurrentIndex(findData(m_rawSource->toValue()));
|
||||
else if (m_rawSwitch)
|
||||
setCurrentIndex(findData(m_rawSwitch->toValue()));
|
||||
|
||||
setLock(false);
|
||||
}
|
||||
|
||||
void AutoBitMappedComboBox::onCurrentIndexChanged(int index)
|
||||
{
|
||||
if (lock() || index < 0)
|
||||
return;
|
||||
|
||||
bool ok;
|
||||
const int val = itemData(index).toInt(&ok);
|
||||
if (!ok)
|
||||
return;
|
||||
|
||||
if (m_field && *m_field != val) {
|
||||
unsigned int fieldmask = (bitmask() << shiftbits());
|
||||
*m_field = (*m_field & ~fieldmask) | (val << shiftbits());
|
||||
}
|
||||
else if (m_rawSource && m_rawSource->toValue() != val) {
|
||||
*m_rawSource = RawSource(val);
|
||||
}
|
||||
else if (m_rawSwitch && m_rawSwitch->toValue() != val) {
|
||||
*m_rawSwitch = RawSwitch(val);
|
||||
}
|
||||
else
|
||||
return;
|
||||
|
||||
emit currentDataChanged(val);
|
||||
dataChanged();
|
||||
}
|
||||
|
||||
unsigned int AutoBitMappedComboBox::shiftbits()
|
||||
{
|
||||
return m_indexBits * m_index + m_offsetBits;
|
||||
}
|
||||
|
||||
unsigned int AutoBitMappedComboBox::bitmask()
|
||||
{
|
||||
int mask = -1;
|
||||
mask = ~(mask << m_bits);
|
||||
return (unsigned int)mask;
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "autowidget.h"
|
||||
#include "rawsource.h"
|
||||
#include "rawswitch.h"
|
||||
|
||||
#include <QComboBox>
|
||||
|
||||
class AutoBitMappedComboBox : public QComboBox, public AutoWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit AutoBitMappedComboBox(QWidget * parent = nullptr);
|
||||
virtual ~AutoBitMappedComboBox();
|
||||
|
||||
// QComboBox
|
||||
virtual void addItem(const QString & item);
|
||||
virtual void addItem(const QString & item, int value);
|
||||
virtual void insertItems(int index, const QStringList & items);
|
||||
// AutoWidget
|
||||
virtual void updateValue() override;
|
||||
|
||||
void clear();
|
||||
|
||||
void setBits(const unsigned int numBits = 1, const unsigned int offsetBits = 0, const unsigned int index = 0,
|
||||
const unsigned int indexBits = 1);
|
||||
void setField(unsigned int & field, GenericPanel * panel = nullptr);
|
||||
void setField(int & field, GenericPanel * panel = nullptr);
|
||||
void setField(RawSource & field, GenericPanel * panel = nullptr);
|
||||
void setField(RawSwitch & field, GenericPanel * panel = nullptr);
|
||||
|
||||
void setAutoIndexes();
|
||||
void setModel(QAbstractItemModel * model);
|
||||
|
||||
signals:
|
||||
void currentDataChanged(int value);
|
||||
|
||||
protected slots:
|
||||
void onCurrentIndexChanged(int index);
|
||||
|
||||
private:
|
||||
int *m_field;
|
||||
int m_next;
|
||||
bool m_hasModel;
|
||||
RawSource *m_rawSource;
|
||||
RawSwitch *m_rawSwitch;
|
||||
unsigned int m_index;
|
||||
unsigned int m_indexBits;
|
||||
unsigned int m_bits;
|
||||
unsigned int m_offsetBits;
|
||||
|
||||
unsigned int shiftbits();
|
||||
unsigned int bitmask();
|
||||
|
||||
void setFieldInit(GenericPanel * panel);
|
||||
};
|
||||
@@ -30,3 +30,5 @@
|
||||
#include "autoslider.h"
|
||||
#include "autospinbox.h"
|
||||
#include "autotimeedit.h"
|
||||
#include "autobitmappedcheckbox.h"
|
||||
#include "autobitmappedcombobox.h"
|
||||
|
||||
Reference in New Issue
Block a user