Tie all the pieces together and make Preferences work as intended

Not Apply / Close without Saving / OK work as designed. And things get
correctly stored and reset.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2013-06-03 21:08:49 +09:00
parent 77c4b8ef87
commit b38e2ab485
5 changed files with 91 additions and 74 deletions

1
main.c
View file

@ -15,6 +15,7 @@
struct preferences prefs; struct preferences prefs;
struct preferences default_prefs = { struct preferences default_prefs = {
.units = SI_UNITS, .units = SI_UNITS,
.unit_system = METRIC,
.visible_cols = { TRUE, FALSE, }, .visible_cols = { TRUE, FALSE, },
.pp_graphs = { .pp_graphs = {
.po2 = FALSE, .po2 = FALSE,

2
pref.h
View file

@ -45,8 +45,10 @@ struct preferences {
int map_provider; int map_provider;
short display_invalid_dives; short display_invalid_dives;
short show_invalid; short show_invalid;
short unit_system;
struct units units; struct units units;
}; };
enum unit_system_values { METRIC, IMPERIAL, PERSONALIZE };
extern struct preferences prefs, default_prefs; extern struct preferences prefs, default_prefs;

View file

@ -379,10 +379,13 @@ void MainWindow::readSettings()
settings.endGroup(); settings.endGroup();
settings.beginGroup("Units"); settings.beginGroup("Units");
if (settings.value("unit_system").toString() == "metric") { if (settings.value("unit_system").toString() == "metric") {
prefs.unit_system = METRIC;
prefs.units = SI_units; prefs.units = SI_units;
} else if (settings.value("unit_system").toString() == "imperial") { } else if (settings.value("unit_system").toString() == "imperial") {
prefs.unit_system = IMPERIAL;
prefs.units = IMPERIAL_units; prefs.units = IMPERIAL_units;
} else { } else {
prefs.unit_system = PERSONALIZE;
GET_UNIT(v, "length", length, units::FEET, units::METERS); GET_UNIT(v, "length", length, units::FEET, units::METERS);
GET_UNIT(v, "pressure", pressure, units::PSI, units::BAR); GET_UNIT(v, "pressure", pressure, units::PSI, units::BAR);
GET_UNIT(v, "volume", volume, units::CUFT, units::LITER); GET_UNIT(v, "volume", volume, units::CUFT, units::LITER);

View file

@ -9,103 +9,112 @@ PreferencesDialog* PreferencesDialog::instance()
return dialog; return dialog;
} }
#define B(V, P) s.value(#V, default_prefs.P).toBool()
#define D(V, P) s.value(#V, default_prefs.P).toDouble()
#define I(V, P) s.value(#V, default_prefs.P).toInt()
PreferencesDialog::PreferencesDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(parent, f), PreferencesDialog::PreferencesDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(parent, f),
ui(new Ui::PreferencesDialog()) ui(new Ui::PreferencesDialog())
{ {
ui->setupUi(this); ui->setupUi(this);
connect(ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(buttonClicked(QAbstractButton*))); connect(ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(buttonClicked(QAbstractButton*)));
reloadPrefs(); setUiFromPrefs();
rememberPrefs();
} }
void PreferencesDialog::showEvent(QShowEvent *event) void PreferencesDialog::showEvent(QShowEvent *event)
{ {
reloadPrefs(); setUiFromPrefs();
rememberPrefs();
QDialog::showEvent(event); QDialog::showEvent(event);
} }
void PreferencesDialog::reloadPrefs() void PreferencesDialog::setUiFromPrefs()
{ {
// graphs
oldPrefs = prefs; ui->phe->setChecked(prefs.pp_graphs.phe);
QSettings s;
// Graph
s.beginGroup("TecDetails");
ui->phe->setChecked(B(phegraph, pp_graphs.phe));
ui->pheThreshold->setEnabled(ui->phe->isChecked()); ui->pheThreshold->setEnabled(ui->phe->isChecked());
ui->po2->setChecked(B(po2graph, pp_graphs.po2)); ui->po2->setChecked(prefs.pp_graphs.po2);
ui->po2Threshold->setEnabled(ui->po2->isChecked()); ui->po2Threshold->setEnabled(ui->po2->isChecked());
ui->pn2->setChecked(B(pn2graph, pp_graphs.pn2)); ui->pn2->setChecked(prefs.pp_graphs.pn2);
ui->pn2Threshold->setEnabled(ui->pn2->isChecked()); ui->pn2Threshold->setEnabled(ui->pn2->isChecked());
ui->pheThreshold->setValue(D(phethreshold, pp_graphs.phe_threshold)); ui->pheThreshold->setValue(prefs.pp_graphs.phe_threshold);
ui->po2Threshold->setValue(D(po2threshold, pp_graphs.po2_threshold)); ui->po2Threshold->setValue(prefs.pp_graphs.po2_threshold);
ui->pn2Threshold->setValue(D(pn2threshold, pp_graphs.pn2_threshold)); ui->pn2Threshold->setValue(prefs.pp_graphs.pn2_threshold);
ui->ead_end_eadd->setChecked(B(ead, ead)); ui->ead_end_eadd->setChecked(prefs.ead);
ui->dc_reported_ceiling->setChecked(B(dcceiling, profile_dc_ceiling)); ui->dc_reported_ceiling->setChecked(prefs.profile_dc_ceiling);
ui->red_ceiling->setEnabled(ui->dc_reported_ceiling->isChecked()); ui->red_ceiling->setEnabled(ui->dc_reported_ceiling->isChecked());
ui->red_ceiling->setChecked(B(redceiling, profile_red_ceiling)); ui->red_ceiling->setChecked(prefs.profile_red_ceiling);
ui->calculated_ceiling->setChecked(B(calcceiling, profile_calc_ceiling)); ui->calculated_ceiling->setChecked(prefs.profile_calc_ceiling);
ui->increment_3m->setEnabled(ui->calculated_ceiling->isChecked()); ui->increment_3m->setEnabled(ui->calculated_ceiling->isChecked());
ui->increment_3m->setChecked(B(calcceiling3m, calc_ceiling_3m_incr)); ui->increment_3m->setChecked(prefs.calc_ceiling_3m_incr);
ui->all_tissues->setEnabled(ui->calculated_ceiling->isChecked()); ui->all_tissues->setEnabled(ui->calculated_ceiling->isChecked());
ui->all_tissues->setChecked(B(calcalltissues, calc_all_tissues)); ui->all_tissues->setChecked(prefs.calc_all_tissues);
ui->groupBox->setEnabled(ui->personalize->isChecked()); ui->groupBox->setEnabled(ui->personalize->isChecked());
ui->gflow->setValue((int)(I(gflow, gflow))); ui->gflow->setValue(prefs.gflow);
ui->gfhigh->setValue((int)(I(gfhigh, gfhigh))); ui->gfhigh->setValue(prefs.gfhigh);
s.endGroup();
// Units // units
s.beginGroup("Units"); if (prefs.unit_system == METRIC)
bool value = s.value("units_metric").toBool(); ui->metric->setChecked(true);
ui->metric->setChecked(value); else if (prefs.unit_system == IMPERIAL)
ui->imperial->setChecked(!value); ui->imperial->setChecked(true);
else
ui->personalize->setChecked(true);
int unit = s.value("temperature").toInt(); ui->celsius->setChecked(prefs.units.temperature == units::CELSIUS);
ui->celsius->setChecked(unit == units::CELSIUS); ui->fahrenheit->setChecked(prefs.units.temperature == units::FAHRENHEIT);
ui->fahrenheit->setChecked(unit == units::FAHRENHEIT); ui->meter->setChecked(prefs.units.length == units::METERS);
ui->feet->setChecked(prefs.units.length == units::FEET);
unit = s.value("length").toInt(); ui->bar->setChecked(prefs.units.pressure == units::BAR);
ui->meter->setChecked(unit == units::METERS); ui->psi->setChecked(prefs.units.pressure == units::PSI);
ui->feet->setChecked(unit == units::FEET); ui->liter->setChecked(prefs.units.volume == units::LITER);
ui->cuft->setChecked(prefs.units.volume == units::CUFT);
unit = s.value("pressure").toInt(); ui->kgs->setChecked(prefs.units.weight == units::KG);
ui->bar->setChecked(unit == units::BAR); ui->lbs->setChecked(prefs.units.weight == units::LBS);
ui->psi->setChecked(unit == units::PSI); ui->font->setFont(QString(prefs.divelist_font));
ui->fontsize->setValue(prefs.font_size);
unit = s.value("volume").toInt(); ui->defaultfilename->setText(prefs.default_filename);
ui->liter->setChecked(unit == units::LITER); ui->displayinvalid->setChecked(prefs.show_invalid);
ui->cuft->setChecked(unit == units::CUFT);
unit = s.value("weight").toInt();
ui->kgs->setChecked(unit == units::KG);
ui->lbs->setChecked(unit == units::LBS);
s.endGroup();
// Defaults
s.beginGroup("GeneralSettings");
ui->font->setFont( QFont(s.value("table_fonts").toString()));
ui->fontsize->setValue(D(font_size, font_size));
ui->defaultfilename->setText(s.value("default_filename").toString());
ui->displayinvalid->setChecked(B(show_invalid, show_invalid));
s.endGroup();
} }
#undef B void PreferencesDialog::restorePrefs()
#undef D
void PreferencesDialog::resetSettings()
{ {
prefs = oldPrefs; prefs = oldPrefs;
} }
void PreferencesDialog::rememberPrefs()
{
oldPrefs = prefs;
}
#define SP(V, B) prefs.V = (int)(B->isChecked() ? 1 : 0)
void PreferencesDialog::setPrefsFromUi()
{
SP(pp_graphs.phe, ui->phe);
SP(pp_graphs.po2, ui->po2);
SP(pp_graphs.pn2, ui->pn2);
prefs.pp_graphs.phe_threshold = ui->pheThreshold->value();
prefs.pp_graphs.po2_threshold = ui->po2Threshold->value();
prefs.pp_graphs.pn2_threshold = ui->pn2Threshold->value();
SP(ead, ui->ead_end_eadd);
SP(profile_dc_ceiling, ui->dc_reported_ceiling);
SP(profile_red_ceiling, ui->red_ceiling);
SP(profile_calc_ceiling, ui->calculated_ceiling);
SP(calc_ceiling_3m_incr, ui->increment_3m);
SP(calc_all_tissues, ui->all_tissues);
prefs.gflow = ui->gflow->value();
prefs.gfhigh = ui->gfhigh->value();
prefs.unit_system = ui->metric->isChecked() ? METRIC : (ui->imperial->isChecked() ? IMPERIAL : PERSONALIZE);
prefs.units.temperature = ui->fahrenheit->isChecked() ? units::FAHRENHEIT : units::CELSIUS;
prefs.units.length = ui->feet->isChecked() ? units::FEET : units::METERS;
prefs.units.pressure = ui->psi->isChecked() ? units::PSI : units::BAR;
prefs.units.volume = ui->cuft->isChecked() ? units::CUFT : units::LITER;
prefs.units.weight = ui->lbs->isChecked() ? units::LBS : units::KG;
prefs.divelist_font = strdup(ui->font->font().family().toUtf8().data());
prefs.font_size = ui->fontsize->value();
prefs.default_filename = strdup(ui->defaultfilename->text().toUtf8().data());
prefs.display_invalid_dives = ui->displayinvalid->isChecked();
}
#define SB(V, B) s.setValue(V, (int)(B->isChecked() ? 1 : 0)) #define SB(V, B) s.setValue(V, (int)(B->isChecked() ? 1 : 0))
void PreferencesDialog::syncSettings() void PreferencesDialog::syncSettings()
@ -157,17 +166,16 @@ void PreferencesDialog::buttonClicked(QAbstractButton* button)
{ {
switch(ui->buttonBox->standardButton(button)){ switch(ui->buttonBox->standardButton(button)){
case QDialogButtonBox::Discard: case QDialogButtonBox::Discard:
prefs = oldPrefs; restorePrefs();
setUiFromPrefs();
syncSettings();
close(); close();
break; break;
case QDialogButtonBox::Apply: case QDialogButtonBox::Apply:
syncSettings(); syncSettings();
emit settingsChanged();
break; break;
case QDialogButtonBox::FirstButton: case QDialogButtonBox::FirstButton:
syncSettings(); syncSettings();
oldPrefs = prefs;
emit settingsChanged();
close(); close();
break; break;
default: default:

View file

@ -20,11 +20,14 @@ signals:
public slots: public slots:
void buttonClicked(QAbstractButton* button); void buttonClicked(QAbstractButton* button);
void syncSettings(); void syncSettings();
void resetSettings(); void restorePrefs();
void rememberPrefs();
private: private:
explicit PreferencesDialog(QWidget* parent = 0, Qt::WindowFlags f = 0); explicit PreferencesDialog(QWidget* parent = 0, Qt::WindowFlags f = 0);
void reloadPrefs(); void setUiFromPrefs();
void setPrefsFromUi();
void setUIFromSettings();
Ui::PreferencesDialog* ui; Ui::PreferencesDialog* ui;
struct preferences oldPrefs; struct preferences oldPrefs;
}; };