[Companion] Verify that radio type in chosen profile actually exists, and add warning to the contrary (at startup and also on profile switch); Add/move some functionality to AppMessages and make it classy (todo: rename file).

This commit is contained in:
Max Paperno
2018-09-19 06:47:35 -04:00
parent fdcee1ca9f
commit 13b156402f
3 changed files with 92 additions and 41 deletions
+28 -32
View File
@@ -53,6 +53,7 @@
#include <QtGui>
#include <QFileInfo>
#include <QDesktopServices>
#include <QMessageBox>
#include <QNetworkAccessManager>
#include <QNetworkProxyFactory>
#include <QNetworkReply>
@@ -129,7 +130,10 @@ MainWindow::MainWindow():
else {
if (!g.previousVersion().isEmpty())
g.warningId(g.warningId() | AppMessages::MSG_UPGRADED);
QTimer::singleShot(updateDelay, this, SLOT(doAutoUpdates()));
if (checkProfileRadioExists(g.sessionId()))
QTimer::singleShot(updateDelay, this, SLOT(doAutoUpdates()));
else
g.warningId(g.warningId() | AppMessages::MSG_NO_RADIO_TYPE);
}
QTimer::singleShot(updateDelay, this, SLOT(displayWarnings()));
@@ -195,39 +199,21 @@ void MainWindow::initWindowOptions()
void MainWindow::displayWarnings()
{
using namespace AppMessages;
static uint shownMsgs = 0;
int showMsgs = g.warningId();
int msgId;
QString infoTxt;
if ((showMsgs & MSG_WELCOME) && !(shownMsgs & MSG_WELCOME)) {
infoTxt = CPN_STR_MSG_WELCOME.arg(VERSION);
msgId = MSG_WELCOME;
static int shownMsgs = 0;
const int showMsgs = g.warningId();
int msgId = 0;
for (int i = 1; i < AppMessages::MSG_ENUM_END; i <<= 1) {
if ((showMsgs & i) && !(shownMsgs & i)) {
msgId = i;
break;
}
}
else if ((showMsgs & MSG_UPGRADED) && !(shownMsgs & MSG_UPGRADED)) {
infoTxt = CPN_STR_MSG_UPGRADED.arg(VERSION);
msgId = MSG_UPGRADED;
}
else {
if (!msgId)
return;
}
QMessageBox msgBox(this);
msgBox.setWindowTitle(CPN_STR_APP_NAME);
msgBox.setIcon(QMessageBox::Information);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setInformativeText(infoTxt);
QCheckBox * cb = new QCheckBox(tr("Show this message again at next startup?"), &msgBox);
msgBox.setCheckBox(cb);
msgBox.exec();
AppMessages::displayMessage(msgId, this);
shownMsgs |= msgId;
if (!cb->isChecked())
g.warningId(showMsgs & ~msgId);
displayWarnings(); // in case more warnings need showing
if (shownMsgs != showMsgs)
displayWarnings(); // in case more warnings need showing
}
void MainWindow::doAutoUpdates()
@@ -773,12 +759,21 @@ void MainWindow::openRecentFile()
}
}
bool MainWindow::checkProfileRadioExists(int profId)
{
const QString profType = g.getProfile(profId).fwType();
return (Firmware::getFirmwareForId(profType)->getFirmwareBase()->getId() == profType.section('-', 0, 1));
}
bool MainWindow::loadProfileId(const unsigned pid) // TODO Load all variables - Also HW!
{
if (pid >= MAX_PROFILES)
return false;
Firmware * newFw = Firmware::getFirmwareForId(g.profile[pid].fwType());
Firmware * newFw = Firmware::getFirmwareForId(g.getProfile(pid).fwType());
// warn if the selected profile doesn't exist
if (!checkProfileRadioExists(pid))
AppMessages::displayMessage(AppMessages::MSG_NO_RADIO_TYPE, this);
// warn if we're switching between incompatible board types and any files have been modified
if (!Boards::isBoardCompatible(Firmware::getCurrentVariant()->getBoard(), newFw->getBoard()) && anyChildrenDirty()) {
if (QMessageBox::question(this, CPN_STR_APP_NAME,
@@ -1628,6 +1623,7 @@ int MainWindow::newProfile(bool loadProfile)
g.profile[i].init();
g.profile[i].name(tr("New Radio"));
g.profile[i].fwType(Firmware::getDefaultVariant()->getId());
if (loadProfile) {
if (loadProfileId(i))
+2
View File
@@ -161,6 +161,8 @@ class MainWindow : public QMainWindow
bool readEepromFromRadio(const QString & filename);
bool readFirmwareFromRadio(const QString & filename);
bool checkProfileRadioExists(int profId);
QMdiArea *mdiArea;
QSignalMapper *windowMapper;
+62 -9
View File
@@ -21,6 +21,12 @@
#ifndef _WARNINGS_H_
#define _WARNINGS_H_
#include "appdata.h"
#include <QtCore>
#include <QCheckBox>
#include <QMessageBox>
#define CPN_STR_MSG_WELCOME QCoreApplication::translate("Companion", \
"<p><b>Welcome to OpenTX v%1.</b></p>" \
"<p>As the first step, please configure the initial Radio Profile by selecting your Radio Type, Menu Language, and Build Options.</p>" \
@@ -36,17 +42,64 @@
"<p>Please visit <a href='http://www.open-tx.org'>open-tx.org</a> for release notes and other documentation.</p>" \
"- The OpenTX Team.")
namespace AppMessages {
#define CPN_STR_MSG_NO_RADIO_TYPE QCoreApplication::translate("Companion", \
"<p>The radio type in the selected profile does not exist. Using the default type instead.</p> <p><b>Please update your profile settings!</b></p>")
class AppMessages
{
Q_DECLARE_TR_FUNCTIONS("AppMessages")
public:
// These are used for saving "[don't] show this message again" user preferences.
enum MessageCodes {
MSG_NONE = 0,
MSG_WELCOME = 0x01,
MSG_UPGRADED = 0x02,
MSG_RESERVED = 0x04, // reserved due to legacy code
MSG_NO_RADIO_TYPE = 0x08,
MSG_ENUM_END
};
static void displayMessage(int id, QWidget * parent = nullptr)
{
QString infoTxt;
QMessageBox::Icon icon = QMessageBox::Information;
bool transient = false;
switch (id) {
case MSG_WELCOME:
infoTxt = CPN_STR_MSG_WELCOME.arg(VERSION);
break;
case MSG_UPGRADED:
infoTxt = CPN_STR_MSG_UPGRADED.arg(VERSION);
break;
case MSG_NO_RADIO_TYPE:
infoTxt = CPN_STR_MSG_NO_RADIO_TYPE;
icon = QMessageBox::Warning;
transient = true;
break;
default:
return;
}
QMessageBox msgBox(parent);
msgBox.setWindowTitle(CPN_STR_APP_NAME);
msgBox.setIcon(icon);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setText(infoTxt);
if (!transient)
msgBox.setCheckBox(new QCheckBox(tr("Show this message again at next startup?")));
msgBox.exec();
if (transient || (msgBox.checkBox() && !msgBox.checkBox()->isChecked()))
g.warningId(g.warningId() & ~id);
}
// These are used for saving "[don't] show this message again" user preferences.
enum MessageCodes {
MSG_NONE = 0,
MSG_WELCOME = 0x01,
MSG_UPGRADED = 0x02,
MSG_RESERVED = 0x04, // reserved due to legacy code
};
} // namespace AppWarnings
#endif // _WARNINGS_H_