Initial version.
[opentx.git] / companion / src / simulation / simulatorstartupdialog.cpp
blob07766b7dad138d978eb92681acd2f4bf766b442f
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "simulatorstartupdialog.h"
22 #include "ui_simulatorstartupdialog.h"
24 #include "appdata.h"
25 #include "constants.h"
26 #include "simulatorinterface.h"
28 #include <QFileDialog>
30 using namespace Simulator;
32 extern AppData g;
34 SimulatorStartupDialog::SimulatorStartupDialog(SimulatorOptions * options, int * profId, QWidget *parent) :
35 QDialog(parent),
36 ui(new Ui::SimulatorStartupDialog),
37 m_options(options),
38 m_profileId(profId)
40 ui->setupUi(this);
42 QMapIterator<int, QString> pi(g.getActiveProfiles());
43 while (pi.hasNext()) {
44 pi.next();
45 ui->radioProfile->addItem(pi.value(), pi.key());
48 ui->radioType->addItems(SimulatorLoader::getAvailableSimulators());
50 ui->optGrp_dataSource->setId(ui->optFile, SimulatorOptions::START_WITH_FILE);
51 ui->optGrp_dataSource->setId(ui->optFolder, SimulatorOptions::START_WITH_FOLDER);
52 ui->optGrp_dataSource->setId(ui->optSdPath, SimulatorOptions::START_WITH_SDPATH);
54 SimulatorIcon icon("folder_open");
55 ui->btnSelectDataFile->setIcon(icon);
56 ui->btnSelectDataFolder->setIcon(icon);
57 ui->btnSelectSdPath->setIcon(icon);
59 loadRadioProfile(*m_profileId);
61 QObject::connect(ui->radioProfile, SIGNAL(currentIndexChanged(int)), this, SLOT(onRadioProfileChanged(int)));
62 QObject::connect(ui->radioType, SIGNAL(currentIndexChanged(int)), this, SLOT(onRadioTypeChanged(int)));
63 QObject::connect(ui->btnSelectDataFile, &QToolButton::clicked, this, &SimulatorStartupDialog::onDataFileSelect);
64 QObject::connect(ui->btnSelectDataFolder, &QToolButton::clicked, this, &SimulatorStartupDialog::onDataFolderSelect);
65 QObject::connect(ui->btnSelectSdPath, &QToolButton::clicked, this, &SimulatorStartupDialog::onSdPathSelect);
69 SimulatorStartupDialog::~SimulatorStartupDialog()
71 delete ui;
74 void SimulatorStartupDialog::changeEvent(QEvent *e)
76 QDialog::changeEvent(e);
77 switch (e->type()) {
78 case QEvent::LanguageChange:
79 ui->retranslateUi(this);
80 break;
81 default:
82 break;
86 // FIXME : need a better way to check for this
87 bool SimulatorStartupDialog::usesCategorizedStorage(const QString & name)
89 return name.contains(QRegExp("(x12|x10|horus)", Qt::CaseInsensitive));
92 bool SimulatorStartupDialog::usesCategorizedStorage()
94 return usesCategorizedStorage(ui->radioType->currentText());
97 QString SimulatorStartupDialog::findRadioId(const QString & str)
99 QString radioId(str);
100 int pos = str.indexOf("-");
101 if (pos > 0) {
102 pos = str.indexOf("-", pos + 1);
103 if (pos > 0) {
104 radioId = str.mid(0, pos);
107 return radioId;
110 // TODO : this could be smarter and actually look for a matching file in the folder
111 QString SimulatorStartupDialog::radioEepromFileName(const QString & firmwareId, QString folder)
113 QString eepromFileName = "", ext = "bin";
115 if (folder.isEmpty())
116 folder = g.eepromDir();
118 QString radioId = findRadioId(firmwareId);
119 int pos = radioId.indexOf("-");
120 if (pos > 0)
121 radioId = radioId.mid(pos+1);
122 if (usesCategorizedStorage(radioId))
123 ext = "otx";
125 eepromFileName = QString("eeprom-%1.%2").arg(radioId, ext);
126 eepromFileName = QDir(folder).filePath(eepromFileName.toLatin1());
127 // qDebug() << "radioId" << radioId << "eepromFileName" << eepromFileName;
129 return eepromFileName;
132 void SimulatorStartupDialog::updateContainerTypes(void)
134 static int oldstate = -1;
135 int state = usesCategorizedStorage();
137 if (state == oldstate)
138 return;
140 oldstate = state;
142 ui->wdgt_dataSource->setVisible(state);
143 ui->layout_options->labelForField(ui->wdgt_dataSource)->setVisible(state);
144 ui->wdgt_dataFolder->setVisible(state);
145 ui->layout_options->labelForField(ui->wdgt_dataFolder)->setVisible(state);
147 if (!state || ui->optGrp_dataSource->checkedId() < 0)
148 ui->optFile->setChecked(true);
152 void SimulatorStartupDialog::loadRadioProfile(int id)
154 QString tmpstr, tmpstr2;
155 int i;
157 if (id < 0 || !g.getActiveProfiles().contains(id))
158 return;
160 i = ui->radioProfile->findData(id);
161 if (i > -1 && ui->radioProfile->currentIndex() != i)
162 ui->radioProfile->setCurrentIndex(i);
164 *m_options = g.profile[id].simulatorOptions();
166 tmpstr = m_options->firmwareId;
167 if (tmpstr.isEmpty() && !g.profile[id].fwType().isEmpty())
168 tmpstr = g.profile[id].fwType();
169 else if (!(tmpstr2 = SimulatorLoader::findSimulatorByFirmwareName(m_options->firmwareId)).isEmpty())
170 tmpstr = tmpstr2;
171 i = ui->radioType->findText(findRadioId(tmpstr), Qt::MatchContains);
172 if (i > -1)
173 ui->radioType->setCurrentIndex(i);
175 tmpstr = m_options->dataFile;
176 if (tmpstr.isEmpty())
177 tmpstr = radioEepromFileName(ui->radioType->currentText());
178 ui->dataFile->setText(tmpstr);
180 tmpstr = m_options->dataFolder;
181 if (tmpstr.isEmpty())
182 tmpstr = g.eepromDir();
183 ui->dataFolder->setText(tmpstr);
185 tmpstr = m_options->sdPath;
186 if (tmpstr.isEmpty())
187 tmpstr = g.profile[id].sdPath();
188 ui->sdPath->setText(tmpstr);
190 foreach (QAbstractButton * btn, ui->optGrp_dataSource->buttons()) {
191 if (ui->optGrp_dataSource->id(btn) == m_options->startupDataType) {
192 btn->setChecked(true);
193 break;
197 updateContainerTypes();
200 void SimulatorStartupDialog::accept()
202 *m_profileId = ui->radioProfile->currentData().toInt();
203 m_options->firmwareId = ui->radioType->currentText();
204 m_options->dataFile = ui->dataFile->text();
205 m_options->dataFolder = ui->dataFolder->text();
206 m_options->sdPath = ui->sdPath->text();
207 m_options->startupDataType = ui->optGrp_dataSource->checkedId();
209 QDialog::accept();
212 void SimulatorStartupDialog::onRadioProfileChanged(int index)
214 if (index < 0)
215 return;
217 loadRadioProfile(ui->radioProfile->currentData().toInt());
220 void SimulatorStartupDialog::onRadioTypeChanged(int index)
222 if (index < 0)
223 return;
224 ui->dataFile->setText(radioEepromFileName(ui->radioType->currentText()));
225 updateContainerTypes();
228 void SimulatorStartupDialog::onDataFileSelect(bool)
230 QString filter = EEPROM_FILES_FILTER % tr("All files (*.*)");
231 QString file = QFileDialog::getSaveFileName(this, tr("Select a data file"), ui->dataFile->text(),
232 filter, NULL, QFileDialog::DontConfirmOverwrite);
233 if (!file.isEmpty()) {
234 ui->dataFile->setText(file);
235 ui->optFile->setChecked(true);
239 void SimulatorStartupDialog::onDataFolderSelect(bool)
241 QString folder = QFileDialog::getExistingDirectory(this, tr("Select Data Directory"),
242 ui->dataFolder->text(), QFileDialog::DontUseNativeDialog);
243 if (!folder.isEmpty()) {
244 ui->dataFolder->setText(folder);
245 if (usesCategorizedStorage())
246 ui->optFolder->setChecked(true);
250 void SimulatorStartupDialog::onSdPathSelect(bool)
252 QString folder = QFileDialog::getExistingDirectory(this, tr("Select SD Card Image Folder"),
253 ui->sdPath->text(), QFileDialog::DontUseNativeDialog);
254 if (!folder.isEmpty()) {
255 ui->sdPath->setText(folder);
256 if (usesCategorizedStorage())
257 ui->optSdPath->setChecked(true);