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
| #!/usr/bin/perl -w
# ########
# Doc
# ########
# http://docstore.milk.ua/orelly/perl3/tk/index.htm
# http://www.hisel.com/perl/TkTutorial/
# http://perl.developpez.com/faq/tk/
# http://articles.mongueurs.net/magazines/linuxmag62.html
# http://search.cpan.org/dist/Tk-TableMatrix/pod/TableMatrix.pod
# http://perso.tls.cena.fr/jestin/Ptk/ptk.html
# http://docstore.mik.ua/orelly/perl3/tk/ch15_02.htm
# PRODUCTION
use strict;
use warnings;
use Tk; # require installation
use utf8;
# These are all the modules that we are using in this script.
use Tk::Frame;
use Tk::Scrollbar;
use Tk::StatusBar; # require installation
use Tk::DialogBox;
use Tk::Balloon; # require installation
use Tk::TableMatrix::SpreadsheetHideRows; # require installation
# DEVELOPPMENT
use Data::Dumper;
use constant {
true => 0,
false =>0,
};
sub open_frame {
my ($dbh, $db_table, $db_fields, $window_title, $array_of_hashes) = @_ ;
my ($nb_col, $nb_row) = (0, 0);
my $minheight=370;
my $minwidth=700;
my $col_width_small = 10;
my $col_width_big = 5 * $col_width_small;
my $padx=10;
my $activecell_bgcolor="yellow";
# ###########################
# Build the spreadsheet hash
# ###########################
my $count_row=0;
my $count_col=0;
# keep initial data stored in a hash
my $spreadsheet_initial= {};
my $spreadsheet_current= {};
foreach my $row (@$array_of_hashes) {
while ( my ($k,$v) = each (%$row) ) {
$spreadsheet_initial->{"$count_row,$count_col"} = $k if ($count_row == 0) ; # title row from hash
$spreadsheet_initial->{"$count_row,$count_col"} = $v if ($count_row > 0) ; # data rows
$spreadsheet_current->{"$count_row,$count_col"} = $spreadsheet_initial->{"$count_row,$count_col"};
$count_col++;
}
$nb_col = ($count_col > $nb_col)? $count_col : $nb_col ;
$count_col=0;
$count_row++;
}
$nb_row = $count_row;
# ###########################
# Création du TopLevel
# ###########################
my $top = MainWindow->new;
$top->minsize( $minwidth, $minheight );
$top->title($window_title);
# Status bar
my $status_bar = add_status_bar($top) ;
# Création des cadres (Widget Frame)
# la zone de données
my $frame1 = $top->Frame()->pack(-fill => 'x'); # "x" =>remplir tte la largeur du widget parent
# la zone des boutons
my $frame2 = $top->Frame()->pack(-fill => 'x');
# Frame 1
my $table = $frame1->Scrolled('SpreadsheetHideRows',
-rows => $nb_row,
-cols => $nb_col,
-width => 0, #Specifies the desired width for the window, in columns. If zero or less, then the desired width for the window is made just large enough to hold all the columns in the table.
-height => 0,
-titlerows => 1, # Titre des colonnes = Number of rows to use as a title area. Defaults to 0.
-titlecols => 0, # Titres des lignes = Number of cols to use as a title area. Defaults to 0.
-variable => $spreadsheet_current,
-selectmode => 'extended', #Specifies one of several styles for manipulating the selection. The value of the option may be arbitrary, but the default bindings expect it to be either single , browse , multiple , or extended ; the default value is browse .
-resizeborders => 'both', #Specifies what kind of interactive border resizing to allow, must be one of row, col, both (default) or none.
-selectorCol => 0,
-scrollbars => 'ose' # o: show only if necessary at positions s=south, e=east
);
# Set spreadsheet columns width
my $count=0;
my $width;
my @col_width = ( $col_width_small, $col_width_small, $col_width_small, $col_width_big);
foreach my $col (0..$nb_col){
$width = ($col_width[$count])? $col_width[$count] : 0 ;
$table->colWidth( $col, $width );
$count++;
}
# Frame 2: Création des widgets Button
my $B_save = $frame2 -> Button (-background => 'green', -text => 'Save', -padx=>3*$padx, -command=> [
\&save_to_db, $dbh, $db_table, $db_fields, $spreadsheet_initial, $spreadsheet_current, $top ] );
my $B_refresh = $frame2 -> Button (-background => 'gray', -text => 'Refresh', -padx=>$padx , -command=> [ sub {
# close current window
$top->destroy;
# open a new window with the call parameters
open_frame($dbh, $db_table, $db_fields, $window_title, $array_of_hashes);
} ]);
my $B_reload = $frame2 -> Button(-background => 'gray', -text => 'Reload', -padx=>$padx);
my $B_close = $frame2 -> Button(-background => 'gray', -text => 'Close', -padx=>$padx, -command=> [ sub {$top->destroy;} ]);
my $B_exit = $frame2 -> Button(-background => 'gray', -text => 'Exit Program', -padx=>$padx, -command=> [ sub { exit 1;} ]);
# Show buttons
$B_save-> pack( -side=>'right');
$B_refresh-> pack( -side=>'right');
$B_reload-> pack( -side=>'right');
$B_close -> pack( -side=>'right');
$B_exit-> pack( -side=>'left');
# Creation des infosbulles sur les boutons
attach_balloon($top, $status_bar, $B_save, "Save the modified data", "This will save the modified data into db");
attach_balloon($top, $status_bar, $B_refresh, "Refresh to initial data", "This will refresh the client data to initial data (need to Save)");
attach_balloon($top, $status_bar, $B_reload, "NOT IMPLEMENTED YET", "This will reload the client data from database");
attach_balloon($top, $status_bar, $B_close, "Exit this window", "This will drive back to the program execution");
attach_balloon($top, $status_bar, $B_exit, "Exit the program", "This will stop completely the program run code");
# ###########################
# "Bind" de la zone de saisie
# ###########################
# Monitor and control entry data changes
$table->tagConfigure( 'modified', -bg =>'orange', -fg=>'black', -relief=>'sunken');
$table->tagConfigure('active', -bg=>$activecell_bgcolor);
$table->tagConfigure('default', -bg =>'white', -fg=>'black', -relief=>'sunken');
# Solution 1: browse and compare both hashes :-(
#$table->configure (
# -browsecmd => sub {
# my ( $previous_index, $current_index ) = @_;
#
# my $new_value = $table->get($current_index) ;
# my $previous_value = $table->get($previous_index) ;
# Check if we did select a cell and not an entire row or a column
# if ( $previous_index =~ m{^\d+,\d+$} ) {
# Store the updated cell into hash for later needs
# $spreadsheet_current->{$current_index} = $new_value ;
# Highlight all changes
# while ( my ($k,$v) = each(%$spreadsheet_initial) ) {
# $table->tagCell('modified', "$k" ) if ($spreadsheet_current->{$k} ne $spreadsheet_initial->{$k});
# }
# }
# },
#);
# Solution 2 by Djibril
# See http://www.developpez.net/forums/d1074157/autres-langages/perl/interfaces-graphiques/tablematrix-monitorer-modification/#post5953572
#$table->configure(
# -browsecmd => sub {
# my ( $previous_index, $current_index ) = @_;
# Valeur de la case selectionnée
# my $value_current_cell = $table->get($current_index);
# A chaque fois que l'utilisateur sélectionne une cellule, on stocke la valeur originale si ce n'était pas fait
# if ( not exists $spreadsheet_initial->{$current_index} ) {
# $spreadsheet_initial->{$current_index} = $value_current_cell;
# }
# Si une case avait déjà était sélectionnée
# if ($previous_index) {
# Valeur de la case précedemment selectionnee
# my $value_previous_cell = $table->get($previous_index);
# Affectons un tag à la cellule si cette dernière est différente de l'originale
# if ( $value_previous_cell ne $spreadsheet_initial->{$previous_index} ) {
# $table->tagCell( 'modified', $previous_index );
# } else {
# $table->tagCell( 'default', $previous_index );
# }
# }
# },
#);
# Solution 3: bind sur la touche ENTER
$table->bind(
'<Key-Return>' =>
sub {
my ($window) = @_;
# NOTE: Prevent defaut <Return> binded motion (move down), or decrement selection row by 1
# Get <entry> cell infos
my $current_index = $window->curselection(); # With no arguments, it returns the sorted indices of the currently selected cells. Otherwise it sets all the selected cells to the given value.
my ($row, $col) = split (',', @$current_index[0], 2) ;
$row-- if $row > 0 ;
$current_index = "$row,$col";
my $current_value = $window->get($current_index);
# trace changes by comparing current cell value with initial data
my $not_matching = ($current_value ne $spreadsheet_initial->{"$current_index"})? true : false ;
# tag the current cell if entry is not matching the initial data
$table->tagCell( 'modified', $current_index ) if $not_matching;
# tag back the current cell if it has been modified back to the initial value
$table->tagCell( 'default', $current_index ) if ! $not_matching;
}
);
# Solution 4: -vcmd
# to be done
# highlight the cell on mouse hover
$table->bind(
'<Motion>',
sub {
my $w = shift;
my $Ev = $w->XEvent;
if ( $w->selectionIncludes( '@' . $Ev->x . "," . $Ev->y ) ) {#
Tk->break;
}
$w->selectionClear('all');
$w->selectionSet( '@' . $Ev->x . "," . $Ev->y );
Tk->break;
## "break" prevents the call to TableMatrixCheckBorder
}
);
# Centrer ma fenêtre
# center_widget($top );
# Affichage
$table->pack(-expand => 1, -fill => 'both');
Tk::MainLoop;
} |
Partager