diff --git a/companion/src/constants.h b/companion/src/constants.h index c736e5360..a31c33226 100644 --- a/companion/src/constants.h +++ b/companion/src/constants.h @@ -34,7 +34,8 @@ enum BoardEnum { BOARD_TARANIS_X9DP, BOARD_TARANIS_X9E, BOARD_FLAMENCO, - BOARD_HORUS + BOARD_HORUS, + BOARD_UNKNOWN = -1 }; #define CPN_MAX_MODELS 60 diff --git a/companion/src/firmwares/opentx/opentxinterface.cpp b/companion/src/firmwares/opentx/opentxinterface.cpp index b34a20ad0..34a350488 100644 --- a/companion/src/firmwares/opentx/opentxinterface.cpp +++ b/companion/src/firmwares/opentx/opentxinterface.cpp @@ -1573,14 +1573,14 @@ void unregisterOpenTxFirmwares() } template -bool loadFromByteArray(T & dest, const QByteArray & data) +OpenTxEepromInterface * loadFromByteArray(T & dest, const QByteArray & data) { foreach(OpenTxEepromInterface * eepromInterface, opentxEEpromInterfaces) { if (eepromInterface->loadFromByteArray(dest, data)) { - return true; + return eepromInterface; } } - return false; + return NULL; } template @@ -1595,12 +1595,12 @@ bool saveToByteArray(const T & dest, QByteArray & data) return false; } -bool loadModelFromByteArray(ModelData & model, const QByteArray & data) +OpenTxEepromInterface * loadModelFromByteArray(ModelData & model, const QByteArray & data) { return loadFromByteArray(model, data); } -bool loadRadioSettingsFromByteArray(GeneralSettings & settings, const QByteArray & data) +OpenTxEepromInterface * loadRadioSettingsFromByteArray(GeneralSettings & settings, const QByteArray & data) { return loadFromByteArray(settings, data); } diff --git a/companion/src/firmwares/opentx/opentxinterface.h b/companion/src/firmwares/opentx/opentxinterface.h index aeb23a5d4..978a51d1b 100644 --- a/companion/src/firmwares/opentx/opentxinterface.h +++ b/companion/src/firmwares/opentx/opentxinterface.h @@ -150,8 +150,8 @@ void unregisterOpenTxFirmwares(); extern QList opentxEEpromInterfaces; -bool loadModelFromByteArray(ModelData & model, const QByteArray & data); -bool loadRadioSettingsFromByteArray(GeneralSettings & settings, const QByteArray & data); +OpenTxEepromInterface * loadModelFromByteArray(ModelData & model, const QByteArray & data); +OpenTxEepromInterface * loadRadioSettingsFromByteArray(GeneralSettings & settings, const QByteArray & data); bool writeModelToByteArray(const ModelData & model, QByteArray & data); bool writeRadioSettingsToByteArray(const GeneralSettings & settings, QByteArray & data); diff --git a/companion/src/mdichild.cpp b/companion/src/mdichild.cpp index 99cfaa021..0772bbb3e 100644 --- a/companion/src/mdichild.cpp +++ b/companion/src/mdichild.cpp @@ -152,13 +152,28 @@ void MdiChild::showModelsListContextMenu(const QPoint & pos) contextMenu.exec(globalPos); } +void MdiChild::setCurrentFileExtension(const QString & ext) +{ + QFileInfo info(curFile); + curFile = info.path() + "/" + info.baseName() + ext; +} + void MdiChild::onFirmwareChanged() { Firmware * previous = firmware; firmware = getCurrentFirmware(); qDebug() << "onFirmwareChanged" << previous->getName() << "=>" << firmware->getName(); - radioData.convert(previous, firmware); + convertStorage(previous->getBoard(), firmware->getBoard()); +} + +void MdiChild::convertStorage(BoardEnum from, BoardEnum to) +{ + QMessageBox::warning(this, "Companion", tr("Models and settings will be automatically converted"), QMessageBox::Ok); + radioData.convert(from, to); + setCurrentFileExtension(".otx"); initModelsList(); + fileChanged = true; + documentWasModified(); } void MdiChild::initModelsList() @@ -491,6 +506,10 @@ bool MdiChild::loadFile(const QString & filename, bool resetCurrentFile) setCurrentFile(filename); } + if (storage.getBoard() != getCurrentBoard()) { + convertStorage(storage.getBoard(), getCurrentBoard()); + } + return true; } @@ -535,21 +554,19 @@ bool MdiChild::saveAs(bool isNew) bool MdiChild::saveFile(const QString & filename, bool setCurrent) { - BoardEnum board = getCurrentBoard(); - QString path = filename; - if (IS_SKY9X(board)) { - path.replace(".eepe", ".bin"); - } - radioData.fixModelFilenames(); - Storage storage(path); + Storage storage(filename); bool result = storage.write(radioData); - - if (result && setCurrent) { - setCurrentFile(path); + if (!result) { + QMessageBox::warning(this, "Companion", tr("Cannot save file"), QMessageBox::Ok); + return false; } - return result; + if (setCurrent) { + setCurrentFile(filename); + } + + return true; } QString MdiChild::userFriendlyCurrentFile() const diff --git a/companion/src/mdichild.h b/companion/src/mdichild.h index d1834a0f5..48e4f1062 100644 --- a/companion/src/mdichild.h +++ b/companion/src/mdichild.h @@ -59,6 +59,8 @@ class MdiChild : public QWidget void copyAvailable(bool val); protected: + void convertStorage(BoardEnum from, BoardEnum to); + void setCurrentFileExtension(const QString & ext); void closeEvent(QCloseEvent * event); protected slots: diff --git a/companion/src/radiodata.cpp b/companion/src/radiodata.cpp index 3ddbb3872..a0d68878d 100644 --- a/companion/src/radiodata.cpp +++ b/companion/src/radiodata.cpp @@ -23,17 +23,17 @@ // TODO here we will move a lot of functions from eeprominterface.cpp when no merge risk -void ModelData::convert(Firmware * before, Firmware * after) +void ModelData::convert(BoardEnum before, BoardEnum after) { // Here we can add explicit conversions when moving from one board to another } -void GeneralSettings::convert(Firmware * before, Firmware * after) +void GeneralSettings::convert(BoardEnum before, BoardEnum after) { // Here we can add explicit conversions when moving from one board to another } -void RadioData::convert(Firmware * before, Firmware * after) +void RadioData::convert(BoardEnum before, BoardEnum after) { generalSettings.convert(before, after); for (unsigned i=0; i categories; std::vector models; - void convert(Firmware * before, Firmware * after); + void convert(BoardEnum before, BoardEnum after); void setCurrentModel(unsigned int index) { diff --git a/companion/src/storage/bineeprom.cpp b/companion/src/storage/bineeprom.cpp index 7bf9450bb..1c7c0fd58 100644 --- a/companion/src/storage/bineeprom.cpp +++ b/companion/src/storage/bineeprom.cpp @@ -50,7 +50,11 @@ bool BinEepromFormat::write(const RadioData & radioData) { bool result; EEPROMInterface * eepromInterface = getCurrentEEpromInterface(); - uint8_t * eeprom = (uint8_t *)malloc(getEEpromSize(eepromInterface->getBoard())); + int size = getEEpromSize(eepromInterface->getBoard()); + if (size == 0) { + return false; + } + uint8_t * eeprom = (uint8_t *)malloc(size); int eeprom_size = eepromInterface->save(eeprom, radioData, 0, getCurrentFirmware()->getVariantNumber()); if (eeprom_size) { result = writeToFile(eeprom, eeprom_size); @@ -91,6 +95,7 @@ bool BinEepromFormat::extract(RadioData & radioData, const QByteArray & eeprom) if (errors.test(HAS_WARNINGS)) { // TODO ShowEepromWarnings(this, QObject::tr("Warning"), errors.to_ulong()); } + board = eepromInterface->getBoard(); return true; } else { diff --git a/companion/src/storage/categorized.cpp b/companion/src/storage/categorized.cpp index 10e2b8329..4b62db23b 100644 --- a/companion/src/storage/categorized.cpp +++ b/companion/src/storage/categorized.cpp @@ -29,9 +29,12 @@ bool CategorizedStorageFormat::load(RadioData & radioData) return false; } - if (!loadRadioSettingsFromByteArray(radioData.generalSettings, radioSettingsBuffer)) { + OpenTxEepromInterface * loadInterface = loadRadioSettingsFromByteArray(radioData.generalSettings, radioSettingsBuffer); + if (!loadInterface) { return false; } + + board = loadInterface->getBoard(); QByteArray modelsListBuffer; if (!loadFile(modelsListBuffer, "RADIO/models.txt")) { diff --git a/companion/src/storage/storage.cpp b/companion/src/storage/storage.cpp index 2667adbcb..b5e4c3f78 100644 --- a/companion/src/storage/storage.cpp +++ b/companion/src/storage/storage.cpp @@ -75,6 +75,7 @@ bool Storage::load(RadioData & radioData) foreach(StorageFactory * factory, registeredStorageFactories) { StorageFormat * format = factory->instance(filename); if (format->load(radioData)) { + board = format->getBoard(); setWarning(format->warning()); return true; } diff --git a/companion/src/storage/storage.h b/companion/src/storage/storage.h index c4f5500e4..d6f3bc64f 100644 --- a/companion/src/storage/storage.h +++ b/companion/src/storage/storage.h @@ -44,7 +44,8 @@ class StorageFormat public: StorageFormat(const QString & filename, uint8_t version=0): filename(filename), - version(version) + version(version), + board(BOARD_UNKNOWN) { } @@ -61,6 +62,10 @@ class StorageFormat virtual QString name() = 0; + virtual BoardEnum getBoard() { + return board; + } + protected: void setError(const QString & error) { @@ -78,6 +83,7 @@ class StorageFormat uint8_t version; QString _error; QString _warning; + BoardEnum board; }; class StorageFactory