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
),
28 Boards board
= Boards(getCurrentBoard());
29 Board::Type btype
= board
.getBoardType();
30 Firmware
* fw
= getCurrentFirmware();
32 // Descending switch direction: NOT (!) switches
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
));
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
;
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
))
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
);
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
),
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
))
110 if (swtch
.type
== SWITCH_TYPE_VIRTUAL
&& context
== GlobalFunctionsContext
)
113 if (swtch
.type
== SWITCH_TYPE_TIMER_MODE
&& context
!= TimersContext
)
116 if (swtch
.type
== SWITCH_TYPE_NONE
&& context
== TimersContext
)
119 if (swtch
.type
== SWITCH_TYPE_SENSOR
&& context
== GlobalFunctionsContext
)
122 if ((swtch
.type
== SWITCH_TYPE_ON
|| swtch
.type
== SWITCH_TYPE_ONE
) && (context
!= SpecialFunctionsContext
&& context
!= GlobalFunctionsContext
))
125 if (!swtch
.isAvailable(modelData
, generalSettings
, getCurrentBoard()))
131 void RawSwitchFilterItemModel::update()