Fix X7 R9M power line not enabled (#5973)
[opentx.git] / companion / src / modeledit / switchitemmodel.cpp
blob3220ea0c749c0a10b287dbbd1c0d466e3ad92d78
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "switchitemmodel.h"
22 #include "eeprominterface.h"
24 RawSwitchItemModel::RawSwitchItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData):
25 generalSettings(generalSettings),
26 modelData(modelData)
28 Boards board = Boards(getCurrentBoard());
29 Board::Type btype = board.getBoardType();
30 Firmware * fw = getCurrentFirmware();
32 // Descending switch direction: NOT (!) switches
33 if (IS_ARM(btype)) {
34 add(SWITCH_TYPE_SENSOR, -CPN_MAX_SENSORS);
35 add(SWITCH_TYPE_TELEMETRY, -1);
36 add(SWITCH_TYPE_FLIGHT_MODE, -fw->getCapability(FlightModes));
38 add(SWITCH_TYPE_VIRTUAL, -fw->getCapability(LogicalSwitches));
39 add(SWITCH_TYPE_ROTARY_ENCODER, -fw->getCapability(RotaryEncoders));
40 add(SWITCH_TYPE_TRIM, -board.getCapability(Board::NumTrimSwitches));
41 add(SWITCH_TYPE_MULTIPOS_POT, -(board.getCapability(Board::MultiposPots) * board.getCapability(Board::MultiposPotsPositions)));
42 add(SWITCH_TYPE_SWITCH, -board.getCapability(Board::SwitchPositions));
44 // Ascending switch direction (including zero)
45 add(SWITCH_TYPE_TIMER_MODE, 5);
46 add(SWITCH_TYPE_NONE, 1);
47 add(SWITCH_TYPE_SWITCH, board.getCapability(Board::SwitchPositions));
48 add(SWITCH_TYPE_MULTIPOS_POT, board.getCapability(Board::MultiposPots) * board.getCapability(Board::MultiposPotsPositions));
49 add(SWITCH_TYPE_TRIM, board.getCapability(Board::NumTrimSwitches));
50 add(SWITCH_TYPE_ROTARY_ENCODER, fw->getCapability(RotaryEncoders));
51 add(SWITCH_TYPE_VIRTUAL, fw->getCapability(LogicalSwitches));
52 if (IS_ARM(btype)) {
53 add(SWITCH_TYPE_FLIGHT_MODE, fw->getCapability(FlightModes));
54 add(SWITCH_TYPE_TELEMETRY, 1);
55 add(SWITCH_TYPE_SENSOR, CPN_MAX_SENSORS);
57 add(SWITCH_TYPE_ON, 1);
58 add(SWITCH_TYPE_ONE, 1);
61 void RawSwitchItemModel::add(const RawSwitchType & type, int count)
63 // Most RawSwitch() indices are one-based (vs. typical zero); these are exceptions to the rule:
64 const static QVector<int> rawSwitchIndexBaseZeroTypes = QVector<int>() << SWITCH_TYPE_NONE << SWITCH_TYPE_ON << SWITCH_TYPE_OFF << SWITCH_TYPE_TIMER_MODE;
66 int rawIdxAdj = 0;
67 const Board::Type board = getCurrentBoard();
68 int i = (count < 0 ? count : 1);
69 const int maxCount = (i < 0 ? 0 : count + i);
71 // handle exceptions in RawSwitch() index values
72 if (rawSwitchIndexBaseZeroTypes.contains(type))
73 rawIdxAdj = -1;
75 for ( ; i < maxCount; ++i) {
76 RawSwitch rs(type, i + rawIdxAdj);
77 QStandardItem * modelItem = new QStandardItem(rs.toString(board, generalSettings, modelData));
78 modelItem->setData(rs.toValue(), Qt::UserRole);
79 appendRow(modelItem);
83 void RawSwitchItemModel::update()
85 for (int i=0; i<rowCount(); i++) {
86 QStandardItem * modelItem = item(i, 0);
87 RawSwitch swtch(modelItem->data(Qt::UserRole).toInt());
88 modelItem->setText(swtch.toString(getCurrentBoard(), generalSettings, modelData));
92 RawSwitchFilterItemModel::RawSwitchFilterItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData, SwitchContext context):
93 generalSettings(generalSettings),
94 modelData(modelData),
95 context(context),
96 parent(new RawSwitchItemModel(generalSettings, modelData))
98 setSourceModel(parent);
99 setDynamicSortFilter(false);
102 bool RawSwitchFilterItemModel::filterAcceptsRow(int sourceRow, const QModelIndex & sourceParent) const
104 QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
105 RawSwitch swtch(sourceModel()->data(index, Qt::UserRole).toInt());
107 if (swtch.type == SWITCH_TYPE_FLIGHT_MODE && (context == MixesContext || context == GlobalFunctionsContext))
108 return false;
110 if (swtch.type == SWITCH_TYPE_VIRTUAL && context == GlobalFunctionsContext)
111 return false;
113 if (swtch.type == SWITCH_TYPE_TIMER_MODE && context != TimersContext)
114 return false;
116 if (swtch.type == SWITCH_TYPE_NONE && context == TimersContext)
117 return false;
119 if (swtch.type == SWITCH_TYPE_SENSOR && context == GlobalFunctionsContext)
120 return false;
122 if ((swtch.type == SWITCH_TYPE_ON || swtch.type == SWITCH_TYPE_ONE) && (context != SpecialFunctionsContext && context != GlobalFunctionsContext))
123 return false;
125 if (!swtch.isAvailable(modelData, generalSettings, getCurrentBoard()))
126 return false;
128 return true;
131 void RawSwitchFilterItemModel::update()
133 parent->update();
134 invalidate();