From b55070f9391675f6744f5ecdfb97695d8e0ae0a5 Mon Sep 17 00:00:00 2001 From: Bertrand Songis Date: Sun, 30 Jul 2017 12:12:27 +0200 Subject: [PATCH] [Companion] Sensor switches support added + some refactoring to hide irrelevant switches in the combobox More refactoring to come: - have only one SwitchModel per model instead of one per tab - only do the update work if something has changed --- companion/src/eeprominterface.cpp | 31 +++- .../src/firmwares/opentx/opentxeeprom.cpp | 10 ++ companion/src/helpers.cpp | 78 ---------- companion/src/helpers.h | 12 -- companion/src/modeledit/CMakeLists.txt | 1 + companion/src/modeledit/customfunctions.cpp | 6 +- companion/src/modeledit/customfunctions.h | 6 +- companion/src/modeledit/expodialog.cpp | 3 +- companion/src/modeledit/flightmodes.cpp | 13 +- companion/src/modeledit/flightmodes.h | 3 + companion/src/modeledit/logicalswitches.cpp | 4 +- companion/src/modeledit/logicalswitches.h | 6 +- companion/src/modeledit/mixerdialog.cpp | 7 +- companion/src/modeledit/setup.cpp | 6 +- companion/src/modeledit/setup.h | 5 +- companion/src/modeledit/switchitemmodel.cpp | 144 ++++++++++++++++++ companion/src/modeledit/switchitemmodel.h | 58 +++++++ companion/src/radiodata.h | 8 +- 18 files changed, 290 insertions(+), 111 deletions(-) create mode 100644 companion/src/modeledit/switchitemmodel.cpp create mode 100644 companion/src/modeledit/switchitemmodel.h diff --git a/companion/src/eeprominterface.cpp b/companion/src/eeprominterface.cpp index 970415fc3..06151e4f1 100644 --- a/companion/src/eeprominterface.cpp +++ b/companion/src/eeprominterface.cpp @@ -622,7 +622,7 @@ bool RawSource::isSlider(int * sliderIndex) const * RawSwitch */ -QString RawSwitch::toString(Board::Type board, const GeneralSettings * const generalSettings) const +QString RawSwitch::toString(Board::Type board, const GeneralSettings * const generalSettings, const ModelData * const modelData) const { if (board == Board::BOARD_UNKNOWN) { board = getCurrentBoard(); @@ -662,7 +662,7 @@ QString RawSwitch::toString(Board::Type board, const GeneralSettings * const gen << CPN_STR_SW_INDICATOR_DN; if (index < 0) { - return CPN_STR_SW_INDICATOR_REV % RawSwitch(type, -index).toString(board, generalSettings); + return CPN_STR_SW_INDICATOR_REV % RawSwitch(type, -index).toString(board, generalSettings, modelData); } else { QString swName; @@ -718,6 +718,15 @@ QString RawSwitch::toString(Board::Type board, const GeneralSettings * const gen case SWITCH_TYPE_TIMER_MODE: return CHECK_IN_ARRAY(timerModes, index); + case SWITCH_TYPE_SENSOR: + if (modelData && index <= CPN_MAX_SENSORS && strlen(modelData->sensorData[index-1].label) > 0) + return QObject::tr("Sensor%1 (%2)").arg(index).arg(modelData->sensorData[index-1].label); + else + return QObject::tr("Sensor%1").arg(index); + + case SWITCH_TYPE_TELEMETRY: + return QObject::tr("Telemetry"); + default: return QObject::tr("???"); } @@ -1745,6 +1754,24 @@ int ModelData::getChannelsMax(bool forceExtendedLimits) const return 100; } +bool ModelData::isAvailable(const RawSwitch & swtch) const +{ + unsigned index = abs(swtch.index) - 1; + + if (swtch.type == SWITCH_TYPE_VIRTUAL) { + return logicalSw[index].func != LS_FN_OFF; + } + else if (swtch.type == SWITCH_TYPE_FLIGHT_MODE) { + return index == 0 || flightModeData[index].swtch.type != SWITCH_TYPE_NONE; + } + else if (swtch.type == SWITCH_TYPE_SENSOR) { + return strlen(sensorData[index].label) > 0; + } + else { + return true; + } +} + QList eepromInterfaces; void unregisterEEpromInterfaces() diff --git a/companion/src/firmwares/opentx/opentxeeprom.cpp b/companion/src/firmwares/opentx/opentxeeprom.cpp index aef18d0be..a5b9f86d5 100644 --- a/companion/src/firmwares/opentx/opentxeeprom.cpp +++ b/companion/src/firmwares/opentx/opentxeeprom.cpp @@ -133,6 +133,7 @@ class SwitchesConversionTable: public ConversionTable { addConversion(RawSwitch(SWITCH_TYPE_OFF), -val+offset); addConversion(RawSwitch(SWITCH_TYPE_ON), val++); + if (version >= 216) { addConversion(RawSwitch(SWITCH_TYPE_ONE, -1), -val+offset); addConversion(RawSwitch(SWITCH_TYPE_ONE, 1), val++); @@ -144,6 +145,15 @@ class SwitchesConversionTable: public ConversionTable { } } + if (IS_ARM(board) && version >= 218) { + addConversion(RawSwitch(SWITCH_TYPE_TELEMETRY, -1), -val+offset); + addConversion(RawSwitch(SWITCH_TYPE_TELEMETRY, 1), val++); + for (int i=1; i<=CPN_MAX_SENSORS; i++) { + addConversion(RawSwitch(SWITCH_TYPE_SENSOR, -i), -val+offset); + addConversion(RawSwitch(SWITCH_TYPE_SENSOR, i), val++); + } + } + if (version < 216) { // previous "moment" switches for (int i=1; i<=MAX_SWITCHES_POSITION(board, version); i++) { diff --git a/companion/src/helpers.cpp b/companion/src/helpers.cpp index a566b2d93..3ce2b4381 100644 --- a/companion/src/helpers.cpp +++ b/companion/src/helpers.cpp @@ -404,84 +404,6 @@ void Helpers::getFileComboBoxValue(QComboBox * b, char * dest, int length) } } -void Helpers::addRawSwitchItems(QStandardItemModel * itemModel, const RawSwitchType & type, int count, const GeneralSettings * const generalSettings) -{ - // Most RawSwitch() indices are one-based (vs. typical zero); these are exceptions to the rule: - const static QVector rawSwitchIndexBaseZeroTypes = QVector() << SWITCH_TYPE_NONE << SWITCH_TYPE_ON << SWITCH_TYPE_OFF << SWITCH_TYPE_TIMER_MODE; - - int rawIdxAdj = 0; - const Board::Type board = getCurrentBoard(); - int i = (count < 0 ? count : 1); - const int maxCount = (i < 0 ? 0 : count + i); - - // handle exceptions in RawSwitch() index values - if (rawSwitchIndexBaseZeroTypes.contains(type)) - rawIdxAdj = -1; - - for ( ; i < maxCount; ++i) { - if (generalSettings) { - if (type == SWITCH_TYPE_SWITCH && IS_HORUS_OR_TARANIS(board) && !generalSettings->switchPositionAllowedTaranis(abs(i))) - continue; - if (type == SWITCH_TYPE_MULTIPOS_POT) { - int pot = div(abs(i) - 1, getCurrentFirmware()->getCapability(MultiposPotsPositions)).quot; - if (!generalSettings->isPotAvailable(pot) || generalSettings->potConfig[pot] != Board::POT_MULTIPOS_SWITCH) - continue; - } - } - RawSwitch rs(type, i + rawIdxAdj); - QStandardItem * modelItem = new QStandardItem(rs.toString(board, generalSettings)); - modelItem->setData(rs.toValue(), Qt::UserRole); - itemModel->appendRow(modelItem); - } -} - -QStandardItemModel * Helpers::getRawSwitchItemModel(const GeneralSettings * const generalSettings, SwitchContext context) -{ - QStandardItemModel * itemModel = new QStandardItemModel(); - Boards board = Boards(getCurrentBoard()); - Board::Type btype = board.getBoardType(); - Firmware * fw = getCurrentFirmware(); - - // Descending switch direction: NOT (!) switches - - if (context != MixesContext && context != GlobalFunctionsContext && IS_ARM(btype)) { - addRawSwitchItems(itemModel, SWITCH_TYPE_FLIGHT_MODE, -fw->getCapability(FlightModes), generalSettings); - } - if (context != GlobalFunctionsContext) { - addRawSwitchItems(itemModel, SWITCH_TYPE_VIRTUAL, -fw->getCapability(LogicalSwitches), generalSettings); - } - addRawSwitchItems(itemModel, SWITCH_TYPE_ROTARY_ENCODER, -fw->getCapability(RotaryEncoders), generalSettings); - addRawSwitchItems(itemModel, SWITCH_TYPE_TRIM, -board.getCapability(Board::NumTrimSwitches), generalSettings); - addRawSwitchItems(itemModel, SWITCH_TYPE_MULTIPOS_POT, -(fw->getCapability(MultiposPots) * fw->getCapability(MultiposPotsPositions)), generalSettings); - addRawSwitchItems(itemModel, SWITCH_TYPE_SWITCH, -board.getCapability(Board::SwitchPositions), generalSettings); - - // Ascending switch direction (including zero) - - if (context == TimersContext) { - addRawSwitchItems(itemModel, SWITCH_TYPE_TIMER_MODE, 5, generalSettings); - } - else { - addRawSwitchItems(itemModel, SWITCH_TYPE_NONE, 1); - } - - addRawSwitchItems(itemModel, SWITCH_TYPE_SWITCH, board.getCapability(Board::SwitchPositions), generalSettings); - addRawSwitchItems(itemModel, SWITCH_TYPE_MULTIPOS_POT, fw->getCapability(MultiposPots) * fw->getCapability(MultiposPotsPositions), generalSettings); - addRawSwitchItems(itemModel, SWITCH_TYPE_TRIM, board.getCapability(Board::NumTrimSwitches), generalSettings); - addRawSwitchItems(itemModel, SWITCH_TYPE_ROTARY_ENCODER, fw->getCapability(RotaryEncoders), generalSettings); - if (context != GlobalFunctionsContext) { - addRawSwitchItems(itemModel, SWITCH_TYPE_VIRTUAL, fw->getCapability(LogicalSwitches), generalSettings); - } - if (context != MixesContext && context != GlobalFunctionsContext && IS_ARM(btype)) { - addRawSwitchItems(itemModel, SWITCH_TYPE_FLIGHT_MODE, fw->getCapability(FlightModes), generalSettings); - } - if (context == SpecialFunctionsContext || context == GlobalFunctionsContext) { - addRawSwitchItems(itemModel, SWITCH_TYPE_ON, 1); - addRawSwitchItems(itemModel, SWITCH_TYPE_ONE, 1); - } - - return itemModel; -} - void Helpers::addRawSourceItems(QStandardItemModel * itemModel, const RawSourceType & type, int count, const GeneralSettings * const generalSettings, const ModelData * const model, const int start, const QList exclude) { diff --git a/companion/src/helpers.h b/companion/src/helpers.h index ebfe970ff..18554287c 100644 --- a/companion/src/helpers.h +++ b/companion/src/helpers.h @@ -129,18 +129,6 @@ class CurveGroup : public QObject { namespace Helpers { - enum SwitchContext - { - LogicalSwitchesContext, - SpecialFunctionsContext, - GlobalFunctionsContext, - TimersContext, - MixesContext - }; - - void addRawSwitchItems(QStandardItemModel * itemModel, const RawSwitchType & type, int count, const GeneralSettings * const generalSettings = NULL); - QStandardItemModel * getRawSwitchItemModel(const GeneralSettings * const generalSettings = NULL, SwitchContext context = LogicalSwitchesContext); - void addRawSourceItems(QStandardItemModel * itemModel, const RawSourceType & type, int count, const GeneralSettings * const generalSettings = NULL, const ModelData * const model = NULL, const int start = 0, const QList exclude = QList()); QStandardItemModel * getRawSourceItemModel(const GeneralSettings * const generalSettings = NULL, const ModelData * const model = NULL, unsigned int flags = 0); diff --git a/companion/src/modeledit/CMakeLists.txt b/companion/src/modeledit/CMakeLists.txt index 3855d5437..2a384cf6a 100644 --- a/companion/src/modeledit/CMakeLists.txt +++ b/companion/src/modeledit/CMakeLists.txt @@ -16,6 +16,7 @@ set(modeledit_NAMES ) set(modeledit_SRCS + switchitemmodel.cpp flightmodes.cpp inputs.cpp mixes.cpp diff --git a/companion/src/modeledit/customfunctions.cpp b/companion/src/modeledit/customfunctions.cpp index 07576ff96..a3d8e3afd 100644 --- a/companion/src/modeledit/customfunctions.cpp +++ b/companion/src/modeledit/customfunctions.cpp @@ -19,6 +19,7 @@ */ #include "customfunctions.h" +#include "switchitemmodel.h" #include "helpers.h" #include "appdata.h" @@ -73,8 +74,7 @@ CustomFunctionsPanel::CustomFunctionsPanel(QWidget * parent, ModelData * model, lock = true; int num_fsw = model ? firmware->getCapability(CustomFunctions) : firmware->getCapability(GlobalFunctions); - rawSwitchItemModel = Helpers::getRawSwitchItemModel(&generalSettings, model ? Helpers::SpecialFunctionsContext : Helpers::GlobalFunctionsContext); - rawSwitchItemModel->setParent(this); + rawSwitchItemModel = new RawSwitchFilterItemModel(&generalSettings, model, model ? SpecialFunctionsContext : GlobalFunctionsContext); rawSrcInputsItemModel = Helpers::getRawSourceItemModel(&generalSettings, model, POPULATE_NONE|POPULATE_SOURCES|POPULATE_VIRTUAL_INPUTS|POPULATE_TRIMS|POPULATE_SWITCHES); rawSrcInputsItemModel->setParent(this); rawSrcAllItemModel = Helpers::getRawSourceItemModel(&generalSettings, model, POPULATE_NONE|POPULATE_SOURCES|POPULATE_VIRTUAL_INPUTS|POPULATE_SWITCHES|POPULATE_GVARS|POPULATE_TRIMS|POPULATE_TELEMETRY|POPULATE_TELEMETRYEXT|POPULATE_SCRIPT_OUTPUTS); @@ -584,6 +584,8 @@ void CustomFunctionsPanel::refreshCustomFunction(int i, bool modified) void CustomFunctionsPanel::update() { + rawSwitchItemModel->update(); + lock = true; int num_fsw = model ? firmware->getCapability(CustomFunctions) : firmware->getCapability(GlobalFunctions); for (int i=0; i #include "modeledit.h" #include "eeprominterface.h" -#include + +class RawSwitchFilterItemModel; class RepeatComboBox: public QComboBox { @@ -74,7 +76,7 @@ class CustomFunctionsPanel : public GenericPanel void populateFuncCB(QComboBox *b, unsigned int value); void populateGVmodeCB(QComboBox *b, unsigned int value); void populateFuncParamCB(QComboBox *b, uint function, unsigned int value, unsigned int adjustmode=0); - QStandardItemModel * rawSwitchItemModel; + RawSwitchFilterItemModel * rawSwitchItemModel; QStandardItemModel * rawSrcInputsItemModel; QStandardItemModel * rawSrcAllItemModel; diff --git a/companion/src/modeledit/expodialog.cpp b/companion/src/modeledit/expodialog.cpp index 4b4a3b6b5..5c51eb232 100644 --- a/companion/src/modeledit/expodialog.cpp +++ b/companion/src/modeledit/expodialog.cpp @@ -20,6 +20,7 @@ #include "expodialog.h" #include "ui_expodialog.h" +#include "switchitemmodel.h" #include "helpers.h" ExpoDialog::ExpoDialog(QWidget *parent, ModelData & model, ExpoData *expoData, GeneralSettings & generalSettings, @@ -61,7 +62,7 @@ ExpoDialog::ExpoDialog(QWidget *parent, ModelData & model, ExpoData *expoData, G curveGroup = new CurveGroup(ui->curveTypeCB, ui->curveGVarCB, ui->curveValueCB, ui->curveValueSB, ed->curve, model, firmware->getCapability(HasInputDiff) ? 0 : (HIDE_DIFF | HIDE_NEGATIVE_CURVES)); - ui->switchesCB->setModel(Helpers::getRawSwitchItemModel(&generalSettings, Helpers::MixesContext)); + ui->switchesCB->setModel(new RawSwitchFilterItemModel(&generalSettings, &model, MixesContext)); ui->switchesCB->setCurrentIndex(ui->switchesCB->findData(ed->swtch.toValue())); ui->sideCB->setCurrentIndex(ed->mode-1); diff --git a/companion/src/modeledit/flightmodes.cpp b/companion/src/modeledit/flightmodes.cpp index cc35ce7bc..f9e479c84 100644 --- a/companion/src/modeledit/flightmodes.cpp +++ b/companion/src/modeledit/flightmodes.cpp @@ -20,6 +20,7 @@ #include "flightmodes.h" #include "ui_flightmode.h" +#include "switchitemmodel.h" #include "helpers.h" FlightModePanel::FlightModePanel(QWidget * parent, ModelData & model, int phaseIdx, GeneralSettings & generalSettings, Firmware * firmware): @@ -28,7 +29,8 @@ FlightModePanel::FlightModePanel(QWidget * parent, ModelData & model, int phaseI phaseIdx(phaseIdx), phase(model.flightModeData[phaseIdx]), reCount(firmware->getCapability(RotaryEncoders)), - gvCount(firmware->getCapability(Gvars)) + gvCount(firmware->getCapability(Gvars)), + rawSwitchItemModel(NULL) { ui->setupUi(this); @@ -50,7 +52,8 @@ FlightModePanel::FlightModePanel(QWidget * parent, ModelData & model, int phaseI // Phase switch if (phaseIdx > 0) { - ui->swtch->setModel(Helpers::getRawSwitchItemModel(&generalSettings, Helpers::MixesContext)); + rawSwitchItemModel = new RawSwitchFilterItemModel(&generalSettings, &model, MixesContext); + ui->swtch->setModel(rawSwitchItemModel); ui->swtch->setCurrentIndex(ui->swtch->findData(phase.swtch.toValue())); connect(ui->swtch, SIGNAL(currentIndexChanged(int)), this, SLOT(phaseSwitch_currentIndexChanged(int))); } @@ -228,6 +231,10 @@ FlightModePanel::~FlightModePanel() void FlightModePanel::update() { + if (rawSwitchItemModel) { + rawSwitchItemModel->update(); + } + ui->name->setText(phase.name); int scale = firmware->getCapability(SlowScale); @@ -524,7 +531,7 @@ FlightModesPanel::FlightModesPanel(QWidget * parent, ModelData & model, GeneralS QGridLayout * gridLayout = new QGridLayout(this); tabWidget = new QTabWidget(this); for (int i=0; isetProperty("index", i); panels << tab; connect(tab, SIGNAL(modified()), this, SLOT(onPhaseModified())); diff --git a/companion/src/modeledit/flightmodes.h b/companion/src/modeledit/flightmodes.h index 464c9ab09..3507c0f5c 100644 --- a/companion/src/modeledit/flightmodes.h +++ b/companion/src/modeledit/flightmodes.h @@ -24,6 +24,8 @@ #include "modeledit.h" #include "eeprominterface.h" +class RawSwitchFilterItemModel; + namespace Ui { class FlightMode; } @@ -74,6 +76,7 @@ class FlightModePanel : public ModelPanel QVector trimsUse; QVector trimsValue; QVector trimsSlider; + RawSwitchFilterItemModel * rawSwitchItemModel; void trimUpdate(unsigned int trim); diff --git a/companion/src/modeledit/logicalswitches.cpp b/companion/src/modeledit/logicalswitches.cpp index fcdc53acd..27d3978fe 100644 --- a/companion/src/modeledit/logicalswitches.cpp +++ b/companion/src/modeledit/logicalswitches.cpp @@ -19,6 +19,7 @@ */ #include "logicalswitches.h" +#include "switchitemmodel.h" #include "helpers.h" LogicalSwitchesPanel::LogicalSwitchesPanel(QWidget * parent, ModelData & model, GeneralSettings & generalSettings, Firmware * firmware): @@ -28,8 +29,7 @@ LogicalSwitchesPanel::LogicalSwitchesPanel(QWidget * parent, ModelData & model, Stopwatch s1("LogicalSwitchesPanel"); int channelsMax = model.getChannelsMax(true); - rawSwitchItemModel = Helpers::getRawSwitchItemModel(&generalSettings, Helpers::LogicalSwitchesContext); - rawSwitchItemModel->setParent(this); + rawSwitchItemModel = new RawSwitchFilterItemModel(&generalSettings, &model, LogicalSwitchesContext); int srcFlags = POPULATE_NONE | POPULATE_SOURCES | POPULATE_SCRIPT_OUTPUTS | POPULATE_VIRTUAL_INPUTS | POPULATE_TRIMS | POPULATE_SWITCHES | POPULATE_TELEMETRY | (firmware->getCapability(GvarsInCS) ? POPULATE_GVARS : 0); rawSourceItemModel = Helpers::getRawSourceItemModel(&generalSettings, &model, srcFlags); rawSourceItemModel->setParent(this); diff --git a/companion/src/modeledit/logicalswitches.h b/companion/src/modeledit/logicalswitches.h index 70342898a..eb9178557 100644 --- a/companion/src/modeledit/logicalswitches.h +++ b/companion/src/modeledit/logicalswitches.h @@ -22,7 +22,9 @@ #define _LOGICALSWITCHES_H_ #include "modeledit.h" -#include "eeprominterface.h" +#include "radiodata.h" + +class RawSwitchFilterItemModel; class LogicalSwitchesPanel : public ModelPanel { @@ -58,7 +60,7 @@ class LogicalSwitchesPanel : public ModelPanel QDoubleSpinBox * cswitchDelay[CPN_MAX_LOGICAL_SWITCHES]; QComboBox * cswitchSource1[CPN_MAX_LOGICAL_SWITCHES]; QComboBox * cswitchSource2[CPN_MAX_LOGICAL_SWITCHES]; - QStandardItemModel * rawSwitchItemModel; + RawSwitchFilterItemModel * rawSwitchItemModel; QStandardItemModel * rawSourceItemModel; void setSwitchWidgetVisibility(int i); int selectedSwitch; diff --git a/companion/src/modeledit/mixerdialog.cpp b/companion/src/modeledit/mixerdialog.cpp index 38021184a..afcfb4754 100644 --- a/companion/src/modeledit/mixerdialog.cpp +++ b/companion/src/modeledit/mixerdialog.cpp @@ -20,10 +20,11 @@ #include "mixerdialog.h" #include "ui_mixerdialog.h" -#include "eeprominterface.h" +#include "radiodata.h" +#include "switchitemmodel.h" #include "helpers.h" -MixerDialog::MixerDialog(QWidget *parent, ModelData & model, MixData *mixdata, GeneralSettings & generalSettings, Firmware * firmware) : +MixerDialog::MixerDialog(QWidget *parent, ModelData & model, MixData * mixdata, GeneralSettings & generalSettings, Firmware * firmware) : QDialog(parent), ui(new Ui::MixerDialog), model(model), @@ -103,7 +104,7 @@ MixerDialog::MixerDialog(QWidget *parent, ModelData & model, MixData *mixdata, G } } - ui->switchesCB->setModel(Helpers::getRawSwitchItemModel(&generalSettings, Helpers::MixesContext)); + ui->switchesCB->setModel(new RawSwitchFilterItemModel(&generalSettings, &model, MixesContext)); ui->switchesCB->setCurrentIndex(ui->switchesCB->findData(md->swtch.toValue())); ui->warningCB->setCurrentIndex(md->mixWarn); ui->mltpxCB->setCurrentIndex(md->mltpx); diff --git a/companion/src/modeledit/setup.cpp b/companion/src/modeledit/setup.cpp index 1a8e4e475..11620aca5 100644 --- a/companion/src/modeledit/setup.cpp +++ b/companion/src/modeledit/setup.cpp @@ -22,6 +22,7 @@ #include "ui_setup.h" #include "ui_setup_timer.h" #include "ui_setup_module.h" +#include "switchitemmodel.h" #include "helpers.h" #include "appdata.h" #include "modelprinter.h" @@ -50,7 +51,8 @@ TimerPanel::TimerPanel(QWidget *parent, ModelData & model, TimerData & timer, Ge } // Mode - ui->mode->setModel(Helpers::getRawSwitchItemModel(&generalSettings, Helpers::TimersContext)); + rawSwitchItemModel = new RawSwitchFilterItemModel(&generalSettings, &model, TimersContext); + ui->mode->setModel(rawSwitchItemModel); ui->mode->setCurrentIndex(ui->mode->findData(timer.mode.toValue())); if (!firmware->getCapability(PermTimers)) { @@ -91,6 +93,8 @@ 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; diff --git a/companion/src/modeledit/setup.h b/companion/src/modeledit/setup.h index 20a81d930..defa5107c 100644 --- a/companion/src/modeledit/setup.h +++ b/companion/src/modeledit/setup.h @@ -24,6 +24,8 @@ #include "modeledit.h" #include "eeprominterface.h" +class RawSwitchFilterItemModel; + namespace Ui { class Setup; class Timer; @@ -49,7 +51,8 @@ class TimerPanel : public ModelPanel private: TimerData & timer; - Ui::Timer *ui; + Ui::Timer * ui; + RawSwitchFilterItemModel * rawSwitchItemModel; }; class ModulePanel : public ModelPanel diff --git a/companion/src/modeledit/switchitemmodel.cpp b/companion/src/modeledit/switchitemmodel.cpp new file mode 100644 index 000000000..9d993e469 --- /dev/null +++ b/companion/src/modeledit/switchitemmodel.cpp @@ -0,0 +1,144 @@ +/* + * 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 "switchitemmodel.h" +#include "eeprominterface.h" + +RawSwitchItemModel::RawSwitchItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData): + generalSettings(generalSettings), + modelData(modelData) +{ + Boards board = Boards(getCurrentBoard()); + Board::Type btype = board.getBoardType(); + Firmware * fw = getCurrentFirmware(); + + // Descending switch direction: NOT (!) switches + if (IS_ARM(btype)) { + add(SWITCH_TYPE_SENSOR, -CPN_MAX_SENSORS); + add(SWITCH_TYPE_TELEMETRY, -1); + add(SWITCH_TYPE_FLIGHT_MODE, -fw->getCapability(FlightModes)); + } + add(SWITCH_TYPE_VIRTUAL, -fw->getCapability(LogicalSwitches)); + add(SWITCH_TYPE_ROTARY_ENCODER, -fw->getCapability(RotaryEncoders)); + add(SWITCH_TYPE_TRIM, -board.getCapability(Board::NumTrimSwitches)); + add(SWITCH_TYPE_MULTIPOS_POT, -(fw->getCapability(MultiposPots) * fw->getCapability(MultiposPotsPositions))); + add(SWITCH_TYPE_SWITCH, -board.getCapability(Board::SwitchPositions)); + + // Ascending switch direction (including zero) + add(SWITCH_TYPE_TIMER_MODE, 5); + add(SWITCH_TYPE_NONE, 1); + add(SWITCH_TYPE_SWITCH, board.getCapability(Board::SwitchPositions)); + add(SWITCH_TYPE_MULTIPOS_POT, fw->getCapability(MultiposPots) * fw->getCapability(MultiposPotsPositions)); + add(SWITCH_TYPE_TRIM, board.getCapability(Board::NumTrimSwitches)); + add(SWITCH_TYPE_ROTARY_ENCODER, fw->getCapability(RotaryEncoders)); + add(SWITCH_TYPE_VIRTUAL, fw->getCapability(LogicalSwitches)); + if (IS_ARM(btype)) { + add(SWITCH_TYPE_FLIGHT_MODE, fw->getCapability(FlightModes)); + add(SWITCH_TYPE_TELEMETRY, 1); + add(SWITCH_TYPE_SENSOR, CPN_MAX_SENSORS); + } + add(SWITCH_TYPE_ON, 1); + add(SWITCH_TYPE_ONE, 1); +} + +void RawSwitchItemModel::add(const RawSwitchType & type, int count) +{ + // Most RawSwitch() indices are one-based (vs. typical zero); these are exceptions to the rule: + const static QVector rawSwitchIndexBaseZeroTypes = QVector() << SWITCH_TYPE_NONE << SWITCH_TYPE_ON << SWITCH_TYPE_OFF << SWITCH_TYPE_TIMER_MODE; + + int rawIdxAdj = 0; + const Board::Type board = getCurrentBoard(); + int i = (count < 0 ? count : 1); + const int maxCount = (i < 0 ? 0 : count + i); + + // handle exceptions in RawSwitch() index values + if (rawSwitchIndexBaseZeroTypes.contains(type)) + rawIdxAdj = -1; + + for ( ; i < maxCount; ++i) { + if (generalSettings) { + if (type == SWITCH_TYPE_SWITCH && IS_HORUS_OR_TARANIS(board) && !generalSettings->switchPositionAllowedTaranis(abs(i))) + continue; + if (type == SWITCH_TYPE_MULTIPOS_POT) { + int pot = div(abs(i) - 1, getCurrentFirmware()->getCapability(MultiposPotsPositions)).quot; + if (!generalSettings->isPotAvailable(pot) || generalSettings->potConfig[pot] != Board::POT_MULTIPOS_SWITCH) + continue; + } + } + RawSwitch rs(type, i + rawIdxAdj); + QStandardItem * modelItem = new QStandardItem(rs.toString(board, generalSettings, modelData)); + modelItem->setData(rs.toValue(), Qt::UserRole); + appendRow(modelItem); + } +} + +void RawSwitchItemModel::update() +{ + for (int i=0; idata(Qt::UserRole).toInt()); + modelItem->setText(swtch.toString(getCurrentBoard(), generalSettings, modelData)); + } +} + +RawSwitchFilterItemModel::RawSwitchFilterItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData, SwitchContext context): + generalSettings(generalSettings), + modelData(modelData), + context(context), + parent(new RawSwitchItemModel(generalSettings, modelData)) +{ + setSourceModel(parent); + setDynamicSortFilter(false); +} + +bool RawSwitchFilterItemModel::filterAcceptsRow(int sourceRow, const QModelIndex & sourceParent) const +{ + QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent); + RawSwitch swtch(sourceModel()->data(index, Qt::UserRole).toInt()); + + if (swtch.type == SWITCH_TYPE_FLIGHT_MODE && (context == MixesContext || context == GlobalFunctionsContext)) + return false; + + if (swtch.type == SWITCH_TYPE_VIRTUAL && context == GlobalFunctionsContext) + return false; + + if (swtch.type == SWITCH_TYPE_TIMER_MODE && context != TimersContext) + return false; + + if (swtch.type == SWITCH_TYPE_NONE && context == TimersContext) + return false; + + if (swtch.type == SWITCH_TYPE_SENSOR && context == GlobalFunctionsContext) + return false; + + if ((swtch.type == SWITCH_TYPE_ON || swtch.type == SWITCH_TYPE_ONE) && (context != SpecialFunctionsContext && context != GlobalFunctionsContext)) + return false; + + if (!modelData->isAvailable(swtch)) + return false; + + return true; +} + +void RawSwitchFilterItemModel::update() +{ + parent->update(); + invalidate(); +} diff --git a/companion/src/modeledit/switchitemmodel.h b/companion/src/modeledit/switchitemmodel.h new file mode 100644 index 000000000..4ac53b910 --- /dev/null +++ b/companion/src/modeledit/switchitemmodel.h @@ -0,0 +1,58 @@ +/* + * 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 +#include +#include "radiodata.h" + +enum SwitchContext +{ + LogicalSwitchesContext, + SpecialFunctionsContext, + GlobalFunctionsContext, + TimersContext, + MixesContext +}; + +class RawSwitchItemModel: public QStandardItemModel { + public: + RawSwitchItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData); + void update(); + + protected: + void add(const RawSwitchType & type, int count); + + const GeneralSettings * generalSettings; + const ModelData * modelData; +}; + +class RawSwitchFilterItemModel: public QSortFilterProxyModel { + public: + RawSwitchFilterItemModel(const GeneralSettings * const generalSettings = NULL, const ModelData * const modelData = NULL, SwitchContext context = LogicalSwitchesContext); + void update(); + + protected: + const GeneralSettings * generalSettings; + const ModelData * modelData; + SwitchContext context; + RawSwitchItemModel * parent; + + bool filterAcceptsRow(int sourceRow, const QModelIndex & sourceParent) const override; +}; diff --git a/companion/src/radiodata.h b/companion/src/radiodata.h index c9fde2a68..9f210cf23 100644 --- a/companion/src/radiodata.h +++ b/companion/src/radiodata.h @@ -250,7 +250,9 @@ enum RawSwitchType { SWITCH_TYPE_OFF, SWITCH_TYPE_ONE, SWITCH_TYPE_FLIGHT_MODE, - SWITCH_TYPE_TIMER_MODE + SWITCH_TYPE_TIMER_MODE, + SWITCH_TYPE_TELEMETRY, + SWITCH_TYPE_SENSOR, }; class RawSwitch { @@ -278,7 +280,7 @@ class RawSwitch { return index >= 0 ? (type * 256 + index) : -(type * 256 - index); } - QString toString(Board::Type board = Board::BOARD_UNKNOWN, const GeneralSettings * const generalSettings = NULL) const; + QString toString(Board::Type board = Board::BOARD_UNKNOWN, const GeneralSettings * const generalSettings = NULL, const ModelData * const modelData = NULL) const; bool operator== ( const RawSwitch& other) { return (this->type == other.type) && (this->index == other.index); @@ -1110,6 +1112,8 @@ class ModelData { int getChannelsMax(bool forceExtendedLimits=false) const; + bool isAvailable(const RawSwitch & swtch) const; + protected: void removeGlobalVar(int & var); };