#!/usr/bin/env perl
# /* {{{1 GNU General Public License
# 
# model_editor for sofea, the Stack Operated Finite Element Analysis program
# Copyright (C) 2004-2005  Al Danial <al.danial@gmail.com>
# 
# This program 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.
# 
# This program 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 this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# GUI to enter csv data manually.
# }}}1 */
use strict;
use warnings;
use XML::Simple qw(:strict) ;
use Tk;
use Tk::PNG;
use Tk::MatchEntry;
BEGIN {
    die "The environment variable TOPS_HOME is not defined; will not be\n",
        "able to find the image files.\n"
        unless $ENV{TOPS_HOME};
    1;
}
my $IMG_DIR = "$ENV{TOPS_HOME}/apps/fea/img";

my $MW = Tk::MainWindow->new;
$MW->configure(-menu => my $menubar = $MW->Menu);
my $TopFrame = $MW->Frame( -height  =>  80,)->pack(-side => 'top');

# load XML data, prepare images {{{1
my %current_entry = ();
my $XML   = XMLin( join("", <DATA>), KeyAttr => [ 'name' ], ForceArray => 0 );
my %Info  = %{$XML->{card}};
my %photo = (); 
foreach my $card (keys %Info) {
    for (my $i = 0; $i < scalar @{$Info{$card}{field}}; $i++) {
        $photo{$card}{$i} = 
            $TopFrame->Photo( -file => "$IMG_DIR/$Info{$card}{field}[$i]{img}");
    }
}
# 1}}}

my @choices = keys %Info;
my $File    = "model.csv";
   $File    = $ARGV[0] if @ARGV;

my $ControlFrame = $TopFrame->Frame( -height  => 1,)->pack(-side => 'top');
my $EnteredCards;

# Open, Save, Quit, Enter buttons {{{1
my $OpenButton = $ControlFrame->Button(
            -text    => 'Open',
            -width   => 4,
            -height  => 1,
            -command => sub {
                $File = $ControlFrame->getOpenFile(
                        -defaultextension => ".csv",
                        -filetypes        => [['.csv files', '.csv' ],
                                              ['All Files',  '*',   ], ],
                        -initialdir       => Cwd::cwd(),
                        -initialfile      => $File,
                        -title            => "Open a .csv file",
                );
                print "selected open file [$File]\n";
                load_file($File);
            },
           )->pack(-side => 'left');

my $SaveButton = $ControlFrame->Button(
            -text    => 'Save',
            -width   => 4,
            -height  => 1,
            -command => sub {
                $File = $ControlFrame->getSaveFile(
                        -defaultextension => ".csv",
                        -filetypes        => [['.csv files', '.csv' ],
                                              ['All Files',  '*',   ], ],
                        -initialdir       => Cwd::cwd(),
                        -initialfile      => $File,
                        -title            => "Save a .csv file",
                );
                open  OUT, ">$File" or return;
                print OUT $EnteredCards->get("1.0", "end");
                close OUT;
                print "Wrote $File\n";
            },
           )->pack(-side => 'left');

my $QuitButton = $ControlFrame->Button(
            -text    => 'Quit',
            -width   => 4,
            -height  => 1,
            -command => sub { exit; },
           )->pack(-side => 'left');

my $EnterButton = $ControlFrame->Button(
            -text    => 'Enter',
            -width   => 4,
            -height  => 1,
            -command => \&EnterLine, 
           )->pack(-side => 'right');
# 1}}}
# Explanation box, Diagram area {{{1
my $Explanation = $TopFrame->Label(
            -text   => 'sofea',
            -width  => 50,
            -height => 10,
           )->pack(-side => 'left', -fill   => 'both',);

my $Diagram = $TopFrame->Button(
            -image  => $photo{'tri3'}{0},
            -width  => 300,
            -height => 200,
           )->pack(-side => 'right', -fill   => 'both',);
# 1}}}

$EnteredCards = $MW->Scrolled(
              qw/Text -relief sunken -borderwidth 2 -setgrid true
               -height 30 -scrollbars e/
              )->pack( qw/-side bottom -fill x/);
load_file($File) if (@ARGV);

# card entry {{{1
my $Card = $MW->MatchEntry(
             -choices         => \@choices,
             -maxheight       => 8,
             -fixedwidth      => 1,
             -ignorecase      => 1,
             -entercmd        => [ \&ProcessCard, \%current_entry ],
             -tabcmd          => [ \&ProcessCard, \%current_entry ],
             -command         => [ \&ProcessCard, \%current_entry ],
             -width           => 14,
           )->pack(-side => 'left');

my $F1 = $MW->Entry(-relief          => 'groove', 
                    -validate        => 'all',
                    -validatecommand => \&a1,
                    -width           =>  10,
                   )->pack(-side => 'left', -expand => 1);
my $F2 = $MW->Entry(-relief          => 'groove', 
                    -validate        => 'all',
                    -validatecommand => \&a2,
                    -width           =>  10,
                   )->pack(-side => 'left', -expand => 1);
my $F3 = $MW->Entry(-relief          => 'groove', 
                    -validate        => 'all',
                    -validatecommand => \&a3,
                    -width           =>  10,
                   )->pack(-side => 'left', -expand => 1);
my $F4 = $MW->Entry(-relief          => 'groove', 
                    -validate        => 'all',
                    -validatecommand => \&a4,
                    -width           =>  10,
                   )->pack(-side => 'left', -expand => 1);
my $F5 = $MW->Entry(-relief          => 'groove', 
                    -validate        => 'all',
                    -validatecommand => \&a5,
                    -width           =>  10,
                   )->pack(-side => 'left', -expand => 1);
my $F6 = $MW->Entry(-relief          => 'groove', 
                    -validate        => 'all',
                    -validatecommand => \&a6,
                    -width           =>  10,
                   )->pack(-side => 'left', -expand => 1);
my $F7 = $MW->Entry(-relief          => 'groove', 
                    -validate        => 'all',
                    -validatecommand => \&a7,
                    -width           =>  10,
                   )->pack(-side => 'left', -expand => 1);
# 1}}}

MainLoop;

# subroutines for fields 1 to 8 {{{1
sub ProcessCard {
    my ($rh_current_entry) = shift;
    my $card = $Card->get('active');

    $rh_current_entry->{card} = $card;
    $Diagram->configure(   -image => $photo{$card}{0});

print "ProcessCard got entry=[$rh_current_entry->{card}]\n";
print "ProcessCard entry='$card'\n";
print "   desc=$Info{$rh_current_entry->{card}}{field}[0]{desc}\n";
print "   expl=$Info{$rh_current_entry->{card}}{field}[0]{expl}\n";

    $Explanation->configure(-text => $Info{$card}{field}[0]{expl});
    1;
}
sub a1 {
    my $text = $F1->get;
    print "a1 card=[$current_entry{card}]\n";
    print "a1 have [$text]\n";
    $Diagram->configure(    -image => $photo{$current_entry{card} }{1});
    $Explanation->configure(-text  => $Info{ $current_entry{card} }{field}[1]{expl});
    $current_entry{a1} = $text;
    1;
}
sub a2 {
    my $text = $F2->get;
    print "a2 have [$text]\n";
    $Diagram->configure(    -image => $photo{$current_entry{card} }{2});
    $Explanation->configure(-text  => $Info{ $current_entry{card} }{field}[2]{expl});
    $current_entry{a2} = $text;
    1;
}
sub a3 {
    my $text = $F3->get;
    print "a3 have [$text]\n";
    $Diagram->configure(    -image => $photo{$current_entry{card} }{3});
    $Explanation->configure(-text  => $Info{ $current_entry{card} }{field}[3]{expl});
    $current_entry{a3} = $text;
    1;
}
sub a4 {
    my $text = $F4->get;
    print "a4 have [$text]\n";
    $Diagram->configure(    -image => $photo{$current_entry{card} }{4});
    $Explanation->configure(-text  => $Info{ $current_entry{card} }{field}[4]{expl});
    $current_entry{a4} = $text;
    1;
}
sub a5 {
    my $text = $F5->get;
    print "a5 have [$text]\n";
    $Diagram->configure(    -image => $photo{$current_entry{card} }{5});
    $Explanation->configure(-text  => $Info{ $current_entry{card} }{field}[5]{expl});
    $current_entry{a5} = $text;
    1;
}
sub a6 {
    my $text = $F6->get;
    print "a6 have [$text]\n";
    $Diagram->configure(    -image => $photo{$current_entry{card} }{6});
    $Explanation->configure(-text  => $Info{ $current_entry{card} }{field}[6]{expl});
    $current_entry{a6} = $text;
    1;
}
sub a7 {
    my $text = $F7->get;
    print "a7 have [$text]\n";
    $Diagram->configure(    -image => $photo{$current_entry{card} }{7});
    $Explanation->configure(-text  => $Info{ $current_entry{card} }{field}[7]{expl});
    $current_entry{a7} = $text;
    1;
}
# 1}}}
sub EnterLine { # {{{1
    my $line = $current_entry{card};
    foreach (qw(a1 a2 a3 a4 a5 a6 a7)) {
        $current_entry{$_} = " " unless $current_entry{$_};
        $line .= ", $current_entry{$_}";
    }
    $line .= "\n";
    $EnteredCards->insert('end', $line);
} # 1}}}
sub load_file { # {{{1
    my $file = shift;
    open  IN, $file or return;
    while (<IN>) {
        $EnteredCards->insert('end', $_);
    }
    close IN;
} # 1}}}

__END__
<opt>
  <card name="node"> <!-- {{{1 -->

    <field>
        <desc>node</desc>
        <expl>The node of a finite element.</expl> 
        <img>node_1.png</img>
    </field>

    <field>
        <desc>nid</desc>
        <expl>Node identifier.</expl> 
        <img>node_1.png</img>
    </field>

    <field>
        <desc>coord_in</desc>
        <expl>
Identifier of the coordinate system used for 
x1, x2, x3.
        </expl> 
        <img>node_2.png</img>
    </field>

    <field>
        <desc>x1</desc>
        <expl>Numeric value of coordinate x1.</expl> 
        <img>node_3.png</img>
    </field>

    <field>
        <desc>x2</desc>
        <expl>Numeric value of coordinate x2.</expl> 
        <img>node_4.png</img>
    </field>

    <field>
        <desc>x3</desc>
        <expl>Numeric value of coordinate x3.</expl> 
        <img>node_5.png</img>
    </field>

    <field>
        <desc>coord_out</desc>
        <expl>Identifier of the coordinate system used for displaying results at this node.</expl> 
        <img>tri3_2.png</img>
    </field>

  </card>
  <!-- 1}}} -->
  <card name="tri3"> <!-- {{{1 -->

    <field>
        <desc>tri3</desc>
        <expl>
Three noded Mindlin-Reiser thick plate triangular 
shell element.  Developed by James F. Doyle, 
Purdue University, as part of his StaDyn finite 
element program.
        </expl> 
        <img>tri3_1.png</img>
    </field>

    <field>
        <desc>eid</desc>
        <expl>Element identifier.</expl> 
        <img>tri3_1.png</img>
    </field>

    <field>
        <desc>pid</desc>
        <expl>Shell property identifier.</expl> 
        <img>tri3_2.png</img>
    </field>

    <field>
        <desc>nid 1</desc>
        <expl>Identifier of node 1.</expl> 
        <img>tri3_3.png</img>
    </field>

    <field>
        <desc>nid 2</desc>
        <expl>Identifier of node 2.</expl> 
        <img>tri3_4.png</img>
    </field>

    <field>
        <desc>nid 3</desc>
        <expl>Identifier of node 3.</expl> 
        <img>tri3_5.png</img>
    </field>

  </card>
  <!-- 1}}} -->
</opt>
