From fae095ea18d3c2fe18f755fa40b8d08abcb53fa1 Mon Sep 17 00:00:00 2001 From: Max Paperno Date: Sat, 28 Jan 2017 18:50:26 -0500 Subject: [PATCH] [Simulator] Fix startup when using a new file for radio data (file is now properly created, errors from Storage are ignored); Do not start simulator at all in case of data errors; Fix startup option dialog when finding radio type from profile for first time; Fix RadioOptions struct version control; Cleanup unused WinConsole code; Fix a warning and a missing const reference; Move ~SimulatedUIWidget() to public. --- companion/src/helpers.cpp | 19 ++-------- companion/src/simulation/simulateduiwidget.h | 3 +- companion/src/simulation/simulator.h | 4 +- companion/src/simulation/simulatordialog.cpp | 38 ++++++++++++------- companion/src/simulation/simulatordialog.h | 6 +-- .../src/simulation/simulatorstartupdialog.cpp | 17 +++++---- companion/src/simulation/widgets/lcdwidget.h | 4 +- .../simulation/widgets/radioswitchwidget.h | 2 +- companion/src/simulator.cpp | 13 ++++--- 9 files changed, 54 insertions(+), 52 deletions(-) diff --git a/companion/src/helpers.cpp b/companion/src/helpers.cpp index 439098b7ca..ca616a2212 100644 --- a/companion/src/helpers.cpp +++ b/companion/src/helpers.cpp @@ -815,17 +815,6 @@ void startSimulation(QWidget * parent, RadioData & radioData, int modelIdx) { SimulatorInterface * simulator = getCurrentSimulator(); if (simulator) { -#if defined(WIN32) && defined(WIN_USE_CONSOLE_STDIO) - bool freeConsoleOnClose = false; - if (!GetConsoleWindow()) { - AllocConsole(); - SetConsoleTitle("Companion Console"); - freeConsoleOnClose = true; - freopen("conin$", "r", stdin); - freopen("conout$", "w", stdout); - freopen("conout$", "w", stderr); - } -#endif RadioData * simuData = new RadioData(radioData); unsigned int flags = 0; @@ -837,19 +826,17 @@ void startSimulation(QWidget * parent, RadioData & radioData, int modelIdx) SimulatorDialog * dialog = new SimulatorDialog(parent, simulator, flags); if (IS_HORUS(getCurrentBoard()) && !dialog->useTempDataPath(true)) { QMessageBox::critical(NULL, QObject::tr("Data Load Error"), QObject::tr("Error: Could not create temporary directory in '%1'").arg(QDir::tempPath())); + delete dialog; + delete simuData; return; } dialog->setRadioData(simuData); qDebug() << __FILE__ << __LINE__ << "Starting" << getCurrentFirmware()->getName() << "simulation with SD path" << dialog->getSdPath() << "and models/settings path" << dialog->getDataPath(); dialog->start(); dialog->exec(); - dialog->deleteLater(); + delete dialog; // TODO Horus tmp directory is deleted on simulator close OR we could use it to get back data from the simulation delete simuData; // TODO same with simuData, could read it back and update the file data -#if defined(WIN32) && defined(WIN_USE_CONSOLE_STDIO) - if (freeConsoleOnClose) - FreeConsole(); -#endif } else { QMessageBox::warning(NULL, diff --git a/companion/src/simulation/simulateduiwidget.h b/companion/src/simulation/simulateduiwidget.h index 0415acbd1b..e60d8674bf 100644 --- a/companion/src/simulation/simulateduiwidget.h +++ b/companion/src/simulation/simulateduiwidget.h @@ -46,10 +46,11 @@ class SimulatedUIWidget : public QWidget protected: explicit SimulatedUIWidget(SimulatorInterface * simulator, SimulatorDialog * simuDialog = NULL, QWidget * parent = NULL); - ~SimulatedUIWidget(); public: + ~SimulatedUIWidget(); + RadioUiAction * addRadioUiAction(RadioUiAction * act); RadioUiAction * addRadioUiAction(int index = -1, int key = 0, const QString &text = "", const QString &descript = ""); RadioUiAction * addRadioUiAction(int index, QList keys, const QString &text = "", const QString &descript = ""); diff --git a/companion/src/simulation/simulator.h b/companion/src/simulation/simulator.h index 62abba57a8..36431f667f 100644 --- a/companion/src/simulation/simulator.h +++ b/companion/src/simulation/simulator.h @@ -45,7 +45,7 @@ struct SimulatorOptions }; private: - quint16 _version = SIMULATOR_OPTIONS_VERSION; // structure definition version + quint16 _version; // structure definition version public: // v1 fields @@ -60,7 +60,7 @@ struct SimulatorOptions friend QDataStream & operator << (QDataStream &out, const SimulatorOptions & o) { - out << o._version << o.startupDataType << o.firmwareId << o.dataFile << o.dataFolder + out << quint16(SIMULATOR_OPTIONS_VERSION) << o.startupDataType << o.firmwareId << o.dataFile << o.dataFolder << o.sdPath << o.windowGeometry << o.controlsState << o.lcdColor; return out; } diff --git a/companion/src/simulation/simulatordialog.cpp b/companion/src/simulation/simulatordialog.cpp index 14379dae86..4ba988dd69 100644 --- a/companion/src/simulation/simulatordialog.cpp +++ b/companion/src/simulation/simulatordialog.cpp @@ -165,7 +165,7 @@ void SimulatorDialog::setRadioSettings(const GeneralSettings settings) * with the same data when start() is called. * If you already have a valid RadioData structure, call setRadioData() instead. */ -void SimulatorDialog::setStartupData(const QByteArray & dataSource, bool fromFile) +bool SimulatorDialog::setStartupData(const QByteArray & dataSource, bool fromFile) { RadioData simuData; quint16 ret = 1; @@ -195,13 +195,18 @@ void SimulatorDialog::setStartupData(const QByteArray & dataSource, bool fromFil } else if (QString(dataSource).endsWith(".otx", Qt::CaseInsensitive)) { // FIXME : Right now there's no way to read data back into the .otx file after simulation finishes. - setRadioData(&simuData); - return; + return setRadioData(&simuData); } else { startupData = dataSource; // save the file name for start() } } + // again there's no way to tell what the error from Storage actually was, so if the file doesn't exist we'll create a new one + else if (!QFile(QString(dataSource)).exists()) { + startupData = dataSource; + startupFromFile = true; + return true; + } else { error = store.error(); } @@ -221,43 +226,50 @@ void SimulatorDialog::setStartupData(const QByteArray & dataSource, bool fromFil if (error.isEmpty()) error = tr("Could not load data, possibly wrong format."); QMessageBox::critical(this, tr("Data Load Error"), error); - return; + return false; } radioSettings = simuData.generalSettings; startupFromFile = fromFile; + + return true; } -void SimulatorDialog::setRadioData(RadioData * radioData) +bool SimulatorDialog::setRadioData(RadioData * radioData) { + bool ret = false; if (radioDataPath.isEmpty()) { QByteArray eeprom(getEEpromSize(m_board), 0); if (firmware->getEEpromInterface()->save((uint8_t *)eeprom.data(), *radioData, 0, firmware->getCapability(SimulatorVariant)) > 0) - setStartupData(eeprom, false); + ret = setStartupData(eeprom, false); } else { - saveRadioData(radioData, radioDataPath); - radioSettings = radioData->generalSettings; + if ((ret = saveRadioData(radioData, radioDataPath))) + radioSettings = radioData->generalSettings; } + return ret; } -void SimulatorDialog::setOptions(SimulatorOptions & options, bool withSave) +bool SimulatorDialog::setOptions(SimulatorOptions & options, bool withSave) { + bool ret = false; setSdPath(options.sdPath); if (options.startupDataType == SimulatorOptions::START_WITH_FOLDER && !options.dataFolder.isEmpty()) { setDataPath(options.dataFolder); - setStartupData(); + ret = setStartupData(); } else if (options.startupDataType == SimulatorOptions::START_WITH_SDPATH && !options.sdPath.isEmpty()) { setDataPath(options.sdPath); - setStartupData(); + ret = setStartupData(); } else if (options.startupDataType == SimulatorOptions::START_WITH_FILE && !options.dataFile.isEmpty()) { - setStartupData(options.dataFile.toLocal8Bit(), true); + ret = setStartupData(options.dataFile.toLocal8Bit(), true); } - if (withSave) + if (ret && withSave) g.profile[radioProfileId].simulatorOptions(options); + + return ret; } bool SimulatorDialog::saveRadioData(RadioData * radioData, const QString & path, QString * error) diff --git a/companion/src/simulation/simulatordialog.h b/companion/src/simulation/simulatordialog.h index 8dcd456a3e..9a2bca1a9e 100644 --- a/companion/src/simulation/simulatordialog.h +++ b/companion/src/simulation/simulatordialog.h @@ -80,9 +80,9 @@ class SimulatorDialog : public QDialog void setDataPath(const QString & dataPath); void setPaths(const QString & sdPath, const QString & dataPath); void setRadioSettings(const GeneralSettings settings); - void setStartupData(const QByteArray & dataSource = NULL, bool fromFile = false); - void setRadioData(RadioData * radioData); - void setOptions(SimulatorOptions & options, bool withSave = true); + bool setStartupData(const QByteArray & dataSource = NULL, bool fromFile = false); + bool setRadioData(RadioData * radioData); + bool setOptions(SimulatorOptions & options, bool withSave = true); bool saveRadioData(RadioData * radioData, const QString & path = "", QString * error = NULL); bool useTempDataPath(bool deleteOnClose = true, bool saveOnClose = false); bool saveTempData(); diff --git a/companion/src/simulation/simulatorstartupdialog.cpp b/companion/src/simulation/simulatorstartupdialog.cpp index 98ad670c57..d1eca2e38e 100644 --- a/companion/src/simulation/simulatorstartupdialog.cpp +++ b/companion/src/simulation/simulatorstartupdialog.cpp @@ -56,13 +56,14 @@ SimulatorStartupDialog::SimulatorStartupDialog(SimulatorOptions * options, int * ui->btnSelectDataFolder->setIcon(icon); ui->btnSelectSdPath->setIcon(icon); + loadRadioProfile(*m_profileId); + QObject::connect(ui->radioProfile, SIGNAL(currentIndexChanged(int)), this, SLOT(onRadioProfileChanged(int))); QObject::connect(ui->radioType, SIGNAL(currentIndexChanged(int)), this, SLOT(onRadioTypeChanged(int))); QObject::connect(ui->btnSelectDataFile, &QToolButton::clicked, this, &SimulatorStartupDialog::onDataFileSelect); QObject::connect(ui->btnSelectDataFolder, &QToolButton::clicked, this, &SimulatorStartupDialog::onDataFolderSelect); QObject::connect(ui->btnSelectSdPath, &QToolButton::clicked, this, &SimulatorStartupDialog::onSdPathSelect); - loadRadioProfile(*m_profileId); } SimulatorStartupDialog::~SimulatorStartupDialog() @@ -98,10 +99,9 @@ QString SimulatorStartupDialog::findRadioId(const QString & str) QString radioId(str); int pos = str.indexOf("-"); if (pos > 0) { - radioId = str.mid(pos+1); - pos = radioId.lastIndexOf("-"); + pos = str.indexOf("-", pos + 1); if (pos > 0) { - radioId = radioId.mid(0, pos); + radioId = str.mid(0, pos); } } return radioId; @@ -116,6 +116,9 @@ QString SimulatorStartupDialog::radioEepromFileName(const QString & firmwareId, folder = g.eepromDir(); QString radioId = findRadioId(firmwareId); + int pos = radioId.indexOf("-"); + if (pos > 0) + radioId = radioId.mid(pos+1); if (usesCategorizedStorage(radioId)) ext = "otx"; @@ -155,10 +158,8 @@ void SimulatorStartupDialog::loadRadioProfile(int id) return; i = ui->radioProfile->findData(id); - if (i > -1 && ui->radioProfile->currentIndex() != i) { + if (i > -1 && ui->radioProfile->currentIndex() != i) ui->radioProfile->setCurrentIndex(i); - return; - } *m_options = g.profile[id].simulatorOptions(); @@ -167,7 +168,7 @@ void SimulatorStartupDialog::loadRadioProfile(int id) tmpstr = g.profile[id].fwType(); else if (getSimulatorFactory(tmpstr)) tmpstr = getSimulatorFactory(tmpstr)->name(); - i = ui->radioType->findText(findRadioId(tmpstr), Qt::MatchFixedString | Qt::MatchContains); + i = ui->radioType->findText(findRadioId(tmpstr), Qt::MatchContains); if (i > -1) ui->radioType->setCurrentIndex(i); diff --git a/companion/src/simulation/widgets/lcdwidget.h b/companion/src/simulation/widgets/lcdwidget.h index 863edf7d2e..50d396d61e 100644 --- a/companion/src/simulation/widgets/lcdwidget.h +++ b/companion/src/simulation/widgets/lcdwidget.h @@ -65,12 +65,12 @@ class LcdWidget : public QWidget memset(previousBuf, 0, lcdSize); } - void setBgDefaultColor(QColor color) + void setBgDefaultColor(const QColor & color) { bgDefaultColor = color; } - void setBackgroundColor(QColor color) + void setBackgroundColor(const QColor & color) { bgColor = color; } diff --git a/companion/src/simulation/widgets/radioswitchwidget.h b/companion/src/simulation/widgets/radioswitchwidget.h index c1f3310fc3..4d365f0208 100644 --- a/companion/src/simulation/widgets/radioswitchwidget.h +++ b/companion/src/simulation/widgets/radioswitchwidget.h @@ -104,7 +104,7 @@ class RadioSwitchWidget : public RadioWidget void setToggleLocked(bool lock) { - if (m_flags != lock) { + if (m_flags != (quint16)lock) { m_flags = lock; setValue((int)lock); emit flagsChanged(m_flags); diff --git a/companion/src/simulator.cpp b/companion/src/simulator.cpp index 44b73a05c8..afe292aa4c 100644 --- a/companion/src/simulator.cpp +++ b/companion/src/simulator.cpp @@ -273,15 +273,16 @@ int main(int argc, char *argv[]) g.id(profileId); g.simuLastProfId(profileId); + int result = 1; SimulatorDialog * dialog = new SimulatorDialog(NULL, simulator, SIMULATOR_FLAGS_STANDALONE); dialog->setRadioProfileId(profileId); - dialog->setOptions(simOptions, true); - dialog->start(); - dialog->show(); - int result = app.exec(); - + if ((result = (int)dialog->setOptions(simOptions, true))) { + dialog->start(); + dialog->show(); + result = app.exec(); + } g.id(oldProfId); - dialog->deleteLater(); + delete dialog; delete simulator; return finish(result);