From fdc9873ab8fe282a723c2506a48c11cb31e1599a Mon Sep 17 00:00:00 2001 From: Kjell Kernen Date: Mon, 17 Feb 2014 01:16:24 +0100 Subject: [PATCH] Profile date is now type safe. Semi working, not complete yet. --- companion/src/appdata.h | 416 ++++++++++++++++++------- companion/src/apppreferencesdialog.cpp | 68 ++-- companion/src/apppreferencesdialog.h | 2 +- companion/src/mainwindow.cpp | 166 ++++------ companion/src/mainwindow.h | 8 +- 5 files changed, 396 insertions(+), 264 deletions(-) diff --git a/companion/src/appdata.h b/companion/src/appdata.h index 007f96c42..2d7add453 100644 --- a/companion/src/appdata.h +++ b/companion/src/appdata.h @@ -10,11 +10,296 @@ #define COMPANY "OpenTX Companion" #define PRODUCT "OpenTX" +#define MAX_PROFILES 15 -class AppData +class DataObj { + public: + void store(const QByteArray newArray, QByteArray &array, const char *tag, const QString group1="", const QString group2="" ) + { + QSettings settings(PRODUCT, COMPANY); + if (!group1.isEmpty()) settings.beginGroup(group1); + if (!group2.isEmpty()) settings.beginGroup(group2); + + settings.setValue(tag, newArray); + array = newArray; + + if (!group1.isEmpty()) settings.endGroup(); + if (!group2.isEmpty()) settings.endGroup(); + } + + void store(const QStringList newSList, QStringList &stringList, const char *tag, const QString group1="", const QString group2="" ) + { + QSettings settings(PRODUCT, COMPANY); + if (!group1.isEmpty()) settings.beginGroup(group1); + if (!group2.isEmpty()) settings.beginGroup(group2); + + settings.setValue(tag, newSList); + stringList = newSList; + + if (!group1.isEmpty()) settings.endGroup(); + if (!group2.isEmpty()) settings.endGroup(); + } + + void store(const QString newString, QString &string, const char *tag, const QString group1="", const QString group2="" ) + { + QSettings settings(PRODUCT, COMPANY); + if (!group1.isEmpty()) settings.beginGroup(group1); + if (!group2.isEmpty()) settings.beginGroup(group2); + + settings.setValue(tag, newString); + string = newString; + + if (!group1.isEmpty()) settings.endGroup(); + if (!group2.isEmpty()) settings.endGroup(); + } + + void store(const bool newTruth, bool &truth, const char *tag, const QString group1="", const QString group2="" ) + { + QSettings settings(PRODUCT, COMPANY); + if (!group1.isEmpty()) settings.beginGroup(group1); + if (!group2.isEmpty()) settings.beginGroup(group2); + + settings.setValue(tag, newTruth); + truth = newTruth; + + if (!group1.isEmpty()) settings.endGroup(); + if (!group2.isEmpty()) settings.endGroup(); + } + + void store(const int newNumber, int &number, const char *tag, const QString group1="", const QString group2="" ) + { + QSettings settings(PRODUCT, COMPANY); + if (!group1.isEmpty()) settings.beginGroup(group1); + if (!group2.isEmpty()) settings.beginGroup(group2); + + settings.setValue(tag, newNumber); + number = newNumber; + + if (!group1.isEmpty()) settings.endGroup(); + if (!group2.isEmpty()) settings.endGroup(); + } + + // Retrieval functions + void retrieve( QByteArray &array, const char *tag, const char *def, const QString group1="", const QString group2="" ) + { + QSettings settings(PRODUCT, COMPANY); + if (!group1.isEmpty()) settings.beginGroup(group1); + if (!group2.isEmpty()) settings.beginGroup(group2); + + array = settings.value(tag, def).toByteArray(); + + if (!group1.isEmpty()) settings.endGroup(); + if (!group2.isEmpty()) settings.endGroup(); + } + + void retrieve( QStringList &stringList, const char *tag, const char *def, const QString group1="", const QString group2="" ) + { + QSettings settings(PRODUCT, COMPANY); + if (!group1.isEmpty()) settings.beginGroup(group1); + if (!group2.isEmpty()) settings.beginGroup(group2); + + stringList = settings.value(tag, def).toStringList(); + + if (!group1.isEmpty()) settings.endGroup(); + if (!group2.isEmpty()) settings.endGroup(); + } + + void retrieve( QString &string, const char *tag, const char *def, const QString group1="", const QString group2="" ) + { + QSettings settings(PRODUCT, COMPANY); + if (!group1.isEmpty()) settings.beginGroup(group1); + if (!group2.isEmpty()) settings.beginGroup(group2); + + string = settings.value(tag, def).toString(); + + if (!group1.isEmpty()) settings.endGroup(); + if (!group2.isEmpty()) settings.endGroup(); + } + + void retrieve( bool &truth, const char *tag, const bool def, const QString group1="", const QString group2="" ) + { + QSettings settings(PRODUCT, COMPANY); + if (!group1.isEmpty()) settings.beginGroup(group1); + if (!group2.isEmpty()) settings.beginGroup(group2); + + truth = settings.value(tag, def).toBool(); + + if (!group1.isEmpty()) settings.endGroup(); + if (!group2.isEmpty()) settings.endGroup(); + } + + void retrieve( int &number, const char *tag, const int def, const QString group1="", const QString group2="" ) + { + QSettings settings(PRODUCT, COMPANY); + if (!group1.isEmpty()) settings.beginGroup(group1); + if (!group2.isEmpty()) settings.beginGroup(group2); + + number = settings.value(tag, def).toInt(); + + if (!group1.isEmpty()) settings.endGroup(); + if (!group2.isEmpty()) settings.endGroup(); + } + + // Retrieve and Store functions + void getset( QByteArray &array, const char *tag, const char *def, const QString group1="", const QString group2="" ) + { + retrieve( array, tag, def, group1, group2); + store(array, array, tag, group1, group2); + } + + void getset( QStringList &stringList, const char *tag, const char *def, const QString group1="", const QString group2="" ) + { + retrieve( stringList, tag, def, group1, group2); + store(stringList, stringList, tag, group1, group2); + } + + void getset( QString &string, const char *tag, const char *def, const QString group1="", const QString group2="" ) + { + retrieve( string, tag, def, group1, group2); + store(string, string, tag, group1, group2); + } + + void getset( bool &truth, const char *tag, const bool def, const QString group1="", const QString group2="" ) + { + retrieve( truth, tag, def, group1, group2); + store(truth, truth, tag, group1, group2); + } + + void getset( int &number, const char *tag, const int def, const QString group1="", const QString group2="" ) + { + retrieve( number, tag, def, group1, group2); + store(number, number, tag, group1, group2); + } +}; + +class Profile:DataObj +{ + private: + // Class Internal Variable + int index; + + // Application Variables + QString _firmware; + QString _Name; + QString _sdPath; + QString _SplashFileName; + bool _burnFirmware; + bool _rename_firmware_files; + int _default_channel_order; + int _default_mode; + + // Firmware Variables + QString _Beeper; + QString _countryCode; + QString _currentCalib; + QString _Display; + QString _GSStickMode; + QString _Haptic; + QString _PPM_Multiplier; + QString _Speaker; + QString _StickPotCalib; + QString _TrainerCalib; + QString _VbatCalib; + QString _vBatWarn; + + public: + // All the get declarations + QString firmware() { return _firmware; } + QString Name() { return _Name; } + QString sdPath() { return _sdPath; } + QString SplashFileName() { return _SplashFileName; } + bool burnFirmware() { return _burnFirmware; } + bool rename_firmware_files() { return _rename_firmware_files; } + int default_channel_order() { return _default_channel_order; } + int default_mode() { return _default_mode; } + + QString Beeper() { return _Beeper; } + QString countryCode() { return _countryCode; } + QString currentCalib() { return _currentCalib; } + QString Display() { return _Display; } + QString GSStickMode() { return _GSStickMode; } + QString Haptic() { return _Haptic; } + QString PPM_Multiplier() { return _PPM_Multiplier; } + QString Speaker() { return _Speaker; } + QString StickPotCalib() { return _StickPotCalib; } + QString TrainerCalib() { return _TrainerCalib; } + QString VbatCalib() { return _VbatCalib; } + QString vBatWarn() { return _vBatWarn; } + + + // All the set declarations + void firmware (const QString str) { store(str, _firmware, "firmware" ,"Profiles", QString("profile%1").arg(index));} + void Name (const QString str) { store(str, _Name, "Name" ,"Profiles", QString("profile%1").arg(index));} + void sdPath (const QString str) { store(str, _sdPath, "sdPath" ,"Profiles", QString("profile%1").arg(index));} + void SplashFileName (const QString str) { store(str, _SplashFileName, "SplashFileName" ,"Profiles", QString("profile%1").arg(index));} + void burnFirmware (const bool bl) { store(bl, _burnFirmware, "burnFirmware" ,"Profiles", QString("profile%1").arg(index));} + void rename_firmware_files (const bool bl) { store(bl, _rename_firmware_files, "rename_firmware_files" ,"Profiles", QString("profile%1").arg(index));} + void default_channel_order (const int it) { store(it, _default_channel_order, "default_channel_order" ,"Profiles", QString("profile%1").arg(index));} + void default_mode (const int it) { store(it, _default_mode, "default_mode" ,"Profiles", QString("profile%1").arg(index));} + + void Beeper (const QString str) { store(str, _Beeper, "Beeper" ,"Profiles", QString("profile%1").arg(index));} + void countryCode (const QString str) { store(str, _countryCode, "countryCode" ,"Profiles", QString("profile%1").arg(index));} + void currentCalib (const QString str) { store(str, _currentCalib, "currentCalib" ,"Profiles", QString("profile%1").arg(index));} + void Display (const QString str) { store(str, _Display, "Display" ,"Profiles", QString("profile%1").arg(index));} + void GSStickMode (const QString str) { store(str, _GSStickMode, "GSStickMode" ,"Profiles", QString("profile%1").arg(index));} + void Haptic (const QString str) { store(str, _Haptic, "Haptic" ,"Profiles", QString("profile%1").arg(index));} + void PPM_Multiplier (const QString str) { store(str, _PPM_Multiplier, "PPM_Multiplier" ,"Profiles", QString("profile%1").arg(index));} + void Speaker (const QString str) { store(str, _Speaker, "Speaker" ,"Profiles", QString("profile%1").arg(index));} + void StickPotCalib (const QString str) { store(str, _StickPotCalib, "StickPotCalib" ,"Profiles", QString("profile%1").arg(index));} + void TrainerCalib (const QString str) { store(str, _TrainerCalib, "TrainerCalib" ,"Profiles", QString("profile%1").arg(index));} + void VbatCalib (const QString str) { store(str, _VbatCalib, "VbatCalib" ,"Profiles", QString("profile%1").arg(index));} + void vBatWarn (const QString str) { store(str, _vBatWarn, "vBatWarn" ,"Profiles", QString("profile%1").arg(index));} + + // Constructor + Profile() + { + index = -1; + } + + void init(int newIndex) + { + index = newIndex; + + QString pName; + retrieve( pName, "Name", "", "Profiles", QString("profile%1").arg(newIndex)); + if ( newIndex > 1 && pName.isEmpty()) + return; + + // Load and store all variables. Use default values if setting values are missing + + getset( _firmware, "firmware" ,"" ,"Profiles", QString("profile%1").arg(index)); + getset( _Name, "Name" ,"----" ,"Profiles", QString("profile%1").arg(index)); + getset( _sdPath, "sdPath" ,"" ,"Profiles", QString("profile%1").arg(index)); + getset( _SplashFileName, "SplashFileName" ,"" ,"Profiles", QString("profile%1").arg(index)); + getset( _burnFirmware, "burnFirmware" ,false ,"Profiles", QString("profile%1").arg(index)); + getset( _rename_firmware_files, "rename_firmware_files" ,false ,"Profiles", QString("profile%1").arg(index)); + getset( _default_channel_order, "default_channel_order" ,0 ,"Profiles", QString("profile%1").arg(index)); + getset( _default_mode, "default_mode" ,1 ,"Profiles", QString("profile%1").arg(index)); + + getset( _Beeper, "Beeper" ,"" ,"Profiles", QString("profile%1").arg(index)); + getset( _countryCode, "countryCode" ,"" ,"Profiles", QString("profile%1").arg(index)); + getset( _currentCalib, "currentCalib" ,"" ,"Profiles", QString("profile%1").arg(index)); + getset( _Display, "Display" ,"" ,"Profiles", QString("profile%1").arg(index)); + getset( _GSStickMode, "GSStickMode" ,"" ,"Profiles", QString("profile%1").arg(index)); + getset( _Haptic, "Haptic" ,"" ,"Profiles", QString("profile%1").arg(index)); + getset( _PPM_Multiplier, "PPM_Multiplier" ,"" ,"Profiles", QString("profile%1").arg(index)); + getset( _Speaker, "Speaker" ,"" ,"Profiles", QString("profile%1").arg(index)); + getset( _StickPotCalib, "StickPotCalib" ,"" ,"Profiles", QString("profile%1").arg(index)); + getset( _TrainerCalib, "TrainerCalib" ,"" ,"Profiles", QString("profile%1").arg(index)); + getset( _VbatCalib, "VbatCalib" ,"" ,"Profiles", QString("profile%1").arg(index)); + getset( _vBatWarn, "vBatWarn" ,"" ,"Profiles", QString("profile%1").arg(index)); + } +}; + +class AppData:DataObj +{ // All the global variables + public: + Profile profile[MAX_PROFILES]; + + private: QStringList _recentFileList; QByteArray _mainWindowGeometry; QByteArray _mainWindowState; @@ -65,101 +350,6 @@ class AppData // Storage functions - void store(const QByteArray newArray, QByteArray &array, const char *tag) - { - QSettings settings(PRODUCT, COMPANY); - settings.setValue(tag, newArray); - array = newArray; - } - - void store(const QStringList newSList, QStringList &stringList, const char *tag) - { - QSettings settings(PRODUCT, COMPANY); - settings.setValue(tag, newSList); - stringList = newSList; - } - - void store(const QString newString, QString &string, const char *tag) - { - QSettings settings(PRODUCT, COMPANY); - settings.setValue(tag, newString); - string = newString; - } - - void store(const bool newTruth, bool &truth, const char *tag) - { - QSettings settings(PRODUCT, COMPANY); - settings.setValue(tag, newTruth); - truth = newTruth; - } - - void store(const int newNumber, int &number, const char *tag) - { - QSettings settings(PRODUCT, COMPANY); - settings.setValue(tag, newNumber); - number = newNumber; - } - - // Retrieval functions - void retrieve( QByteArray &array, const char *tag, const char *def) - { - QSettings settings(PRODUCT, COMPANY); - array = settings.value(tag, def).toByteArray(); - } - void retrieve( QStringList &stringList, const char *tag, const char *def) - { - QSettings settings(PRODUCT, COMPANY); - stringList = settings.value(tag, def).toStringList(); - } - - void retrieve( QString &string, const char *tag, const char *def) - { - QSettings settings(PRODUCT, COMPANY); - string = settings.value(tag, def).toString(); - } - - void retrieve( bool &truth, const char *tag, const bool def) - { - QSettings settings(PRODUCT, COMPANY); - truth = settings.value(tag, def).toBool(); - } - - void retrieve( int &number, const char *tag, const int def) - { - QSettings settings(PRODUCT, COMPANY); - number = settings.value(tag, def).toInt(); - } - - // Retrieve and Store functions - void getset( QByteArray &array, const char *tag, const char *def) - { - retrieve( array, tag, def); - store(array, array, tag); - } - - void getset( QStringList &stringList, const char *tag, const char *def) - { - retrieve( stringList, tag, def); - store(stringList, stringList, tag); - } - - void getset( QString &string, const char *tag, const char *def) - { - retrieve( string, tag, def); - store(string, string, tag); - } - - void getset( bool &truth, const char *tag, const bool def) - { - retrieve( truth, tag, def); - store(truth, truth, tag); - } - - void getset( int &number, const char *tag, const int def) - { - retrieve( number, tag, def); - store(number, number, tag); - } public: // All the get declarations @@ -273,6 +463,12 @@ public: settings.setValue(*i, c9x_settings.value(*i)); } } + + //Initialize the index variables of the profiles + for (int i=0; isdPath->setText(Path); } - ui->profileIndexLE->setText(QString(glob.profileId())); + ui->profileIndexLE->setText(QString("%1").arg(glob.profileId())); ui->profileNameLE->setText(glob.Name()); QString fileName=glob.SplashFileName(); @@ -260,54 +260,34 @@ void appPreferencesDialog::on_sdPathButton_clicked() void appPreferencesDialog::saveProfile() { - QSettings settings; - - QString profile=QString("profile") + glob.profileId(); + // The profile name may NEVER be empty + QString profile=QString("profile") + QString("%1").arg(glob.profileId()); QString name=ui->profileNameLE->text(); if (name.isEmpty()) { name = profile; ui->profileNameLE->setText(name); } - settings.beginGroup("Profiles"); - settings.beginGroup(profile); - settings.setValue("Name",name); - settings.setValue("default_channel_order", ui->channelorderCB->currentIndex()); - settings.setValue("default_mode", ui->stickmodeCB->currentIndex()); - settings.setValue("burnFirmware", ui->burnFirmware->isChecked()); - settings.setValue("rename_firmware_files", ui->renameFirmware->isChecked()); - settings.setValue("sdPath", ui->sdPath->text()); - settings.setValue("SplashFileName", ui->SplashFileName->text()); - settings.setValue("firmware", ui->firmwareLE->text()); - settings.endGroup(); - settings.endGroup(); + glob.profile[glob.profileId()].Name( name ); + glob.profile[glob.profileId()].default_channel_order( ui->channelorderCB->currentIndex()); + glob.profile[glob.profileId()].default_mode( ui->stickmodeCB->currentIndex()); + glob.profile[glob.profileId()].burnFirmware( ui->burnFirmware->isChecked()); + glob.profile[glob.profileId()].rename_firmware_files( ui->renameFirmware->isChecked()); + glob.profile[glob.profileId()].sdPath( ui->sdPath->text()); + glob.profile[glob.profileId()].SplashFileName( ui->SplashFileName->text()); + glob.profile[glob.profileId()].firmware( ui->firmwareLE->text()); } -void appPreferencesDialog::loadProfileString(QString profile, QString label) +void appPreferencesDialog::loadFromProfile() { - QSettings settings; - QString value; - - settings.beginGroup("Profiles"); - settings.beginGroup(profile); - value = settings.value(label).toString(); - settings.endGroup(); - settings.endGroup(); - - settings.setValue( label, value ); -} - -void appPreferencesDialog::loadProfile() -{ - QString profile=QString("profile") + glob.profileId(); - - loadProfileString( profile, "Name" ); - loadProfileString( profile, "default_channel_order" ); - loadProfileString( profile, "default_mode" ); - loadProfileString( profile, "burnFirmware" ); - loadProfileString( profile, "rename_firmware_files" ); - loadProfileString( profile, "sdPath" ); - loadProfileString( profile, "SplashFileName" ); - loadProfileString( profile, "firmware" ); + int i = glob.profileId(); + glob.Name( glob.profile[i].Name() ); + glob.default_channel_order( glob.profile[i].default_channel_order()); + glob.default_mode( glob.profile[i].default_mode()); + glob.burnFirmware( glob.profile[i].burnFirmware()); + glob.rename_firmware_files( glob.profile[i].rename_firmware_files()); + glob.sdPath( glob.profile[i].sdPath()); + glob.SplashFileName( glob.profile[i].SplashFileName()); + glob.firmware( glob.profile[i].firmware()); } void appPreferencesDialog::on_removeProfileButton_clicked() @@ -317,13 +297,13 @@ void appPreferencesDialog::on_removeProfileButton_clicked() QMessageBox::information(this, tr("Not possible to remove profile"), tr("The default profile can not be removed.")); else { - QString profile=QString("profile") + glob.profileId(); + QString profile=QString("profile") + QString("%1").arg(glob.profileId()); settings.beginGroup("Profiles"); settings.remove(profile); settings.endGroup(); - settings.setValue("profileId", "1"); + glob.profileId( 1 ); - loadProfile(); + loadFromProfile(); initSettings(); } } diff --git a/companion/src/apppreferencesdialog.h b/companion/src/apppreferencesdialog.h index b6f6c3896..18ce2f1e3 100644 --- a/companion/src/apppreferencesdialog.h +++ b/companion/src/apppreferencesdialog.h @@ -26,7 +26,7 @@ private: bool displayImage( QString fileName ); void saveProfile(); void loadProfileString(QString profile, QString label); - void loadProfile(); + void loadFromProfile(); private slots: void writeValues(); diff --git a/companion/src/mainwindow.cpp b/companion/src/mainwindow.cpp index 991861451..8d395e570 100644 --- a/companion/src/mainwindow.cpp +++ b/companion/src/mainwindow.cpp @@ -90,23 +90,23 @@ MainWindow::MainWindow(): mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); setCentralWidget(mdiArea); - connect(mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)), - this, SLOT(updateMenus())); + connect(mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(updateMenus())); windowMapper = new QSignalMapper(this); - connect(windowMapper, SIGNAL(mapped(QWidget*)), - this, SLOT(setActiveSubWindow(QWidget*))); + connect(windowMapper, SIGNAL(mapped(QWidget*)), this, SLOT(setActiveSubWindow(QWidget*))); - MaxRecentFiles=MAX_RECENT; - QSettings settings; - - restoreGeometry(settings.value("mainWindowGeometry").toByteArray()); createActions(); - createMenus(); createToolBars(); createStatusBar(); updateMenus(); - readSettings(); + + restoreState(glob.mainWindowState()); + + if (glob.profileId() == 0) + { + createProfile(); + glob.profileId( 1 ); + } setUnifiedTitleAndToolBarOnMac(true); this->setWindowIcon(QIcon(":/icon.png")); @@ -116,7 +116,7 @@ MainWindow::MainWindow(): // give time to the splash to disappear and main window to open before starting updates int updateDelay = 1000; - bool showSplash = settings.value("show_splash", true).toBool(); + bool showSplash = glob.show_splash(); if (showSplash) { updateDelay += (SPLASH_TIME*1000); } @@ -169,8 +169,7 @@ MainWindow::MainWindow(): void MainWindow::displayWarnings() { - QSettings settings; - int warnId=settings.value("warningId", 0 ).toInt(); + int warnId=glob.warningId(); if (warnId0) { @@ -181,10 +180,10 @@ void MainWindow::displayWarnings() res = QMessageBox::question(this, "Companion",tr("Display previous message again at startup ?"),QMessageBox::Yes | QMessageBox::No); } if (res == QMessageBox::No) { - settings.setValue("warningId", WARNING_ID); + glob.warningId(WARNING_ID); } } else if (warnId==0) { - settings.setValue("warningId", WARNING_ID); + glob.warningId(WARNING_ID); } } @@ -203,12 +202,11 @@ void MainWindow::checkForUpdates(bool ignoreSettings, QString & fwId) showcheckForUpdatesResult = ignoreSettings; check1done = true; check2done = true; - QSettings settings; fwToUpdate = fwId; QString stamp = GetFirmware(fwToUpdate)->stamp; if (!stamp.isEmpty()) { - if (checkFW || ignoreSettings) { + if (glob.startup_check_fw() || ignoreSettings) { check1done=false; manager1 = new QNetworkAccessManager(this); connect(manager1, SIGNAL(finished(QNetworkReply*)), this, SLOT(reply1Finished(QNetworkReply*))); @@ -219,7 +217,7 @@ void MainWindow::checkForUpdates(bool ignoreSettings, QString & fwId) } } - if (checkCompanion || ignoreSettings) { + if (glob.startup_check_companion() || ignoreSettings) { check2done = false; manager2 = new QNetworkAccessManager(this); connect(manager2, SIGNAL(finished(QNetworkReply*)),this, SLOT(checkForUpdateFinished(QNetworkReply*))); @@ -269,16 +267,14 @@ void MainWindow::checkForUpdateFinished(QNetworkReply * reply) "Would you like to download it?").arg(version) , QMessageBox::Yes | QMessageBox::No); - QSettings settings; - if (ret == QMessageBox::Yes) { #if defined __APPLE__ - QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), settings.value("lastUpdatesDir").toString() + QString(C9X_INSTALLER).arg(version)); + QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), glob.lastUpdatesDir() + QString(C9X_INSTALLER).arg(version)); #else - QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), settings.value("lastUpdatesDir").toString() + QString(C9X_INSTALLER).arg(version), tr("Executable (*.exe)")); + QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), glob.lastUpdatesDir() + QString(C9X_INSTALLER).arg(version), tr("Executable (*.exe)")); #endif if (!fileName.isEmpty()) { - settings.setValue("lastUpdatesDir", QFileInfo(fileName).dir().absolutePath()); + glob.lastUpdatesDir(QFileInfo(fileName).dir().absolutePath()); downloadDialog * dd = new downloadDialog(this, QString(OPENTX_COMPANION_DOWNLOADS C9X_INSTALLER).arg(version), fileName); installer_fileName = fileName; connect(dd, SIGNAL(accepted()), this, SLOT(updateDownloaded())); @@ -311,11 +307,10 @@ void MainWindow::updateDownloaded() void MainWindow::downloadLatestFW(FirmwareInfo * firmware, const QString & firmwareId) { QString url, ext, cpuid; - QSettings settings; url = firmware->getUrl(firmwareId); - cpuid=settings.value("cpuid","").toString(); + cpuid=glob.cpu_id(); ext = url.mid(url.lastIndexOf(".")); - QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), settings.value("lastFlashDir").toString() + "/" + firmwareId + ext); + QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), glob.lastFlashDir() + "/" + firmwareId + ext); if (!fileName.isEmpty()) { downloadedFW = firmwareId; needRename=true; @@ -324,7 +319,7 @@ void MainWindow::downloadLatestFW(FirmwareInfo * firmware, const QString & firmw url.append("&cpuid="); url.append(cpuid); } - settings.setValue("lastFlashDir", QFileInfo(fileName).dir().absolutePath()); + glob.lastFlashDir(QFileInfo(fileName).dir().absolutePath()); downloadDialog * dd = new downloadDialog(this, url, fileName); connect(dd, SIGNAL(accepted()), this, SLOT(reply1Accepted())); dd->exec(); @@ -457,7 +452,7 @@ void MainWindow::reply1Finished(QNetworkReply * reply) } QSettings settings; - cpuid=settings.value("cpuid","").toString(); + cpuid=glob.cpu_id(); QByteArray qba = reply->readAll(); int i = qba.indexOf("SVN_VERS"); int warning = qba.indexOf("WARNING"); @@ -471,9 +466,7 @@ void MainWindow::reply1Finished(QNetworkReply * reply) if(!cres) { QMessageBox::warning(this, "Companion", tr("Unable to check for updates.")); - int server = settings.value("fwserver",0).toInt(); - server++; - settings.setValue("fwserver",server); + glob.fwserver(glob.fwserver()+1); return; } @@ -571,12 +564,12 @@ void MainWindow::reply1Finished(QNetworkReply * reply) QString url = GetFirmware(fwToUpdate)->getUrl(fwToUpdate); QString ext = url.mid(url.lastIndexOf(".")); needRename=false; - bool addversion=settings.value("rename_firmware_files", false).toBool(); + bool addversion=glob.rename_firmware_files(); QString fileName; if (addversion) { - fileName = QFileDialog::getSaveFileName(this, tr("Save As"), settings.value("lastFlashDir").toString() + "/" + fwToUpdate + newrev + ext); + fileName = QFileDialog::getSaveFileName(this, tr("Save As"), glob.lastFlashDir() + "/" + fwToUpdate + newrev + ext); } else { - fileName = QFileDialog::getSaveFileName(this, tr("Save As"), settings.value("lastFlashDir").toString() + "/" + fwToUpdate + ext); + fileName = QFileDialog::getSaveFileName(this, tr("Save As"), glob.lastFlashDir() + "/" + fwToUpdate + ext); } if (!fileName.isEmpty()) { if (!cpuid.isEmpty()) { @@ -584,7 +577,7 @@ void MainWindow::reply1Finished(QNetworkReply * reply) url.append(cpuid); } downloadedFWFilename = fileName; - settings.setValue("lastFlashDir", QFileInfo(fileName).dir().absolutePath()); + glob.lastFlashDir(QFileInfo(fileName).dir().absolutePath()); downloadDialog * dd = new downloadDialog(this, url, fileName); currentFWrev_temp = NewFwRev; connect(dd, SIGNAL(accepted()), this, SLOT(reply1Accepted())); @@ -598,9 +591,7 @@ void MainWindow::reply1Finished(QNetworkReply * reply) } else { if(check1done && check2done) { QMessageBox::warning(this, "Companion", tr("Unable to check for updates.")); - int server = settings.value("fwserver",0).toInt(); - server++; - settings.setValue("fwserver",server); + glob.fwserver(glob.fwserver()+1); return; } } @@ -608,9 +599,8 @@ void MainWindow::reply1Finished(QNetworkReply * reply) void MainWindow::closeEvent(QCloseEvent *event) { - QSettings settings; - settings.setValue("mainWindowGeometry", saveGeometry()); - settings.setValue("mainWindowState", saveState()); + glob.mainWindowGeometry(saveGeometry()); + glob.mainWindowState(saveState()); mdiArea->closeAllSubWindows(); if (mdiArea->currentSubWindow()) { event->ignore(); @@ -622,8 +612,7 @@ void MainWindow::closeEvent(QCloseEvent *event) void MainWindow::setLanguage(QString langString) { - QSettings settings; - settings.setValue("locale", langString ); + glob.locale( langString ); QMessageBox msgBox; msgBox.setText(tr("The selected language will be used the next time you start Companion.")); @@ -634,8 +623,7 @@ void MainWindow::setLanguage(QString langString) void MainWindow::setTheme(int index) { - QSettings settings; - settings.setValue("theme", index ); + glob.theme( index ); QMessageBox msgBox; msgBox.setText(tr("The new theme will be loaded the next time you start Companion.")); @@ -646,8 +634,7 @@ void MainWindow::setTheme(int index) void MainWindow::setIconThemeSize(int index) { - QSettings settings; - settings.setValue("icon_size", index ); + glob.icon_size( index ); QMessageBox msgBox; msgBox.setText(tr("The icon size will be used the next time you start Companion.")); @@ -671,10 +658,9 @@ void MainWindow::openDocURL() void MainWindow::openFile() { - QSettings settings; - QString fileName = QFileDialog::getOpenFileName(this, tr("Open"), settings.value("lastDir").toString(),tr(EEPROM_FILES_FILTER)); + QString fileName = QFileDialog::getOpenFileName(this, tr("Open"), glob.lastDir(), tr(EEPROM_FILES_FILTER)); if (!fileName.isEmpty()) { - settings.setValue("lastDir", QFileInfo(fileName).dir().absolutePath()); + glob.lastDir(QFileInfo(fileName).dir().absolutePath()); QMdiSubWindow *existing = findMdiChild(fileName); if (existing) { @@ -704,11 +690,9 @@ void MainWindow::saveAs() void MainWindow::openRecentFile() { - QSettings settings; QAction *action = qobject_cast(sender()); if (action) { QString fileName=action->data().toString(); - // settings.setValue("lastDir", QFileInfo(fileName).dir().absolutePath()); QMdiSubWindow *existing = findMdiChild(fileName); if (existing) { @@ -1113,14 +1097,14 @@ void MainWindow::writeFileToEeprom() burnDialog *cd = new burnDialog(this, 1, &fileName, &backup); cd->exec(); if (!fileName.isEmpty()) { - settings.setValue("lastDir", QFileInfo(fileName).dir().absolutePath()); + glob.lastDir(QFileInfo(fileName).dir().absolutePath()); int ret = QMessageBox::question(this, "Companion", tr("Write Models and settings from %1 to the Tx?").arg(QFileInfo(fileName).fileName()), QMessageBox::Yes | QMessageBox::No); if (ret != QMessageBox::Yes) return; if (!isValidEEPROM(fileName)) ret = QMessageBox::question(this, "Companion", tr("The file %1\nhas not been recognized as a valid Models and Settings file\nWrite anyway ?").arg(QFileInfo(fileName).fileName()), QMessageBox::Yes | QMessageBox::No); if (ret != QMessageBox::Yes) return; - bool backupEnable = settings.value("backupEnable", true).toBool(); - QString backupPath = settings.value("backupPath", "").toString(); + bool backupEnable = glob.backupEnable(); + QString backupPath = glob.backupPath(); if (!backupPath.isEmpty()) { if (!QDir(backupPath).exists()) { if (backupEnable) { @@ -1361,9 +1345,8 @@ bool MainWindow::convertEEPROM(QString backupFile, QString restoreFile, QString void MainWindow::writeFlash(QString fileToFlash) { - QSettings settings; QString fileName; - bool backup = settings.value("backupOnFlash", false).toBool(); + bool backup = glob.backupOnFlash(); if(!fileToFlash.isEmpty()) fileName = fileToFlash; burnDialog *cd = new burnDialog(this, 2, &fileName, &backup); @@ -1371,12 +1354,12 @@ void MainWindow::writeFlash(QString fileToFlash) if (IS_TARANIS(GetEepromInterface()->getBoard())) backup=false; if (!fileName.isEmpty()) { - settings.setValue("backupOnFlash", backup); + glob.backupOnFlash(backup); if (backup) { QString tempDir = QDir::tempPath(); QString backupFile = tempDir + "/backup.bin"; - bool backupEnable=settings.value("backupEnable", true).toBool(); - QString backupPath=settings.value("backupPath", "").toString(); + bool backupEnable=glob.backupEnable(); + QString backupPath=glob.backupPath(); if (!backupPath.isEmpty() && !IS_TARANIS(GetEepromInterface()->getBoard())) { if (!QDir(backupPath).exists()) { if (backupEnable) { @@ -1430,8 +1413,8 @@ void MainWindow::writeFlash(QString fileToFlash) } } else { - bool backupEnable=settings.value("backupEnable", true).toBool(); - QString backupPath=settings.value("backupPath", "").toString(); + bool backupEnable=glob.backupEnable(); + QString backupPath=glob.backupPath(); if (!QDir(backupPath).exists()) { if (backupEnable) { QMessageBox::warning(this, tr("Backup is impossible"), tr("The backup dir set in preferences does not exist")); @@ -1460,8 +1443,7 @@ void MainWindow::writeFlash(QString fileToFlash) void MainWindow::readEepromToFile() { - QSettings settings; - QString fileName = QFileDialog::getSaveFileName(this, tr("Save transmitter Models and Settings to File"), settings.value("lastDir").toString(), tr(EXTERNAL_EEPROM_FILES_FILTER)); + QString fileName = QFileDialog::getSaveFileName(this, tr("Save transmitter Models and Settings to File"), glob.lastDir(), tr(EXTERNAL_EEPROM_FILES_FILTER)); if (!fileName.isEmpty()) { EEPROMInterface *eepromInterface = GetEepromInterface(); if (IS_TARANIS(eepromInterface->getBoard())) { @@ -1480,7 +1462,7 @@ void MainWindow::readEepromToFile() } } else { - settings.setValue("lastDir", QFileInfo(fileName).dir().absolutePath()); + glob.lastDir(QFileInfo(fileName).dir().absolutePath()); QStringList str = GetReceiveEEpromCommand(fileName); avrOutputDialog *ad = new avrOutputDialog(this, GetAvrdudeLocation(), str, tr("Read Models and Settings From Tx")); ad->setWindowIcon(CompanionIcon("read_eeprom.png")); @@ -1492,14 +1474,13 @@ void MainWindow::readEepromToFile() void MainWindow::readFlash() { - QSettings settings; - QString fileName = QFileDialog::getSaveFileName(this,tr("Read Tx Firmware to File"), settings.value("lastFlashDir").toString(),tr(FLASH_FILES_FILTER)); + QString fileName = QFileDialog::getSaveFileName(this,tr("Read Tx Firmware to File"), glob.lastFlashDir(),tr(FLASH_FILES_FILTER)); if (!fileName.isEmpty()) { QFile file(fileName); if (file.exists()) { file.remove(); } - settings.setValue("lastFlashDir",QFileInfo(fileName).dir().absolutePath()); + glob.lastFlashDir(QFileInfo(fileName).dir().absolutePath()); QStringList str = GetReceiveFlashCommand(fileName); avrOutputDialog *ad = new avrOutputDialog(this, GetAvrdudeLocation(), str, "Read Firmware From Tx"); ad->setWindowIcon(CompanionIcon("read_flash.png")); @@ -1578,8 +1559,7 @@ void MainWindow::updateMenus() updateIconSizeActions(); updateIconThemeActions(); - QSettings settings; - setWindowTitle(tr("OpenTX Companion - FW: %1 - Profile: %2").arg(GetCurrentFirmware()->name).arg(settings.value("profileId").toString())); + setWindowTitle(tr("OpenTX Companion - FW: %1 - Profile: %2").arg(GetCurrentFirmware()->name).arg( QString("%1").arg(glob.profileId()) )); } MdiChild *MainWindow::createMdiChild() @@ -1630,7 +1610,7 @@ void MainWindow::createActions() separatorAct = new QAction(this); separatorAct->setSeparator(true); - for (int i = 0; i < MaxRecentFiles; ++i) { + for (int i = 0; i < MAX_RECENT; ++i) { recentFileActs[i] = new QAction(this); recentFileActs[i]->setVisible(false); connect(recentFileActs[i], SIGNAL(triggered()), this, SLOT(openRecentFile())); @@ -1728,7 +1708,7 @@ void MainWindow::createMenus() fileMenu->addAction(saveAsAct); fileMenu->addMenu(recentFileMenu); recentFileMenu->setIcon(CompanionIcon("recentdocument.png")); - for (int i=0; iaddAction(recentFileActs[i]); fileMenu->addSeparator(); fileMenu->addAction(logsAct); @@ -1810,7 +1790,7 @@ void MainWindow::createMenus() QMenu *MainWindow::createRecentFileMenu() { QMenu *recentFileMenu = new QMenu(this); - for ( int i = 0; i < MaxRecentFiles; ++i) + for ( int i = 0; i < MAX_RECENT; ++i) recentFileMenu->addAction(recentFileActs[i]); return recentFileMenu; } @@ -1831,10 +1811,8 @@ QMenu *MainWindow::createProfilesMenu() void MainWindow::createToolBars() { - QSettings settings; - int icon_size=settings.value("icon_size",2 ).toInt(); QSize size; - switch(icon_size) { + switch(glob.icon_size()) { case 0: size=QSize(16,16); break; @@ -1919,20 +1897,6 @@ void MainWindow::createStatusBar() statusBar()->showMessage(tr("Ready")); } -void MainWindow::readSettings() -{ - QSettings settings; - restoreState(settings.value("mainWindowState").toByteArray()); - checkCompanion = settings.value("startup_check_companion", true).toBool(); - checkFW = settings.value("startup_check_fw", true).toBool(); - MaxRecentFiles =settings.value("history_size",10).toInt(); - if (settings.value("profileId",0).toInt() == 0) - { - createProfile(); - settings.setValue("profileId", "1"); - } -} - MdiChild *MainWindow::activeMdiChild() { if (QMdiSubWindow *activeSubWindow = mdiArea->activeSubWindow()) @@ -1965,7 +1929,7 @@ void MainWindow::updateRecentFileActions() QSettings settings; QStringList files = settings.value("recentFileList").toStringList(); - numRecentFiles = qMin(files.size(), (int)MaxRecentFiles); + numRecentFiles = qMin(files.size(), glob.history_size()); for ( i = 0; i < numRecentFiles; ++i) { QString text = tr("&%1 %2").arg(i + 1).arg(strippedName(files[i])); @@ -1973,15 +1937,14 @@ void MainWindow::updateRecentFileActions() recentFileActs[i]->setData(files[i]); recentFileActs[i]->setVisible(true); } - for ( j = numRecentFiles; j < MaxRecentFiles; ++j) + for ( j = numRecentFiles; j < glob.history_size(); ++j) recentFileActs[j]->setVisible(false); } void MainWindow::updateIconSizeActions() { - QSettings settings; - int size = settings.value("icon_size","0").toInt(); - switch (size){ + switch (glob.icon_size()) + { case 0: smallIconAct->setChecked(true); break; case 1: normalIconAct->setChecked(true); break; case 2: bigIconAct->setChecked(true); break; @@ -1991,8 +1954,7 @@ void MainWindow::updateIconSizeActions() void MainWindow::updateLanguageActions() { - QSettings settings; - QString langId = settings.value("locale","").toString(); + QString langId = glob.locale(); if (langId=="") sysLangAct->setChecked(true); @@ -2022,9 +1984,8 @@ void MainWindow::updateLanguageActions() void MainWindow::updateIconThemeActions() { - QSettings settings; - int size = settings.value("theme","1").toInt(); - switch (size){ + switch (glob.theme()) + { case 0: classicThemeAct->setChecked(true); break; case 1: newThemeAct->setChecked(true); break; case 2: monoWhiteAct->setChecked(true); break; @@ -2037,7 +1998,7 @@ void MainWindow::updateProfilesActions() { int i; QSettings settings; - int activeProfile = settings.value("profileId").toInt(); + int activeProfile = glob.profileId(); settings.beginGroup("Profiles"); for (i=0; i