1
0
Fork 0
mirror of https://github.com/EdgeTX/edgetx.git synced 2025-07-24 00:35:14 +03:00

[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.

This commit is contained in:
Max Paperno 2017-01-28 18:50:26 -05:00
parent 32dc0dd2a0
commit fae095ea18
9 changed files with 54 additions and 52 deletions

View file

@ -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,

View file

@ -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<int> keys, const QString &text = "", const QString &descript = "");

View file

@ -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;
}

View file

@ -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)

View file

@ -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();

View file

@ -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);

View file

@ -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;
}

View file

@ -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);

View file

@ -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);