[Companion] Refactor RawSource & RawSwitch data models to abstract the base classes and reduce redundancy; Move data models to own folder.

This commit is contained in:
Max Paperno
2018-07-12 13:20:07 -04:00
parent 70aa7c6ad8
commit ca63b92e5b
20 changed files with 440 additions and 457 deletions
+1 -3
View File
@@ -16,7 +16,6 @@ set(modeledit_NAMES
)
set(modeledit_SRCS
switchitemmodel.cpp
flightmodes.cpp
inputs.cpp
mixes.cpp
@@ -29,7 +28,6 @@ set(modeledit_SRCS
)
set(modeledit_HDRS
switchitemmodel.h
flightmodes.h
inputs.h
mixes.h
@@ -59,4 +57,4 @@ qt5_wrap_ui(modeledit_SRCS ${modeledit_UIS})
qt5_wrap_cpp(modeledit_SRCS ${modeledit_HDRS})
add_library(modeledit ${modeledit_SRCS})
target_link_libraries(modeledit PRIVATE ${CPN_COMMON_LIB} Qt5::Multimedia)
target_link_libraries(modeledit PRIVATE datamodels ${CPN_COMMON_LIB} Qt5::Multimedia)
+1 -3
View File
@@ -19,8 +19,7 @@
*/
#include "customfunctions.h"
#include "switchitemmodel.h"
#include "rawsourceitemmodel.h"
#include "rawitemfilteredmodel.h"
#include "helpers.h"
#include "appdata.h"
@@ -381,7 +380,6 @@ void CustomFunctionsPanel::refreshCustomFunction(int i, bool modified)
unsigned int widgetsMask = 0;
if (modified) {
const bool wasEmpty = cfn.isEmpty();
cfn.swtch = RawSwitch(fswtchSwtch[i]->currentData().toInt());
cfn.func = func;
cfn.enabled = fswtchEnable[i]->isChecked();
+1 -2
View File
@@ -20,8 +20,7 @@
#include "expodialog.h"
#include "ui_expodialog.h"
#include "switchitemmodel.h"
#include "rawsourceitemmodel.h"
#include "rawitemfilteredmodel.h"
#include "helpers.h"
ExpoDialog::ExpoDialog(QWidget *parent, ModelData & model, ExpoData *expoData, GeneralSettings & generalSettings,
+1 -1
View File
@@ -20,7 +20,7 @@
#include "flightmodes.h"
#include "ui_flightmode.h"
#include "switchitemmodel.h"
#include "rawitemfilteredmodel.h"
#include "helpers.h"
#include "customdebug.h"
+1 -1
View File
@@ -21,7 +21,7 @@
#include "heli.h"
#include "ui_heli.h"
#include "helpers.h"
#include "rawsourceitemmodel.h"
#include "rawitemfilteredmodel.h"
HeliPanel::HeliPanel(QWidget *parent, ModelData & model, GeneralSettings & generalSettings, Firmware * firmware):
ModelPanel(parent, model, generalSettings, firmware),
+2 -3
View File
@@ -19,8 +19,7 @@
*/
#include "logicalswitches.h"
#include "switchitemmodel.h"
#include "rawsourceitemmodel.h"
#include "rawitemfilteredmodel.h"
#include "helpers.h"
#include <TimerEdit>
@@ -33,7 +32,7 @@ LogicalSwitchesPanel::LogicalSwitchesPanel(QWidget * parent, ModelData & model,
rawSwitchItemModel = new RawSwitchFilterItemModel(&generalSettings, &model, RawSwitch::LogicalSwitchesContext, this);
const unsigned srcGroups = firmware->getCapability(GvarsInCS) ? RawSource::AllSourceGroups : (RawSource::AllSourceGroups & ~RawSource::GVarsGroup);
const int srcGroups = firmware->getCapability(GvarsInCS) ? 0 : (RawSource::AllSourceGroups & ~RawSource::GVarsGroup);
rawSourceItemModel = new RawSourceFilterItemModel(&generalSettings, &model, srcGroups, this);
QStringList headerLabels;
+1 -2
View File
@@ -21,8 +21,7 @@
#include "mixerdialog.h"
#include "ui_mixerdialog.h"
#include "radiodata.h"
#include "switchitemmodel.h"
#include "rawsourceitemmodel.h"
#include "rawitemfilteredmodel.h"
#include "helpers.h"
MixerDialog::MixerDialog(QWidget *parent, ModelData & model, MixData * mixdata, GeneralSettings & generalSettings, Firmware * firmware) :
+1 -1
View File
@@ -22,7 +22,7 @@
#include "ui_setup.h"
#include "ui_setup_timer.h"
#include "ui_setup_module.h"
#include "switchitemmodel.h"
#include "rawitemfilteredmodel.h"
#include "appdata.h"
#include "modelprinter.h"
#include "multiprotocols.h"
-168
View File
@@ -1,168 +0,0 @@
/*
* 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, QObject * parent) :
QStandardItemModel(parent),
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, -(board.getCapability(Board::MultiposPots) * board.getCapability(Board::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, board.getCapability(Board::MultiposPots) * board.getCapability(Board::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::setDynamicItemData(QStandardItem * item, const RawSwitch & rsw) const
{
const Board::Type board = getCurrentBoard();
item->setText(rsw.toString(board, generalSettings, modelData));
item->setData(rsw.isAvailable(modelData, generalSettings, board), IsAvailableRole);
}
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;
// handle exceptions in RawSwitch() index values
const short rawIdxAdj = rawSwitchIndexBaseZeroTypes.contains(type) ? -1 : 0;
// determine cotext flags
unsigned context = RawSwitch::AllSwitchContexts;
switch (type) {
case SWITCH_TYPE_FLIGHT_MODE:
context &= ~RawSwitch::MixesContext;
// fallthrough
case SWITCH_TYPE_VIRTUAL:
case SWITCH_TYPE_SENSOR:
context &= ~RawSwitch::GlobalFunctionsContext;
break;
case SWITCH_TYPE_TIMER_MODE:
context = RawSwitch::TimersContext;
break;
case SWITCH_TYPE_NONE:
context &= ~RawSwitch::TimersContext;
break;
case SWITCH_TYPE_ON:
case SWITCH_TYPE_ONE:
context = RawSwitch::SpecialFunctionsContext | RawSwitch::GlobalFunctionsContext;
break;
default:
break;
}
int i = (count < 0 ? count : 1);
count = (i < 0 ? 0 : count + i);
for ( ; i < count; ++i) {
RawSwitch rs(type, i + rawIdxAdj);
QStandardItem * modelItem = new QStandardItem();
modelItem->setData(rs.toValue(), ItemIdRole);
modelItem->setData(type, ItemTypeRole);
modelItem->setData(context, ContextRole);
setDynamicItemData(modelItem, rs);
appendRow(modelItem);
}
}
void RawSwitchItemModel::update() const
{
for (int i=0; i < rowCount(); ++i)
setDynamicItemData(item(i), RawSwitch(item(i)->data(ItemIdRole).toInt()));
}
//
// RawSwitchFilterItemModel
//
RawSwitchFilterItemModel::RawSwitchFilterItemModel(QAbstractItemModel * sourceModel, RawSwitch::SwitchContext context, QObject * parent) :
QSortFilterProxyModel(parent)
{
setFilterRole(RawSwitchItemModel::IsAvailableRole);
setFilterKeyColumn(0);
setSwitchContext(context);
setDynamicSortFilter(true);
setSourceModel(sourceModel);
}
RawSwitchFilterItemModel::RawSwitchFilterItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData, RawSwitch::SwitchContext context, QObject * parent):
RawSwitchFilterItemModel(new RawSwitchItemModel(generalSettings, modelData, parent), context, parent)
{
}
void RawSwitchFilterItemModel::setSwitchContext(RawSwitch::SwitchContext ctxt)
{
if (ctxt != context) {
context = ctxt;
invalidateFilter();
}
}
bool RawSwitchFilterItemModel::filterAcceptsRow(int sourceRow, const QModelIndex & sourceParent) const
{
const QModelIndex & srcIdx = sourceModel()->index(sourceRow, 0, sourceParent);
if (!srcIdx.isValid() || !sourceModel()->data(srcIdx, filterRole()).toBool())
return false;
bool ok;
const RawSwitch::SwitchContext ctxt = RawSwitch::SwitchContext(sourceModel()->data(srcIdx, RawSwitchItemModel::ContextRole).toUInt(&ok));
return (ok && (ctxt == RawSwitch::AllSwitchContexts || context != RawSwitch::NoSwitchContext || (context & ctxt)));
}
void RawSwitchFilterItemModel::update() const
{
RawSwitchItemModel * model = qobject_cast<RawSwitchItemModel *>(sourceModel());
if (model)
model->update();
}
-63
View File
@@ -1,63 +0,0 @@
/*
* 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 "rawswitch.h"
class GeneralSettings;
class ModelData;
class RawSwitchItemModel: public QStandardItemModel
{
Q_OBJECT
public:
enum DataRoles { ItemIdRole = Qt::UserRole, ItemTypeRole, ContextRole, IsAvailableRole };
Q_ENUM(DataRoles)
RawSwitchItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData, QObject * parent = nullptr);
public slots:
void update() const;
protected:
void setDynamicItemData(QStandardItem * item, const RawSwitch & rsw) const;
void add(const RawSwitchType & type, int count);
const GeneralSettings * generalSettings;
const ModelData * modelData;
};
class RawSwitchFilterItemModel: public QSortFilterProxyModel
{
Q_OBJECT
public:
RawSwitchFilterItemModel(QAbstractItemModel * sourceModel, RawSwitch::SwitchContext context, QObject * parent = nullptr);
RawSwitchFilterItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData, RawSwitch::SwitchContext context, QObject * parent = nullptr);
public slots:
void setSwitchContext(RawSwitch::SwitchContext ctxt);
void update() const;
protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex & sourceParent) const override;
RawSwitch::SwitchContext context = RawSwitch::NoSwitchContext;
};
+1 -1
View File
@@ -25,7 +25,7 @@
#include "ui_telemetry_sensor.h"
#include "helpers.h"
#include "appdata.h"
#include "rawsourceitemmodel.h"
#include "rawitemfilteredmodel.h"
#include <TimerEdit>