diff --git a/companion/src/appdata.h b/companion/src/appdata.h index 2d7add453..0a1d33589 100644 --- a/companion/src/appdata.h +++ b/companion/src/appdata.h @@ -230,8 +230,12 @@ class Profile:DataObj // All the set declarations + void Name (const QString str) { if (str.isEmpty()) // Name may never be empty! + store("----", _Name, "Name" ,"Profiles", QString("profile%1").arg(index)); + else + store(str, _Name, "Name" ,"Profiles", QString("profile%1").arg(index));} + 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));} @@ -258,17 +262,33 @@ class Profile:DataObj index = -1; } - void init(int newIndex) + void remove() + { + // Remove all profile values from settings file + QSettings settings(PRODUCT, COMPANY); + settings.beginGroup("Profiles"); + settings.remove(QString("profile%1").arg(index)); + settings.endGroup(); + + // Reset all profile variables to initial values + init(index); + } + + bool existsOnDisk() { - index = newIndex; + QSettings settings(PRODUCT, COMPANY); + settings.beginGroup("Profiles"); + settings.beginGroup(QString("profile%1").arg(index)); + QStringList keyList = settings.allKeys(); + settings.endGroup(); + settings.endGroup(); - QString pName; - retrieve( pName, "Name", "", "Profiles", QString("profile%1").arg(newIndex)); - if ( newIndex > 1 && pName.isEmpty()) - return; + return (keyList.length() > 0); + } + void flush() + { // 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)); @@ -289,7 +309,43 @@ class Profile:DataObj 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)); + getset( _vBatWarn, "vBatWarn" ,"" ,"Profiles", QString("profile%1").arg(index)); + } + + void init(int newIndex) + { + index = newIndex; + + _firmware = ""; + _Name = ""; + _sdPath = ""; + _SplashFileName = ""; + _burnFirmware = false; + _rename_firmware_files = false; + _default_channel_order = 0; + _default_mode = 1; + + _Beeper = ""; + _countryCode = ""; + _currentCalib = ""; + _Display = ""; + _GSStickMode = ""; + _Haptic = ""; + _PPM_Multiplier = ""; + _Speaker = ""; + _StickPotCalib = ""; + _TrainerCalib = ""; + _VbatCalib = ""; + _vBatWarn = ""; + + QString pName; + retrieve( pName, "Name", "", "Profiles", QString("profile%1").arg(index)); + + // Do not write empty profiles to disk except the default (0) profile. + if ( index > 0 && !existsOnDisk()) + return; + + flush(); } }; @@ -307,7 +363,6 @@ class AppData:DataObj QString _firmware; QString _locale; QString _cpu_id; - QString _SplashImage; QString _Name; QString _SplashFileName; QString _modelEditGeometry; @@ -360,7 +415,6 @@ public: QString firmware() { return _firmware; } QString locale() { return _locale; } QString cpu_id() { return _cpu_id; } - QString SplashImage() { return _SplashImage; } QString Name() { return _Name; } QString SplashFileName() { return _SplashFileName; } QString modelEditGeometry() { return _modelEditGeometry; } @@ -409,7 +463,6 @@ public: void firmware (const QString str) { store(str, _firmware, "firmware" );} void locale (const QString str) { store(str, _locale, "locale" );} void cpu_id (const QString str) { store(str, _cpu_id, "cpu_id" );} - void SplashImage (const QString str) { store(str, _SplashImage, "SplashImage" );} void Name (const QString str) { store(str, _Name, "Name" );} void SplashFileName (const QString str) { store(str, _SplashFileName, "SplashFileName" );} void modelEditGeometry (const QString str) { store(str, _modelEditGeometry, "modelEditGeometry" );} @@ -466,7 +519,7 @@ public: //Initialize the index variables of the profiles for (int i=0; iprofileNameLE->text()); glob.sdPath(ui->sdPath->text()); glob.SplashFileName(ui->SplashFileName->text()); - if (!ui->SplashFileName->text().isEmpty()) - glob.SplashImage(""); glob.firmware(ui->firmwareLE->text()); saveProfile(); @@ -85,13 +83,10 @@ void appPreferencesDialog::initSettings() { ui->snapshotClipboardCKB->setChecked(glob.snapshot_to_clipboard()); ui->burnFirmware->setChecked(glob.burnFirmware()); - - QString Path=glob.snapshotpath(); - if (QDir(Path).exists()) { - ui->snapshotPath->setText(Path); - ui->snapshotPath->setReadOnly(true); - } - if (ui->snapshotClipboardCKB->isChecked()) { + ui->snapshotPath->setText(glob.snapshotpath()); + ui->snapshotPath->setReadOnly(true); + if (ui->snapshotClipboardCKB->isChecked()) + { ui->snapshotPath->setDisabled(true); ui->snapshotPathButton->setDisabled(true); } @@ -102,19 +97,12 @@ void appPreferencesDialog::initSettings() ui->historySize->setValue(glob.history_size()); ui->backLightColor->setCurrentIndex(glob.backLight()); ui->simuSW->setChecked(glob.simuSW()); + ui->libraryPath->setText(glob.libraryPath()); + ui->ge_lineedit->setText(glob.gePath()); - Path=glob.libraryPath(); - if (QDir(Path).exists()) { - ui->libraryPath->setText(Path); - } - Path=glob.gePath(); - if (QFile(Path).exists()) { - ui->ge_lineedit->setText(Path); - } - Path=glob.backupPath(); - if (!Path.isEmpty()) { - if (QDir(Path).exists()) { - ui->backupPath->setText(Path); + if (!glob.backupPath().isEmpty()) { + if (QDir(glob.backupPath()).exists()) { + ui->backupPath->setText(glob.backupPath()); ui->backupEnable->setEnabled(true); ui->backupEnable->setChecked(glob.backupEnable()); } else { @@ -156,22 +144,12 @@ void appPreferencesDialog::initSettings() ui->channelorderCB->setCurrentIndex(glob.default_channel_order()); ui->stickmodeCB->setCurrentIndex(glob.default_mode()); ui->renameFirmware->setChecked(glob.rename_firmware_files()); - Path=glob.sdPath(); - if (QDir(Path).exists()) { - ui->sdPath->setText(Path); - } + ui->sdPath->setText(glob.sdPath()); ui->profileIndexLE->setText(QString("%1").arg(glob.profileId())); ui->profileNameLE->setText(glob.Name()); - - QString fileName=glob.SplashFileName(); - if (!fileName.isEmpty()) { - QFile file(fileName); - if (file.exists()){ - ui->SplashFileName->setText(fileName); - displayImage( fileName ); - } - } ui->firmwareLE->setText(glob.firmware()); + ui->SplashFileName->setText(glob.SplashFileName()); + displayImage( glob.SplashFileName() ); } void appPreferencesDialog::on_libraryPathButton_clicked() @@ -261,13 +239,10 @@ void appPreferencesDialog::on_sdPathButton_clicked() void appPreferencesDialog::saveProfile() { // 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); - } - glob.profile[glob.profileId()].Name( name ); + if (ui->profileNameLE->text().isEmpty()) + ui->profileNameLE->setText("----"); + + glob.profile[glob.profileId()].Name( ui->profileNameLE->text() ); 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()); @@ -293,16 +268,12 @@ void appPreferencesDialog::loadFromProfile() void appPreferencesDialog::on_removeProfileButton_clicked() { QSettings settings; - if ( glob.profileId() == 1 ) + if ( glob.profileId() == 0 ) QMessageBox::information(this, tr("Not possible to remove profile"), tr("The default profile can not be removed.")); else { - QString profile=QString("profile") + QString("%1").arg(glob.profileId()); - settings.beginGroup("Profiles"); - settings.remove(profile); - settings.endGroup(); - glob.profileId( 1 ); - + glob.profile[glob.profileId()].remove(); + glob.profileId( 0 ); loadFromProfile(); initSettings(); } @@ -310,6 +281,9 @@ void appPreferencesDialog::on_removeProfileButton_clicked() bool appPreferencesDialog::displayImage( QString fileName ) { + // Start by filling the pixmap with white + ui->imageLabel->setPixmap(QPixmap()); + QImage image(fileName); if (image.isNull()) return false; @@ -361,11 +335,11 @@ void appPreferencesDialog::on_SplashSelect_clicked() QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image to load"), glob.lastImagesDir(), tr("Images (%1)").arg(supportedImageFormats)); - if (!fileName.isEmpty()) { + if (!fileName.isEmpty()){ glob.lastImagesDir(QFileInfo(fileName).dir().absolutePath()); - - if (displayImage(fileName)) - ui->SplashFileName->setText(fileName); + + displayImage(fileName); + ui->SplashFileName->setText(fileName); } } diff --git a/companion/src/apppreferencesdialog.ui b/companion/src/apppreferencesdialog.ui index 9a7680a42..c15874915 100644 --- a/companion/src/apppreferencesdialog.ui +++ b/companion/src/apppreferencesdialog.ui @@ -7,7 +7,7 @@ 0 0 685 - 462 + 446 @@ -839,16 +839,6 @@ This is used by the templated to determine which channel goes to what number out - - - - - 0 - 0 - - - - @@ -872,7 +862,35 @@ This is used by the templated to determine which channel goes to what number out - + + + + Firmware + + + + + + + false + + + + + + + Select Image + + + + + + + Clear Image + + + + false @@ -885,17 +903,13 @@ This is used by the templated to determine which channel goes to what number out - - - - Firmware - - - - - - - false + + + + + 0 + 0 + @@ -933,20 +947,6 @@ This is used by the templated to determine which channel goes to what number out - - - - Select Image - - - - - - - Clear Image - - - diff --git a/companion/src/mainwindow.cpp b/companion/src/mainwindow.cpp index 8d395e570..8918796be 100644 --- a/companion/src/mainwindow.cpp +++ b/companion/src/mainwindow.cpp @@ -102,12 +102,6 @@ MainWindow::MainWindow(): restoreState(glob.mainWindowState()); - if (glob.profileId() == 0) - { - createProfile(); - glob.profileId( 1 ); - } - setUnifiedTitleAndToolBarOnMac(true); this->setWindowIcon(QIcon(":/icon.png")); this->setIconSize(QSize(32,32)); @@ -708,42 +702,33 @@ void MainWindow::openRecentFile() } } -void MainWindow::loadProfile() +void MainWindow::loadProfile() //TODO Load all variables - Also HW! { - QSettings settings; QAction *action = qobject_cast(sender()); if (action) { + // Set the new profile number int profnum=action->data().toInt(); - QSettings settings; - settings.setValue("profileId",profnum); - settings.beginGroup("Profiles"); - QString profile=QString("profile%1").arg(profnum); - settings.beginGroup(profile); - QString profileName=settings.value("Name", "").toString(); - int chord=settings.value("default_channel_order", 0).toInt(); - int defmod=settings.value("default_mode", 0).toInt(); - bool burnfw=settings.value("burnFirmware", false).toBool(); - QString sdPath=settings.value("sdPath", ".").toString(); - bool renfw=settings.value("rename_firmware_files", false).toBool(); - QString SplashFileName=settings.value("SplashFileName","").toString(); - QString SplashImage=settings.value("SplashImage", "").toString(); - QString firmware_id=settings.value("firmware", default_firmware_variant.id).toString(); - firmware_id.replace("open9x","opentx"); - firmware_id.replace("x9da","taranis"); - settings.setValue("firmware", firmware_id); - settings.endGroup(); - settings.endGroup(); - settings.setValue("Name", profileName ); - settings.setValue("default_channel_order", chord); - settings.setValue("default_mode", defmod); - settings.setValue("burnFirmware", burnfw); - settings.setValue("rename_firmware_files", renfw); - settings.setValue("sdPath", sdPath); - settings.setValue("SplashFileName", SplashFileName); - settings.setValue("SplashImage", SplashImage); - settings.setValue("firmware", firmware_id); - current_firmware_variant = GetFirmwareVariant(firmware_id); + glob.profileId( profnum ); + + // Upgrade old firmware names + QString firmware = glob.profile[profnum].firmware(); + firmware.replace("open9x","opentx"); + firmware.replace("x9da","taranis"); + glob.profile[profnum].firmware(firmware); + + // Copy profile data from profile to main variables + glob.Name( glob.profile[profnum].Name() ); + glob.default_channel_order( glob.profile[profnum].default_channel_order() ); + glob.default_mode( glob.profile[profnum].default_mode() ); + glob.burnFirmware( glob.profile[profnum].burnFirmware() ); + glob.rename_firmware_files( glob.profile[profnum].rename_firmware_files() ); + glob.sdPath( glob.profile[profnum].sdPath() ); + glob.SplashFileName( glob.profile[profnum].SplashFileName() ); + glob.firmware( glob.profile[profnum].firmware() ); + + // TODO Get rid of this global variable - glob.firmware is the real source + current_firmware_variant = GetFirmwareVariant(glob.firmware()); foreach (QMdiSubWindow *window, mdiArea->subWindowList()) { MdiChild *mdiChild = qobject_cast(window->widget()); @@ -1091,7 +1076,6 @@ void MainWindow::readEeprom() void MainWindow::writeFileToEeprom() { - QSettings settings; QString fileName; bool backup = false; burnDialog *cd = new burnDialog(this, 1, &fileName, &backup); @@ -1559,7 +1543,7 @@ void MainWindow::updateMenus() updateIconSizeActions(); updateIconThemeActions(); - setWindowTitle(tr("OpenTX Companion - FW: %1 - Profile: %2").arg(GetCurrentFirmware()->name).arg( QString("%1").arg(glob.profileId()) )); + setWindowTitle(tr("OpenTX Companion - FW: %1 - Profile: %2").arg(GetCurrentFirmware()->name).arg( glob.Name() )); } MdiChild *MainWindow::createMdiChild() @@ -1802,8 +1786,8 @@ QMenu *MainWindow::createProfilesMenu() for ( i = 0; i < MAX_PROFILES; ++i) { profilesMenu->addAction(profileActs[i]); } - if ( i>0 ) - profilesMenu->addSeparator(); + profilesMenu->addSeparator(); + profilesMenu->addAction(createProfileAct); profilesMenu->setIcon(CompanionIcon("profiles.png")); return profilesMenu; @@ -1996,52 +1980,34 @@ void MainWindow::updateIconThemeActions() void MainWindow::updateProfilesActions() { - int i; - QSettings settings; - int activeProfile = glob.profileId(); - - settings.beginGroup("Profiles"); - for (i=0; isetText(text); - profileActs[i]->setData(i+1); + profileActs[i]->setData(i); profileActs[i]->setVisible(true); - if ((i+1) == activeProfile) + if (i == glob.profileId()) profileActs[i]->setChecked(true); } - else { + else + { profileActs[i]->setVisible(false); } - settings.endGroup(); } } void MainWindow::createProfile() -{ - int firstFreeIndex = 0; - QSettings settings; - settings.beginGroup("Profiles"); - for (int i=0; firstFreeIndex ==0 && i