From 13b156402ff98ec6417dcf315f7148c45da57f36 Mon Sep 17 00:00:00 2001 From: Max Paperno Date: Sun, 9 Sep 2018 08:32:07 -0400 Subject: [PATCH] [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). --- companion/src/mainwindow.cpp | 60 ++++++++++++++---------------- companion/src/mainwindow.h | 2 + companion/src/warnings.h | 71 +++++++++++++++++++++++++++++++----- 3 files changed, 92 insertions(+), 41 deletions(-) diff --git a/companion/src/mainwindow.cpp b/companion/src/mainwindow.cpp index 03a13caa8..6cc2f5ab8 100644 --- a/companion/src/mainwindow.cpp +++ b/companion/src/mainwindow.cpp @@ -53,6 +53,7 @@ #include #include #include +#include #include #include #include @@ -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)) diff --git a/companion/src/mainwindow.h b/companion/src/mainwindow.h index 5a65022dd..542515fb0 100644 --- a/companion/src/mainwindow.h +++ b/companion/src/mainwindow.h @@ -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; diff --git a/companion/src/warnings.h b/companion/src/warnings.h index 118b0cf87..3498ea5c2 100644 --- a/companion/src/warnings.h +++ b/companion/src/warnings.h @@ -21,6 +21,12 @@ #ifndef _WARNINGS_H_ #define _WARNINGS_H_ +#include "appdata.h" + +#include +#include +#include + #define CPN_STR_MSG_WELCOME QCoreApplication::translate("Companion", \ "

Welcome to OpenTX v%1.

" \ "

As the first step, please configure the initial Radio Profile by selecting your Radio Type, Menu Language, and Build Options.

" \ @@ -36,17 +42,64 @@ "

Please visit open-tx.org for release notes and other documentation.

" \ "- The OpenTX Team.") -namespace AppMessages { +#define CPN_STR_MSG_NO_RADIO_TYPE QCoreApplication::translate("Companion", \ + "

The radio type in the selected profile does not exist. Using the default type instead.

Please update your profile settings!

") + +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_