Profiles working.

Next all duplicated variables will be removed from global and only exists in profile.
This commit is contained in:
Kjell Kernen
2014-02-17 15:28:50 +01:00
parent fdc9873ab8
commit 2633b61433
4 changed files with 173 additions and 181 deletions
+68 -16
View File
@@ -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; i<MAX_PROFILES; i++)
profile[i].init( i+1 );
profile[i].init( i );
@@ -479,8 +532,7 @@ public:
getset( _firmware, "firmware" ,"" );
getset( _locale, "locale" ,"" );
getset( _cpu_id, "cpu_id" ,"" );
getset( _SplashImage, "SplashImage" ,"" );
getset( _Name, "Name" ,"profile1" );
getset( _Name, "Name" ,"----" );
getset( _SplashFileName, "SplashFileName" ,"" );
getset( _modelEditGeometry, "modelEditGeometry" ,"" );
@@ -516,7 +568,7 @@ public:
getset( _js_ctrl, "js_ctrl" ,0 );
getset( _history_size, "history_size" ,10 );
getset( _modelEditTab, "modelEditTab" ,0 );
getset( _profileId, "profileId" ,1 );
getset( _profileId, "profileId" ,0 );
getset( _theme, "theme" ,1 );
getset( _warningId, "warningId" ,0 );
}
+26 -52
View File
@@ -64,8 +64,6 @@ void appPreferencesDialog::writeValues()
glob.Name(ui->profileNameLE->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);
}
}
+37 -37
View File
@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>685</width>
<height>462</height>
<height>446</height>
</rect>
</property>
<property name="sizePolicy">
@@ -839,16 +839,6 @@ This is used by the templated to determine which channel goes to what number out
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QLineEdit" name="profileNameLE">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</widget>
</item>
<item row="9" column="1" colspan="6">
<widget class="Line" name="line_6">
<property name="orientation">
@@ -872,7 +862,35 @@ This is used by the templated to determine which channel goes to what number out
</property>
</widget>
</item>
<item row="0" column="1">
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Firmware</string>
</property>
</widget>
</item>
<item row="1" column="1" colspan="2">
<widget class="QLineEdit" name="firmwareLE">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item row="4" column="6">
<widget class="QPushButton" name="SplashSelect">
<property name="text">
<string>Select Image</string>
</property>
</widget>
</item>
<item row="7" column="6">
<widget class="QPushButton" name="clearImageButton">
<property name="text">
<string>Clear Image</string>
</property>
</widget>
</item>
<item row="0" column="6">
<widget class="QLineEdit" name="profileIndexLE">
<property name="enabled">
<bool>false</bool>
@@ -885,17 +903,13 @@ This is used by the templated to determine which channel goes to what number out
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Firmware</string>
</property>
</widget>
</item>
<item row="1" column="1" colspan="2">
<widget class="QLineEdit" name="firmwareLE">
<property name="enabled">
<bool>false</bool>
<item row="0" column="1">
<widget class="QLineEdit" name="profileNameLE">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</widget>
</item>
@@ -933,20 +947,6 @@ This is used by the templated to determine which channel goes to what number out
</property>
</widget>
</item>
<item row="4" column="6">
<widget class="QPushButton" name="SplashSelect">
<property name="text">
<string>Select Image</string>
</property>
</widget>
</item>
<item row="7" column="6">
<widget class="QPushButton" name="clearImageButton">
<property name="text">
<string>Clear Image</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
+42 -76
View File
@@ -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<QAction *>(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<MdiChild *>(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; i<MAX_PROFILES; i++) {
QString profile=QString("profile%1").arg(i+1);
settings.beginGroup(profile);
QString name=settings.value("Name","").toString();
if (!name.isEmpty()) {
QString text = tr("&%1 %2").arg(i + 1).arg(name);
for (int i=0; i<MAX_PROFILES; i++)
{
if (!glob.profile[i].Name().isEmpty())
{
QString text = tr("&%1: %2").arg(i).arg(glob.profile[i].Name());
profileActs[i]->setText(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<MAX_PROFILES; i++) {
QString profile=QString("profile%1").arg(i+1);
settings.beginGroup(profile);
QString name=settings.value("Name","").toString();
if (name.isEmpty())
firstFreeIndex = i+1;
settings.endGroup();
}
settings.endGroup();
if (firstFreeIndex == 0) // Could not find free index
{ int i;
for (i=0; i<MAX_PROFILES && !glob.profile[i].Name().isEmpty(); i++)
;
if (i==MAX_PROFILES) //Failed to find free slot
return;
settings.beginGroup("Profiles");
settings.beginGroup(QString("profile%1").arg(firstFreeIndex));
settings.setValue("Name",QString("profile%1").arg(firstFreeIndex));
settings.endGroup();
settings.endGroup();
// Create profile name and force a flush to file
glob.profile[i].Name( QString("profile%1").arg(i));
glob.profile[i].flush();
updateMenus();
}