File: Unrecognized.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 (356 lines) | stat: -rw-r--r-- 10,641 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
# -*- 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::Unrecognized;

use parent 'AMC::Gui';

use AMC::Basic;

use File::Spec::Functions qw/splitpath/;

use Gtk3;

use constant {
    INCONNU_FILE    => 0,
    INCONNU_SCAN    => 1,
    INCONNU_TIME    => 2,
    INCONNU_TIME_N  => 3,
    INCONNU_PREPROC => 4,
};

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

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

    $self->merge_config(
        {
            capture => '',
            callback_self => '',
            update_analysis_callback =>
              sub { debug "Missing update_analysis_callback"; },
            analysis_callback => sub { debug "Missing analysis_callback"; },
        },
        %oo
    );

    $self->open_window();

    return $self;
}

sub open_window {
    my ($self) = @_;
    
    my $store = Gtk3::ListStore->new(
        'Glib::String', 'Glib::String', 'Glib::String', 'Glib::String',
        'Glib::String'
        );
    $self->{store} = $store;

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

    $self->read_glade(
        $glade_xml,
        qw/unrecognized
          inconnu_tree scan_area preprocessed_area
          inconnu_hpaned inconnu_vpaned
          state_scanrecog state_scanrecog_label
          unrecog_process_button
          unrecog_delete_button
          unrecog_next_button unrecog_previous_button
          ur_frame_scan/
    );

    for (qw/scan preprocessed/) {
        AMC::Gui::PageArea::add_feuille( $self->get_ui( $_ . '_area' ) );
    }

    my $tree = $self->get_ui('inconnu_tree');
    
    $store->set_sort_column_id( INCONNU_TIME_N, 'ascending' );

    $tree->set_model($store);

    my $renderer;
    my $column;

    $renderer = Gtk3::CellRendererText->new;
    $column   = Gtk3::TreeViewColumn->new_with_attributes( "scan", $renderer,
        text => INCONNU_SCAN );
    $tree->append_column($column);
    $column->set_sort_column_id(INCONNU_SCAN);

    $renderer = Gtk3::CellRendererText->new;
    $column   = Gtk3::TreeViewColumn->new_with_attributes( "date", $renderer,
        text => INCONNU_TIME );
    $tree->append_column($column);
    $column->set_sort_column_id(INCONNU_TIME_N);

    $self->update();

    $tree->get_selection->set_mode('multiple');
    $tree->get_selection->signal_connect( "changed", \&line, $self );

    $self->get_ui('unrecognized')->show();

    $tree->get_selection->select_iter( $store->get_iter_first );
}

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

    $self->{capture}->begin_read_transaction('UNRC');
    my $failed =
      $self->{capture}
      ->dbh->selectall_arrayref( $self->{capture}->statement('failedList'),
        { Slice => {} } );
    $self->{capture}->end_transaction('UNRC');

    $self->{store}->clear;
    for my $ff (@$failed) {
        my $iter = $self->{store}->append;
        my $f    = $ff->{filename};
        $f =~ s:.*/::;
        my ( undef, undef, $scan_n ) =
          splitpath( $self->absolu( $ff->{filename} ) );
        my $preproc_file =
            $self->absolu('%PROJET/cr/diagnostic') . "/"
          . $scan_n . ".png";

        $self->{store}->set(
            $iter,                           INCONNU_SCAN,
            $f,                              INCONNU_FILE,
            $ff->{filename},                 INCONNU_TIME,
            format_date( $ff->{timestamp} ), INCONNU_TIME_N,
            $ff->{timestamp},                INCONNU_PREPROC,
            $preproc_file,
        );
    }
}

sub next {
    my ($self, $widget, @sel) = @_;
    
    @sel = () if ( !defined( $sel[0] ) );
    if ( !@sel ) {
        @sel = $self->get_ui('inconnu_tree')->get_selection->get_selected_rows;
        if ( $sel[0] ) {
            @sel = @{ $sel[0] };
        } else {
            @sel = ();
        }
    }
    my $iter;
    my $ok = 0;
    if ( @sel && defined( $sel[$#sel] ) ) {
        $iter = $self->{store}->get_iter( $sel[$#sel] );
        $ok   = $self->{store}->iter_next($iter);
    }
    $iter = $self->{store}->get_iter_first if ( !$ok );

    $self->get_ui('inconnu_tree')->get_selection->unselect_all;
    $self->get_ui('inconnu_tree')->get_selection->select_iter($iter) if ($iter);
}

sub prev {
    my ($self) = @_;
    
    my @sel = $self->get_ui('inconnu_tree')->get_selection->get_selected_rows;
    my $first_selected = $sel[0]->[0];
    my $iter;
    if ( defined($first_selected) ) {
        my $p =
          $self->{store}->get_path( $self->{store}->get_iter($first_selected) );
        if ( $p->prev ) {
            $iter = $self->{store}->get_iter($p);
        } else {
            $iter = '';
        }
    }
    $iter = $self->{store}->get_iter_first if ( !$iter );

    $self->get_ui('inconnu_tree')->get_selection->unselect_all;
    $self->get_ui('inconnu_tree')->get_selection->select_iter($iter) if ($iter);
}

sub delete {
    my ($self) = @_;
    
    my @iters;
    my @sel = ( $self->get_ui('inconnu_tree')->get_selection->get_selected_rows );
    @sel = @{ $sel[0] };
    return if ( !@sel );

    $self->{capture}->begin_transaction('rmUN');
    for my $s (@sel) {
        my $iter = $self->{store}->get_iter($s);
        my $file = $self->{store}->get( $iter, INCONNU_FILE );
        $self->{capture}->statement('deleteFailed')->execute($file);
        unlink $self->absolu($file);
        push @iters, $iter;
    }
    $self->next( '', @sel );
    for (@iters) { $self->{store}->remove($_); }
    &{$self->{update_analysis_callback}}( $self->{callback_self} );
    $self->{capture}->end_transaction('rmUN');
}

# call AMC-analyse to build the diagnostic image

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

    my @sel = $self->get_ui('inconnu_tree')->get_selection->get_selected_rows;
    my $first_selected = $sel[0]->[0];
    if ( defined($first_selected) ) {
        my $iter = $self->{store}->get_iter($first_selected);
        my $scan =
          $self->{config}->{shortcuts}
          ->absolu( $self->{store}->get( $iter, INCONNU_FILE ) );
        my $diagnostic_file = $self->{store}->get( $iter, INCONNU_PREPROC );

        if ( !-f $diagnostic_file ) {
            &{ $self->{analysis_callback} }(
                 $self->{callback_self},
                 f          => [$scan],
                 text       => __("Making diagnostic image..."),
                 progres    => 'diagnostic',
                 diagnostic => 1,
                 fin        => sub {
                     $self->line();
                 },
            );
        }
    }
}

sub set_hpaned {
    my ($self, $prop) = @_;

    $self->get_ui('inconnu_hpaned')->set_position($prop * $self->get_ui('inconnu_hpaned')->get_property('max-position'));
}

sub line {
    my ($self, $data) = @_;

    # when used as Gtk signal callback, $self is the second argument
    
    if ( $data && $data->isa("AMC::Gui::Unrecognized") ) {
        $self = $data;
    }

    if ( $self->{store}->get_iter_first ) {
        my @sel =
          $self->get_ui('inconnu_tree')->get_selection->get_selected_rows;
        my $first_selected = $sel[0]->[0];
        my $iter           = '';
        if ( defined($first_selected) ) {
            $iter = $self->{store}->get_iter($first_selected);
        }
        if ($iter) {
            $self->get_ui('inconnu_tree')
              ->scroll_to_cell( $first_selected, undef, 0, 0, 0 );
            my $scan =
              $self->{config}->{shortcuts}
              ->absolu( $self->{store}->get( $iter, INCONNU_FILE ) );
            if ( -f $scan ) {
                $self->get_ui('scan_area')->set_content( image => $scan );
            } else {
                debug_and_stderr "Scan not found: $scan";
                $self->get_ui('scan_area')->set_content();
            }

            my $preproc = $self->{store}->get( $iter, INCONNU_PREPROC );

            if ( -f $preproc ) {
                $self->get_ui('preprocessed_area')
                    ->set_content( image => $preproc );
                $self->set_hpaned(0.5);
            } else {
                $self->get_ui('preprocessed_area')->set_content();
                $self->set_hpaned(1);
            }

            if ( $self->get_ui('scan_area')->get_image ) {
                my $scan_n = $scan;
                $scan_n =~ s:^.*/::;
                $self->set_state( 'question', $scan_n );
            } else {
                $self->set_state( 'error',
                    sprintf( ( __ "Error loading scan %s" ), $scan ) );
            }
            $self->actions( -f $preproc ? 2 : 1 );
        } else {
            $self->get_ui('scan_area')->set_content();
            $self->get_ui('preprocessed_area')->set_content();
            $self->set_state( 'question', __ "No scan selected" );
            $self->actions(0);
        }
    } else {

        # empty list
        $self->set_state( 'info', __ "No more unrecognized scans" );
        $self->get_ui('scan_area')->set_content();
        $self->get_ui('preprocessed_area')->set_content();
        $self->actions(-1);
    }
}

sub actions {
    my ( $self, $available ) = @_;
    my %actions = (
        delete   => $available > 0,
        process  => $available == 1,
        next     => $available >= 0,
        previous => $available >= 0,
    );
    for my $k ( keys %actions ) {
        if ( $actions{$k} > 0 ) {
            $self->get_ui( 'unrecog_' . $k . '_button' )->show;
        } else {
            $self->get_ui( 'unrecog_' . $k . '_button' )->hide;
        }
    }
}
    
sub set_state {
    my ( $self, $type, $message ) = @_;
    my $w     = $self->get_ui('scan_recog');
    my $label = $self->get_ui('scan_recog_label');
    if ( defined($type) && $w ) {
        if ( $type eq 'none' ) {
            $w->hide();
        } else {
            $w->show();
            $w->set_message_type($type);
        }
    }
    $label->set_text($message)
      if ( defined($message) && $label );
}

1;