diff --git a/companion/src/shared/CMakeLists.txt b/companion/src/shared/CMakeLists.txt index d63045061..bfd4d60fa 100644 --- a/companion/src/shared/CMakeLists.txt +++ b/companion/src/shared/CMakeLists.txt @@ -1,26 +1,18 @@ include_directories(${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) set(shared_NAMES + genericpanel + verticalscrollarea + autobitsetcheckbox autocheckbox -) -set(shared_SRCS - genericpanel.cpp - hexspinbox.cpp - verticalscrollarea.cpp -) - -set(shared_HDRS - autobitsetcheckbox.h - autocombobox.h - autodoublespinbox.h - autohexspinbox.h - autolineedit.h - genericpanel.h - hexspinbox.h - autoprecisioncombobox.h - autotimeedit.h - autospinbox.h - autoslider.h + autocombobox + autodoublespinbox + autohexspinbox + autolineedit + autoprecisioncombobox + autoslider + autospinbox + autotimeedit ) foreach(name ${shared_NAMES}) diff --git a/companion/src/shared/autobitsetcheckbox.cpp b/companion/src/shared/autobitsetcheckbox.cpp new file mode 100644 index 000000000..93faf35e5 --- /dev/null +++ b/companion/src/shared/autobitsetcheckbox.cpp @@ -0,0 +1,160 @@ +/* + * 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 "autobitsetcheckbox.h" + +AutoBitsetCheckBox::AutoBitsetCheckBox(QWidget * parent) : + AutoBitsetCheckBox(QString(), parent) +{ +} + +AutoBitsetCheckBox::AutoBitsetCheckBox(const QString & text, QWidget * parent) : + QCheckBox(text, parent), + AutoWidget() +{ + init(); +} + +// int field constructors +AutoBitsetCheckBox::AutoBitsetCheckBox(int & field, int bitmask, const QString & text, QWidget * parent) : + AutoBitsetCheckBox(field, bitmask, false, text, parent) +{ +} + +AutoBitsetCheckBox::AutoBitsetCheckBox(int & field, int bitmask, bool invert, const QString & text, QWidget * parent) : + QCheckBox(text, parent), + AutoWidget() +{ + setField(field, bitmask, invert); + init(); +} + +AutoBitsetCheckBox::AutoBitsetCheckBox(int & field, int bitmask, int toggleMask, const QString & text, QWidget * parent) : + QCheckBox(text, parent), + AutoWidget() +{ + setField(field, bitmask, false, toggleMask); + init(); +} + +// unsigned field constructors +AutoBitsetCheckBox::AutoBitsetCheckBox(unsigned & field, int bitmask, const QString & text, QWidget * parent) : + AutoBitsetCheckBox(field, bitmask, false, text, parent) +{ +} + +AutoBitsetCheckBox::AutoBitsetCheckBox(unsigned & field, int bitmask, bool invert, const QString & text, QWidget * parent) : + QCheckBox(text, parent), + AutoWidget() +{ + setField(field, bitmask, invert); + init(); +} + +AutoBitsetCheckBox::AutoBitsetCheckBox(unsigned & field, int bitmask, int toggleMask, const QString & text, QWidget * parent) : + QCheckBox(text, parent), + AutoWidget() +{ + setField(field, bitmask, false, toggleMask); + init(); +} + +AutoBitsetCheckBox::~AutoBitsetCheckBox() +{ +} + +void AutoBitsetCheckBox::setField(int & field, int bitmask, bool invert, int toggleMask) +{ + m_field = &field; + initField(bitmask, invert, toggleMask); +} + +void AutoBitsetCheckBox::setField(unsigned & field, int bitmask, bool invert, int toggleMask) +{ + m_field = (int *)&field; + initField(bitmask, invert, toggleMask); +} + +void AutoBitsetCheckBox::setBitmask(int bitmask) +{ + m_bitmask = bitmask; + updateValue(); +} + +void AutoBitsetCheckBox::setToggleMask(int toggleMask) +{ + m_toggleMask = toggleMask; + updateValue(); +} + +void AutoBitsetCheckBox::setInverted(bool invert) +{ + m_invert = invert; + updateValue(); +} + +void AutoBitsetCheckBox::updateValue() +{ + if (m_field) + setLock(true); + setChecked((m_invert && !m_toggleMask) != bool(*m_field & m_bitmask)); + setLock(false); +} + +void AutoBitsetCheckBox::onToggled(bool checked) +{ + if (!m_field || lock()) + return; + + if (m_toggleMask) { + if (checked) { + *m_field &= ~m_toggleMask; + *m_field |= m_bitmask; + } + else { + *m_field &= ~m_bitmask; + *m_field |= m_toggleMask; + } + } + else { + if (m_invert) + checked = !checked; + if (checked) + *m_field |= m_bitmask; + else + *m_field &= ~m_bitmask; + } + + emit currentDataChanged(checked); + dataChanged(); +} + +void AutoBitsetCheckBox::init() +{ + connect(this, &QCheckBox::toggled, this, &AutoBitsetCheckBox::onToggled); +} + +void AutoBitsetCheckBox::initField(int bitmask, bool invert, int toggleMask) +{ + m_bitmask = bitmask; + m_toggleMask = toggleMask; + m_invert = toggleMask ? false : invert; + updateValue(); +} diff --git a/companion/src/shared/autobitsetcheckbox.h b/companion/src/shared/autobitsetcheckbox.h index af831948e..6c3fef5c7 100644 --- a/companion/src/shared/autobitsetcheckbox.h +++ b/companion/src/shared/autobitsetcheckbox.h @@ -20,165 +20,50 @@ #pragma once -#include -#include "genericpanel.h" +#include "autowidget.h" -class AutoBitsetCheckBox: public QCheckBox +#include + +class AutoBitsetCheckBox: public QCheckBox, AutoWidget { Q_OBJECT - //! \property bitmask The value (shifted bit(s)) to set/clear based on checked state. - Q_PROPERTY(int bitmask READ bitmask WRITE setBitmask) - //! \property inverted If \e true, then the \p bitmask is cleared when the checkbox is checked, and vice versa. Default behavior is to set the bitmask when checked. - Q_PROPERTY(bool inverted READ inverted WRITE setInverted) - //! \property toggleMask If > 0, then set this value when clearing the \p bitmask value (toggles between the two masks being set). \p inverted is ignored if \p toggleMask is set. - Q_PROPERTY(int toggleMask READ toggleMask WRITE setToggleMask) public: - explicit AutoBitsetCheckBox(QWidget *parent = nullptr) : - AutoBitsetCheckBox(QString(), parent) {} - explicit AutoBitsetCheckBox(const QString &text, QWidget *parent = nullptr) : - QCheckBox(text, parent) - { - init(); - } + explicit AutoBitsetCheckBox(QWidget * parent = nullptr); + explicit AutoBitsetCheckBox(const QString &text, QWidget * parent = nullptr); // int field constructors - explicit AutoBitsetCheckBox(int & field, int bitmask, const QString &text = QString(), QWidget *parent = nullptr) : - AutoBitsetCheckBox(field, bitmask, false, text, parent) {} - explicit AutoBitsetCheckBox(int & field, int bitmask, bool invert, const QString &text = QString(), QWidget *parent = nullptr) : - QCheckBox(text, parent) - { - setField(field, bitmask, invert); - init(); - } - explicit AutoBitsetCheckBox(int & field, int bitmask, int toggleMask, const QString &text = QString(), QWidget *parent = nullptr) : - QCheckBox(text, parent) - { - setField(field, bitmask, false, toggleMask); - init(); - } + explicit AutoBitsetCheckBox(int & field, int bitmask, const QString & text = QString(), QWidget * parent = nullptr); + explicit AutoBitsetCheckBox(int & field, int bitmask, bool invert, const QString & text = QString(), QWidget * parent = nullptr); + explicit AutoBitsetCheckBox(int & field, int bitmask, int toggleMask, const QString & text = QString(), QWidget * parent = nullptr); // unsigned field constructors - explicit AutoBitsetCheckBox(unsigned & field, int bitmask, const QString &text = QString(), QWidget *parent = nullptr) : - AutoBitsetCheckBox(field, bitmask, false, text, parent) {} - explicit AutoBitsetCheckBox(unsigned & field, int bitmask, bool invert, const QString &text = QString(), QWidget *parent = nullptr) : - QCheckBox(text, parent) - { - setField(field, bitmask, invert); - init(); - } - explicit AutoBitsetCheckBox(unsigned & field, int bitmask, int toggleMask, const QString &text = QString(), QWidget *parent = nullptr) : - QCheckBox(text, parent) - { - setField(field, bitmask, false, toggleMask); - init(); - } + explicit AutoBitsetCheckBox(unsigned & field, int bitmask, const QString & text = QString(), QWidget * parent = nullptr); + explicit AutoBitsetCheckBox(unsigned & field, int bitmask, bool invert, const QString & text = QString(), QWidget * parent = nullptr); + explicit AutoBitsetCheckBox(unsigned & field, int bitmask, int toggleMask, const QString & text = QString(), QWidget * parent = nullptr); - inline int bitmask() const { return m_bitmask; } - inline int toggleMask() const { return m_toggleMask; } - inline bool inverted() const { return m_toggleMask ? false : m_invert; } + virtual ~AutoBitsetCheckBox(); - public slots: - void setField(int & field, int bitmask = 0, bool invert = false, int toggleMask = 0) - { - m_field = &field; - initField(bitmask, invert, toggleMask); - } + virtual void updateValue() override; - void setField(unsigned & field, int bitmask = 0, bool invert = false, int toggleMask = 0) - { - m_field = reinterpret_cast(&field); - initField(bitmask, invert, toggleMask); - } + void setField(int & field, int bitmask = 0, bool invert = false, int toggleMask = 0); + void setField(unsigned & field, int bitmask = 0, bool invert = false, int toggleMask = 0); + void setBitmask(int bitmask); + void setToggleMask(int toggleMask); + void setInverted(bool invert); - void setBitmask(int bitmask) - { - m_bitmask = bitmask; - updateValue(); - } - - void setToggleMask(int toggleMask) - { - m_toggleMask = toggleMask; - updateValue(); - } - - void setInverted(bool invert) - { - m_invert = invert; - updateValue(); - } - - void updateValue() - { - if (m_panel && m_panel->lock) - return; - if (!m_field) - return; - const bool oldLock = setLocked(true); - setChecked((m_invert && !m_toggleMask) != bool(*m_field & m_bitmask)); - setLocked(oldLock); - } - - void setPanel(GenericPanel * panel) - { - m_panel = panel; - } + signals: + void currentDataChanged(bool checked); protected slots: - void init() - { - connect(this, &AutoBitsetCheckBox::toggled, this, &AutoBitsetCheckBox::onToggled); - } + void onToggled(bool checked); - void initField(int bitmask, bool invert, int toggleMask) - { - m_bitmask = bitmask; - m_toggleMask = toggleMask; - m_invert = toggleMask ? false : invert; - updateValue(); - } - - void onToggled(bool checked) - { - if (!m_field) - return; - - if (m_toggleMask) { - if (checked) { - *m_field &= ~m_toggleMask; - *m_field |= m_bitmask; - } - else { - *m_field &= ~m_bitmask; - *m_field |= m_toggleMask; - } - } - else { - if (m_invert) - checked = !checked; - if (checked) - *m_field |= m_bitmask; - else - *m_field &= ~m_bitmask; - } - - if (m_panel && !m_lock) - emit m_panel->modified(); - } - - bool setLocked(bool lock) - { - const bool ret = m_lock; - m_lock = lock; - return ret; - } - - protected: - int * m_field = nullptr; + private: + int *m_field = nullptr; int m_bitmask = 0; int m_toggleMask = 0; bool m_invert = false; - bool m_lock = false; - GenericPanel * m_panel = nullptr; + + void init(); + void initField(int bitmask, bool invert, int toggleMask); }; diff --git a/companion/src/shared/autocheckbox.cpp b/companion/src/shared/autocheckbox.cpp index 73b21294c..a2af1395c 100644 --- a/companion/src/shared/autocheckbox.cpp +++ b/companion/src/shared/autocheckbox.cpp @@ -49,7 +49,7 @@ void AutoCheckBox::setInvert(bool invert) void AutoCheckBox::onToggled(bool checked) { - if (!lock() && m_field) { + if (m_field && !lock()) { const bool val = m_invert ? !checked : checked; *m_field = val; emit currentDataChanged(val); diff --git a/companion/src/shared/autocheckbox.h b/companion/src/shared/autocheckbox.h index a9a1ef94e..4f4e3c992 100644 --- a/companion/src/shared/autocheckbox.h +++ b/companion/src/shared/autocheckbox.h @@ -30,7 +30,7 @@ class AutoCheckBox : public QCheckBox, AutoWidget public: explicit AutoCheckBox(QWidget * parent = nullptr); - ~AutoCheckBox(); + virtual ~AutoCheckBox(); virtual void updateValue() override; diff --git a/companion/src/shared/autocombobox.cpp b/companion/src/shared/autocombobox.cpp new file mode 100644 index 000000000..a14e08f2e --- /dev/null +++ b/companion/src/shared/autocombobox.cpp @@ -0,0 +1,177 @@ +/* + * 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 "autocombobox.h" + +AutoComboBox::AutoComboBox(QWidget * parent): + QComboBox(parent), + AutoWidget(), + m_field(nullptr), + m_next(0), + m_hasModel(false), + m_rawSource(nullptr), + m_rawSwitch(nullptr) +{ + connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int))); +} + +AutoComboBox::~AutoComboBox() +{ +} + +void AutoComboBox::clear() +{ + if (m_hasModel) + return; + + setLock(true); + QComboBox::clear(); + m_next = 0; + setLock(false); +} + +void AutoComboBox::insertItems(int index, const QStringList & items) +{ + if (m_hasModel) + return; + + foreach(QString item, items) { + addItem(item); + } +} + +void AutoComboBox::addItem(const QString & item) +{ + if (m_hasModel) + return; + + addItem(item, m_next++); +} + +void AutoComboBox::addItem(const QString & item, int value) +{ + if (m_hasModel) + return; + + setLock(true); + QComboBox::addItem(item, value); + setLock(false); + updateValue(); +} + +void AutoComboBox::setField(unsigned int & field, GenericPanel * panel) +{ + m_field = (int *)&field; + m_rawSource = nullptr; + m_rawSwitch = nullptr; + setFieldInit(panel); +} + +void AutoComboBox::setField(int & field, GenericPanel * panel) +{ + m_field = &field; + m_rawSource = nullptr; + m_rawSwitch = nullptr; + setFieldInit(panel); +} + +void AutoComboBox::setField(RawSource & field, GenericPanel * panel) +{ + m_rawSource = &field; + m_rawSwitch = nullptr; + m_field = nullptr; + setFieldInit(panel); +} + +void AutoComboBox::setField(RawSwitch & field, GenericPanel * panel) +{ + m_rawSwitch = &field; + m_rawSource = nullptr; + m_field = nullptr; + setFieldInit(panel); +} + +void AutoComboBox::setFieldInit(GenericPanel * panel) +{ + setPanel(panel); + updateValue(); +} + +void AutoComboBox::setModel(QAbstractItemModel * model) +{ + setLock(true); + QComboBox::setModel(model); + setLock(false); + m_hasModel = true; + updateValue(); +} + +void AutoComboBox::setAutoIndexes() +{ + if (m_hasModel) + return; + + for (int i = 0; i < count(); ++i) + setItemData(i, i); + updateValue(); +} + +void AutoComboBox::updateValue() +{ + if (!m_field && !m_rawSource && !m_rawSwitch) + return; + + setLock(true); + + if (m_field) + setCurrentIndex(findData(*m_field)); + else if (m_rawSource) + setCurrentIndex(findData(m_rawSource->toValue())); + else if (m_rawSwitch) + setCurrentIndex(findData(m_rawSwitch->toValue())); + + setLock(false); +} + +void AutoComboBox::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) { + *m_field = val; + } + 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(); +} diff --git a/companion/src/shared/autocombobox.h b/companion/src/shared/autocombobox.h index ca6054555..5cecd3e70 100644 --- a/companion/src/shared/autocombobox.h +++ b/companion/src/shared/autocombobox.h @@ -20,174 +20,49 @@ #pragma once -#include -#include "genericpanel.h" +#include "autowidget.h" #include "rawsource.h" #include "rawswitch.h" -class AutoComboBox: public QComboBox +#include + +class AutoComboBox : public QComboBox, AutoWidget { Q_OBJECT public: - explicit AutoComboBox(QWidget * parent = nullptr): - QComboBox(parent), - field(nullptr), - panel(nullptr), - next(0), - lock(false), - hasModel(false), - rawSource(nullptr), - rawSwitch(nullptr) - { - connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int))); - } + explicit AutoComboBox(QWidget * parent = nullptr); + virtual ~AutoComboBox(); - void clear() - { - if (!hasModel) { - lock = true; - QComboBox::clear(); - next = 0; - lock = false; - } - } + // 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; - virtual void insertItems(int index, const QStringList & items) - { - if (!hasModel) { - foreach(QString item, items) { - addItem(item); - } - } - } + void clear(); - virtual void addItem(const QString & item) - { - if (!hasModel) - addItem(item, next++); - } + 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); - virtual void addItem(const QString & item, int value) - { - if (!hasModel) { - lock = true; - QComboBox::addItem(item, value); - lock = false; - updateValue(); - } - } - - void setField(unsigned int & field, GenericPanel * panel = nullptr) - { - this->field = (int *)&field; - this->rawSource = nullptr; - this->rawSwitch = nullptr; - this->panel = panel; - updateValue(); - } - - void setField(int & field, GenericPanel * panel = nullptr) - { - this->field = &field; - this->rawSource = nullptr; - this->rawSwitch = nullptr; - this->panel = panel; - updateValue(); - } - - void setField(RawSource & field, GenericPanel * panel = nullptr) - { - this->rawSource = &field; - this->rawSwitch = nullptr; - this->field = nullptr; - this->panel = panel; - updateValue(); - } - - void setField(RawSwitch & field, GenericPanel * panel = nullptr) - { - this->rawSwitch = &field; - this->rawSource = nullptr; - this->field = nullptr; - this->panel = panel; - updateValue(); - } - - void setModel(QAbstractItemModel * model) - { - lock = true; - QComboBox::setModel(model); - lock = false; - hasModel = true; - updateValue(); - } - - void setAutoIndexes() - { - if (!hasModel) { - for (int i = 0; i < count(); ++i) - setItemData(i, i); - updateValue(); - } - } - - void updateValue() - { - if (!field && !rawSource && !rawSwitch) - return; - - lock = true; - - if (field) - setCurrentIndex(findData(*field)); - else if (rawSource) - setCurrentIndex(findData(rawSource->toValue())); - else if (rawSwitch) - setCurrentIndex(findData(rawSwitch->toValue())); - - lock = false; - } + void setAutoIndexes(); + void setModel(QAbstractItemModel * model); signals: void currentDataChanged(int value); protected slots: - void onCurrentIndexChanged(int index) - { - if (panel && panel->lock) - return; - if (lock || index < 0) - return; + void onCurrentIndexChanged(int index); - bool ok; - const int val = itemData(index).toInt(&ok); - if (!ok) - return; + private: + int *m_field; + int m_next; + bool m_hasModel; + RawSource *m_rawSource; + RawSwitch *m_rawSwitch; - if (field && *field != val) { - *field = val; - } - else if (rawSource && rawSource->toValue() != val) { - *rawSource = RawSource(val); - } - else if (rawSwitch && rawSwitch->toValue() != val) { - *rawSwitch = RawSwitch(val); - } - else - return; - - emit currentDataChanged(val); - if (panel) - emit panel->modified(); - } - - protected: - int *field = nullptr; - GenericPanel *panel = nullptr; - int next = 0; - bool lock = false; - bool hasModel = false; - RawSource *rawSource = nullptr; - RawSwitch *rawSwitch = nullptr; + void setFieldInit(GenericPanel * panel); }; diff --git a/companion/src/shared/autodoublespinbox.cpp b/companion/src/shared/autodoublespinbox.cpp new file mode 100644 index 000000000..a5590035f --- /dev/null +++ b/companion/src/shared/autodoublespinbox.cpp @@ -0,0 +1,92 @@ +/* + * 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 "autodoublespinbox.h" + +AutoDoubleSpinBox::AutoDoubleSpinBox(QWidget * parent): + QDoubleSpinBox(parent), + AutoWidget(), + m_field(nullptr), + m_offset(0) +{ + connect(this, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged(double))); +} + +AutoDoubleSpinBox::~AutoDoubleSpinBox() +{ +} + +void AutoDoubleSpinBox::setField(int & field, GenericPanel * panel) +{ + m_field = &field; + setPanel(panel); + updateValue(); +} + +void AutoDoubleSpinBox::setField(unsigned int & field, GenericPanel * panel) +{ + m_field = (int *)&field; + setPanel(panel); + updateValue(); +} + +void AutoDoubleSpinBox::setDecimals(int prec) +{ + QDoubleSpinBox::setDecimals(prec); + updateValue(); +} + +void AutoDoubleSpinBox::setOffset(int offset) +{ + m_offset = offset; + updateValue(); +} + +void AutoDoubleSpinBox::updateValue() +{ + if (m_field) { + setLock(true); + setValue(float(*m_field + m_offset) / multiplier()); + setLock(false); + } +} + +int AutoDoubleSpinBox::multiplier() +{ + switch (decimals()) { + case 1: + return 10; + case 2: + return 100; + case 3: + return 1000; + default: + return 1; + } + } + +void AutoDoubleSpinBox::onValueChanged(double value) +{ + if (m_field && !lock()) { + *m_field = round(value * multiplier() - m_offset); + emit currentDataChanged(*m_field); + dataChanged(); + } +} diff --git a/companion/src/shared/autodoublespinbox.h b/companion/src/shared/autodoublespinbox.h index f9614d882..fb3b26591 100644 --- a/companion/src/shared/autodoublespinbox.h +++ b/companion/src/shared/autodoublespinbox.h @@ -20,95 +20,38 @@ #pragma once +#include "autowidget.h" + #include -#include "genericpanel.h" + #if __GNUC__ #include #endif -class AutoDoubleSpinBox: public QDoubleSpinBox +class AutoDoubleSpinBox : public QDoubleSpinBox, AutoWidget { Q_OBJECT public: - explicit AutoDoubleSpinBox(QWidget * parent = nullptr): - QDoubleSpinBox(parent), - field(nullptr), - panel(nullptr), - lock(false), - offset(0) - { - connect(this, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged(double))); - } + explicit AutoDoubleSpinBox(QWidget * parent = nullptr); + virtual ~AutoDoubleSpinBox(); - void setField(int & field, GenericPanel * panel = nullptr) - { - this->field = &field; - this->panel = panel; - updateValue(); - } + virtual void updateValue() override; - void setField(unsigned int & field, GenericPanel * panel = nullptr) - { - this->field = (int *)&field; - this->panel = panel; - updateValue(); - } - - void updateValue() - { - if (field) { - lock = true; - setValue(float(*field + offset) / multiplier()); - lock = false; - } - } - - void setDecimals(int prec) - { - QDoubleSpinBox::setDecimals(prec); - updateValue(); - } - - void setOffset(int offset) - { - this->offset = offset; - updateValue(); - } - - protected: - int multiplier() - { - switch (decimals()) { - case 1: - return 10; - case 2: - return 100; - default: - return 1; - } - } + void setField(int & field, GenericPanel * panel = nullptr); + void setField(unsigned int & field, GenericPanel * panel = nullptr); + void setDecimals(int prec); + void setOffset(int offset); signals: void currentDataChanged(double value); protected slots: - void onValueChanged(double value) - { - if (panel && panel->lock) - return; - if (field && !lock) { - *field = round(value * multiplier() - offset); - emit currentDataChanged(*field); - if (panel) { - emit panel->modified(); - } - } - } + void onValueChanged(double value); - protected: - int * field = nullptr; - GenericPanel * panel = nullptr; - bool lock = false; - int offset = 0; + private: + int *m_field; + int m_offset; + + int multiplier(); }; diff --git a/companion/src/shared/autohexspinbox.cpp b/companion/src/shared/autohexspinbox.cpp new file mode 100644 index 000000000..3096a1c2f --- /dev/null +++ b/companion/src/shared/autohexspinbox.cpp @@ -0,0 +1,95 @@ +/* + * 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 "autohexspinbox.h" + +AutoHexSpinBox::AutoHexSpinBox(QWidget * parent): + QSpinBox(parent), + AutoWidget(), + m_field(nullptr) +{ + setRange(0, AUTOHEXSPINBOX_MAX_VALUE); + connect(this, QOverload::of(&QSpinBox::valueChanged), this, &AutoHexSpinBox::onValueChanged); +} + +AutoHexSpinBox::~AutoHexSpinBox() +{ +} + +void AutoHexSpinBox::setField(unsigned int & field, const unsigned int min, const unsigned int max, GenericPanel * panel) +{ + m_field = &field; + setRange(min, max); + setPanel(panel); + updateValue(); +} + +void AutoHexSpinBox::setField(unsigned int & field, GenericPanel * panel) +{ + m_field = &field; + setRange(0, AUTOHEXSPINBOX_MAX_VALUE); + setPanel(panel); + updateValue(); +} + +void AutoHexSpinBox::setRange(const unsigned int min, const unsigned int max) +{ + QSpinBox::setRange(min, max); + + m_length = QString("%1").arg(max, 0, 16).size(); + + m_validator = new QRegExpValidator(QRegExp(QString("[0-9A-Fa-f]{1,%1}").arg(m_length)), this); +} + +void AutoHexSpinBox::updateValue() +{ + if (m_field) { + setLock(true); + setValue(*m_field); + setLock(false); + } +} + +void AutoHexSpinBox::onValueChanged(int value) +{ + if (m_field && !lock()) { + if (*m_field != (unsigned)value) { + *m_field = (unsigned)value; + emit currentDataChanged(value); + dataChanged(); + } + } +} + +// reimplemented +QValidator::State AutoHexSpinBox::validate(QString &text, int &pos) const +{ + return m_validator->validate(text, pos); +} + +int AutoHexSpinBox::valueFromText(const QString &text) const +{ + return text.toInt(0, 16); +} + +QString AutoHexSpinBox::textFromValue(int value) const +{ + return QString("%1").arg(value, m_length, 16, QChar('0')).toUpper(); +} diff --git a/companion/src/shared/autohexspinbox.h b/companion/src/shared/autohexspinbox.h index 40cc24da4..440fb4740 100644 --- a/companion/src/shared/autohexspinbox.h +++ b/companion/src/shared/autohexspinbox.h @@ -20,58 +20,42 @@ #pragma once -#include "hexspinbox.h" -#include "genericpanel.h" +#include "autowidget.h" -class AutoHexSpinBox: public HexSpinBox +#include +#include + +class QRegExpValidator; + +class AutoHexSpinBox : public QSpinBox, AutoWidget { Q_OBJECT public: - explicit AutoHexSpinBox(QWidget * parent = nullptr): - HexSpinBox(parent), - field(nullptr), - panel(nullptr), - lock(false) - { - connect(this, SIGNAL(valueChanged(int)), this, SLOT(onValueChanged(int))); - } + constexpr static unsigned int AUTOHEXSPINBOX_MAX_VALUE {65535}; - void setField(unsigned int & field, GenericPanel * panel = nullptr) - { - this->field = &field; - this->panel = panel; - updateValue(); - } + explicit AutoHexSpinBox(QWidget * parent = nullptr); + virtual ~AutoHexSpinBox(); - void updateValue() - { - if (field) { - lock = true; - setValue(*field); - lock = false; - } - } + virtual void updateValue(); + + void setField(unsigned int & field, const unsigned int min = 0, const unsigned int max = AUTOHEXSPINBOX_MAX_VALUE, GenericPanel * panel = nullptr); + void setField(unsigned int & field, GenericPanel * panel = nullptr); + void setRange(unsigned int min = 0, unsigned int max = AUTOHEXSPINBOX_MAX_VALUE); + + protected: + QValidator::State validate(QString &text, int &pos) const; + int valueFromText(const QString &text) const; + QString textFromValue(int value) const; signals: void currentDataChanged(int value); protected slots: - void onValueChanged(int value) - { - if (panel && panel->lock) - return; - if (field && !lock) { - *field = value; - emit currentDataChanged(value); - if (panel) { - emit panel->modified(); - } - } - } + void onValueChanged(int value); - protected: - unsigned int * field = nullptr; - GenericPanel * panel = nullptr; - bool lock = false; + private: + unsigned int *m_field; + QRegExpValidator *m_validator; + unsigned int m_length; }; diff --git a/companion/src/shared/autolineedit.cpp b/companion/src/shared/autolineedit.cpp new file mode 100644 index 000000000..79e92d482 --- /dev/null +++ b/companion/src/shared/autolineedit.cpp @@ -0,0 +1,82 @@ +/* + * 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 "autolineedit.h" + +AutoLineEdit::AutoLineEdit(QWidget * parent, bool updateOnChange): + QLineEdit(parent), + AutoWidget(), + m_charField(NULL), + m_strField(nullptr) +{ + if (updateOnChange) + connect(this, &QLineEdit::textChanged, this, &AutoLineEdit::onEdited); + else + connect(this, &QLineEdit::editingFinished, this, &AutoLineEdit::onEdited); +} + +AutoLineEdit::~AutoLineEdit() +{ +} + +void AutoLineEdit::setField(char * field, int len, GenericPanel * panel) +{ + m_charField = field; + setPanel(panel); + setValidator(new QRegExpValidator(QRegExp("[ A-Za-z0-9_.-,]*"), this)); + if (len) + setMaxLength(len); + updateValue(); +} + +void AutoLineEdit::setField(QString & field, int len, GenericPanel * panel) +{ + m_strField = &field; + if (len) + setMaxLength(len); + setPanel(panel); + updateValue(); +} + +void AutoLineEdit::updateValue() +{ + setLock(true); + if (m_strField) + setText(*m_strField); + else if (m_charField) + setText(m_charField); + setLock(false); +} + +void AutoLineEdit::onEdited() +{ + if (lock()) + return; + + if (m_strField && *m_strField != text()) + *m_strField = text(); + else if (m_charField && strcmp(m_charField, text().toLatin1())) + strcpy(m_charField, text().toLatin1()); + else + return; + + emit currentDataChanged(); + dataChanged(); +} diff --git a/companion/src/shared/autolineedit.h b/companion/src/shared/autolineedit.h index b2815796c..0820bc5dc 100644 --- a/companion/src/shared/autolineedit.h +++ b/companion/src/shared/autolineedit.h @@ -20,83 +20,31 @@ #pragma once +#include "autowidget.h" + #include #include -#include "genericpanel.h" -class AutoLineEdit: public QLineEdit +class AutoLineEdit: public QLineEdit, AutoWidget { Q_OBJECT public: - explicit AutoLineEdit(QWidget * parent = nullptr, bool updateOnChange = false): - QLineEdit(parent), - field(NULL), - strField(nullptr), - panel(nullptr), - lock(false) - { - if (updateOnChange) - connect(this, &AutoLineEdit::textChanged, this, &AutoLineEdit::onEdited); - else - connect(this, &AutoLineEdit::editingFinished, this, &AutoLineEdit::onEdited); - } + explicit AutoLineEdit(QWidget * parent = nullptr, bool updateOnChange = false); + virtual ~AutoLineEdit(); - void setField(char * field, int len, GenericPanel * panel = nullptr) - { - this->field = field; - this->panel = panel; - setValidator(new QRegExpValidator(QRegExp("[ A-Za-z0-9_.-,]*"), this)); - setMaxLength(len); - updateValue(); - } + virtual void updateValue() override; - void setField(QString & field, int len = 0, GenericPanel * panel = nullptr) - { - strField = &field; - this->panel = panel; - if (len) - setMaxLength(len); - updateValue(); - } - - void updateValue() - { - lock = true; - if (field) - setText(field); - else if (strField) - setText(*strField); - lock = false; - } + void setField(char * field, int len, GenericPanel * panel = nullptr); + void setField(QString & field, int len = 0, GenericPanel * panel = nullptr); signals: void currentDataChanged(); protected slots: - void onEdited() - { - if (panel && panel->lock) - return; - if (lock) - return; + void onEdited(); - if (field) - strcpy(field, text().toLatin1()); - else if (strField) - *strField = text(); - else - return; - - emit currentDataChanged(); - - if (panel) - emit panel->modified(); - } - - protected: - char * field; - QString * strField = nullptr; - GenericPanel * panel = nullptr; - bool lock = false; + private: + char *m_charField; + QString *m_strField; }; diff --git a/companion/src/shared/autoprecisioncombobox.cpp b/companion/src/shared/autoprecisioncombobox.cpp new file mode 100644 index 000000000..6add0ff92 --- /dev/null +++ b/companion/src/shared/autoprecisioncombobox.cpp @@ -0,0 +1,227 @@ +/* + * 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 "autoprecisioncombobox.h" + +constexpr unsigned int APCB_DECIMALS_MIN {0}; +constexpr unsigned int APCB_DECIMALS_MAX {6}; + +AutoPrecisionComboBox::AutoPrecisionComboBox(QWidget * parent): + QComboBox(parent), + AutoWidget() +{ + setFieldInit(); + init(); +} + +AutoPrecisionComboBox::AutoPrecisionComboBox(unsigned int & field, unsigned int minDecimals, unsigned int maxDecimals, bool padding, QString suffix, QWidget * parent, GenericPanel * panel): + QComboBox(parent), + AutoWidget() +{ + m_field = &field; + setFieldInit(minDecimals, maxDecimals, padding, suffix, panel); + init(); +} + +AutoPrecisionComboBox::AutoPrecisionComboBox(int & field, unsigned int minDecimals, unsigned int maxDecimals, bool padding, QString suffix, QWidget * parent, GenericPanel * panel): + QComboBox(parent), + AutoWidget() +{ + m_field = (unsigned int *)&field; + setFieldInit(minDecimals, maxDecimals, padding, suffix, panel); + init(); +} + +AutoPrecisionComboBox::~AutoPrecisionComboBox() +{ +} + +void AutoPrecisionComboBox::setField(int & field, unsigned int minDecimals, unsigned int maxDecimals, bool padding, QString suffix, GenericPanel * panel) +{ + m_field = (unsigned int *)&field; + setFieldInit(minDecimals, maxDecimals, padding, suffix, panel); + setPanel(panel); +} + +void AutoPrecisionComboBox::setField(unsigned int & field, unsigned int minDecimals, unsigned int maxDecimals, bool padding, QString suffix, GenericPanel * panel) +{ + m_field = &field; + setFieldInit(minDecimals, maxDecimals, padding, suffix, panel); + setPanel(panel); +} + +void AutoPrecisionComboBox::setMinDecimals(unsigned int minDecimals) +{ + m_minDecimals = rangecheckDecimals(minDecimals); + if (m_minDecimals > m_maxDecimals) + m_minDecimals = m_maxDecimals; + if (!lock()) + updateList(); +} + +void AutoPrecisionComboBox::setMaxDecimals(unsigned int maxDecimals) +{ + m_maxDecimals = rangecheckDecimals(maxDecimals); + if (m_maxDecimals < m_minDecimals) + m_maxDecimals = m_minDecimals; + if (!lock()) + updateList(); +} + +void AutoPrecisionComboBox::setDecimalsRange(unsigned int min, unsigned int max) +{ + setMinDecimals(min); + setMaxDecimals(max); +} + +void AutoPrecisionComboBox::setPadding(bool padding) +{ + m_padding = padding; + if (!lock()) + updateList(); +} + +void AutoPrecisionComboBox::setSuffix(QString suffix) +{ + m_suffix = suffix.trimmed(); + if (!lock()) + updateList(); +} + +void AutoPrecisionComboBox::setValue(int value) +{ + if (!m_field) + return; + + const unsigned int val = (unsigned int)value; + if (*m_field != val) { + *m_field = rangecheckDecimals(val); + updateValue(); + emit currentDataChanged(value); + dataChanged(); + } +} + +void AutoPrecisionComboBox::setValue(unsigned int value) +{ + if (!m_field) + return; + + if (*m_field != value) { + *m_field = rangecheckDecimals(value); + updateValue(); + emit currentDataChanged((int)value); + dataChanged(); + } +} + +void AutoPrecisionComboBox::updateValue() +{ + if (!m_field) + return; + + if (!isValidDecimals(*m_field)) + return; + + if (*m_field > m_maxDecimals) + setMaxDecimals(*m_field); + + setLock(true); + setCurrentIndex(findData(*m_field)); + setLock(false); +} + +void AutoPrecisionComboBox::setAutoIndexes() +{ + for (int i = 0; i < count(); ++i) + setItemData(i, i); + updateValue(); +} + +void AutoPrecisionComboBox::init() +{ + connect(this, QOverload::of(&QComboBox::currentIndexChanged), this, &AutoPrecisionComboBox::onCurrentIndexChanged); +} + +void AutoPrecisionComboBox::setFieldInit(unsigned int minDecimals, unsigned int maxDecimals, bool padding, QString suffix, GenericPanel * panel) +{ + setLock(true); + setMinDecimals(minDecimals); + setMaxDecimals(maxDecimals); + setPadding(padding); + setSuffix(suffix); + setLock(false); + setPanel(panel); + updateList(); +} + +void AutoPrecisionComboBox::updateList() +{ + setLock(true); + unsigned int i; + int j; + clear(); + + for (i = APCB_DECIMALS_MIN, j = (int)m_maxDecimals; j >= 0; i++, j--) { + if (i >= m_minDecimals) { + QString str = QString("0.%1").arg(QString(i, '0')); + if (m_padding) + str.append(QString(j, '_')); + if (m_suffix != "") + str.append(QString(" %1").arg(m_suffix)); + addItem(str, i); + } + } + + setSizeAdjustPolicy(QComboBox::AdjustToContents); + setLock(false); +} + +void AutoPrecisionComboBox::onCurrentIndexChanged(int index) +{ + if (!m_field || index < 0 || lock()) + return; + + if (*m_field != itemData(index).toUInt()) { + *m_field = itemData(index).toUInt(); + emit currentDataChanged(index); + dataChanged(); + } +} + +unsigned int AutoPrecisionComboBox::rangecheckDecimals(unsigned int decimals) +{ + unsigned int ret; + if (decimals < APCB_DECIMALS_MIN) + ret = APCB_DECIMALS_MIN; + else if (decimals > APCB_DECIMALS_MAX) + ret = APCB_DECIMALS_MAX; + else + ret = decimals; + return ret; +} + +bool AutoPrecisionComboBox::isValidDecimals(unsigned int value) +{ + if (value >= APCB_DECIMALS_MIN && value <= APCB_DECIMALS_MAX) + return true; + else + return false; +} diff --git a/companion/src/shared/autoprecisioncombobox.h b/companion/src/shared/autoprecisioncombobox.h index 72dd89406..7cebeaa36 100644 --- a/companion/src/shared/autoprecisioncombobox.h +++ b/companion/src/shared/autoprecisioncombobox.h @@ -20,235 +20,50 @@ #pragma once -#include "genericpanel.h" +#include "autowidget.h" #include #include -class AutoPrecisionComboBox: public QComboBox +class AutoPrecisionComboBox : public QComboBox, AutoWidget { - Q_OBJECT - //! \property minDecimals Determines the first list item - Q_PROPERTY(unsigned int minDecimals READ minDecimals WRITE setMinDecimals) - //! \property maxDecimals Determines the last list item - Q_PROPERTY(unsigned int maxDecimals READ maxDecimals WRITE setMaxDecimals) - //! \property padding If true, add trailing underscore placeholders to each list item - Q_PROPERTY(bool padding READ padding WRITE setPadding) - //! \property suffix Optional text suffix to each list value - Q_PROPERTY(QString suffix READ suffix WRITE setSuffix) + Q_OBJECT public: - explicit AutoPrecisionComboBox(QWidget * parent = nullptr): - QComboBox(parent) - { - init(); - } + explicit AutoPrecisionComboBox(QWidget * parent = nullptr); + explicit AutoPrecisionComboBox(unsigned int & field, unsigned int minDecimals = 0, unsigned int maxDecimals = 1, bool padding = false, QString suffix = "", QWidget * parent = nullptr, GenericPanel * panel = nullptr); + explicit AutoPrecisionComboBox(int & field, unsigned int minDecimals = 0, unsigned int maxDecimals = 1, bool padding = false, QString suffix = "", QWidget * parent = nullptr, GenericPanel * panel = nullptr); + virtual ~AutoPrecisionComboBox(); - explicit AutoPrecisionComboBox(unsigned int & field, unsigned int minDecimals = 0, unsigned int maxDecimals = 1, bool padding = false, QString suffix = "", QWidget * parent = nullptr, GenericPanel * panel = nullptr): - QComboBox(parent) - { - setField(field, minDecimals, maxDecimals, padding, suffix, panel); - init(); - } + virtual void updateValue() override; - explicit AutoPrecisionComboBox(int & field, unsigned int minDecimals = 0, unsigned int maxDecimals = 1, bool padding = false, QString suffix = "", QWidget * parent = nullptr, GenericPanel * panel = nullptr): - QComboBox(parent) - { - setField(field, minDecimals, maxDecimals, padding, suffix, panel); - init(); - } - - inline int minDecimals() const { return m_minDecimals; } - inline int maxDecimals() const { return m_maxDecimals; } - inline bool padding() const { return m_padding; } - inline QString suffix() const { return m_suffix; } - - public slots: - void setField(int & field, unsigned int minDecimals = 0, unsigned int maxDecimals = 1, bool padding = false, QString suffix = "", GenericPanel * panel = nullptr) - { - m_field = reinterpret_cast(&field); - initField(minDecimals, maxDecimals, padding, suffix); - setPanel(panel); - } - - void setField(unsigned int & field, unsigned int minDecimals = 0, unsigned int maxDecimals = 1, bool padding = false, QString suffix = "", GenericPanel * panel = nullptr) - { - m_field = &field; - initField(minDecimals, maxDecimals, padding, suffix); - setPanel(panel); - } - - void setMinDecimals(unsigned int minDecimals) - { - m_minDecimals = rangecheckDecimals(minDecimals); - if (m_minDecimals > m_maxDecimals) - m_minDecimals = m_maxDecimals; - if (!m_lock) - updateList(); - } - - void setMaxDecimals(unsigned int maxDecimals) - { - m_maxDecimals = rangecheckDecimals(maxDecimals); - if (m_maxDecimals < m_minDecimals) - m_maxDecimals = m_minDecimals; - if (!m_lock) - updateList(); - } - - void setPadding(bool padding) - { - m_padding = padding; - if (!m_lock) - updateList(); - } - - void setSuffix(QString suffix) - { - m_suffix = suffix.trimmed(); - if (!m_lock) - updateList(); - } - - void setPanel(GenericPanel * panel) - { - m_panel = panel; - } - - void setValue(int value) - { - if (!m_field) - return; - const unsigned int val = (unsigned int)value; - if (*m_field != val) { - *m_field = rangecheckDecimals(val); - updateValue(); - emit currentDataChanged(value); - } - } - - void setValue(unsigned int value) - { - if (!m_field) - return; - if (*m_field != value) { - *m_field = rangecheckDecimals(value); - updateValue(); - emit currentDataChanged((int)value); - } - } - - void updateValue() - { - if (!m_field) - return; - if (!isValidDecimals(*m_field)) - return; - if (*m_field > m_maxDecimals) { - setMaxDecimals(*m_field); - } - const bool savedlock = setLocked(true); - setCurrentIndex(findData(*m_field)); - setLocked(savedlock); - } - - void setAutoIndexes() - { - for (int i = 0; i < count(); ++i) - setItemData(i, i); - updateValue(); - } + void setField(int & field, unsigned int minDecimals = 0, unsigned int maxDecimals = 1, bool padding = false, QString suffix = "", GenericPanel * panel = nullptr); + void setField(unsigned int & field, unsigned int minDecimals = 0, unsigned int maxDecimals = 1, bool padding = false, QString suffix = "", GenericPanel * panel = nullptr); + void setMinDecimals(unsigned int minDecimals); + void setMaxDecimals(unsigned int maxDecimals); + void setDecimalsRange(unsigned int min, unsigned int max); + void setPadding(bool padding); + void setSuffix(QString suffix); + void setValue(int value); + void setValue(unsigned int value); + void setAutoIndexes(); signals: void currentDataChanged(int index); protected slots: - void init() - { - connect(this, QOverload::of(&AutoPrecisionComboBox::currentIndexChanged), this, &AutoPrecisionComboBox::onCurrentIndexChanged); - } + void onCurrentIndexChanged(int index); - void initField(unsigned int minDecimals, unsigned int maxDecimals, bool padding, QString suffix) - { - const bool savedlock = setLocked(true); - setMinDecimals(minDecimals); - setMaxDecimals(maxDecimals); - setPadding(padding); - setSuffix(suffix); - setLocked(savedlock); - updateList(); - } - - void updateList() - { - const bool savedlock = setLocked(true); - unsigned int i; - int j; - clear(); - - for (i = APCB_DECIMALS_MIN, j = (int)m_maxDecimals; j >= 0; i++, j--) { - if (i >= m_minDecimals) { - QString str = QString("0.%1").arg(QString(i, '0')); - if (m_padding) - str.append(QString(j, '_')); - if (m_suffix != "") - str.append(QString(" %1").arg(m_suffix)); - addItem(str, i); - } - } - - setSizeAdjustPolicy(QComboBox::AdjustToContents); - setLocked(savedlock); - } - - void onCurrentIndexChanged(int index) - { - if (index < 0 || (m_panel && m_panel->lock) || m_lock) - return; - if (m_field) { - *m_field = itemData(index).toUInt(); - emit currentDataChanged(index); - if (m_panel) - emit m_panel->modified(); - } - } - - unsigned int rangecheckDecimals(unsigned int decimals) - { - unsigned int ret; - if (decimals < APCB_DECIMALS_MIN) - ret = APCB_DECIMALS_MIN; - else if (decimals > APCB_DECIMALS_MAX) - ret = APCB_DECIMALS_MAX; - else - ret = decimals; - return ret; - } - - bool isValidDecimals(unsigned int value) - { - if (value >= APCB_DECIMALS_MIN && value <= APCB_DECIMALS_MAX) - return true; - else - return false; - } - - bool setLocked(bool lock) - { - const bool ret = m_lock; - m_lock = lock; - return ret; - } - - protected: - constexpr static unsigned int APCB_DECIMALS_MIN = {0}; - constexpr static unsigned int APCB_DECIMALS_MAX = {6}; - - unsigned int * m_field = nullptr; - GenericPanel * m_panel = nullptr; + private: + unsigned int *m_field = nullptr; unsigned int m_minDecimals = 0; - unsigned int m_maxDecimals = 0; + unsigned int m_maxDecimals = 1; bool m_padding = false; QString m_suffix = ""; - bool m_lock = false; + + void init(); + void setFieldInit(unsigned int minDecimals = 0, unsigned int maxDecimals = 1, bool padding = false, QString suffix = "", GenericPanel * panel = nullptr); + bool isValidDecimals(unsigned int value); + unsigned int rangecheckDecimals(unsigned int decimals); + void updateList(); }; diff --git a/companion/src/shared/autoslider.cpp b/companion/src/shared/autoslider.cpp new file mode 100644 index 000000000..5b02b3603 --- /dev/null +++ b/companion/src/shared/autoslider.cpp @@ -0,0 +1,89 @@ +/* + * 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 "autoslider.h" + +AutoSlider::AutoSlider(QWidget * parent): + QSlider(parent), + AutoWidget() +{ + init(); +} + +AutoSlider::AutoSlider(Qt::Orientation orientation, QWidget * parent): + QSlider(orientation, parent), + AutoWidget() +{ + init(); +} + +AutoSlider::~AutoSlider() +{ +} + +void AutoSlider::setField(int & field, int min, int max, GenericPanel * panel) +{ + m_field = &field; + setFieldInit(min, max, panel); +} + +void AutoSlider::setField(unsigned int & field, int min, int max, GenericPanel * panel) +{ + m_field = (int *)&field; + setFieldInit(min, max, panel); +} + +void AutoSlider::setTick(int interval, QSlider::TickPosition position) +{ + setTickInterval(interval); + setTickPosition(position); +} + +void AutoSlider::updateValue() +{ + if (m_field) { + setLock(true); + setValue(*m_field); + setLock(false); + } +} + +void AutoSlider::init() +{ + connect(this, &QSlider::valueChanged, this, &AutoSlider::onValueChanged); +} + +void AutoSlider::setFieldInit(int min, int max, GenericPanel * panel) +{ + setPanel(panel); + setRange(min, max); + updateValue(); +} + +void AutoSlider::onValueChanged(int value) +{ + if (m_field && !lock()) { + if (*m_field != value) { + *m_field = value; + emit currentDataChanged(value); + dataChanged(); + } + } +} diff --git a/companion/src/shared/autoslider.h b/companion/src/shared/autoslider.h index e81ea66b4..f83177c87 100644 --- a/companion/src/shared/autoslider.h +++ b/companion/src/shared/autoslider.h @@ -20,85 +20,34 @@ #pragma once -#include "genericpanel.h" +#include "autowidget.h" #include -class AutoSlider : public QSlider +class AutoSlider : public QSlider, AutoWidget { Q_OBJECT public: - explicit AutoSlider(QWidget * parent = nullptr): - QSlider(parent) - { - init(); - } + explicit AutoSlider(QWidget * parent = nullptr); + explicit AutoSlider(Qt::Orientation orientation, QWidget * parent = nullptr); + virtual ~AutoSlider(); - explicit AutoSlider(Qt::Orientation orientation, QWidget * parent = nullptr): - QSlider(orientation, parent) - { - init(); - } + virtual void updateValue() override; - void setField(int & field, int min, int max, GenericPanel * panel = nullptr) - { - this->field = &field; - setFieldInit(min, max, panel); - } - - void setField(unsigned int & field, int min, int max, GenericPanel * panel = nullptr) - { - this->field = reinterpret_cast(&field); - setFieldInit(min, max, panel); - } - - void setTick(int interval, QSlider::TickPosition position) - { - setTickInterval(interval); - setTickPosition(position); - } - - void updateValue() - { - if (field) { - lock = true; - setValue(*field); - lock = false; - } - } + void setField(int & field, int min, int max, GenericPanel * panel = nullptr); + void setField(unsigned int & field, int min, int max, GenericPanel * panel = nullptr); + void setTick(int interval, QSlider::TickPosition position); signals: void currentDataChanged(int value); protected slots: - void init() - { - connect(this, &AutoSlider::valueChanged, this, &AutoSlider::onValueChanged); - } + void onValueChanged(int value); - void onValueChanged(int value) - { - if (panel && panel->lock) - return; - if (field && !lock) { - *field = value; - emit currentDataChanged(value); - if (panel) { - emit panel->modified(); - } - } - } + private: + int *m_field = nullptr; - protected: - void setFieldInit(int min, int max, GenericPanel * panel) - { - this->panel = panel; - setRange(min, max); - updateValue(); - } - - int *field = nullptr; - GenericPanel *panel = nullptr; - bool lock = false; + void init(); + void setFieldInit(int min, int max, GenericPanel * panel); }; diff --git a/companion/src/shared/autospinbox.cpp b/companion/src/shared/autospinbox.cpp new file mode 100644 index 000000000..ecee90edd --- /dev/null +++ b/companion/src/shared/autospinbox.cpp @@ -0,0 +1,71 @@ +/* + * 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 "autospinbox.h" + +AutoSpinBox::AutoSpinBox(QWidget * parent): + QSpinBox(parent), + AutoWidget(), + m_field(nullptr) +{ + connect(this, QOverload::of(&QSpinBox::valueChanged), this, &AutoSpinBox::onValueChanged); +} + +AutoSpinBox::~AutoSpinBox() +{ +} + +void AutoSpinBox::setField(int & field, GenericPanel * panel) +{ + m_field = &field; + setFieldInit(panel); +} + +void AutoSpinBox::setField(unsigned int & field, GenericPanel * panel) +{ + m_field = (int *)&field; + setFieldInit(panel); +} + +void AutoSpinBox::setFieldInit(GenericPanel * panel) +{ + setPanel(panel); + updateValue(); +} + +void AutoSpinBox::updateValue() +{ + if (m_field) { + setLock(true); + setValue(*m_field); + setLock(false); + } +} + +void AutoSpinBox::onValueChanged(int value) +{ + if (m_field && !lock()) { + if (*m_field != value) { + *m_field = value; + emit currentDataChanged(value); + dataChanged(); + } + } +} diff --git a/companion/src/shared/autospinbox.h b/companion/src/shared/autospinbox.h index 65b49c0f3..a92d6db56 100644 --- a/companion/src/shared/autospinbox.h +++ b/companion/src/shared/autospinbox.h @@ -20,62 +20,31 @@ #pragma once +#include "autowidget.h" + #include -#include "genericpanel.h" - -class AutoSpinBox : public QSpinBox +class AutoSpinBox : public QSpinBox, AutoWidget { Q_OBJECT public: - explicit AutoSpinBox(QWidget * parent = nullptr): - QSpinBox(parent), - field(nullptr), - panel(nullptr), - lock(false) - { - connect(this, QOverload::of(&AutoSpinBox::valueChanged), this, &AutoSpinBox::onValueChanged); - } + explicit AutoSpinBox(QWidget * parent = nullptr); + virtual ~AutoSpinBox(); - void setField(int & field, GenericPanel * panel = nullptr) - { - this->field = &field; - this->panel = panel; - updateValue(); - } + virtual void updateValue() override; - void setField(unsigned int & field, GenericPanel * panel = nullptr) - { - this->field = (int *)&field; - this->panel = panel; - updateValue(); - } + void setField(int & field, GenericPanel * panel = nullptr); + void setField(unsigned int & field, GenericPanel * panel = nullptr); - void updateValue() - { - lock = true; - if (field) { - setValue(*field); - } - lock = false; - } + signals: + void currentDataChanged(int value); protected slots: - void onValueChanged(int value) - { - if (panel && panel->lock) - return; - if (field && !lock) { - *field = value; - if (panel) { - emit panel->modified(); - } - } - } + void onValueChanged(int value); - protected: - int * field; - GenericPanel * panel; - bool lock; + private: + int *m_field; + + void setFieldInit(GenericPanel * panel); }; diff --git a/companion/src/shared/autotimeedit.cpp b/companion/src/shared/autotimeedit.cpp new file mode 100644 index 000000000..7b0c4422a --- /dev/null +++ b/companion/src/shared/autotimeedit.cpp @@ -0,0 +1,71 @@ +/* + * 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 "autotimeedit.h" + +AutoTimeEdit::AutoTimeEdit(QWidget * parent): + QTimeEdit(parent), + AutoWidget(), + m_field(nullptr) +{ + connect(this, &QTimeEdit::timeChanged, this, &AutoTimeEdit::onTimeChanged); +} + +AutoTimeEdit::~AutoTimeEdit() +{ +} + +void AutoTimeEdit::setField(unsigned int & field, GenericPanel * panel) +{ + m_field = &field; + setPanel(panel); + updateValue(); +} + +void AutoTimeEdit::setTimeRange(const QTime min, const QTime max) +{ + QTimeEdit::setMinimumTime(min); + QTimeEdit::setMaximumTime(max); +} + +void AutoTimeEdit::updateValue() +{ + if (m_field) { + setLock(true); + int hour = *m_field / 3600; + int min = (*m_field - (hour * 3600)) / 60; + int sec = (*m_field - (hour * 3600)) % 60; + setTime(QTime(hour, min, sec)); + setLock(false); + } +} + +void AutoTimeEdit::onTimeChanged(QTime time) +{ + if (!m_field || lock()) + return; + + unsigned int val = time.hour() * 3600 + time.minute() * 60 + time.second(); + if (*m_field != val) { + *m_field = val; + emit currentDataChanged(val); + dataChanged(); + } +} diff --git a/companion/src/shared/autotimeedit.h b/companion/src/shared/autotimeedit.h index 2ba75d70e..e27607ce5 100644 --- a/companion/src/shared/autotimeedit.h +++ b/companion/src/shared/autotimeedit.h @@ -20,80 +20,30 @@ #pragma once -#include -#include "genericpanel.h" +#include "autowidget.h" -class AutoTimeEdit: public QTimeEdit +#include + +class AutoTimeEdit : public QTimeEdit, AutoWidget { Q_OBJECT public: - explicit AutoTimeEdit(QWidget * parent = nullptr): - QTimeEdit(parent), - field(nullptr), - panel(nullptr), - lock(false) - { - connect(this, SIGNAL(timeChanged(QTime)), this, SLOT(onTimeChanged(QTime))); - } + explicit AutoTimeEdit(QWidget * parent = nullptr); + virtual ~AutoTimeEdit(); - void setField(unsigned int & field, GenericPanel * panel = nullptr) - { - this->field = &field; - this->panel = panel; - updateValue(); - } + virtual void updateValue() override; - void setMinimumTime(const QTime time) - { - QTimeEdit::setMinimumTime(time); - } - - void setMaximumTime(const QTime time) - { - QTimeEdit::setMaximumTime(time); - } - - void setEnabled(bool enabled) - { - QTimeEdit::setEnabled(enabled); - } - - void updateValue() - { - if (field) { - lock = true; - int hour = *field / 3600; - int min = (*field - (hour * 3600)) / 60; - int sec = (*field - (hour * 3600)) % 60; - setTime(QTime(hour, min, sec)); - lock = false; - } - } + void setField(unsigned int & field, GenericPanel * panel = nullptr); + void setTimeRange(const QTime min, const QTime max); signals: - void currentDataChanged(unsigned int value); + void currentDataChanged(unsigned int val); protected slots: - void onTimeChanged(QTime time) - { - if (panel && panel->lock) - return; - if (!field || lock) - return; + void onTimeChanged(QTime time); - unsigned int val = time.hour() * 3600 + time.minute() * 60 + time.second(); - if (*field != val) { - *field = val; - emit currentDataChanged(val); - if (panel) - emit panel->modified(); - } - } - - protected: - unsigned int *field = nullptr; - GenericPanel *panel = nullptr; - bool lock = false; + private: + unsigned int *m_field; }; diff --git a/companion/src/shared/autowidget.h b/companion/src/shared/autowidget.h index 68d47547d..9680726b7 100644 --- a/companion/src/shared/autowidget.h +++ b/companion/src/shared/autowidget.h @@ -22,7 +22,7 @@ class GenericPanel; -// Note: cannot be a Qt object as it will create a compiler ambiguity in the inheriting class +// Note: cannot be a Qt object otherwise it will create a compiler ambiguity in the inheriting class class AutoWidget { public: diff --git a/companion/src/shared/genericpanel.h b/companion/src/shared/genericpanel.h index c0ccb949b..f013584bd 100644 --- a/companion/src/shared/genericpanel.h +++ b/companion/src/shared/genericpanel.h @@ -32,17 +32,7 @@ class GenericPanel : public QWidget { Q_OBJECT - friend class AutoComboBox; - friend class AutoDoubleSpinBox; - friend class AutoHexSpinBox; - friend class AutoLineEdit; friend class GVarGroup; - friend class AutoPrecisionComboBox; - friend class AutoBitsetCheckBox; - friend class AutoTimeEdit; - friend class AutoSpinBox; - friend class AutoSlider; - // this should be the only auto friend required excl GVArGroup friend class AutoWidget; public: diff --git a/companion/src/shared/hexspinbox.cpp b/companion/src/shared/hexspinbox.cpp deleted file mode 100644 index 63889ef11..000000000 --- a/companion/src/shared/hexspinbox.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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 "hexspinbox.h" - -HexSpinBox::HexSpinBox(QWidget *parent): - QSpinBox(parent) -{ - validator = new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1,8}"), this); -} - -QValidator::State HexSpinBox::validate(QString &text, int &pos) const -{ - return validator->validate(text, pos); -} - -int HexSpinBox::valueFromText(const QString &text) const -{ - return text.toInt(0, 16); -} - -QString HexSpinBox::textFromValue(int value) const -{ - QString text; - text.sprintf("%04X", value); - return text; -} - - - diff --git a/companion/src/shared/hexspinbox.h b/companion/src/shared/hexspinbox.h deleted file mode 100644 index 204cc8a55..000000000 --- a/companion/src/shared/hexspinbox.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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. - */ - -#pragma once - -#include - -class QRegExpValidator; - -class HexSpinBox : public QSpinBox -{ - Q_OBJECT - - public: - HexSpinBox(QWidget *parent = 0); - - protected: - QValidator::State validate(QString &text, int &pos) const; - int valueFromText(const QString &text) const; - QString textFromValue(int value) const; - - private: - QRegExpValidator *validator; -}; diff --git a/companion/src/shared/verticalscrollarea.h b/companion/src/shared/verticalscrollarea.h index d409a18ca..4f6d2af18 100644 --- a/companion/src/shared/verticalscrollarea.h +++ b/companion/src/shared/verticalscrollarea.h @@ -26,6 +26,8 @@ class GenericPanel; class VerticalScrollArea : public QScrollArea { + Q_OBJECT + public: VerticalScrollArea(QWidget * parent, GenericPanel * panel);