Commit 94f11c81 authored by Stefan Tauner's avatar Stefan Tauner
Browse files

fiji_scripts: fix .ini open and save and load defaults for FIUs

parent 5e160122
......@@ -8,6 +8,7 @@ package FIJI::Settings;
use strict;
use warnings;
use Scalar::Util 'blessed';
use Log::Log4perl qw(get_logger);
use Scalar::Util "looks_like_number";
use Config::Simple;
......@@ -115,7 +116,6 @@ sub read_settingsfile {
return undef;
}
my $fiji_settings = (defined($existing_settings)) ? $existing_settings : {};
my $fiji_consts = $fiji_ini->get_block("CONSTS");
if (!(%$fiji_consts)) {
$logger->error("Could not fetch CONSTS block from config file \"$fiji_ini_file\"");
......@@ -126,7 +126,19 @@ sub read_settingsfile {
$logger->error("Design constants in FIJI Settings invalid");
return undef;
}
$fiji_settings->{'design'} = $fiji_consts;
my $fiji_settings_ref;
if (!defined($existing_settings)) {
$fiji_settings_ref = \{};
} else {
$fiji_settings_ref = $existing_settings;
if (!blessed($fiji_settings_ref) || !$fiji_settings_ref->isa("FIJI::Settings")) {
$logger->error("Given settings are not of type FIJI::Settings.");
return undef;
}
$fiji_settings_ref->{'design'} = {};
$fiji_settings_ref->{'FIUs'} = ();
}
$fiji_settings_ref->{'design'} = $fiji_consts;
my $fiu_num = 0;
while (1) {
......@@ -140,12 +152,13 @@ sub read_settingsfile {
$logger->error("Constants for $fiu_name in FIJI Settings are invalid");
return undef;
}
push(@{$fiji_settings->{'FIUs'}}, $fiji_fiu);
push(@{$fiji_settings_ref->{'FIUs'}}, $fiji_fiu);
$fiu_num++;
$logger->trace("Read in $fiu_name from FIJI Settings file successfully.");
}
if ($fiu_num == 0) {
# FIXME: is this always an error?
$logger->error("Could not fetch any FIU block from config file \"$fiji_ini_file\"");
return undef;
}
......@@ -160,7 +173,31 @@ sub read_settingsfile {
}
$logger->info("Successfully read in design constants and $fiu_num FIU definitions from FIJI Settings file.");
return $fiji_settings;
return $fiji_settings_ref;
}
## @function set_fiu_defaults (%$fiu_ref)
# @brief Overwrite existing fields (if defined) with defaults defined in FIJI.pm.
#
# FIXME: almost 1:1 copy from _set_defaults
sub set_fiu_defaults ($) {
my $logger = get_logger();
my ($self, $consts_ref) = @_;
my $map_ref = FIUMAP;
foreach my $k (keys($map_ref)) {
my $ini_name = $map_ref->{$k}->{'ini_name'};
if (exists($map_ref->{$k}->{'default'})) {
$consts_ref->{$k} = $map_ref->{$k}->{default};
# If the default key is there but its value is undef then
# the value will be set somewhere else later (used for e.g. FIU_NUM)
if (!defined($consts_ref->{$k})) {
next;
}
$logger->trace(sprintf("Adding default constant: %s (%s) = %s.", $k, $ini_name, $map_ref->{$k}->{default}));
} elsif ($map_ref->{$k}->{'not_supplied'}) {
$consts_ref->{$k} = undef;
}
}
}
## @function _set_defaults (%$map_ref, %$consts_ref)
......@@ -186,7 +223,7 @@ sub _set_defaults {
if (exists($map_ref->{$k}->{'default'})) {
$consts_ref->{$k} = $map_ref->{$k}->{default};
# If the default key is there but its value is undef then
# the value will be set somewhere else later (used for FIU_NUM)
# the value will be set somewhere else later (used for e.g. FIU_NUM)
if (!defined($consts_ref->{$k})) {
next;
}
......
......@@ -38,7 +38,7 @@ sub Populate {
my $logger = get_logger();
my($self, $args) = @_;
my $settings = delete $args->{'-settings'};
if (!blessed(${$settings}) || !${$settings}->isa("FIJI::Settings")) {
if (!defined($settings) || ref($settings) ne 'REF' || !blessed(${$settings}) || !${$settings}->isa("FIJI::Settings")) {
$logger->error("Given settings are not of type FIJI::Settings. No way to report this back from the constructor...");
} else {
$self->{'settings'} = $settings;
......@@ -107,8 +107,8 @@ sub update {
my $fiu_cnt = $self->_fiu_cnt();
for (my $i = 0; $i < $fiu_cnt; $i++) {
my $fiu = @{${$self->{'settings'}}->{'FIUs'}}[$i];
$self->_add_fiu($fiu, $i);
my $fiu_ref = @{${$self->{'settings'}}->{'FIUs'}}[$i];
$self->_add_fiu($fiu_ref, $i);
}
}
......@@ -244,7 +244,9 @@ sub _add_fiu ($$) {
$i = $self->_fiu_cnt(); # default is to append
}
if (!defined($fiu)) {
$fiu = {}; # default is to add a new/empty FIU
my %tmp_hash = (); # default is to add a new/empty FIU
$fiu = \%tmp_hash;
${$self->{'settings'}}->set_fiu_defaults($fiu);
push(${$self->{'settings'}}->{'FIUs'}, $fiu);
}
Tk::grid(
......
......@@ -35,10 +35,10 @@ sub main {
# $hash{'cfg'} = $cfg;
my $ini_name = $cfg{"cli.fiji_cfg"};
$hash{'ini_name'} = $ini_name;
my $settings = FIJI::Settings->new();
return 1 if !defined($settings);
$hash{'settings'} = \$settings;
$self->{'ini_name'} = \$ini_name;
my $settings_ref = FIJI::Settings->new();
return 1 if !defined($settings_ref);
$self->{'settings'} = $settings_ref;
my $mw = MainWindow->new(
-title => 'FIJI Settings Editor'
......@@ -47,8 +47,8 @@ sub main {
_ctrl_frame($self, $mw);
$mw->FIJISettingsViewer(
-settings => $self->{'settings'},
$self->{'FIJISettingsViewer'} = $mw->FIJISettingsViewer(
-settings => \$self->{'settings'},
)->pack(
'-fill' => 'both',
'-expand' => 1
......@@ -109,18 +109,20 @@ sub _ctrl_frame {
$logger->debug("User aborted open action");
return;
}
$self->{'settings'} = FIJI::Settings->new($filename, $self->{'settings'});
if (!defined($self->{'settings'})) {
my $tmp_settings = FIJI::Settings->new($filename, $self->{'settings'});
if (!defined($tmp_settings)) {
my $msg = "Settings file $filename could not be loaded correctly";
$logger->error($msg);
$fr->Dialog(
-title => 'Open failed!',
-text => "$msg!\n",
)->Show();
$self->{'ini_name'} = undef;
# ${$self->{'ini_name'}} = undef; leave old file name alone
return;
}
$self->{'ini_name'} = \$filename;
$self->{'settings'} = $tmp_settings;
${$self->{'ini_name'}} = $filename;
$self->{'FIJISettingsViewer'}->update();
# my $state = defined($filename) ? 'normal' : 'disabled';
# $btn_open->configure(-state => $state);
# $btn_save->configure(-state => $state);
......@@ -157,16 +159,20 @@ sub _ctrl_frame {
sub _save_file {
my ($self, $filename) = @_;
my $err = ${$self->{'settings'}}->save($filename);
my $logger = get_logger();
my $err = $self->{'settings'}->save($filename);
if (defined($err)) {
my $msg = "Saving to file $filename failed!\n$err";
$logger->error($msg);
$self->{'mw'}->Dialog(
-title => 'Save failed!',
-text => "Saving to file $filename failed!\n$err\n",
-text => "$msg\n",
)->Show();
return;
}
$self->{'ini_name'} = \$filename;
${$self->{'ini_name'}} = $filename;
$logger->info("Successfully saved to file $filename.");
}
......@@ -175,7 +181,7 @@ sub _save {
if (!defined($self->{'ini_name'})) {
return $self->_save_as();
} else {
return $self->_save_file($self->{'ini_name'});
return $self->_save_file(${$self->{'ini_name'}});
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment