diff --git a/companion/src/shared/CMakeLists.txt b/companion/src/shared/CMakeLists.txt index e54116b3c..d63045061 100644 --- a/companion/src/shared/CMakeLists.txt +++ b/companion/src/shared/CMakeLists.txt @@ -1,5 +1,8 @@ include_directories(${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) +set(shared_NAMES + autocheckbox +) set(shared_SRCS genericpanel.cpp hexspinbox.cpp @@ -8,7 +11,6 @@ set(shared_SRCS set(shared_HDRS autobitsetcheckbox.h - autocheckbox.h autocombobox.h autodoublespinbox.h autohexspinbox.h @@ -21,7 +23,18 @@ set(shared_HDRS autoslider.h ) +foreach(name ${shared_NAMES}) + set(shared_SRCS ${shared_SRCS} ${name}.cpp) + set(shared_HDRS ${shared_HDRS} ${name}.h) +endforeach() + qt5_wrap_cpp(shared_SRCS ${shared_HDRS}) +# add after moc generation to stop warnings +set(shared_SRCS ${shared_SRCS} + autowidget.h + autowidget.cpp +) + add_library(shared ${shared_SRCS}) target_link_libraries(shared PRIVATE Qt5::Widgets ${CPN_COMMON_LIB}) diff --git a/companion/src/shared/autocheckbox.cpp b/companion/src/shared/autocheckbox.cpp new file mode 100644 index 000000000..73b21294c --- /dev/null +++ b/companion/src/shared/autocheckbox.cpp @@ -0,0 +1,67 @@ +/* + * 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 "autocheckbox.h" + +AutoCheckBox::AutoCheckBox(QWidget * parent): + QCheckBox(parent), + AutoWidget(), + m_field(nullptr), + m_invert(false) +{ + connect(this, &QCheckBox::toggled, this, &AutoCheckBox::onToggled); +} + +AutoCheckBox::~AutoCheckBox() +{ +} + +void AutoCheckBox::setField(bool & field, GenericPanel * panel, bool invert) +{ + m_field = &field; + m_invert = invert; + setPanel(panel); + updateValue(); +} + +void AutoCheckBox::setInvert(bool invert) +{ + m_invert = invert; + updateValue(); +} + +void AutoCheckBox::onToggled(bool checked) +{ + if (!lock() && m_field) { + const bool val = m_invert ? !checked : checked; + *m_field = val; + emit currentDataChanged(val); + dataChanged(); + } +} + +void AutoCheckBox::updateValue() +{ + if (m_field) { + setLock(true); + setChecked(m_invert ? !(*m_field) : *m_field); + setLock(false); + } +} diff --git a/companion/src/shared/autocheckbox.h b/companion/src/shared/autocheckbox.h index 6355b6c53..a9a1ef94e 100644 --- a/companion/src/shared/autocheckbox.h +++ b/companion/src/shared/autocheckbox.h @@ -20,72 +20,30 @@ #pragma once -#include -#include "genericpanel.h" +#include "autowidget.h" -class AutoCheckBox: public QCheckBox +#include + +class AutoCheckBox : public QCheckBox, AutoWidget { Q_OBJECT public: - explicit AutoCheckBox(QWidget * parent = nullptr): - QCheckBox(parent), - field(nullptr), - panel(nullptr), - lock(false), - invert(false) - { - connect(this, &AutoCheckBox::toggled, this, &AutoCheckBox::onToggled); - } + explicit AutoCheckBox(QWidget * parent = nullptr); + ~AutoCheckBox(); - void setField(bool & field, GenericPanel * panel = nullptr, bool invert = false) - { - this->field = &field; - this->panel = panel; - this->invert = invert; - updateValue(); - } + virtual void updateValue() override; - void setEnabled(bool enabled) - { - QCheckBox::setEnabled(enabled); - } - - void setInvert(bool invert) - { - this->invert = invert; - updateValue(); - } - - void updateValue() - { - if (field) { - lock = true; - setChecked(invert ? !(*field) : *field); - lock = false; - } - } + void setField(bool & field, GenericPanel * panel = nullptr, bool invert = false); + void setInvert(bool invert); signals: void currentDataChanged(bool value); protected slots: - void onToggled(bool checked) - { - if (panel && panel->lock) - return; - if (field && !lock) { - const bool val = invert ? !checked : checked; - *field = val; - emit currentDataChanged(val); - if (panel) - emit panel->modified(); - } - } + void onToggled(bool checked); - protected: - bool *field = nullptr; - GenericPanel *panel = nullptr; - bool lock = false; - bool invert = false; + private: + bool *m_field; + bool m_invert; }; diff --git a/companion/src/shared/autowidget.cpp b/companion/src/shared/autowidget.cpp new file mode 100644 index 000000000..75fd1fb26 --- /dev/null +++ b/companion/src/shared/autowidget.cpp @@ -0,0 +1,61 @@ +/* + * 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 "autowidget.h" +#include "genericpanel.h" + +AutoWidget::AutoWidget(): + m_panel(nullptr), + m_lock(false) +{ +} + +AutoWidget::~AutoWidget() +{ +} + +bool AutoWidget::lock() +{ + return panelLock() | m_lock; +} + +void AutoWidget::setLock(bool lock) +{ + m_lock = lock; +} + +void AutoWidget::setPanel(GenericPanel * panel) +{ + m_panel = panel; +} + +void AutoWidget::dataChanged() +{ + if (m_panel) + emit m_panel->modified(); +} + +bool AutoWidget::panelLock() +{ + if (m_panel) + return m_panel->lock; + else + return false; +} diff --git a/companion/src/shared/autowidget.h b/companion/src/shared/autowidget.h new file mode 100644 index 000000000..68d47547d --- /dev/null +++ b/companion/src/shared/autowidget.h @@ -0,0 +1,45 @@ +/* + * 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 + +class GenericPanel; + +// Note: cannot be a Qt object as it will create a compiler ambiguity in the inheriting class +class AutoWidget +{ + public: + explicit AutoWidget(); + ~AutoWidget(); + + protected: + virtual void updateValue() = 0; + + bool lock(); + void setLock(bool lock); + void setPanel(GenericPanel * panel); + + void dataChanged(); + bool panelLock(); + + private: + GenericPanel *m_panel; + bool m_lock; +}; diff --git a/companion/src/shared/autowidgets.h b/companion/src/shared/autowidgets.h new file mode 100644 index 000000000..b2619acdb --- /dev/null +++ b/companion/src/shared/autowidgets.h @@ -0,0 +1,32 @@ +/* + * 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 "autobitsetcheckbox.h" +#include "autocheckbox.h" +#include "autocombobox.h" +#include "autodoublespinbox.h" +#include "autohexspinbox.h" +#include "autolineedit.h" +#include "autoprecisioncombobox.h" +#include "autoslider.h" +#include "autospinbox.h" +#include "autotimeedit.h" diff --git a/companion/src/shared/genericpanel.h b/companion/src/shared/genericpanel.h index e55a54371..c0ccb949b 100644 --- a/companion/src/shared/genericpanel.h +++ b/companion/src/shared/genericpanel.h @@ -34,7 +34,6 @@ class GenericPanel : public QWidget friend class AutoComboBox; friend class AutoDoubleSpinBox; - friend class AutoCheckBox; friend class AutoHexSpinBox; friend class AutoLineEdit; friend class GVarGroup; @@ -43,6 +42,8 @@ class GenericPanel : public QWidget friend class AutoTimeEdit; friend class AutoSpinBox; friend class AutoSlider; + // this should be the only auto friend required excl GVArGroup + friend class AutoWidget; public: GenericPanel(QWidget *parent, ModelData * model, GeneralSettings & generalSettings, Firmware * firmware);