[Companion] Refactor FlightModePanel and TimerPanel to use shared RawSwitch data models instead of one per instance.
This commit is contained in:
@@ -24,7 +24,7 @@
|
||||
#include "helpers.h"
|
||||
#include "customdebug.h"
|
||||
|
||||
FlightModePanel::FlightModePanel(QWidget * parent, ModelData & model, int phaseIdx, GeneralSettings & generalSettings, Firmware * firmware):
|
||||
FlightModePanel::FlightModePanel(QWidget * parent, ModelData & model, int phaseIdx, GeneralSettings & generalSettings, Firmware * firmware, RawSwitchFilterItemModel * switchModel):
|
||||
ModelPanel(parent, model, generalSettings, firmware),
|
||||
ui(new Ui::FlightMode),
|
||||
phaseIdx(phaseIdx),
|
||||
@@ -53,10 +53,8 @@ FlightModePanel::FlightModePanel(QWidget * parent, ModelData & model, int phaseI
|
||||
|
||||
// Phase switch
|
||||
if (phaseIdx > 0) {
|
||||
rawSwitchItemModel = new RawSwitchFilterItemModel(&generalSettings, &model, RawSwitch::MixesContext, this);
|
||||
ui->swtch->setModel(rawSwitchItemModel);
|
||||
ui->swtch->setCurrentIndex(ui->swtch->findData(phase.swtch.toValue()));
|
||||
connect(ui->swtch, SIGNAL(currentIndexChanged(int)), this, SLOT(phaseSwitch_currentIndexChanged(int)));
|
||||
ui->swtch->setModel(switchModel);
|
||||
connect(ui->swtch, SIGNAL(activated(int)), this, SLOT(phaseSwitchChanged(int)));
|
||||
}
|
||||
else {
|
||||
ui->swtch->hide();
|
||||
@@ -287,11 +285,8 @@ FlightModePanel::~FlightModePanel()
|
||||
|
||||
void FlightModePanel::update()
|
||||
{
|
||||
if (rawSwitchItemModel) {
|
||||
rawSwitchItemModel->update();
|
||||
}
|
||||
|
||||
ui->name->setText(phase.name);
|
||||
ui->swtch->setCurrentIndex(ui->swtch->findData(phase.swtch.toValue()));
|
||||
|
||||
int scale = firmware->getCapability(SlowScale);
|
||||
ui->fadeIn->setValue(float(phase.fadeIn)/scale);
|
||||
@@ -382,11 +377,15 @@ void FlightModePanel::phaseName_editingFinished()
|
||||
emit nameModified();
|
||||
}
|
||||
|
||||
void FlightModePanel::phaseSwitch_currentIndexChanged(int index)
|
||||
void FlightModePanel::phaseSwitchChanged(int index)
|
||||
{
|
||||
if (!lock) {
|
||||
QComboBox *comboBox = qobject_cast<QComboBox*>(sender());
|
||||
phase.swtch = RawSwitch(comboBox->itemData(index).toInt());
|
||||
if (lock)
|
||||
return;
|
||||
|
||||
bool ok;
|
||||
const RawSwitch rs(ui->swtch->itemData(index).toInt(&ok));
|
||||
if (ok && phase.swtch.toValue() != rs.toValue()) {
|
||||
phase.swtch = rs;
|
||||
emit modified();
|
||||
}
|
||||
}
|
||||
@@ -719,14 +718,18 @@ FlightModesPanel::FlightModesPanel(QWidget * parent, ModelData & model, GeneralS
|
||||
ModelPanel(parent, model, generalSettings, firmware),
|
||||
modesCount(firmware->getCapability(FlightModes))
|
||||
{
|
||||
|
||||
RawSwitchFilterItemModel * swModel = new RawSwitchFilterItemModel(&generalSettings, &model, RawSwitch::MixesContext, this);
|
||||
connect(this, &FlightModesPanel::updated, swModel, &RawSwitchFilterItemModel::update);
|
||||
|
||||
QGridLayout * gridLayout = new QGridLayout(this);
|
||||
tabWidget = new QTabWidget(this);
|
||||
for (int i=0; i<modesCount; i++) {
|
||||
FlightModePanel * tab = new FlightModePanel(tabWidget, model, i, generalSettings, firmware);
|
||||
FlightModePanel * tab = new FlightModePanel(tabWidget, model, i, generalSettings, firmware, swModel);
|
||||
tab->setProperty("index", i);
|
||||
panels << tab;
|
||||
connect(tab, SIGNAL(modified()), this, SLOT(onPhaseModified()));
|
||||
connect(tab, SIGNAL(nameModified()), this, SLOT(onPhaseNameChanged()));
|
||||
connect(tab, &FlightModePanel::modified, this, &FlightModesPanel::modified);
|
||||
connect(tab, &FlightModePanel::nameModified, this, &FlightModesPanel::onPhaseNameChanged);
|
||||
connect(this, &FlightModesPanel::updated, tab, &FlightModePanel::update);
|
||||
tabWidget->addTab(tab, getTabName(i));
|
||||
}
|
||||
gridLayout->addWidget(tabWidget, 0, 0, 1, 1);
|
||||
@@ -737,11 +740,6 @@ FlightModesPanel::~FlightModesPanel()
|
||||
{
|
||||
}
|
||||
|
||||
void FlightModesPanel::onPhaseModified()
|
||||
{
|
||||
emit modified();
|
||||
}
|
||||
|
||||
QString FlightModesPanel::getTabName(int index)
|
||||
{
|
||||
QString result = tr("Flight Mode %1").arg(index);
|
||||
@@ -764,10 +762,5 @@ void FlightModesPanel::onPhaseNameChanged()
|
||||
|
||||
void FlightModesPanel::update()
|
||||
{
|
||||
on_tabWidget_currentChanged(tabWidget->currentIndex());
|
||||
}
|
||||
|
||||
void FlightModesPanel::on_tabWidget_currentChanged(int index)
|
||||
{
|
||||
panels[index]->update();
|
||||
emit updated();
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ class FlightModePanel : public ModelPanel
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
FlightModePanel(QWidget *parent, ModelData &model, int modeIdx, GeneralSettings & generalSettings, Firmware * firmware);
|
||||
FlightModePanel(QWidget *parent, ModelData &model, int modeIdx, GeneralSettings & generalSettings, Firmware * firmware, RawSwitchFilterItemModel * switchModel);
|
||||
virtual ~FlightModePanel();
|
||||
|
||||
virtual void update();
|
||||
@@ -45,7 +45,7 @@ class FlightModePanel : public ModelPanel
|
||||
|
||||
private slots:
|
||||
void phaseName_editingFinished();
|
||||
void phaseSwitch_currentIndexChanged(int index);
|
||||
void phaseSwitchChanged(int index);
|
||||
void phaseFadeIn_editingFinished();
|
||||
void phaseFadeOut_editingFinished();
|
||||
void phaseTrimUse_currentIndexChanged(int index);
|
||||
@@ -104,18 +104,20 @@ class FlightModesPanel : public ModelPanel
|
||||
FlightModesPanel(QWidget *parent, ModelData & model, GeneralSettings & generalSettings, Firmware * firmware);
|
||||
virtual ~FlightModesPanel();
|
||||
|
||||
virtual void update();
|
||||
public slots:
|
||||
virtual void update() override;
|
||||
|
||||
signals:
|
||||
void updated();
|
||||
|
||||
private slots:
|
||||
void onPhaseModified();
|
||||
void onPhaseNameChanged();
|
||||
void on_tabWidget_currentChanged(int index);
|
||||
|
||||
private:
|
||||
QString getTabName(int index);
|
||||
|
||||
int modesCount;
|
||||
QTabWidget *tabWidget;
|
||||
QString getTabName(int index);
|
||||
QVector<FlightModePanel *> panels;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
#include "multiprotocols.h"
|
||||
#include "checklistdialog.h"
|
||||
|
||||
TimerPanel::TimerPanel(QWidget *parent, ModelData & model, TimerData & timer, GeneralSettings & generalSettings, Firmware * firmware, QWidget * prevFocus):
|
||||
TimerPanel::TimerPanel(QWidget *parent, ModelData & model, TimerData & timer, GeneralSettings & generalSettings, Firmware * firmware, QWidget * prevFocus, RawSwitchFilterItemModel * switchModel):
|
||||
ModelPanel(parent, model, generalSettings, firmware),
|
||||
timer(timer),
|
||||
ui(new Ui::Timer)
|
||||
@@ -50,9 +50,9 @@ TimerPanel::TimerPanel(QWidget *parent, ModelData & model, TimerData & timer, Ge
|
||||
}
|
||||
|
||||
// Mode
|
||||
rawSwitchItemModel = new RawSwitchFilterItemModel(&generalSettings, &model, RawSwitch::TimersContext, this);
|
||||
ui->mode->setModel(rawSwitchItemModel);
|
||||
ui->mode->setModel(switchModel);
|
||||
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();
|
||||
@@ -92,12 +92,11 @@ TimerPanel::~TimerPanel()
|
||||
|
||||
void TimerPanel::update()
|
||||
{
|
||||
rawSwitchItemModel->update();
|
||||
|
||||
int hour = timer.val / 3600;
|
||||
int min = (timer.val - (hour * 3600)) / 60;
|
||||
int sec = (timer.val - (hour * 3600)) % 60;
|
||||
|
||||
ui->mode->setCurrentIndex(ui->mode->findData(timer.mode.toValue()));
|
||||
ui->value->setTime(QTime(hour, min, sec));
|
||||
|
||||
if (firmware->getCapability(PermTimers)) {
|
||||
@@ -131,10 +130,15 @@ void TimerPanel::on_value_editingFinished()
|
||||
}
|
||||
}
|
||||
|
||||
void TimerPanel::on_mode_currentIndexChanged(int index)
|
||||
void TimerPanel::onModeChanged(int index)
|
||||
{
|
||||
if (!lock) {
|
||||
timer.mode = RawSwitch(ui->mode->itemData(index).toInt());
|
||||
if (lock)
|
||||
return;
|
||||
|
||||
bool ok;
|
||||
const RawSwitch rs(ui->mode->itemData(index).toInt(&ok));
|
||||
if (ok && timer.mode.toValue() != rs.toValue()) {
|
||||
timer.mode = rs;
|
||||
emit modified();
|
||||
}
|
||||
}
|
||||
@@ -937,11 +941,15 @@ SetupPanel::SetupPanel(QWidget * parent, ModelData & model, GeneralSettings & ge
|
||||
}
|
||||
|
||||
QWidget * prevFocus = ui->image;
|
||||
RawSwitchFilterItemModel * swModel = new RawSwitchFilterItemModel(&generalSettings, &model, RawSwitch::TimersContext, this);
|
||||
connect(this, &SetupPanel::updated, swModel, &RawSwitchFilterItemModel::update);
|
||||
|
||||
for (int i=0; i<CPN_MAX_TIMERS; i++) {
|
||||
if (i<firmware->getCapability(Timers)) {
|
||||
timers[i] = new TimerPanel(this, model, model.timers[i], generalSettings, firmware, prevFocus);
|
||||
timers[i] = new TimerPanel(this, model, model.timers[i], generalSettings, firmware, prevFocus, swModel);
|
||||
ui->gridLayout->addWidget(timers[i], 1+i, 1);
|
||||
connect(timers[i], &TimerPanel::modified, this, &SetupPanel::modified);
|
||||
connect(this, &SetupPanel::updated, timers[i], &TimerPanel::update);
|
||||
prevFocus = timers[i]->getLastFocus();
|
||||
}
|
||||
else {
|
||||
@@ -1219,15 +1227,13 @@ void SetupPanel::update()
|
||||
updatePotWarnings();
|
||||
}
|
||||
|
||||
for (int i=0; i<firmware->getCapability(Timers); i++) {
|
||||
timers[i]->update();
|
||||
}
|
||||
|
||||
for (int i=0; i<CPN_MAX_MODULES+1; i++) {
|
||||
if (modules[i]) {
|
||||
modules[i]->update();
|
||||
}
|
||||
}
|
||||
|
||||
emit updated();
|
||||
}
|
||||
|
||||
void SetupPanel::updateBeepCenter()
|
||||
|
||||
@@ -37,14 +37,14 @@ class TimerPanel : public ModelPanel
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
TimerPanel(QWidget *parent, ModelData & model, TimerData & timer, GeneralSettings & generalSettings, Firmware * firmware, QWidget *prevFocus);
|
||||
TimerPanel(QWidget *parent, ModelData & model, TimerData & timer, GeneralSettings & generalSettings, Firmware * firmware, QWidget *prevFocus, RawSwitchFilterItemModel * switchModel);
|
||||
virtual ~TimerPanel();
|
||||
|
||||
virtual void update();
|
||||
QWidget * getLastFocus();
|
||||
|
||||
private slots:
|
||||
void on_mode_currentIndexChanged(int index);
|
||||
void onModeChanged(int index);
|
||||
void on_value_editingFinished();
|
||||
void on_minuteBeep_toggled(bool checked);
|
||||
void on_name_editingFinished();
|
||||
@@ -52,7 +52,6 @@ class TimerPanel : public ModelPanel
|
||||
private:
|
||||
TimerData & timer;
|
||||
Ui::Timer * ui;
|
||||
RawSwitchFilterItemModel * rawSwitchItemModel;
|
||||
};
|
||||
|
||||
class ModulePanel : public ModelPanel
|
||||
@@ -126,6 +125,7 @@ class SetupPanel : public ModelPanel
|
||||
|
||||
signals:
|
||||
void extendedLimitsToggled();
|
||||
void updated();
|
||||
|
||||
private slots:
|
||||
void on_name_editingFinished();
|
||||
|
||||
Reference in New Issue
Block a user