[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
This commit is contained in:
Bertrand Songis
2017-07-30 12:12:27 +02:00
parent b814f997f8
commit b55070f939
18 changed files with 290 additions and 111 deletions
+1
View File
@@ -16,6 +16,7 @@ set(modeledit_NAMES
)
set(modeledit_SRCS
switchitemmodel.cpp
flightmodes.cpp
inputs.cpp
mixes.cpp
+4 -2
View File
@@ -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<num_fsw; i++) {
+4 -2
View File
@@ -21,9 +21,11 @@
#ifndef _CUSTOMFUNCTIONS_H_
#define _CUSTOMFUNCTIONS_H_
#include <QMediaPlayer>
#include "modeledit.h"
#include "eeprominterface.h"
#include <QMediaPlayer>
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;
+2 -1
View File
@@ -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);
+10 -3
View File
@@ -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; i<modesCount; i++) {
FlightModePanel *tab = new FlightModePanel(tabWidget, model, i, generalSettings, firmware);
FlightModePanel * tab = new FlightModePanel(tabWidget, model, i, generalSettings, firmware);
tab->setProperty("index", i);
panels << tab;
connect(tab, SIGNAL(modified()), this, SLOT(onPhaseModified()));
+3
View File
@@ -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<QComboBox *> trimsUse;
QVector<QSpinBox *> trimsValue;
QVector<QSlider *> trimsSlider;
RawSwitchFilterItemModel * rawSwitchItemModel;
void trimUpdate(unsigned int trim);
+2 -2
View File
@@ -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);
+4 -2
View File
@@ -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;
+4 -3
View File
@@ -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);
+5 -1
View File
@@ -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;
+4 -1
View File
@@ -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
+144
View File
@@ -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<int> rawSwitchIndexBaseZeroTypes = QVector<int>() << 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; i<rowCount(); i++) {
QStandardItem * modelItem = item(i, 0);
RawSwitch swtch(modelItem->data(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();
}
+58
View File
@@ -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 <QStandardItemModel>
#include <QSortFilterProxyModel>
#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;
};