File: Preferences.pm

package info (click to toggle)
auto-multiple-choice 1.5.0~rc2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 56,204 kB
  • sloc: perl: 27,850; xml: 23,201; cpp: 1,810; python: 700; makefile: 496; sh: 217; ansic: 195
file content (597 lines) | stat: -rw-r--r-- 18,227 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# -*- perl -*-
#
# Copyright (C) 2020-2021 Alexis Bienvenüe <paamc@passoire.fr>
#
# This file is part of Auto-Multiple-Choice
#
# Auto-Multiple-Choice is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 2 of
# the License, or (at your option) any later version.
#
# Auto-Multiple-Choice is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Auto-Multiple-Choice.  If not, see
# <http://www.gnu.org/licenses/>.

use warnings;
use 5.012;

package AMC::Gui::Preferences;

use parent 'AMC::Gui';

use AMC::Basic;

use Module::Load;
use Module::Load::Conditional qw/check_install/;

my @widgets_disabled_when_preferences_opened = (qw/menu_popover/);

sub new {
    my ( $class, %oo ) = @_;

    my $self = $class->SUPER::new(%oo);
    bless( $self, $class );

    $self->merge_config(
        {
            open_project_name        => '',
            widgets                  => '',
            capture                  => '',
            callback_self            => '',
            decode_callback          => '',
            detect_analysis_callback => '',
        },
        %oo
    );

    $self->stores();
    $self->dialog();

    return $self;
}

sub printing_methods {
    my ($self) = @_;

    my @printing_methods = ();
    for my $m (
        { name => "CUPS",   description => "CUPS" },
        { name => "CUPSlp", description => "CUPS (via lp)" },
      )
    {
        my $mod = "AMC::Print::" . lc( $m->{name} );
        load($mod);
        my $error = $mod->check_available();
        if ( !$error ) {
            push @printing_methods, $m->{name}, $m->{description};
            if ( !$self->get("methode_impression") ) {
                $self->set( "global:methode_impression", $m->{name} );
                debug "Switching to printing method <$m->{name}>.";
            }
        } else {
            if ( $self->get("methode_impression") eq $m->{name} ) {
                $self->set( "global:methode_impression", '' );
                debug "Printing method <$m->{name}> is not available: $error";
            }
        }
    }
    if ( !$self->get("methode_impression") ) {
        $self->set( "global:methode_impression", 'commande' );
        debug "Switching to printing method <commande>.";
    }

    push @printing_methods,
        'commande', __(
            # TRANSLATORS: One of the printing methods: use a command
            # (This is not the command name itself). This is a menu
            # entry.
            "command"),
        'file', __(
            # TRANSLATORS: One of the printing methods: print to
            # files. This is a menu entry.
            "to files");

    return (@printing_methods);
}

sub stores {
    my ($self) = @_;

    # Reads decoders list and build ComboBox model.

    my @decoders = perl_module_search('AMC::Decoder::register');
    for my $m (@decoders) {
        load("AMC::Decoder::register::$m");
    }
    @decoders = sort {
        "AMC::Decoder::register::$a"->weight <=>
          "AMC::Decoder::register::$b"->weight
    } @decoders;

    my $nftype_store = cb_model(
        '' => __ "Image",
        map { $_ => "AMC::Decoder::register::$_"->name() } (@decoders)
    );

    my $rounding_store = cb_model(
        'inf',
        __(
            # TRANSLATORS: One of the rounding method for marks. This
            # is a menu entry.
            "floor"
        ),
        'normal',
        __(
            # TRANSLATORS: One of the rounding method for marks. This
            # is a menu entry.
            "rounding"
        ),
        'sup',
        __(
            # TRANSLATORS: One of the rounding method for marks. This
            # is a menu entry.
            "ceiling"
        )
    );

    $self->store_register(
        encodage_latex => cb_model(
            map { $_->{iso} => $_->{txt} } (AMC::Encodings::encodings())
        ),

        delimiteur_decimal => cb_model(
            ',',
            __(
                # TRANSLATORS: One option for decimal point: use a
                # comma. This is a menu entry
                ", (comma)"
            ),

            '.',
            __(
                # TRANSLATORS: One option for decimal point: use a
                # point. This is a menu entry.
                ". (dot)"
            )
          ),
        methode_impression => cb_model( $self->printing_methods ),
        print_extract_with => cb_model(
            pdftk           => 'pdftk',
            gs              => 'gs (ghostscript)',
            qpdf            => 'qpdf',
            'sejda-console' => 'sejda-console',
        ),

        manuel_image_type => cb_model(
            ppm =>
            __p(
                # TRANSLATORS: you can omit the [...] part, just here to
                # explain context
                "(none) [No transitional image type (direct processing)]"),
            xpm => 'XPM',
            gif => 'GIF'
        ),
        defaut_name_field_type => $nftype_store,
        name_field_type        => $nftype_store,
        note_arrondi           => $rounding_store,
        defaut_note_arrondi    => $rounding_store,
        embedded_format        => cb_model(
            png  => 'PNG',
            jpeg => 'JPEG'
        ),
        email_transport => cb_model(
            sendmail => __(
                # TRANSLATORS: One of the ways to send mail: use sendmail
                # command. This is a menu entry.
                "sendmail"
            ),

            SMTP => __(
                # TRANSLATORS: One of the ways to send mail: use a
                # SMTP server. This is a menu entry.
                "SMTP"
            )
          ),

        email_smtp_ssl => cb_model(
            0        => __p(
                # TRANSLATORS: SMTP security mode: None (nor SSL nor
                # STARTTLS)
                "None [SMTP security]"),
            1        => 'SSL',
            starttls => 'STARTTLS'
        ),

        annote_position => cb_model(
            none =>
            __p(
                # TRANSLATORS: you can omit the [...] part, just here to
                # explain context
                "(none) [No annotation position (do not write anything)]"),

            marge => __(
                # TRANSLATORS: One of the possible location for
                # questions scores on annotated completed answer
                # sheet: in one margin. This is a menu entry.
                "in one margin"),

            marges => __(
                # TRANSLATORS: One of the possible location for
                # questions scores on annotated completed answer
                # sheet: in one of the two margins. This is a menu
                # entry.
                "in the margins"),

            case => __(
                # TRANSLATORS: One of the possible location for
                # questions scores on annotated completed answer
                # sheet: near the boxes. This is a menu entry.
                "near boxes"),

            zones => __(
                # TRANSLATORS: One of the possible location for
                # questions scores on annotated completed answer
                # sheet: in the zones defined in the source file
                "where defined in the source"),
        ),
    );

    my $symbole_type_cb = cb_model(
        none => __(
            # TRANSLATORS: One of the signs that can be drawn on
            # annotated answer sheets to tell if boxes are to be
            # ticked or not, and if they were detected as ticked or
            # not.
            "nothing"),

        circle => __(
            # TRANSLATORS: One of the signs that can be drawn on
            # annotated answer sheets to tell if boxes are to be
            # ticked or not, and if they were detected as ticked or
            # not.
            "circle"),

        mark => __(
            # TRANSLATORS: One of the signs that can be drawn on
            # annotated answer sheets to tell if boxes are to be
            # ticked or not, and if they were detected as ticked or
            # not. Here, a cross.
            "mark"),

        box => __(
            # TRANSLATORS: One of the signs that can be drawn on
            # annotated answer sheets to tell if boxes are to be
            # ticked or not, and if they were detected as ticked or
            # not. Here, the box outline.
            "box"),
    );

    for my $k (qw/0_0 0_1 1_0 1_1/) {
        $self->store_register( "symbole_" . $k . "_type" => $symbole_type_cb );
    }

}

sub dialog {
    my ($self) = @_;

    my $glade_xml = __FILE__;
    $glade_xml =~ s/\.p[ml]$/.glade/i;

    $self->dependent_widgets_sensitivity(0);

    $self->read_glade(
        $glade_xml, qw/edit_preferences
          pref_projet_tous pref_projet_annonce
          pref_x_print_command_pdf pref_c_methode_impression
          email_group_sendmail email_group_SMTP/
    );

    # copy tooltip from note_* to defaut_note_*
    my $marking_prefs = {
        min         => 'x',
        max         => 'x',
        null        => 'x',
        grain       => 'x',
        max_plafond => 'v',
        arrondi     => 'c'
    };
    for my $k ( keys %$marking_prefs ) {
        $self->{main}
          ->get_object( "pref_" . $marking_prefs->{$k} . "_defaut_note_$k" )
          ->set_tooltip_text(
            $self->{main}->get_object(
                "pref_projet_" . $marking_prefs->{$k} . "_note_$k"
            )->get_tooltip_text
          );
    }

    # tableau type/couleurs pour correction

    $self->{prefs}->widget_store_clear( store => 'prefwindow' );

    $self->{prefs}->transmet_pref(
        $self->{main},
        store  => 'prefwindow',
        prefix => 'pref',
        root   => 'global:'
    );

    if ( $self->{open_project_name} ) {
        $self->{prefs}->transmet_pref(
            $self->{main},
            store  => 'prefwindow_project',
            prefix => 'pref_projet',
            root   => 'project:'
        );
        $self->get_ui('pref_projet_annonce')->set_label(
            '<i>'
              . sprintf(
                __ "Project \"%s\" preferences",
                $self->{open_project_name}
              )
              . '</i>.'
        );
    } else {
        $self->get_ui('pref_projet_tous')->set_sensitive(0);
        $self->get_ui('pref_projet_annonce')
          ->set_label( '<i>' . __("Project preferences") . '</i>' );
    }

    # unavailable options, managed by the filter:
    if ( $self->get('filter') ) {
        for
          my $k ( ( "AMC::Filter::register::" . $self->get('filter') )
            ->forced_options() )
        {
          TYPES: for my $t (qw/c cb ce col f s t v x/) {
                if ( my $w =
                    $self->{main}->get_object( "pref_projet_" . $t . "_" . $k )
                  )
                {
                    $w->set_sensitive(0);
                    last TYPES;
                }
            }
        }
    }

    $self->change_methode_impression();

    my $resp = $self->get_ui('edit_preferences')->run();

    if ($resp) {
        $self->accept();
    } else {
        $self->cancel();
    }

}

sub change_methode_impression {
    my ($self) = @_;

    if ( $self->get_ui('pref_x_print_command_pdf') ) {
        my $m = '';
        my ( $ok, $iter ) =
          $self->get_ui('pref_c_methode_impression')->get_active_iter;
        if ($ok) {
            $m =
              $self->get_ui('pref_c_methode_impression')
              ->get_model->get( $iter, COMBO_ID );
        }
        $self->get_ui('pref_x_print_command_pdf')
          ->set_sensitive( $m eq 'commande' );
    }
}

sub change_delivery {
    my ($self) = @_;

    $self->set_local_keys('email_transport');
    $self->{prefs}->reprend_pref(
        store     => 'prefwindow',
        prefix    => 'pref',
        container => 'local'
    );
    my $transport = $self->get('local:email_transport');
    if ($transport) {
        for my $k (qw/sendmail SMTP/) {
            $self->get_ui( 'email_group_' . $k )
              ->set_sensitive( $k eq $transport );
        }
    } else {
        debug "WARNING: could not retrieve email_transport!";
    }
}

sub dependent_widgets_sensitivity {
    my ( $self, $sensitive ) = @_;

    for my $k (@widgets_disabled_when_preferences_opened) {
        $self->{widgets}->{$k}->set_sensitive($sensitive);
    }
}

sub close_dialog {
    my ($self) = @_;

    $self->get_ui('edit_preferences')->destroy();
    $self->dependent_widgets_sensitivity(1);
}

sub cancel {
    my ($self) = @_;

    $self->close_dialog();
}

sub accept {
    my ($self) = @_;

    $self->{prefs}->reprend_pref( store => 'prefwindow', prefix => 'pref' );
    $self->{prefs}->reprend_pref(
        store  => 'prefwindow_project',
        prefix => 'pref_projet'
    ) if ( $self->{open_project_name} );

    my %pm;
    my %gm;
    my @dgm;
    my %labels;

    if ( $self->{open_project_name} ) {
        %pm = map { $_ => 1 } ( $self->{config}->changed_keys('project') );
        %gm = map { $_ => 1 } ( $self->{config}->changed_keys('global') );
        @dgm = grep { /^defaut_/ } ( keys %gm );

        for my $k (@dgm) {
            my $l = $self->{main}->get_object( 'label_' . $k );
            $labels{$k} = $l->get_text() if ($l);
            my $kp = $k;
            $kp =~ s/^defaut_//;
            $l = $self->{main}->get_object( 'label_' . $kp );
            $labels{$kp} = $l->get_text() if ($l);
        }
    }

    $self->close_dialog();

    if ( $self->{open_project_name} ) {

        # Check if annotations are still valid (same options)

        my $changed = 0;
        for (
            qw/annote_chsign symboles_trait
            embedded_format embedded_max_size embedded_jpeg_quality
            symboles_indicatives annote_font_name annote_ecart/
          )
        {
            $changed = 1 if ( $gm{$_} );
        }
        for my $tag (qw/0_0 0_1 1_0 1_1/) {
            $changed = 1
              if ( $gm{ "symbole_" . $tag . "_type" }
                || $gm{ "symbole_" . $tag . "_color" } );
        }
        for (qw/annote_position verdict verdict_q annote_rtl/) {
            $changed = 1 if ( $pm{$_} );
        }

        if ($changed) {
            annotate_source_change( $self->{capture}, 1 );
        }

        # Look at modified default values...

        debug "Labels: " . join( ',', keys %labels );

        for my $k (@dgm) {
            my $kp = $k;
            $kp =~ s/^defaut_//;

            debug "Test G:$k / P:$kp";
            if (   ( !$pm{$kp} )
                && ( $self->get($kp) ne $self->get($k) ) )
            {

                # project option has NOT been modified, and the new
                # value of general default option is different from
                # project option. Ask the user for modifying also the
                # project option value
                my $label_projet  = $labels{$kp};
                my $label_general = $labels{$k};

                debug "Ask user $label_general | $label_projet";

                if ( $label_projet && $label_general ) {
                    my $dialog =
                      Gtk3::MessageDialog->new( $self->{parent_window},
                        'destroy-with-parent', 'question', 'yes-no', '' );
                    $dialog->set_markup(
                        sprintf(
                            __(
"You modified \"<b>%s</b>\" value, which is the default value used when creating new projects. Do you want to change also \"<b>%s</b>\" for the opened <i>%s</i> project?"
                            ),
                            $label_general,
                            $label_projet,
                            $self->{open_project_name}
                        )
                    );
                    $dialog->get_widget_for_response('yes')
                      ->get_style_context()->add_class("suggested-action");
                    my $reponse = $dialog->run;
                    $dialog->destroy;

                    debug "Reponse: $reponse";

                    if ( $reponse eq 'yes' ) {

                        # change also project option value
                        $self->set( $kp, $self->get($k) );
                    }

                }
            }
        }

        # Run again decoding if the name field type has changed

        if ( $pm{name_field_type} ) {
            my $response;

            if ( $self->get('name_field_type') ) {
                my $dialog = Gtk3::MessageDialog->new( $self->{parent_window},
                    'destroy-with-parent', 'question', 'ok-cancel', '' );
                $dialog->set_markup(
                    __(
"You modified the name field type, so that the name fields has to be decoded again."
                    )
                );
                $dialog->get_widget_for_response('ok')->get_style_context()
                  ->add_class("suggested-action");
                $response = $dialog->run;
                $dialog->destroy;
            } else {
                $response = 'ok';
            }
            if ( $response eq 'ok' ) {
                &{ $self->{decode_callback} }( $self->{callback_self} );
            }
        }

        for my $k (qw/note_null note_min note_max note_grain/) {
            my $v = $self->get($k);
            $v =~ s/\s+//g;
            $self->set( $k, $v );
        }
    }

    if ( $self->{config}->key_changed("projects_home")
        && !$self->{open_project_name} )
    {
        $self->{config}
          ->set_projects_home( $self->get_absolute('projects_home') );
    }

    $self->{config}->test_commands();

    if (   $self->{config}->key_changed("seuil")
        || $self->{config}->key_changed("seuil_up") )
    {
        if ( $self->{capture}->n_pages_transaction() > 0 ) {
            &{ $self->{detect_analysis_callback} }( $self->{callback_self} );
        }
    }

    $self->{config}->save();
}

1;