From ca63b92e5b2aeea5d4377303ae07f2278f97f4f1 Mon Sep 17 00:00:00 2001 From: Max Paperno Date: Wed, 11 Jul 2018 10:12:17 -0400 Subject: [PATCH] [Companion] Refactor RawSource & RawSwitch data models to abstract the base classes and reduce redundancy; Move data models to own folder. --- companion/src/CMakeLists.txt | 6 +- companion/src/datamodels/CMakeLists.txt | 24 +++ .../src/datamodels/rawitemdatamodels.cpp | 172 ++++++++++++++++++ companion/src/datamodels/rawitemdatamodels.h | 84 +++++++++ .../src/datamodels/rawitemfilteredmodel.cpp | 74 ++++++++ .../src/datamodels/rawitemfilteredmodel.h | 74 ++++++++ .../src/datamodels/rawsourceitemmodel.cpp | 127 ------------- companion/src/datamodels/rawsourceitemmodel.h | 77 -------- companion/src/firmwares/rawswitch.h | 1 - companion/src/modeledit/CMakeLists.txt | 4 +- companion/src/modeledit/customfunctions.cpp | 4 +- companion/src/modeledit/expodialog.cpp | 3 +- companion/src/modeledit/flightmodes.cpp | 2 +- companion/src/modeledit/heli.cpp | 2 +- companion/src/modeledit/logicalswitches.cpp | 5 +- companion/src/modeledit/mixerdialog.cpp | 3 +- companion/src/modeledit/setup.cpp | 2 +- companion/src/modeledit/switchitemmodel.cpp | 168 ----------------- companion/src/modeledit/switchitemmodel.h | 63 ------- companion/src/modeledit/telemetry.cpp | 2 +- 20 files changed, 440 insertions(+), 457 deletions(-) create mode 100644 companion/src/datamodels/CMakeLists.txt create mode 100644 companion/src/datamodels/rawitemdatamodels.cpp create mode 100644 companion/src/datamodels/rawitemdatamodels.h create mode 100644 companion/src/datamodels/rawitemfilteredmodel.cpp create mode 100644 companion/src/datamodels/rawitemfilteredmodel.h delete mode 100644 companion/src/datamodels/rawsourceitemmodel.cpp delete mode 100644 companion/src/datamodels/rawsourceitemmodel.h delete mode 100644 companion/src/modeledit/switchitemmodel.cpp delete mode 100644 companion/src/modeledit/switchitemmodel.h diff --git a/companion/src/CMakeLists.txt b/companion/src/CMakeLists.txt index 288767939..4626b0f9f 100644 --- a/companion/src/CMakeLists.txt +++ b/companion/src/CMakeLists.txt @@ -106,7 +106,6 @@ include_directories( ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR} ${COMPANION_SRC_DIRECTORY} - ${COMPANION_SRC_DIRECTORY}/datamodels ${COMPANION_SRC_DIRECTORY}/shared ${SIMU_SRC_DIRECTORY} ${COMPANION_SRC_DIRECTORY}/thirdparty/qcustomplot @@ -205,6 +204,7 @@ set(CPN_COMMON_LIB common) ############# Supporting libraries ############### +add_subdirectory(datamodels) add_subdirectory(firmwares) add_subdirectory(shared) add_subdirectory(modeledit) @@ -249,8 +249,7 @@ set(companion_SRCS wizarddata.cpp wizarddialog.cpp styleeditdialog.cpp - datamodels/rawsourceitemmodel.cpp # TODO move to own lib with other item models - dialogs/filesyncdialog.cpp # also move all dialogs into subfolder + dialogs/filesyncdialog.cpp # TODO move to own lib with other dialogs ) set(companion_MOC_HDRS @@ -284,7 +283,6 @@ set(companion_MOC_HDRS modelslist.h styleeditdialog.h helpers_html.h - datamodels/rawsourceitemmodel.h dialogs/filesyncdialog.h ) diff --git a/companion/src/datamodels/CMakeLists.txt b/companion/src/datamodels/CMakeLists.txt new file mode 100644 index 000000000..beb7d4d2d --- /dev/null +++ b/companion/src/datamodels/CMakeLists.txt @@ -0,0 +1,24 @@ + +set(datamodels_SRCS + rawitemdatamodels.cpp + rawitemfilteredmodel.cpp +) + +set(datamodels_HDRS + rawitemdatamodels.h + rawitemfilteredmodel.h +) + +qt5_wrap_cpp(datamodels_SRCS ${datamodels_HDRS}) + +add_library(datamodels ${datamodels_SRCS} ${datamodels_HDRS}) +target_link_libraries(datamodels PRIVATE ${CPN_COMMON_LIB}) + +target_include_directories(datamodels + PRIVATE + "${CMAKE_SOURCE_DIR}" + "${CMAKE_CURRENT_BINARY_DIR}" + "${COMPANION_SRC_DIRECTORY}" + PUBLIC + "${CMAKE_CURRENT_LIST_DIR}" +) diff --git a/companion/src/datamodels/rawitemdatamodels.cpp b/companion/src/datamodels/rawitemdatamodels.cpp new file mode 100644 index 000000000..4ccd8f429 --- /dev/null +++ b/companion/src/datamodels/rawitemdatamodels.cpp @@ -0,0 +1,172 @@ +/* + * 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 "rawitemdatamodels.h" +#include "generalsettings.h" +#include "eeprominterface.h" +#include "modeldata.h" + +RawSourceItemModel::RawSourceItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData, QObject * parent) : + AbstractRawItemDataModel(generalSettings, modelData, parent) +{ + const Boards board = Boards(getCurrentBoard()); + Firmware * fw = getCurrentFirmware(); + const int anasCount = CPN_MAX_STICKS + board.getCapability(Board::Pots) + board.getCapability(Board::Sliders) + board.getCapability(Board::MouseAnalogs); + + addItems(SOURCE_TYPE_NONE, RawSource::NoneGroup, 1); + for (int i=0; i < fw->getCapability(LuaScripts); i++) + addItems(SOURCE_TYPE_LUA_OUTPUT, RawSource::ScriptsGroup, fw->getCapability(LuaOutputsPerScript), i * 16); + addItems(SOURCE_TYPE_VIRTUAL_INPUT, RawSource::InputsGroup, fw->getCapability(VirtualInputs)); + addItems(SOURCE_TYPE_STICK, RawSource::SourcesGroup, anasCount); + addItems(SOURCE_TYPE_ROTARY_ENCODER, RawSource::SourcesGroup, fw->getCapability(RotaryEncoders)); + addItems(SOURCE_TYPE_TRIM, RawSource::TrimsGroup, board.getCapability(Board::NumTrims)); + addItems(SOURCE_TYPE_MAX, RawSource::SourcesGroup, 1); + addItems(SOURCE_TYPE_SWITCH, RawSource::SwitchesGroup, board.getCapability(Board::Switches)); + addItems(SOURCE_TYPE_CUSTOM_SWITCH, RawSource::SwitchesGroup, fw->getCapability(LogicalSwitches)); + addItems(SOURCE_TYPE_CYC, RawSource::SourcesGroup, CPN_MAX_CYC); + addItems(SOURCE_TYPE_PPM, RawSource::SourcesGroup, fw->getCapability(TrainerInputs)); + addItems(SOURCE_TYPE_CH, RawSource::SourcesGroup, fw->getCapability(Outputs)); + addItems(SOURCE_TYPE_SPECIAL, RawSource::TelemGroup, 5); + addItems(SOURCE_TYPE_TELEMETRY, RawSource::TelemGroup, CPN_MAX_SENSORS * 3); + addItems(SOURCE_TYPE_GVAR, RawSource::GVarsGroup, fw->getCapability(Gvars)); +} + +void RawSourceItemModel::setDynamicItemData(QStandardItem * item, const RawSource & src) const +{ + Board::Type board = getCurrentBoard(); + item->setText(src.toString(modelData, generalSettings, board)); + item->setData(src.isAvailable(modelData, generalSettings, board), IsAvailableRole); +} + +void RawSourceItemModel::addItems(const RawSourceType & type, const int group, const int count, const int start) +{ + for (int i = start; i < start + count; i++) { + const RawSource src = RawSource(type, i); + QStandardItem * modelItem = new QStandardItem(); + modelItem->setData(src.toValue(), ItemIdRole); + modelItem->setData(type, ItemTypeRole); + modelItem->setData(group, ItemFlagsRole); + setDynamicItemData(modelItem, src); + appendRow(modelItem); + } +} + +void RawSourceItemModel::update() const +{ + for (int i=0; i < rowCount(); ++i) + setDynamicItemData(item(i), RawSource(item(i)->data(ItemIdRole).toInt())); +} + + +// +// RawSwitchItemModel +// + +RawSwitchItemModel::RawSwitchItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData, QObject * parent) : + AbstractRawItemDataModel(generalSettings, modelData, parent) +{ + Boards board = Boards(getCurrentBoard()); + Firmware * fw = getCurrentFirmware(); + + // Descending switch direction: NOT (!) switches + addItems(SWITCH_TYPE_SENSOR, -CPN_MAX_SENSORS); + addItems(SWITCH_TYPE_TELEMETRY, -1); + addItems(SWITCH_TYPE_FLIGHT_MODE, -fw->getCapability(FlightModes)); + addItems(SWITCH_TYPE_VIRTUAL, -fw->getCapability(LogicalSwitches)); + addItems(SWITCH_TYPE_ROTARY_ENCODER, -fw->getCapability(RotaryEncoders)); + addItems(SWITCH_TYPE_TRIM, -board.getCapability(Board::NumTrimSwitches)); + addItems(SWITCH_TYPE_MULTIPOS_POT, -(board.getCapability(Board::MultiposPots) * board.getCapability(Board::MultiposPotsPositions))); + addItems(SWITCH_TYPE_SWITCH, -board.getCapability(Board::SwitchPositions)); + + // Ascending switch direction (including zero) + addItems(SWITCH_TYPE_TIMER_MODE, 5); + addItems(SWITCH_TYPE_NONE, 1); + addItems(SWITCH_TYPE_SWITCH, board.getCapability(Board::SwitchPositions)); + addItems(SWITCH_TYPE_MULTIPOS_POT, board.getCapability(Board::MultiposPots) * board.getCapability(Board::MultiposPotsPositions)); + addItems(SWITCH_TYPE_TRIM, board.getCapability(Board::NumTrimSwitches)); + addItems(SWITCH_TYPE_ROTARY_ENCODER, fw->getCapability(RotaryEncoders)); + addItems(SWITCH_TYPE_VIRTUAL, fw->getCapability(LogicalSwitches)); + addItems(SWITCH_TYPE_FLIGHT_MODE, fw->getCapability(FlightModes)); + addItems(SWITCH_TYPE_TELEMETRY, 1); + addItems(SWITCH_TYPE_SENSOR, CPN_MAX_SENSORS); + addItems(SWITCH_TYPE_ON, 1); + addItems(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::addItems(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; + // handle exceptions in RawSwitch() index values + const short rawIdxAdj = rawSwitchIndexBaseZeroTypes.contains(type) ? -1 : 0; + + // determine cotext flags + int 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, ItemFlagsRole); + 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())); +} diff --git a/companion/src/datamodels/rawitemdatamodels.h b/companion/src/datamodels/rawitemdatamodels.h new file mode 100644 index 000000000..59eb6025e --- /dev/null +++ b/companion/src/datamodels/rawitemdatamodels.h @@ -0,0 +1,84 @@ +/* + * 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. + */ + +#ifndef RAWITEMDATAMODELS_H +#define RAWITEMDATAMODELS_H + +#include "rawsource.h" +#include "rawswitch.h" + +#include + +class GeneralSettings; +class ModelData; + +class AbstractRawItemDataModel: public QStandardItemModel +{ + Q_OBJECT + public: + enum DataRoles { ItemIdRole = Qt::UserRole, ItemTypeRole, ItemFlagsRole, IsAvailableRole }; + Q_ENUM(DataRoles) + + explicit AbstractRawItemDataModel(const GeneralSettings * const generalSettings, const ModelData * const modelData, QObject * parent = nullptr) : + QStandardItemModel(parent), + generalSettings(generalSettings), + modelData(modelData) + {} + + public slots: + virtual void update() const = 0; + + protected: + const GeneralSettings * generalSettings; + const ModelData * modelData; +}; + + +class RawSourceItemModel: public AbstractRawItemDataModel +{ + Q_OBJECT + public: + explicit RawSourceItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData, QObject * parent = nullptr); + + public slots: + void update() const override; + + protected: + void setDynamicItemData(QStandardItem * item, const RawSource & src) const; + void addItems(const RawSourceType & type, const int group, const int count, const int start = 0); +}; + + +class RawSwitchItemModel: public AbstractRawItemDataModel +{ + Q_OBJECT + public: + explicit RawSwitchItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData, QObject * parent = nullptr); + + public slots: + void update() const override; + + protected: + void setDynamicItemData(QStandardItem * item, const RawSwitch & rsw) const; + void addItems(const RawSwitchType & type, int count); +}; + + +#endif // RAWITEMDATAMODELS_H diff --git a/companion/src/datamodels/rawitemfilteredmodel.cpp b/companion/src/datamodels/rawitemfilteredmodel.cpp new file mode 100644 index 000000000..1de9e26b2 --- /dev/null +++ b/companion/src/datamodels/rawitemfilteredmodel.cpp @@ -0,0 +1,74 @@ +/* + * 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 "rawitemfilteredmodel.h" +#include "rawitemdatamodels.h" + +RawItemFilteredModel::RawItemFilteredModel(QAbstractItemModel * sourceModel, int flags, QObject * parent) : + QSortFilterProxyModel(parent), + filterFlags(0) +{ + setFilterRole(AbstractRawItemDataModel::IsAvailableRole); + setFilterKeyColumn(0); + setFilterFlags(flags); + setDynamicSortFilter(true); + setSourceModel(sourceModel); +} + +void RawItemFilteredModel::setFilterFlags(int flags) +{ + if (filterFlags != flags) { + filterFlags = flags; + invalidateFilter(); + } +} + +bool RawItemFilteredModel::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; + + if (!filterFlags) + return true; + + bool ok; + const int flags = sourceModel()->data(srcIdx, AbstractRawItemDataModel::ItemFlagsRole).toInt(&ok); + return (ok && (!flags || (filterFlags & flags))); +} + +void RawItemFilteredModel::update() const +{ + AbstractRawItemDataModel * model = qobject_cast(sourceModel()); + if (model) + model->update(); +} + + +RawSourceFilterItemModel::RawSourceFilterItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData, int flags, QObject * parent) : + RawItemFilteredModel(new RawSourceItemModel(generalSettings, modelData, parent), flags, parent) +{ +} + + +RawSwitchFilterItemModel::RawSwitchFilterItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData, int context, QObject * parent) : + RawItemFilteredModel(new RawSwitchItemModel(generalSettings, modelData, parent), context, parent) +{ +} diff --git a/companion/src/datamodels/rawitemfilteredmodel.h b/companion/src/datamodels/rawitemfilteredmodel.h new file mode 100644 index 000000000..2d95dff71 --- /dev/null +++ b/companion/src/datamodels/rawitemfilteredmodel.h @@ -0,0 +1,74 @@ +/* + * 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. + */ + +#ifndef RAWITEMFILTEREDMODEL_H +#define RAWITEMFILTEREDMODEL_H + +#include + +class GeneralSettings; +class ModelData; +class RawSourceItemModel; +class RawSwitchItemModel; + +class RawItemFilteredModel: public QSortFilterProxyModel +{ + Q_OBJECT + public: + explicit RawItemFilteredModel(QAbstractItemModel * sourceModel, int flags, QObject * parent = nullptr); + explicit RawItemFilteredModel(QAbstractItemModel * sourceModel, QObject * parent = nullptr) : RawItemFilteredModel(sourceModel, 0, parent) {} + + public slots: + void setFilterFlags(int flags); + void update() const; + + protected: + bool filterAcceptsRow(int sourceRow, const QModelIndex & sourceParent) const override; + + int filterFlags; +}; + + +// The specialized "convenience" types below can go away once centralalized RawSource/RawSwitch item models are established. +// These proxy classes will automatically create a source model of the corresponding type. + +class RawSwitchFilterItemModel: public RawItemFilteredModel +{ + Q_OBJECT + public: + using RawItemFilteredModel::RawItemFilteredModel; + + explicit RawSwitchFilterItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData, int context, QObject * parent = nullptr); +}; + + +class RawSourceFilterItemModel: public RawItemFilteredModel +{ + Q_OBJECT + public: + using RawItemFilteredModel::RawItemFilteredModel; + + explicit RawSourceFilterItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData, int flags, QObject * parent = nullptr); + + explicit RawSourceFilterItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData, QObject * parent = nullptr) : + RawSourceFilterItemModel(generalSettings, modelData, 0, parent) {} +}; + +#endif // RAWITEMFILTEREDMODEL_H diff --git a/companion/src/datamodels/rawsourceitemmodel.cpp b/companion/src/datamodels/rawsourceitemmodel.cpp deleted file mode 100644 index afb39ec05..000000000 --- a/companion/src/datamodels/rawsourceitemmodel.cpp +++ /dev/null @@ -1,127 +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 "rawsourceitemmodel.h" -#include "generalsettings.h" -#include "eeprominterface.h" -#include "modeldata.h" - -RawSourceItemModel::RawSourceItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData, QObject * parent) : - QStandardItemModel(parent), - generalSettings(generalSettings), - modelData(modelData) -{ - const Boards board = Boards(getCurrentBoard()); - Firmware * fw = getCurrentFirmware(); - const int anasCount = CPN_MAX_STICKS + board.getCapability(Board::Pots) + board.getCapability(Board::Sliders) + board.getCapability(Board::MouseAnalogs); - - addItems(SOURCE_TYPE_NONE, RawSource::NoneGroup, 1); - for (int i=0; i < fw->getCapability(LuaScripts); i++) - addItems(SOURCE_TYPE_LUA_OUTPUT, RawSource::ScriptsGroup, fw->getCapability(LuaOutputsPerScript), i * 16); - addItems(SOURCE_TYPE_VIRTUAL_INPUT, RawSource::InputsGroup, fw->getCapability(VirtualInputs)); - addItems(SOURCE_TYPE_STICK, RawSource::SourcesGroup, anasCount); - addItems(SOURCE_TYPE_ROTARY_ENCODER, RawSource::SourcesGroup, fw->getCapability(RotaryEncoders)); - addItems(SOURCE_TYPE_TRIM, RawSource::TrimsGroup, board.getCapability(Board::NumTrims)); - addItems(SOURCE_TYPE_MAX, RawSource::SourcesGroup, 1); - addItems(SOURCE_TYPE_SWITCH, RawSource::SwitchesGroup, board.getCapability(Board::Switches)); - addItems(SOURCE_TYPE_CUSTOM_SWITCH, RawSource::SwitchesGroup, fw->getCapability(LogicalSwitches)); - addItems(SOURCE_TYPE_CYC, RawSource::SourcesGroup, CPN_MAX_CYC); - addItems(SOURCE_TYPE_PPM, RawSource::SourcesGroup, fw->getCapability(TrainerInputs)); - addItems(SOURCE_TYPE_CH, RawSource::SourcesGroup, fw->getCapability(Outputs)); - addItems(SOURCE_TYPE_SPECIAL, RawSource::TelemGroup, 5); - addItems(SOURCE_TYPE_TELEMETRY, RawSource::TelemGroup, CPN_MAX_SENSORS * 3); - addItems(SOURCE_TYPE_GVAR, RawSource::GVarsGroup, fw->getCapability(Gvars)); -} - -void RawSourceItemModel::setDynamicItemData(QStandardItem * item, const RawSource & src) const -{ - Board::Type board = getCurrentBoard(); - item->setText(src.toString(modelData, generalSettings, board)); - item->setData(src.isAvailable(modelData, generalSettings, board), IsAvailableRole); -} - -void RawSourceItemModel::addItems(const RawSourceType & type, const unsigned group, const int count, const int start) -{ - for (int i = start; i < start + count; i++) { - const RawSource src = RawSource(type, i); - QStandardItem * modelItem = new QStandardItem(); - modelItem->setData(src.toValue(), ItemIdRole); - modelItem->setData(type, ItemTypeRole); - modelItem->setData(group, ItemGroupRole); - setDynamicItemData(modelItem, src); - appendRow(modelItem); - } -} - -void RawSourceItemModel::update() const -{ - for (int i=0; i < rowCount(); ++i) - setDynamicItemData(item(i), RawSource(item(i)->data(ItemIdRole).toInt())); -} - - -// -// RawSourceFilterItemModel -// - -RawSourceFilterItemModel::RawSourceFilterItemModel(QAbstractItemModel * sourceModel, unsigned groups, QObject * parent) : - QSortFilterProxyModel(parent), - filterGroups(0) -{ - setFilterRole(RawSourceItemModel::IsAvailableRole); - setFilterKeyColumn(0); - setFilterGroups(groups); - setDynamicSortFilter(true); - setSourceModel(sourceModel); -} - -RawSourceFilterItemModel::RawSourceFilterItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData, unsigned groups, QObject * parent) : - RawSourceFilterItemModel(new RawSourceItemModel(generalSettings, modelData, parent), groups, parent) -{ -} - -void RawSourceFilterItemModel::setFilterGroups(unsigned groups) -{ - if (filterGroups != groups) { - filterGroups = groups; - invalidateFilter(); - } -} - -bool RawSourceFilterItemModel::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; - - if (filterGroups && filterGroups != RawSource::AllSourceGroups) { - bool ok; - const unsigned group = sourceModel()->data(srcIdx, RawSourceItemModel::ItemGroupRole).toUInt(&ok); - return (ok && (filterGroups & group)); - } - return true; -} - -void RawSourceFilterItemModel::update() const -{ - RawSourceItemModel * model = qobject_cast(sourceModel()); - if (model) - model->update(); -} diff --git a/companion/src/datamodels/rawsourceitemmodel.h b/companion/src/datamodels/rawsourceitemmodel.h deleted file mode 100644 index ac4337d31..000000000 --- a/companion/src/datamodels/rawsourceitemmodel.h +++ /dev/null @@ -1,77 +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. - */ - -#ifndef RAWSOURCEITEMMODEL_H -#define RAWSOURCEITEMMODEL_H - -#include "rawsource.h" - -#include -#include - -class GeneralSettings; -class ModelData; - -class RawSourceItemModel: public QStandardItemModel -{ - Q_OBJECT - public: - enum DataRoles { ItemIdRole = Qt::UserRole, ItemTypeRole, ItemGroupRole, IsAvailableRole }; - Q_ENUM(DataRoles) - - RawSourceItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData, QObject * parent = nullptr); - - public slots: - void update() const; - - protected: - void setDynamicItemData(QStandardItem * item, const RawSource & src) const; - void addItems(const RawSourceType & type, const unsigned group, const int count, const int start = 0); - - const GeneralSettings * generalSettings; - const ModelData * modelData; -}; - - -class RawSourceFilterItemModel: public QSortFilterProxyModel -{ - Q_OBJECT - public: - explicit RawSourceFilterItemModel(QAbstractItemModel * sourceModel, unsigned groups, QObject * parent = nullptr); - explicit RawSourceFilterItemModel(QAbstractItemModel * sourceModel, QObject * parent = nullptr) : - RawSourceFilterItemModel(sourceModel, RawSource::AllSourceGroups, parent) - {} - - explicit RawSourceFilterItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData, unsigned groups, QObject * parent = nullptr); - explicit RawSourceFilterItemModel(const GeneralSettings * const generalSettings, const ModelData * const modelData, QObject * parent = nullptr) : - RawSourceFilterItemModel(generalSettings, modelData, RawSource::AllSourceGroups, parent) - {} - - public slots: - void setFilterGroups(unsigned groups); - void update() const; - - protected: - bool filterAcceptsRow(int sourceRow, const QModelIndex & sourceParent) const override; - - unsigned int filterGroups; -}; - -#endif // RAWSOURCEITEMMODEL_H diff --git a/companion/src/firmwares/rawswitch.h b/companion/src/firmwares/rawswitch.h index f3f4a9aad..598e25f01 100644 --- a/companion/src/firmwares/rawswitch.h +++ b/companion/src/firmwares/rawswitch.h @@ -54,7 +54,6 @@ class RawSwitch { public: enum SwitchContext { - NoSwitchContext = 0, LogicalSwitchesContext = 0x01, SpecialFunctionsContext = 0x02, GlobalFunctionsContext = 0x04, diff --git a/companion/src/modeledit/CMakeLists.txt b/companion/src/modeledit/CMakeLists.txt index 1ecdc2c48..300fb27cc 100644 --- a/companion/src/modeledit/CMakeLists.txt +++ b/companion/src/modeledit/CMakeLists.txt @@ -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) diff --git a/companion/src/modeledit/customfunctions.cpp b/companion/src/modeledit/customfunctions.cpp index e5510c656..809985293 100644 --- a/companion/src/modeledit/customfunctions.cpp +++ b/companion/src/modeledit/customfunctions.cpp @@ -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(); diff --git a/companion/src/modeledit/expodialog.cpp b/companion/src/modeledit/expodialog.cpp index 1b087d28e..b82da1028 100644 --- a/companion/src/modeledit/expodialog.cpp +++ b/companion/src/modeledit/expodialog.cpp @@ -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, diff --git a/companion/src/modeledit/flightmodes.cpp b/companion/src/modeledit/flightmodes.cpp index c2ecdab42..ba185a1a5 100644 --- a/companion/src/modeledit/flightmodes.cpp +++ b/companion/src/modeledit/flightmodes.cpp @@ -20,7 +20,7 @@ #include "flightmodes.h" #include "ui_flightmode.h" -#include "switchitemmodel.h" +#include "rawitemfilteredmodel.h" #include "helpers.h" #include "customdebug.h" diff --git a/companion/src/modeledit/heli.cpp b/companion/src/modeledit/heli.cpp index fb70d081a..c0a4e39d8 100644 --- a/companion/src/modeledit/heli.cpp +++ b/companion/src/modeledit/heli.cpp @@ -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), diff --git a/companion/src/modeledit/logicalswitches.cpp b/companion/src/modeledit/logicalswitches.cpp index 84594466c..c9754fbd0 100644 --- a/companion/src/modeledit/logicalswitches.cpp +++ b/companion/src/modeledit/logicalswitches.cpp @@ -19,8 +19,7 @@ */ #include "logicalswitches.h" -#include "switchitemmodel.h" -#include "rawsourceitemmodel.h" +#include "rawitemfilteredmodel.h" #include "helpers.h" #include @@ -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; diff --git a/companion/src/modeledit/mixerdialog.cpp b/companion/src/modeledit/mixerdialog.cpp index 7ff917a43..1b17e9471 100644 --- a/companion/src/modeledit/mixerdialog.cpp +++ b/companion/src/modeledit/mixerdialog.cpp @@ -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) : diff --git a/companion/src/modeledit/setup.cpp b/companion/src/modeledit/setup.cpp index 4355afc18..9f9665662 100644 --- a/companion/src/modeledit/setup.cpp +++ b/companion/src/modeledit/setup.cpp @@ -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" diff --git a/companion/src/modeledit/switchitemmodel.cpp b/companion/src/modeledit/switchitemmodel.cpp deleted file mode 100644 index 183cc20cf..000000000 --- a/companion/src/modeledit/switchitemmodel.cpp +++ /dev/null @@ -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 rawSwitchIndexBaseZeroTypes = QVector() << 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(sourceModel()); - if (model) - model->update(); -} diff --git a/companion/src/modeledit/switchitemmodel.h b/companion/src/modeledit/switchitemmodel.h deleted file mode 100644 index b9c78008c..000000000 --- a/companion/src/modeledit/switchitemmodel.h +++ /dev/null @@ -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 -#include -#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; -}; diff --git a/companion/src/modeledit/telemetry.cpp b/companion/src/modeledit/telemetry.cpp index 39cd025b9..043437f18 100644 --- a/companion/src/modeledit/telemetry.cpp +++ b/companion/src/modeledit/telemetry.cpp @@ -25,7 +25,7 @@ #include "ui_telemetry_sensor.h" #include "helpers.h" #include "appdata.h" -#include "rawsourceitemmodel.h" +#include "rawitemfilteredmodel.h" #include