chore(cpn): implement ui bindings to auto widgets and apply to timers (#7331)
This commit is contained in:
@@ -4,6 +4,7 @@ project(datamodels)
|
||||
set(${PROJECT_NAME}_NAMES
|
||||
compounditemmodels
|
||||
filtereditemmodels
|
||||
itemmodeleventhandler
|
||||
)
|
||||
|
||||
AddHeadersSources()
|
||||
|
||||
@@ -19,8 +19,7 @@
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef FILTEREDITEMMODELS_H
|
||||
#define FILTEREDITEMMODELS_H
|
||||
#pragma once
|
||||
|
||||
#include "compounditemmodels.h"
|
||||
|
||||
@@ -112,5 +111,3 @@ class CurveRefFilteredFactory : public FilteredItemModelFactory
|
||||
|
||||
static QString fidToString(const int value);
|
||||
};
|
||||
|
||||
#endif // FILTEREDITEMMODELS_H
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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 "itemmodeleventhandler.h"
|
||||
|
||||
ItemModelEventHandler::ItemModelEventHandler(FilteredItemModel * itemModel,
|
||||
bool & lock,
|
||||
std::function<void()> updateCallback) :
|
||||
QObject(nullptr),
|
||||
m_lock(lock),
|
||||
m_updateCallback(std::move(updateCallback)),
|
||||
m_cnt(0)
|
||||
{
|
||||
connect(itemModel, &FilteredItemModel::aboutToBeUpdated, this, [this] ()
|
||||
{
|
||||
m_lock = true;
|
||||
m_cnt++;
|
||||
}
|
||||
);
|
||||
|
||||
connect(itemModel, &FilteredItemModel::updateComplete, this, [this] ()
|
||||
{
|
||||
m_cnt--;
|
||||
|
||||
if (m_cnt < 1) {
|
||||
if (m_updateCallback)
|
||||
m_updateCallback();
|
||||
m_lock = false;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* 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 "filtereditemmodels.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <functional>
|
||||
|
||||
class ItemModelEventHandler : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit ItemModelEventHandler(FilteredItemModel * filteredModel,
|
||||
bool & lock, std::function<void()> callback);
|
||||
virtual ~ItemModelEventHandler() = default;
|
||||
|
||||
private:
|
||||
bool &m_lock;
|
||||
std::function<void()> m_updateCallback;
|
||||
int m_cnt;
|
||||
};
|
||||
@@ -23,52 +23,82 @@
|
||||
#include "ui_setup_timer.h"
|
||||
#include "namevalidator.h"
|
||||
|
||||
TimerPanel::TimerPanel(QWidget * parent, ModelData & model, TimerData & timer, GeneralSettings & generalSettings, Firmware * firmware,
|
||||
QWidget * prevFocus, FilteredItemModelFactory * panelFilteredModels, CompoundItemModelFactory * panelItemModels):
|
||||
TimerPanel::TimerPanel(QWidget * parent, ModelData & model, TimerData & timer,
|
||||
GeneralSettings & generalSettings, Firmware * firmware,
|
||||
QWidget * prevFocus,
|
||||
FilteredItemModelFactory * panelFilteredModels,
|
||||
CompoundItemModelFactory * panelItemModels):
|
||||
ModelPanel(parent, model, generalSettings, firmware),
|
||||
timer(timer),
|
||||
ui(new Ui::Timer),
|
||||
modelsUpdateCnt(0)
|
||||
ui(new Ui::Timer)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
connectItemModelEvents(panelFilteredModels->getItemModel(FIM_TIMERSWITCH));
|
||||
|
||||
lock = true;
|
||||
imEventHandler = new ItemModelEventHandler(
|
||||
panelFilteredModels->getItemModel(FIM_TIMERSWITCH),
|
||||
lock,
|
||||
[this] { this->update(); }
|
||||
);
|
||||
|
||||
// Name
|
||||
int length = firmware->getCapability(TimersName);
|
||||
if (length == 0)
|
||||
ui->name->hide();
|
||||
else {
|
||||
ui->name->setValidator(new NameValidator(firmware->getBoard(), this));
|
||||
ui->name->setField(timer.name, length, this);
|
||||
connect(ui->name, SIGNAL(currentDataChanged()), this, SLOT(onNameChanged()));
|
||||
}
|
||||
|
||||
ui->value->setField(timer.val, this);
|
||||
ui->value->setMaximumTime(firmware->getMaxTimerStart());
|
||||
connect(ui->value, &AutoTimeEdit::currentDataChanged, [=](const int val) {
|
||||
update();
|
||||
ui->name->setValidator(new NameValidator(firmware->getBoard(), this));
|
||||
ui->name->setField(timer.name, firmware->getCapability(TimersName), this);
|
||||
ui->name->setBindPostChanged([this] {
|
||||
emit this->nameChanged();
|
||||
});
|
||||
|
||||
// Start
|
||||
ui->value->setField(timer.val, this);
|
||||
ui->value->setMaximumTime(firmware->getMaxTimerStart());
|
||||
ui->value->setBindPostChanged([this]{
|
||||
this->update();
|
||||
});
|
||||
|
||||
// Mode
|
||||
ui->mode->setModel(panelItemModels->getItemModel(AIM_TIMER_MODE));
|
||||
ui->mode->setField(timer.mode, this);
|
||||
connect(ui->mode, SIGNAL(currentDataChanged(int)), this, SLOT(onModeChanged(int)));
|
||||
ui->mode->setBindPostChanged([this] {
|
||||
this->timer.modeChanged();
|
||||
this->update();
|
||||
emit this->modeChanged();
|
||||
});
|
||||
|
||||
// Switch
|
||||
ui->swtch->setModel(panelFilteredModels->getItemModel(FIM_TIMERSWITCH));
|
||||
ui->swtch->setField(timer.swtch, this);
|
||||
ui->swtch->setBindEnabled([this] {
|
||||
return this->timer.mode != TimerData::TIMERMODE_OFF;
|
||||
});
|
||||
|
||||
// Countdown
|
||||
ui->countdownBeep->setModel(panelItemModels->getItemModel(AIM_TIMER_COUNTDOWNBEEP));
|
||||
ui->countdownBeep->setField(timer.countdownBeep, this);
|
||||
connect(ui->countdownBeep, SIGNAL(currentDataChanged(int)), this, SLOT(onCountdownBeepChanged(int)));
|
||||
|
||||
ui->minuteBeep->setField(timer.minuteBeep, this);
|
||||
ui->persistent->setModel(panelItemModels->getItemModel(AIM_TIMER_PERSISTENT));
|
||||
ui->persistent->setField(timer.persistent, this);
|
||||
ui->countdownBeep->setBindPostChanged([this] {
|
||||
this->timer.countdownBeepChanged();
|
||||
this->update();
|
||||
});
|
||||
ui->countdownStart->setModel(panelItemModels->getItemModel(AIM_TIMER_COUNTDOWNSTART));
|
||||
ui->countdownStart->setField(timer.countdownStart, this);
|
||||
ui->countdownStart->setBindEnabled([this] {
|
||||
return this->timer.countdownBeep != TimerData::COUNTDOWNBEEP_SILENT;
|
||||
});
|
||||
ui->countdownStart->addBuddyWidget(ui->countdownStartLabel);
|
||||
|
||||
// Minute call
|
||||
ui->minuteBeep->setField(timer.minuteBeep, this);
|
||||
|
||||
// Persistence
|
||||
ui->persistent->setModel(panelItemModels->getItemModel(AIM_TIMER_PERSISTENT));
|
||||
ui->persistent->setField(timer.persistent, this);
|
||||
|
||||
// Time type
|
||||
ui->showElapsed->setModel(panelItemModels->getItemModel(AIM_TIMER_SHOWELAPSED));
|
||||
ui->showElapsed->setField(timer.showElapsed, this);
|
||||
ui->showElapsed->setBindEnabled([this] { return this->timer.val; });
|
||||
|
||||
// Time
|
||||
// do not use setField as the raw value is meaningless
|
||||
ui->persistentValue->setBindText([this] { return this->timer.pvalueToString(); });
|
||||
|
||||
disableMouseScrolling();
|
||||
QWidget::setTabOrder(prevFocus, ui->name);
|
||||
@@ -81,51 +111,18 @@ TimerPanel::TimerPanel(QWidget * parent, ModelData & model, TimerData & timer, G
|
||||
QWidget::setTabOrder(ui->persistent, ui->showElapsed);
|
||||
|
||||
update();
|
||||
lock = false;
|
||||
}
|
||||
|
||||
TimerPanel::~TimerPanel()
|
||||
{
|
||||
delete ui;
|
||||
delete imEventHandler;
|
||||
}
|
||||
|
||||
void TimerPanel::update()
|
||||
{
|
||||
lock = true;
|
||||
|
||||
ui->name->updateValue();
|
||||
ui->mode->updateValue();
|
||||
ui->swtch->updateValue();
|
||||
ui->value->updateValue();
|
||||
ui->countdownBeep->updateValue();
|
||||
ui->minuteBeep->updateValue();
|
||||
ui->countdownStart->updateValue();
|
||||
ui->showElapsed->updateValue();
|
||||
|
||||
if (timer.mode != TimerData::TIMERMODE_OFF)
|
||||
ui->swtch->setEnabled(true);
|
||||
else
|
||||
ui->swtch->setEnabled(false);
|
||||
|
||||
if (timer.countdownBeep == TimerData::COUNTDOWNBEEP_SILENT) {
|
||||
ui->countdownStartLabel->setEnabled(false);
|
||||
ui->countdownStart->setEnabled(false);
|
||||
}
|
||||
else {
|
||||
ui->countdownStartLabel->setEnabled(true);
|
||||
ui->countdownStart->setEnabled(true);
|
||||
}
|
||||
|
||||
ui->persistent->updateValue();
|
||||
ui->persistentValue->setText(timer.pvalueToString());
|
||||
|
||||
if (timer.val) {
|
||||
ui->showElapsed->setEnabled(true);
|
||||
}
|
||||
else {
|
||||
ui->showElapsed->setEnabled(false);
|
||||
}
|
||||
|
||||
updateAutoWidgets();
|
||||
lock = false;
|
||||
}
|
||||
|
||||
@@ -133,42 +130,3 @@ QWidget * TimerPanel::getLastFocus()
|
||||
{
|
||||
return ui->persistent;
|
||||
}
|
||||
|
||||
void TimerPanel::connectItemModelEvents(const FilteredItemModel * itemModel)
|
||||
{
|
||||
connect(itemModel, &FilteredItemModel::aboutToBeUpdated, this, &TimerPanel::onItemModelAboutToBeUpdated);
|
||||
connect(itemModel, &FilteredItemModel::updateComplete, this, &TimerPanel::onItemModelUpdateComplete);
|
||||
}
|
||||
|
||||
void TimerPanel::onItemModelAboutToBeUpdated()
|
||||
{
|
||||
lock = true;
|
||||
modelsUpdateCnt++;
|
||||
}
|
||||
|
||||
void TimerPanel::onItemModelUpdateComplete()
|
||||
{
|
||||
modelsUpdateCnt--;
|
||||
if (modelsUpdateCnt < 1) {
|
||||
update();
|
||||
lock = false;
|
||||
}
|
||||
}
|
||||
|
||||
void TimerPanel::onNameChanged()
|
||||
{
|
||||
emit nameChanged();
|
||||
}
|
||||
|
||||
void TimerPanel::onCountdownBeepChanged(int index)
|
||||
{
|
||||
timer.countdownBeepChanged();
|
||||
update();
|
||||
}
|
||||
|
||||
void TimerPanel::onModeChanged(int index)
|
||||
{
|
||||
timer.modeChanged();
|
||||
update();
|
||||
emit modeChanged();
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
#include "modeledit.h"
|
||||
#include "eeprominterface.h"
|
||||
#include "filtereditemmodels.h"
|
||||
#include "itemmodeleventhandler.h"
|
||||
|
||||
constexpr char FIM_TIMERSWITCH[] {"Timer Switch"};
|
||||
constexpr char MIMETYPE_TIMER[] {"application/x-companion-timer"};
|
||||
@@ -44,15 +44,6 @@ class TimerPanel : public ModelPanel
|
||||
virtual void update();
|
||||
QWidget * getLastFocus();
|
||||
|
||||
public slots:
|
||||
void onNameChanged();
|
||||
void onModeChanged(int index);
|
||||
|
||||
private slots:
|
||||
void onItemModelAboutToBeUpdated();
|
||||
void onItemModelUpdateComplete();
|
||||
void onCountdownBeepChanged(int index);
|
||||
|
||||
signals:
|
||||
void nameChanged();
|
||||
void modeChanged();
|
||||
@@ -60,6 +51,5 @@ class TimerPanel : public ModelPanel
|
||||
private:
|
||||
TimerData & timer;
|
||||
Ui::Timer * ui;
|
||||
void connectItemModelEvents(const FilteredItemModel * itemModel);
|
||||
int modelsUpdateCnt;
|
||||
ItemModelEventHandler *imEventHandler;
|
||||
};
|
||||
|
||||
@@ -48,7 +48,7 @@
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="currentSection">
|
||||
<enum>QDateTimeEdit::HourSection</enum>
|
||||
<enum>QDateTimeEdit::Section::HourSection</enum>
|
||||
</property>
|
||||
<property name="displayFormat">
|
||||
<string notr="true">hh:mm:ss</string>
|
||||
@@ -62,7 +62,7 @@
|
||||
<widget class="AutoComboBox" name="swtch"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="countdownBeepLabel">
|
||||
<widget class="AutoLabel" name="countdownBeepLabel">
|
||||
<property name="text">
|
||||
<string>Countdown</string>
|
||||
</property>
|
||||
@@ -72,7 +72,7 @@
|
||||
<widget class="AutoComboBox" name="countdownBeep"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="countdownStartLabel">
|
||||
<widget class="AutoLabel" name="countdownStartLabel">
|
||||
<property name="text">
|
||||
<string>Start</string>
|
||||
</property>
|
||||
@@ -95,7 +95,7 @@
|
||||
<widget class="AutoComboBox" name="showElapsed"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="persistentValue">
|
||||
<widget class="AutoLabel" name="persistentValue">
|
||||
<property name="text">
|
||||
<string notr="true"> (00:00:00)</string>
|
||||
</property>
|
||||
@@ -104,7 +104,7 @@
|
||||
<item>
|
||||
<spacer name="spacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
<enum>Qt::Orientation::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
@@ -137,6 +137,11 @@
|
||||
<extends>QTimeEdit</extends>
|
||||
<header>autotimeedit.h</header>
|
||||
</customwidget>
|
||||
<customwidget>
|
||||
<class>AutoLabel</class>
|
||||
<extends>QLabel</extends>
|
||||
<header>autolabel.h</header>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<resources/>
|
||||
<connections/>
|
||||
|
||||
@@ -22,6 +22,7 @@ set(${PROJECT_NAME}_NAMES
|
||||
scrollarea
|
||||
textvalidator
|
||||
exclusivecombogroup
|
||||
widgetbindings
|
||||
)
|
||||
|
||||
AddHeadersSources()
|
||||
|
||||
@@ -76,7 +76,7 @@ void AutoBitMappedCheckBox::onToggled(bool checked)
|
||||
unsigned int fieldmask = (bitmask() << shiftbits());
|
||||
*m_field = (*m_field & ~fieldmask) | (val << shiftbits());
|
||||
emit currentDataChanged(val);
|
||||
dataChanged();
|
||||
runPostChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,3 +101,8 @@ unsigned int AutoBitMappedCheckBox::bitmask()
|
||||
mask = ~(mask << m_bits);
|
||||
return (unsigned int)mask;
|
||||
}
|
||||
|
||||
void AutoBitMappedCheckBox::setAutoText(QString text)
|
||||
{
|
||||
QCheckBox::setText(text);
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ class AutoBitMappedCheckBox : public QCheckBox, public AutoWidget
|
||||
virtual ~AutoBitMappedCheckBox();
|
||||
|
||||
virtual void updateValue() override;
|
||||
void setBindModel(std::function<QAbstractItemModel*()> fn) = delete;
|
||||
|
||||
void setField(int & field, GenericPanel * panel = nullptr, bool invert = false);
|
||||
void setField(unsigned int & field, GenericPanel * panel = nullptr, bool invert = false);
|
||||
@@ -47,6 +48,9 @@ class AutoBitMappedCheckBox : public QCheckBox, public AutoWidget
|
||||
protected slots:
|
||||
void onToggled(bool checked);
|
||||
|
||||
protected:
|
||||
virtual void setAutoText(QString text) override;
|
||||
|
||||
private:
|
||||
int *m_field;
|
||||
bool m_invert;
|
||||
|
||||
@@ -78,6 +78,11 @@ void AutoBitMappedComboBox::addItem(const QString & item, int value)
|
||||
updateValue();
|
||||
}
|
||||
|
||||
void AutoBitMappedComboBox::setAutoModel(QAbstractItemModel * model)
|
||||
{
|
||||
AutoBitMappedComboBox::setModel(model);
|
||||
}
|
||||
|
||||
void AutoBitMappedComboBox::setField(unsigned int & field, GenericPanel * panel)
|
||||
{
|
||||
m_field = (int *)&field;
|
||||
@@ -130,11 +135,13 @@ void AutoBitMappedComboBox::setBits(const unsigned int numBits, const unsigned i
|
||||
|
||||
void AutoBitMappedComboBox::setModel(QAbstractItemModel * model)
|
||||
{
|
||||
setLock(true);
|
||||
QComboBox::setModel(model);
|
||||
setLock(false);
|
||||
m_hasModel = true;
|
||||
updateValue();
|
||||
if (model && QComboBox::model() != model) {
|
||||
setLock(true);
|
||||
QComboBox::setModel(model);
|
||||
setLock(false);
|
||||
m_hasModel = true;
|
||||
updateValue();
|
||||
}
|
||||
}
|
||||
|
||||
void AutoBitMappedComboBox::setAutoIndexes()
|
||||
@@ -190,7 +197,7 @@ void AutoBitMappedComboBox::onCurrentIndexChanged(int index)
|
||||
return;
|
||||
|
||||
emit currentDataChanged(val);
|
||||
dataChanged();
|
||||
runPostChanged();
|
||||
}
|
||||
|
||||
unsigned int AutoBitMappedComboBox::shiftbits()
|
||||
|
||||
@@ -41,6 +41,8 @@ class AutoBitMappedComboBox : public QComboBox, public AutoWidget
|
||||
virtual void insertItems(int index, const QStringList & items);
|
||||
// AutoWidget
|
||||
virtual void updateValue() override;
|
||||
virtual void setAutoModel(QAbstractItemModel * model);
|
||||
void setBindText(std::function<QString()> fn) = delete;
|
||||
|
||||
void clear();
|
||||
|
||||
|
||||
@@ -145,7 +145,7 @@ void AutoBitsetCheckBox::onToggled(bool checked)
|
||||
}
|
||||
|
||||
emit currentDataChanged(checked);
|
||||
dataChanged();
|
||||
runPostChanged();
|
||||
}
|
||||
|
||||
void AutoBitsetCheckBox::init()
|
||||
@@ -160,3 +160,8 @@ void AutoBitsetCheckBox::initField(int bitmask, bool invert, int toggleMask)
|
||||
m_invert = toggleMask ? false : invert;
|
||||
updateValue();
|
||||
}
|
||||
|
||||
void AutoBitsetCheckBox::setAutoText(QString text)
|
||||
{
|
||||
QCheckBox::setText(text);
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ class AutoBitsetCheckBox: public QCheckBox, public AutoWidget
|
||||
virtual ~AutoBitsetCheckBox();
|
||||
|
||||
virtual void updateValue() override;
|
||||
void setBindModel(std::function<QAbstractItemModel*()> fn) = delete;
|
||||
|
||||
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);
|
||||
@@ -59,6 +60,9 @@ class AutoBitsetCheckBox: public QCheckBox, public AutoWidget
|
||||
protected slots:
|
||||
void onToggled(bool checked);
|
||||
|
||||
protected:
|
||||
virtual void setAutoText(QString text) override;
|
||||
|
||||
private:
|
||||
int *m_field = nullptr;
|
||||
int m_bitmask = 0;
|
||||
|
||||
@@ -34,6 +34,16 @@ AutoCheckBox::~AutoCheckBox()
|
||||
{
|
||||
}
|
||||
|
||||
void AutoCheckBox::onToggled(bool checked)
|
||||
{
|
||||
if (m_field && !lock()) {
|
||||
const bool val = m_invert ? !checked : checked;
|
||||
*m_field = val;
|
||||
emit currentDataChanged(val);
|
||||
runPostChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void AutoCheckBox::setField(bool & field, GenericPanel * panel, bool invert)
|
||||
{
|
||||
m_field = &field;
|
||||
@@ -48,14 +58,9 @@ void AutoCheckBox::setInvert(bool invert)
|
||||
updateValue();
|
||||
}
|
||||
|
||||
void AutoCheckBox::onToggled(bool checked)
|
||||
void AutoCheckBox::setAutoText(QString text)
|
||||
{
|
||||
if (m_field && !lock()) {
|
||||
const bool val = m_invert ? !checked : checked;
|
||||
*m_field = val;
|
||||
emit currentDataChanged(val);
|
||||
dataChanged();
|
||||
}
|
||||
QCheckBox::setText(text);
|
||||
}
|
||||
|
||||
void AutoCheckBox::updateValue()
|
||||
|
||||
@@ -34,6 +34,7 @@ class AutoCheckBox : public QCheckBox, public AutoWidget
|
||||
virtual ~AutoCheckBox();
|
||||
|
||||
virtual void updateValue() override;
|
||||
void setBindModel(std::function<QAbstractItemModel*()> fn) = delete;
|
||||
|
||||
void setField(bool & field, GenericPanel * panel = nullptr, bool invert = false);
|
||||
void setInvert(bool invert);
|
||||
@@ -44,6 +45,9 @@ class AutoCheckBox : public QCheckBox, public AutoWidget
|
||||
protected slots:
|
||||
void onToggled(bool checked);
|
||||
|
||||
protected:
|
||||
virtual void setAutoText(QString text) override;
|
||||
|
||||
private:
|
||||
bool *m_field;
|
||||
bool m_invert;
|
||||
|
||||
@@ -82,6 +82,11 @@ void AutoComboBox::addItem(const QString & item, int value)
|
||||
}
|
||||
}
|
||||
|
||||
void AutoComboBox::setAutoModel(QAbstractItemModel * model)
|
||||
{
|
||||
AutoComboBox::setModel(model);
|
||||
}
|
||||
|
||||
void AutoComboBox::setField(unsigned int & field, GenericPanel * panel)
|
||||
{
|
||||
setFieldInit(panel);
|
||||
@@ -153,11 +158,13 @@ void AutoComboBox::setFieldInit(GenericPanel * panel)
|
||||
|
||||
void AutoComboBox::setModel(QAbstractItemModel * model)
|
||||
{
|
||||
setLock(true);
|
||||
QComboBox::setModel(model);
|
||||
setLock(false);
|
||||
m_hasModel = true;
|
||||
updateValue();
|
||||
if (model && QComboBox::model() != model) {
|
||||
setLock(true);
|
||||
QComboBox::setModel(model);
|
||||
setLock(false);
|
||||
m_hasModel = true;
|
||||
updateValue();
|
||||
}
|
||||
}
|
||||
|
||||
void AutoComboBox::setAutoIndexes()
|
||||
@@ -231,6 +238,6 @@ void AutoComboBox::onCurrentIndexChanged(int index)
|
||||
return;
|
||||
|
||||
emit currentDataChanged(val);
|
||||
dataChanged();
|
||||
runPostChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,6 +49,8 @@ class AutoComboBox : public QComboBox, public AutoWidget
|
||||
virtual void insertItems(int index, const QStringList & items);
|
||||
// AutoWidget
|
||||
virtual void updateValue() override;
|
||||
virtual void setAutoModel(QAbstractItemModel * model);
|
||||
void setBindText(std::function<QString()> fn) = delete;
|
||||
|
||||
void clear();
|
||||
|
||||
|
||||
@@ -88,6 +88,6 @@ void AutoDoubleSpinBox::onValueChanged(double value)
|
||||
if (m_field && !lock()) {
|
||||
*m_field = round(value * multiplier() - m_offset);
|
||||
emit currentDataChanged(*m_field);
|
||||
dataChanged();
|
||||
runPostChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,8 @@ class AutoDoubleSpinBox : public QDoubleSpinBox, public AutoWidget
|
||||
virtual ~AutoDoubleSpinBox();
|
||||
|
||||
virtual void updateValue() override;
|
||||
void setBindModel(std::function<QAbstractItemModel*()> fn) = delete;
|
||||
void setBindText(std::function<QString()> fn) = delete;
|
||||
|
||||
void setField(int & field, GenericPanel * panel = nullptr);
|
||||
void setField(unsigned int & field, GenericPanel * panel = nullptr);
|
||||
|
||||
@@ -75,7 +75,7 @@ void AutoHexSpinBox::onValueChanged(int value)
|
||||
if (*m_field != (unsigned)value) {
|
||||
*m_field = (unsigned)value;
|
||||
emit currentDataChanged(value);
|
||||
dataChanged();
|
||||
runPostChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,8 @@ class AutoHexSpinBox : public QSpinBox, public AutoWidget
|
||||
virtual ~AutoHexSpinBox();
|
||||
|
||||
virtual void updateValue();
|
||||
void setBindModel(std::function<QAbstractItemModel*()> fn) = delete;
|
||||
void setBindText(std::function<QString()> fn) = delete;
|
||||
|
||||
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);
|
||||
|
||||
@@ -61,8 +61,13 @@ void AutoLabel::updateValue()
|
||||
{
|
||||
setLock(true);
|
||||
if (m_strField)
|
||||
setText(*m_strField);
|
||||
QLabel::setText(*m_strField);
|
||||
else if (m_charField)
|
||||
setText(m_charField);
|
||||
QLabel::setText(m_charField);
|
||||
setLock(false);
|
||||
}
|
||||
|
||||
void AutoLabel::setAutoText(QString text)
|
||||
{
|
||||
QLabel::setText(text);
|
||||
}
|
||||
|
||||
@@ -34,11 +34,15 @@ class AutoLabel: public QLabel, public AutoWidget
|
||||
virtual ~AutoLabel();
|
||||
|
||||
virtual void updateValue() override;
|
||||
void setBindModel(std::function<QAbstractItemModel*()> fn) = delete;
|
||||
|
||||
void setField(char * field, GenericPanel * panel = nullptr);
|
||||
void setField(QString & field, GenericPanel * panel = nullptr);
|
||||
void setWidth(int numChars);
|
||||
|
||||
protected:
|
||||
virtual void setAutoText(QString text) override;
|
||||
|
||||
private:
|
||||
char *m_charField;
|
||||
QString *m_strField;
|
||||
|
||||
@@ -61,9 +61,9 @@ void AutoLineEdit::updateValue()
|
||||
{
|
||||
setLock(true);
|
||||
if (m_strField)
|
||||
setText(*m_strField);
|
||||
QLineEdit::setText(*m_strField);
|
||||
else if (m_charField)
|
||||
setText(m_charField);
|
||||
QLineEdit::setText(m_charField);
|
||||
setLock(false);
|
||||
}
|
||||
|
||||
@@ -80,5 +80,12 @@ void AutoLineEdit::onEdited()
|
||||
return;
|
||||
|
||||
emit currentDataChanged();
|
||||
dataChanged();
|
||||
runPostChanged();
|
||||
}
|
||||
|
||||
void AutoLineEdit::setAutoText(QString text)
|
||||
{
|
||||
setLock(true);
|
||||
QLineEdit::setText(text);
|
||||
setLock(false);
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ class AutoLineEdit: public QLineEdit, public AutoWidget
|
||||
virtual ~AutoLineEdit();
|
||||
|
||||
virtual void updateValue() override;
|
||||
void setBindModel(std::function<QAbstractItemModel*()> fn) = delete;
|
||||
|
||||
void setField(char * field, int len, GenericPanel * panel = nullptr);
|
||||
void setField(QString & field, int len = 0, GenericPanel * panel = nullptr);
|
||||
@@ -44,6 +45,9 @@ class AutoLineEdit: public QLineEdit, public AutoWidget
|
||||
protected slots:
|
||||
void onEdited();
|
||||
|
||||
protected:
|
||||
virtual void setAutoText(QString text) override;
|
||||
|
||||
private:
|
||||
char *m_charField;
|
||||
QString *m_strField;
|
||||
|
||||
@@ -114,9 +114,9 @@ void AutoPrecisionComboBox::setValue(int value)
|
||||
const unsigned int val = (unsigned int)value;
|
||||
if (*m_field != val) {
|
||||
*m_field = rangecheckDecimals(val);
|
||||
updateValue();
|
||||
emit currentDataChanged(value);
|
||||
dataChanged();
|
||||
runPostChanged();
|
||||
updateValue();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,9 +127,9 @@ void AutoPrecisionComboBox::setValue(unsigned int value)
|
||||
|
||||
if (*m_field != value) {
|
||||
*m_field = rangecheckDecimals(value);
|
||||
updateValue();
|
||||
emit currentDataChanged((int)value);
|
||||
dataChanged();
|
||||
runPostChanged();
|
||||
updateValue();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,7 +203,7 @@ void AutoPrecisionComboBox::onCurrentIndexChanged(int index)
|
||||
if (*m_field != itemData(index).toUInt()) {
|
||||
*m_field = itemData(index).toUInt();
|
||||
emit currentDataChanged(index);
|
||||
dataChanged();
|
||||
runPostChanged();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,6 +37,8 @@ class AutoPrecisionComboBox : public QComboBox, public AutoWidget
|
||||
virtual ~AutoPrecisionComboBox();
|
||||
|
||||
virtual void updateValue() override;
|
||||
void setBindModel(std::function<QAbstractItemModel*()> fn) = delete;
|
||||
void setBindText(std::function<QString()> fn) = delete;
|
||||
|
||||
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);
|
||||
|
||||
@@ -84,7 +84,7 @@ void AutoSlider::onValueChanged(int value)
|
||||
if (*m_field != value) {
|
||||
*m_field = value;
|
||||
emit currentDataChanged(value);
|
||||
dataChanged();
|
||||
runPostChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,8 @@ class AutoSlider : public QSlider, public AutoWidget
|
||||
virtual ~AutoSlider();
|
||||
|
||||
virtual void updateValue() override;
|
||||
void setBindModel(std::function<QAbstractItemModel*()> fn) = delete;
|
||||
void setBindText(std::function<QString()> fn) = delete;
|
||||
|
||||
void setField(int & field, int min, int max, GenericPanel * panel = nullptr);
|
||||
void setField(unsigned int & field, int min, int max, GenericPanel * panel = nullptr);
|
||||
|
||||
@@ -66,7 +66,7 @@ void AutoSpinBox::onValueChanged(int value)
|
||||
if (*m_field != value) {
|
||||
*m_field = value;
|
||||
emit currentDataChanged(value);
|
||||
dataChanged();
|
||||
runPostChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,8 @@ class AutoSpinBox : public QSpinBox, public AutoWidget
|
||||
virtual ~AutoSpinBox();
|
||||
|
||||
virtual void updateValue() override;
|
||||
void setBindModel(std::function<QAbstractItemModel*()> fn) = delete;
|
||||
void setBindText(std::function<QString()> fn) = delete;
|
||||
|
||||
void setField(int & field, GenericPanel * panel = nullptr);
|
||||
void setField(unsigned int & field, GenericPanel * panel = nullptr);
|
||||
|
||||
@@ -67,6 +67,6 @@ void AutoTimeEdit::onTimeChanged(QTime time)
|
||||
if (*m_field != val) {
|
||||
*m_field = val;
|
||||
emit currentDataChanged(val);
|
||||
dataChanged();
|
||||
runPostChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,8 @@ class AutoTimeEdit : public QTimeEdit, public AutoWidget
|
||||
virtual ~AutoTimeEdit();
|
||||
|
||||
virtual void updateValue() override;
|
||||
void setBindModel(std::function<QAbstractItemModel*()> fn) = delete;
|
||||
void setBindText(std::function<QString()> fn) = delete;
|
||||
|
||||
void setField(unsigned int & field, GenericPanel * panel = nullptr);
|
||||
void setTimeRange(const QTime min, const QTime max);
|
||||
|
||||
@@ -22,14 +22,112 @@
|
||||
#include "autowidget.h"
|
||||
#include "genericpanel.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
AutoWidget::AutoWidget():
|
||||
m_panel(nullptr),
|
||||
m_lock(false)
|
||||
m_lock(false),
|
||||
m_buddyWidgets(QList<AutoWidget *> {nullptr}),
|
||||
m_enabled(nullptr),
|
||||
m_model(nullptr),
|
||||
m_text(nullptr),
|
||||
m_visible(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
AutoWidget::~AutoWidget()
|
||||
AutoWidget::~AutoWidget() = default;
|
||||
|
||||
void AutoWidget::addBuddyWidget(AutoWidget * wgt)
|
||||
{
|
||||
if (wgt) {
|
||||
m_buddyWidgets.append(wgt);
|
||||
// just in case set as we want parent to control
|
||||
clearBuddyBinds(wgt);
|
||||
}
|
||||
}
|
||||
|
||||
void AutoWidget::addBuddyWidgets(QList<AutoWidget *> wgts)
|
||||
{
|
||||
m_buddyWidgets.append(wgts);
|
||||
// just in case set as we want parent to control
|
||||
for (const auto wgt: m_buddyWidgets) {
|
||||
if (wgt) clearBuddyBinds(wgt);
|
||||
}
|
||||
}
|
||||
|
||||
// Apply bindings to this AutoWidget also enabled and visible to all buddy AutoWidgets
|
||||
void AutoWidget::applyBindings()
|
||||
{
|
||||
if (m_enabled) {
|
||||
// buddy widget has no bind so no code executed when it is updated
|
||||
// so apply parent value
|
||||
bool val = m_enabled();
|
||||
setAutoEnabled(val);
|
||||
|
||||
for (const auto wgt: m_buddyWidgets) {
|
||||
if (wgt) wgt->setAutoEnabled(val);
|
||||
}
|
||||
}
|
||||
|
||||
if (m_visible) {
|
||||
// buddy widget has no bind so no code executed when it is updated
|
||||
// so apply parent value
|
||||
bool val = m_visible();
|
||||
setAutoVisible(val);
|
||||
|
||||
for (const auto wgt: m_buddyWidgets) {
|
||||
if (wgt) wgt->setAutoVisible(val);
|
||||
}
|
||||
}
|
||||
|
||||
if (m_text) {
|
||||
// makes no sense to apply parent bind to buddies
|
||||
// leave it to each buddy to apply when it is updated
|
||||
setAutoText(m_text());
|
||||
}
|
||||
|
||||
if (m_model) {
|
||||
// makes no sense to apply parent bind to buddies
|
||||
// leave it to each buddy to apply when it is updated
|
||||
setAutoModel(m_model());
|
||||
}
|
||||
}
|
||||
|
||||
void AutoWidget::clearBindEnabled()
|
||||
{
|
||||
m_enabled = nullptr;
|
||||
}
|
||||
|
||||
void AutoWidget::clearBindVisible()
|
||||
{
|
||||
m_visible = nullptr;
|
||||
}
|
||||
|
||||
void AutoWidget::clearBuddyBinds(AutoWidget * wgt)
|
||||
{
|
||||
if (wgt) {
|
||||
wgt->clearBindEnabled();
|
||||
wgt->clearBindVisible();
|
||||
}
|
||||
}
|
||||
|
||||
void AutoWidget::clearBuddyWidget(AutoWidget * wgt)
|
||||
{
|
||||
if (wgt) {
|
||||
if (!m_buddyWidgets.removeOne(wgt))
|
||||
qDebug() << "Warning: widget not removed from list";
|
||||
}
|
||||
}
|
||||
|
||||
void AutoWidget::clearBuddyWidgets()
|
||||
{
|
||||
m_buddyWidgets.clear();
|
||||
}
|
||||
|
||||
void AutoWidget::dataChanged()
|
||||
{
|
||||
if (m_panel)
|
||||
emit m_panel->modified();
|
||||
}
|
||||
|
||||
bool AutoWidget::lock()
|
||||
@@ -37,6 +135,56 @@ bool AutoWidget::lock()
|
||||
return panelLock() | m_lock;
|
||||
}
|
||||
|
||||
bool AutoWidget::panelLock()
|
||||
{
|
||||
return m_panel ? m_panel->lock : false;
|
||||
}
|
||||
|
||||
void AutoWidget::runPostChanged()
|
||||
{
|
||||
dataChanged();
|
||||
|
||||
if (m_postChanged)
|
||||
m_postChanged();
|
||||
}
|
||||
|
||||
void AutoWidget::setAutoEnabled(bool enabled)
|
||||
{
|
||||
QWidget *wgt = dynamic_cast<QWidget *>(this);
|
||||
if (wgt) wgt->setEnabled(enabled);
|
||||
}
|
||||
|
||||
void AutoWidget::setAutoVisible(bool visible)
|
||||
{
|
||||
QWidget *wgt = dynamic_cast<QWidget *>(this);
|
||||
if (wgt) wgt->setVisible(visible);
|
||||
}
|
||||
|
||||
void AutoWidget::setBindEnabled(std::function<bool()> pred)
|
||||
{
|
||||
m_enabled = std::move(pred);
|
||||
}
|
||||
|
||||
void AutoWidget::setBindModel(std::function<QAbstractItemModel*()> fn)
|
||||
{
|
||||
m_model = std::move(fn);
|
||||
}
|
||||
|
||||
void AutoWidget::setBindPostChanged(std::function<void()> fn)
|
||||
{
|
||||
m_postChanged = std::move(fn);
|
||||
}
|
||||
|
||||
void AutoWidget::setBindText(std::function<QString()> fn)
|
||||
{
|
||||
m_text = std::move(fn);
|
||||
}
|
||||
|
||||
void AutoWidget::setBindVisible(std::function<bool()> pred)
|
||||
{
|
||||
m_visible = std::move(pred);
|
||||
}
|
||||
|
||||
void AutoWidget::setLock(bool lock)
|
||||
{
|
||||
m_lock = lock;
|
||||
@@ -46,17 +194,3 @@ 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;
|
||||
}
|
||||
|
||||
@@ -21,9 +21,17 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include <QWidget>
|
||||
#include <QString>
|
||||
#include <QList>
|
||||
#include <QAbstractItemModel>
|
||||
|
||||
class GenericPanel;
|
||||
|
||||
// Note: cannot be a Qt object otherwise it will create a compiler ambiguity in the inheriting class
|
||||
// Base class for all Auto name prefixed widgets
|
||||
// Note: ths class cannot be a Qt object as it will trigger a compile time ambiguity in the inheriting class
|
||||
class AutoWidget
|
||||
{
|
||||
friend class GenericPanel;
|
||||
@@ -32,17 +40,51 @@ class AutoWidget
|
||||
explicit AutoWidget();
|
||||
~AutoWidget();
|
||||
|
||||
// buddy Auto widgets have their bindings mirroring the parent
|
||||
// example buddies are AutoLabel and AutoComboBox
|
||||
void addBuddyWidget(AutoWidget * wgt);
|
||||
void addBuddyWidgets(QList<AutoWidget *> wgts);
|
||||
void clearBuddyWidget(AutoWidget * wgt);
|
||||
void clearBuddyWidgets();
|
||||
|
||||
// mark those unsupported by the base Qt widget as deleted in the child widget
|
||||
// these are not marked as virtual to support deletion
|
||||
void setBindEnabled(std::function<bool()> pred);
|
||||
void setBindModel(std::function<QAbstractItemModel*()> fn);
|
||||
void setBindPostChanged(std::function<void()> fn);
|
||||
void setBindText(std::function<QString()> fn);
|
||||
void setBindVisible(std::function<bool()> pred);
|
||||
|
||||
protected:
|
||||
virtual void setAutoModel(QAbstractItemModel * model) {};
|
||||
virtual void setAutoText(QString text) {};
|
||||
|
||||
virtual void updateValue() = 0;
|
||||
|
||||
void applyBindings();
|
||||
void clearBindEnabled();
|
||||
void clearBindVisible();
|
||||
void clearBuddyBinds(AutoWidget * wgt);
|
||||
bool lock();
|
||||
bool panelLock();
|
||||
void runPostChanged();
|
||||
void setAutoEnabled(bool enabled);
|
||||
void setAutoVisible(bool visible);
|
||||
void setLock(bool lock);
|
||||
void setPanel(GenericPanel * panel);
|
||||
|
||||
void dataChanged();
|
||||
bool panelLock();
|
||||
|
||||
private:
|
||||
GenericPanel *m_panel;
|
||||
bool m_lock;
|
||||
|
||||
QList<AutoWidget *> m_buddyWidgets;
|
||||
|
||||
std::function<bool()> m_enabled;
|
||||
std::function<QAbstractItemModel*()> m_model;
|
||||
std::function<QString()> m_text;
|
||||
std::function<bool()> m_visible;
|
||||
std::function<void()> m_postChanged;
|
||||
|
||||
void dataChanged();
|
||||
};
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
#include "genericpanel.h"
|
||||
#include "autowidget.h"
|
||||
#include "widgetbindings.h"
|
||||
|
||||
#include <TimerEdit>
|
||||
#include <QComboBox>
|
||||
@@ -39,9 +40,7 @@ GenericPanel::GenericPanel(QWidget * parent, ModelData * model, GeneralSettings
|
||||
{
|
||||
}
|
||||
|
||||
GenericPanel::~GenericPanel()
|
||||
{
|
||||
}
|
||||
GenericPanel::~GenericPanel() = default;
|
||||
|
||||
void GenericPanel::update()
|
||||
{
|
||||
@@ -137,6 +136,20 @@ void GenericPanel::updateAutoWidgets()
|
||||
AutoWidget *autowdgt = dynamic_cast<AutoWidget *>(wdgt);
|
||||
if (autowdgt) {
|
||||
autowdgt->updateValue();
|
||||
autowdgt->applyBindings();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
WidgetBindings *GenericPanel::bindings()
|
||||
{
|
||||
if (!m_bindings)
|
||||
m_bindings = std::make_unique<WidgetBindings>();
|
||||
return m_bindings.get();
|
||||
}
|
||||
|
||||
void GenericPanel::applyBindings()
|
||||
{
|
||||
if (m_bindings)
|
||||
m_bindings->applyAll();
|
||||
}
|
||||
|
||||
@@ -22,12 +22,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <QWidget>
|
||||
#include <memory>
|
||||
|
||||
class ModelData;
|
||||
class GeneralSettings;
|
||||
class Firmware;
|
||||
class QGridLayout;
|
||||
class QString;
|
||||
class WidgetBindings;
|
||||
|
||||
class GenericPanel : public QWidget
|
||||
{
|
||||
@@ -62,4 +64,10 @@ class GenericPanel : public QWidget
|
||||
void setFocusFilter(QWidget * w);
|
||||
void disableMouseScrolling();
|
||||
void updateAutoWidgets();
|
||||
|
||||
WidgetBindings *bindings();
|
||||
void applyBindings();
|
||||
|
||||
private:
|
||||
std::unique_ptr<WidgetBindings> m_bindings;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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 "widgetbindings.h"
|
||||
|
||||
void WidgetBindings::bindVisible(QWidget *widget, std::function<bool()> pred)
|
||||
{
|
||||
m_visible.push_back({widget, std::move(pred)});
|
||||
}
|
||||
|
||||
void WidgetBindings::bindEnabled(QWidget *widget, std::function<bool()> pred)
|
||||
{
|
||||
m_enabled.push_back({widget, std::move(pred)});
|
||||
}
|
||||
|
||||
void WidgetBindings::bindText(QLabel *label, std::function<QString()> fn)
|
||||
{
|
||||
m_text.push_back({label, std::move(fn)});
|
||||
}
|
||||
|
||||
void WidgetBindings::applyAll()
|
||||
{
|
||||
for (const auto &b : m_visible)
|
||||
b.widget->setVisible(b.pred());
|
||||
|
||||
for (const auto &b : m_enabled)
|
||||
b.widget->setEnabled(b.pred());
|
||||
|
||||
for (const auto &b : m_text)
|
||||
b.label->setText(b.fn());
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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 <QWidget>
|
||||
#include <QLabel>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
class WidgetBindings
|
||||
{
|
||||
public:
|
||||
WidgetBindings() = default;
|
||||
|
||||
void bindVisible(QWidget *widget, std::function<bool()> pred);
|
||||
void bindEnabled(QWidget *widget, std::function<bool()> pred);
|
||||
void bindText(QLabel *label, std::function<QString()> fn);
|
||||
void applyAll();
|
||||
|
||||
private:
|
||||
struct VisibleBinding { QWidget *widget; std::function<bool()> pred; };
|
||||
struct EnabledBinding { QWidget *widget; std::function<bool()> pred; };
|
||||
struct TextBinding { QLabel *label; std::function<QString()> fn; };
|
||||
|
||||
std::vector<VisibleBinding> m_visible;
|
||||
std::vector<EnabledBinding> m_enabled;
|
||||
std::vector<TextBinding> m_text;
|
||||
};
|
||||
Reference in New Issue
Block a user