Merge pull request #8397 from opentx/elecpower/cpn-timerdata-refactor

Timers refactoring and model setup code enhancements
This commit is contained in:
Bertrand Songis
2021-04-05 10:41:32 +02:00
committed by GitHub
31 changed files with 802 additions and 415 deletions
+86 -146
View File
@@ -22,7 +22,6 @@
#include "ui_setup.h"
#include "ui_setup_timer.h"
#include "ui_setup_module.h"
#include "filtereditemmodels.h"
#include "appdata.h"
#include "modelprinter.h"
#include "multiprotocols.h"
@@ -31,54 +30,53 @@
#include <QDir>
TimerPanel::TimerPanel(QWidget *parent, ModelData & model, TimerData & timer, GeneralSettings & generalSettings, Firmware * firmware,
QWidget * prevFocus, FilteredItemModel * rawSwitchFilteredModel):
constexpr char FIM_TIMERSWITCH[] {"Timer Switch"};
constexpr char FIM_THRSOURCE[] {"Throttle Source"};
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)
ui(new Ui::Timer),
modelsUpdateCnt(0)
{
ui->setupUi(this);
connectItemModelEvents(rawSwitchFilteredModel);
connectItemModelEvents(panelFilteredModels->getItemModel(FIM_TIMERSWITCH));
lock = true;
// Name
int length = firmware->getCapability(TimersName);
if (length == 0) {
if (length == 0)
ui->name->hide();
else {
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());
ui->mode->setModel(panelFilteredModels->getItemModel(FIM_TIMERSWITCH));
ui->mode->setField(timer.mode, this);
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);
if (firmware->getCapability(PermTimers)) {
ui->persistent->setModel(panelItemModels->getItemModel(AIM_TIMER_PERSISTENT));
ui->persistent->setField(timer.persistent, this);
}
else {
ui->name->setMaxLength(length);
}
// Mode
ui->mode->setModel(rawSwitchFilteredModel);
ui->mode->setCurrentIndex(ui->mode->findData(timer.mode.toValue()));
connect(ui->mode, SIGNAL(activated(int)), this, SLOT(onModeChanged(int)));
if (!firmware->getCapability(PermTimers)) {
ui->persistent->hide();
ui->persistentValue->hide();
}
ui->countdownBeep->setField(timer.countdownBeep, this);
ui->countdownBeep->addItem(tr("Silent"), TimerData::COUNTDOWN_SILENT);
ui->countdownBeep->addItem(tr("Beeps"), TimerData::COUNTDOWN_BEEPS);
ui->countdownBeep->addItem(tr("Voice"), TimerData::COUNTDOWN_VOICE);
ui->countdownBeep->addItem(tr("Haptic"), TimerData::COUNTDOWN_HAPTIC);
ui->value->setMaximumTime(firmware->getMaxTimerStart());
ui->persistent->setField(timer.persistent, this);
ui->persistent->addItem(tr("Not persistent"), 0);
ui->persistent->addItem(tr("Persistent (flight)"), 1);
ui->persistent->addItem(tr("Persistent (manual reset)"), 2);
ui->countdownStart->setModel(panelItemModels->getItemModel(AIM_TIMER_COUNTDOWNSTART));
ui->countdownStart->setField(timer.countdownStart, this);
ui->countdownStart->addItem("5s", 1);
ui->countdownStart->addItem("10s", 0);
ui->countdownStart->addItem("20s", -1);
ui->countdownStart->addItem("30s", -2);
disableMouseScrolling();
QWidget::setTabOrder(prevFocus, ui->name);
@@ -102,33 +100,27 @@ void TimerPanel::update()
{
lock = true;
ui->name->setText(timer.name);
ui->name->updateValue();
ui->mode->updateValue();
ui->value->updateValue();
ui->countdownBeep->updateValue();
ui->minuteBeep->updateValue();
ui->countdownStart->updateValue();
int hour = timer.val / 3600;
int min = (timer.val - (hour * 3600)) / 60;
int sec = (timer.val - (hour * 3600)) % 60;
if (timer.countdownBeep == TimerData::COUNTDOWNBEEP_SILENT) {
ui->countdownStartLabel->setEnabled(false);
ui->countdownStart->setEnabled(false);
}
else {
ui->countdownStartLabel->setEnabled(true);
ui->countdownStart->setEnabled(true);
}
ui->mode->setCurrentIndex(ui->mode->findData(timer.mode.toValue()));
ui->value->setTime(QTime(hour, min, sec));
if (firmware->getCapability(PermTimers)) {
int sign = 1;
int pvalue = timer.pvalue;
if (pvalue < 0) {
pvalue = -pvalue;
sign = -1;
}
int hours = pvalue / 3600;
pvalue -= hours * 3600;
int minutes = pvalue / 60;
int seconds = pvalue % 60;
ui->persistentValue->setText(QString(" %1(%2:%3:%4)").arg(sign < 0 ? "-" :" ").arg(hours, 2, 10, QLatin1Char('0')).arg(minutes, 2, 10, QLatin1Char('0')).arg(seconds, 2, 10, QLatin1Char('0')));
ui->persistent->updateValue();
ui->persistentValue->setText(timer.pvalueToString());
}
ui->countdownBeep->updateValue();
ui->minuteBeep->setChecked(timer.minuteBeep);
ui->persistent->updateValue();
ui->countdownStart->updateValue();
lock = false;
}
@@ -137,57 +129,6 @@ QWidget * TimerPanel::getLastFocus()
return ui->persistent;
}
void TimerPanel::on_countdownBeep_currentIndexChanged(int index)
{
if(index == TimerData::COUNTDOWN_SILENT)
ui->countdownStart->hide();
else
ui->countdownStart->show();
}
void TimerPanel::on_value_editingFinished()
{
if (!lock) {
unsigned val = ui->value->time().hour() * 3600 + ui->value->time().minute() * 60 + ui->value->time().second();
if (timer.val != val) {
timer.val = val;
emit modified();
}
}
}
void TimerPanel::onModeChanged(int index)
{
if (!lock) {
bool ok;
const RawSwitch rs(ui->mode->itemData(index).toInt(&ok));
if (ok && timer.mode.toValue() != rs.toValue()) {
timer.mode = rs;
emit modified();
}
}
}
void TimerPanel::on_minuteBeep_toggled(bool checked)
{
if (!lock) {
timer.minuteBeep = checked;
emit modified();
}
}
void TimerPanel::on_name_editingFinished()
{
if (!lock) {
if (QString(timer.name) != ui->name->text()) {
int length = ui->name->maxLength();
strncpy(timer.name, ui->name->text().toLatin1(), length);
emit nameChanged();
emit modified();
}
}
}
void TimerPanel::connectItemModelEvents(const FilteredItemModel * itemModel)
{
connect(itemModel, &FilteredItemModel::aboutToBeUpdated, this, &TimerPanel::onItemModelAboutToBeUpdated);
@@ -197,12 +138,27 @@ void TimerPanel::connectItemModelEvents(const FilteredItemModel * itemModel)
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();
lock = false;
}
/******************************************************************************/
@@ -1023,16 +979,26 @@ SetupPanel::SetupPanel(QWidget * parent, ModelData & model, GeneralSettings & ge
{
ui->setupUi(this);
rawSwitchFilteredModel = new FilteredItemModel(sharedItemModels->getItemModel(AbstractItemModel::IMID_RawSwitch), RawSwitch::TimersContext);
connectItemModelEvents(rawSwitchFilteredModel);
lock = true;
thrSourceFilteredModel = new FilteredItemModel(sharedItemModels->getItemModel(AbstractItemModel::IMID_ThrSource));
connectItemModelEvents(thrSourceFilteredModel);
panelFilteredModels = new FilteredItemModelFactory();
panelFilteredModels->registerItemModel(new FilteredItemModel(sharedItemModels->getItemModel(AbstractItemModel::IMID_RawSwitch),
RawSwitch::TimersContext),
FIM_TIMERSWITCH);
connectItemModelEvents(panelFilteredModels->getItemModel(FIM_TIMERSWITCH));
panelFilteredModels->registerItemModel(new FilteredItemModel(sharedItemModels->getItemModel(AbstractItemModel::IMID_ThrSource)),
FIM_THRSOURCE);
connectItemModelEvents(panelFilteredModels->getItemModel(FIM_THRSOURCE));
panelItemModels = new CompoundItemModelFactory(&generalSettings, &model);
panelItemModels->registerItemModel(TimerData::countdownBeepItemModel());
panelItemModels->registerItemModel(TimerData::countdownStartItemModel());
panelItemModels->registerItemModel(TimerData::persistentItemModel());
Board::Type board = firmware->getBoard();
lock = true;
memset(modules, 0, sizeof(modules));
QRegExp rx(CHAR_FOR_NAMES_REGEX);
@@ -1112,7 +1078,7 @@ SetupPanel::SetupPanel(QWidget * parent, ModelData & model, GeneralSettings & ge
for (int i = 0; i < CPN_MAX_TIMERS; i++) {
if (i < timersCount) {
timers[i] = new TimerPanel(this, model, model.timers[i], generalSettings, firmware, prevFocus, rawSwitchFilteredModel);
timers[i] = new TimerPanel(this, model, model.timers[i], generalSettings, firmware, prevFocus, panelFilteredModels, panelItemModels);
ui->gridLayout->addWidget(timers[i], 1+i, 1);
connect(timers[i], &TimerPanel::modified, this, &SetupPanel::modified);
connect(timers[i], &TimerPanel::nameChanged, this, &SetupPanel::onTimerNameChanged);
@@ -1148,6 +1114,9 @@ SetupPanel::SetupPanel(QWidget * parent, ModelData & model, GeneralSettings & ge
ui->toplcdTimer->hide();
}
ui->throttleSource->setModel(panelFilteredModels->getItemModel(FIM_THRSOURCE));
ui->throttleSource->setField(model.thrTraceSrc, this);
if (!firmware->getCapability(HasDisplayText)) {
ui->displayText->hide();
ui->editText->hide();
@@ -1279,8 +1248,8 @@ SetupPanel::SetupPanel(QWidget * parent, ModelData & model, GeneralSettings & ge
SetupPanel::~SetupPanel()
{
delete ui;
delete rawSwitchFilteredModel;
delete thrSourceFilteredModel;
delete panelFilteredModels;
delete panelItemModels;
}
void SetupPanel::on_extendedLimits_toggled(bool checked)
@@ -1314,14 +1283,6 @@ void SetupPanel::on_trimIncrement_currentIndexChanged(int index)
emit modified();
}
void SetupPanel::on_throttleSource_currentIndexChanged(int index)
{
if (!lock) {
model->thrTraceSrc = ui->throttleSource->currentData().toUInt();
emit modified();
}
}
void SetupPanel::on_throttleTrimSwitch_currentIndexChanged(int index)
{
if (!lock) {
@@ -1380,28 +1341,6 @@ void SetupPanel::on_image_currentIndexChanged(int index)
}
}
void SetupPanel::populateThrottleSourceCB()
{
Board::Type board = firmware->getBoard();
lock = true;
ui->throttleSource->clear();
ui->throttleSource->addItem(tr("THR"), 0);
int idx = 1;
for (int i = 0; i < getBoardCapability(board, Board::Pots) + getBoardCapability(board, Board::Sliders); i++, idx++) {
if (RawSource(SOURCE_TYPE_STICK, 4 + i).isAvailable(model, &generalSettings, board)) {
ui->throttleSource->addItem(firmware->getAnalogInputName(4 + i), idx);
}
}
for (int i = 0; i < firmware->getCapability(Outputs); i++, idx++) {
ui->throttleSource->addItem(RawSource(SOURCE_TYPE_CH, i).toString(model, &generalSettings), idx);
}
int thrTraceSrcIdx = ui->throttleSource->findData(model->thrTraceSrc);
ui->throttleSource->setCurrentIndex(thrTraceSrcIdx);
lock = false;
}
void SetupPanel::populateThrottleTrimSwitchCB()
{
Board::Type board = firmware->getBoard();
@@ -1429,7 +1368,7 @@ void SetupPanel::update()
{
ui->name->setText(model->name);
ui->throttleReverse->setChecked(model->throttleReversed);
populateThrottleSourceCB();
ui->throttleSource->updateValue();
populateThrottleTrimSwitchCB();
ui->throttleWarning->setChecked(!model->disableThrottleWarning);
ui->trimIncrement->setCurrentIndex(model->trimInc+2);
@@ -1800,6 +1739,7 @@ void SetupPanel::onItemModelUpdateComplete()
void SetupPanel::updateItemModels()
{
sharedItemModels->update(AbstractItemModel::IMUE_Timers);
emit updated();
}
void SetupPanel::onModuleUpdateItemModels()
+11 -19
View File
@@ -18,17 +18,15 @@
* GNU General Public License for more details.
*/
#ifndef _SETUP_H_
#define _SETUP_H_
#pragma once
#include "modeledit.h"
#include "eeprominterface.h"
#include "compounditemmodels.h"
#include "filtereditemmodels.h"
constexpr char MIMETYPE_TIMER[] = "application/x-companion-timer";
class CompoundItemModelFactory;
class FilteredItemModel;
namespace Ui {
class Setup;
class Timer;
@@ -40,21 +38,18 @@ class TimerPanel : public ModelPanel
Q_OBJECT
public:
TimerPanel(QWidget *parent, ModelData & model, TimerData & timer, GeneralSettings & generalSettings, Firmware * firmware,
QWidget *prevFocus, FilteredItemModel * switchModel);
TimerPanel(QWidget * parent, ModelData & model, TimerData & timer, GeneralSettings & generalSettings, Firmware * firmware,
QWidget * prevFocus, FilteredItemModelFactory * panelFilteredModels, CompoundItemModelFactory * panelItemModels);
virtual ~TimerPanel();
virtual void update();
QWidget * getLastFocus();
private slots:
void onModeChanged(int index);
void on_value_editingFinished();
void on_minuteBeep_toggled(bool checked);
void on_countdownBeep_currentIndexChanged(int index);
void on_name_editingFinished();
void onNameChanged();
void onItemModelAboutToBeUpdated();
void onItemModelUpdateComplete();
void onCountdownBeepChanged(int index);
signals:
void nameChanged();
@@ -63,6 +58,7 @@ class TimerPanel : public ModelPanel
TimerData & timer;
Ui::Timer * ui;
void connectItemModelEvents(const FilteredItemModel * itemModel);
int modelsUpdateCnt;
};
class ModulePanel : public ModelPanel
@@ -146,7 +142,6 @@ class SetupPanel : public ModelPanel
private slots:
void on_name_editingFinished();
void on_throttleSource_currentIndexChanged(int index);
void on_throttleTrimSwitch_currentIndexChanged(int index);
void on_throttleTrim_toggled(bool checked);
void on_extendedLimits_toggled(bool checked);
@@ -184,12 +179,11 @@ class SetupPanel : public ModelPanel
QVector<QCheckBox *> startupSwitchesCheckboxes;
QVector<QCheckBox *> potWarningCheckboxes;
QVector<QCheckBox *> centerBeepCheckboxes;
ModulePanel * modules[CPN_MAX_MODULES+1];
ModulePanel * modules[CPN_MAX_MODULES + 1];
TimerPanel * timers[CPN_MAX_TIMERS];
void updateStartupSwitches();
void updatePotWarnings();
void updateBeepCenter();
void populateThrottleSourceCB();
void populateThrottleTrimSwitchCB();
int timersCount;
int selectedTimerIndex;
@@ -199,10 +193,8 @@ class SetupPanel : public ModelPanel
bool moveTimerUpAllowed() const;
void swapTimerData(int idx1, int idx2);
CompoundItemModelFactory * sharedItemModels;
FilteredItemModel * rawSwitchFilteredModel;
FilteredItemModel * thrSourceFilteredModel;
void updateItemModels();
void connectItemModelEvents(const FilteredItemModel * itemModel);
CompoundItemModelFactory * panelItemModels;
FilteredItemModelFactory * panelFilteredModels;
};
#endif // _SETUP_H_
+1 -1
View File
@@ -604,7 +604,7 @@ If this is checked the throttle will be reversed. Idle will be forward, trim wi
</widget>
</item>
<item row="0" column="0">
<widget class="QComboBox" name="throttleSource">
<widget class="AutoComboBox" name="throttleSource">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
+27 -5
View File
@@ -9,7 +9,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>675</width>
<width>874</width>
<height>33</height>
</rect>
</property>
@@ -33,7 +33,7 @@
<number>0</number>
</property>
<item>
<widget class="QLineEdit" name="name">
<widget class="AutoLineEdit" name="name">
<property name="minimumSize">
<size>
<width>80</width>
@@ -43,7 +43,7 @@
</widget>
</item>
<item>
<widget class="QTimeEdit" name="value">
<widget class="AutoTimeEdit" name="value">
<property name="accelerated">
<bool>true</bool>
</property>
@@ -56,7 +56,7 @@
</widget>
</item>
<item>
<widget class="QComboBox" name="mode"/>
<widget class="AutoComboBox" name="mode"/>
</item>
<item>
<widget class="QLabel" name="countdownBeepLabel">
@@ -68,11 +68,18 @@
<item>
<widget class="AutoComboBox" name="countdownBeep"/>
</item>
<item>
<widget class="QLabel" name="countdownStartLabel">
<property name="text">
<string>Start</string>
</property>
</widget>
</item>
<item>
<widget class="AutoComboBox" name="countdownStart"/>
</item>
<item>
<widget class="QCheckBox" name="minuteBeep">
<widget class="AutoCheckBox" name="minuteBeep">
<property name="text">
<string>Minute Call</string>
</property>
@@ -109,6 +116,21 @@
<extends>QComboBox</extends>
<header>autocombobox.h</header>
</customwidget>
<customwidget>
<class>AutoLineEdit</class>
<extends>QLineEdit</extends>
<header>autolineedit.h</header>
</customwidget>
<customwidget>
<class>AutoCheckBox</class>
<extends>QCheckBox</extends>
<header>autocheckbox.h</header>
</customwidget>
<customwidget>
<class>AutoTimeEdit</class>
<extends>QTimeEdit</extends>
<header>autotimeedit.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>