GUIDeFATE.pm untested

filter: Statementstmt 0.0% 0 / 254 Branchbran 0.0% 0 / 100 Conditioncond 0.0% 0 / 15 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 380 CC 52 SCAR 73.9
CC
52
Cov
0%
convert
2070.0
new
56.0
debugGui
6.0
CRAP 1613.1
1 package GUIDeFATE;
2  
3 use strict;
4 use warnings;
5
6 our $VERSION = '0.09';
7
8 use Exporter 'import';
9
10 our @EXPORT_OK = qw<$frame>; # allows manipulation of frame from main.
11 our $target="";
12 our $AppObject;
13 our $winX=30;
14 our $winY=30;
15 our $winWidth;
16 our $winHeight;
17 our $winTitle;
18 our $winScale=6.5;
19 my $autoGen="";
20 my $log="";
21  
22 sub new{
23 (my $class,my $textGUI,$target,my $assist)=@_;
24 if ((!$target)||($target=~/^wx/i)){
25 $target="wx";
26 eval " use GFwx;";
27 convert($textGUI,$assist);
28 return GFwx->new(); ;
29 }
30 elsif ($target =~m/^gtk/i){
31 $target="gtk";
32 eval " use GFgtk;";
33 convert($textGUI, $assist);
34 return GFgtk->new();
35
36 }
37 elsif ($target =~m/^tk/i){
38 $target="tk";
39 eval " use GFtk;";
40 convert($textGUI, $assist);
41 return GFtk->new();
42 }
43 elsif ($target =~m/^qt/i){
44 $target="qt";
45 eval " use GFqt;";
46
47 convert($textGUI, $assist);
48 my $qtWin=GFqt->new();
49 return $qtWin;
50 }
51 elsif ($target =~m/^win32/i){
52 $target="win32";
53 eval " use GFwin32;";
54 convert($textGUI, $assist);
55 return GFwin32->new();
56 }
57 elsif ($target =~m/^html/i){
58 $target="mojo";
59 eval " use GFhtml;";
60 convert($textGUI, $assist);
61 return GFhtml->new();
62 }
63 }
64  
65 sub convert{
66 my($textGUI,$assist)=@_;
67 my @lines=(split /\n/ ,$textGUI) ;
68 if (!$assist){$assist="q"};
69
70 my $verbose= $assist=~/^v/i;
71 my $debug= $assist=~/^d/i;
72 my $auto= $assist=~/^a/i;
73
74 if (!exists &{"setScale"}){print "Error exists in GF$target\n"; return;}
75 setScale($winScale); # makes scaling in the two modules match
76
77 if ($lines[0] =~ /\-(\d+)x(\d+)-/){
78 $winWidth=$1;
79 $winHeight=$2;
80 }
81 else{
82 $winWidth=$winScale*(2*(length $lines[0])-2);
83 }
84 shift @lines;
85 if ($lines[0]=~/\|T\s*(\S.*\S)\s*\|/){
86 $winTitle=$1;
87 if ($verbose){print "Title=".$winTitle."\n"};
88 shift @lines;
89 }
90 my $l=0;my $bid=0;
91
92
93 foreach my $line (@lines){
94 last if ($line eq ""); # blank line determines end of window
95 while ($line =~m/(\+([A-z]?)[A-z\-]+\+)/){
96 my $ps=length($`); my $fl=length($1)-2;my $fh=1; my $panelType=$2;
97 $lines[$l]=~s/(\+([A-z]?)[A-z\-]+\+)/" " x ($fl+2)/e;
98 my $reg=qr/^.{$ps}\K(\|.{$fl}\|)/;my $content=""; #\K operator protects the previous match from the deletion to follow
99 while ($ps && ($lines[$l+$fh] =~m/$reg/g)){
100 my $tmp=$1;
101 $tmp=~s/^\||\|//g;
102 $content.=$tmp;
103 $lines[$l+$fh]=~s/$reg/" " x ($fl+2)/e; #delete the frame by overwriting with spaces
104 $fh++;
105 }
106 $fh++;
107 if ($ps && ($fh-2)) {
108 $content=~s/^\s+|\s+$//g;
109 $log="SubPanel '$panelType' Id $bid found position $ps height $fh width $fl at row $l with content $content \n";##
110 if ($verbose){ print $log; }
111 if ($auto){ $autoGen.="#".$log; }
112 addWidget(["sp",$bid,$panelType,$content,[$winScale*($ps*2-1),$winScale*$l*4],[$winScale*($fl*2+3),$winScale*$fh*4]]);
113 $bid+=2; # id goes up by 2, one for the panel and one for the content;
114 };
115 }
116
117 while ($line =~m/(\^([A-z]+)\s*\^)/g){ #ComboBoxes
118 my $ps=length($`);my $label=$2; my $len=length ($label);$label=~s/^(\s+)|(\s+)$//g;
119 $line=~s/(\^([A-z]+)\s*\^)/" " x length($1)/e;
120 $log= "combobox calls function &combo$bid\n"; ##
121 if ($verbose){ print $log; }
122 if ($auto){ $autoGen.=makeSub("combo$bid", "combobox with data from \@$label"); }
123 addWidget(["combo",$bid,$label,[$winScale*($ps*2-1),$winScale*$l*4],[$winScale*($len*2+3),$winScale*4], \&{"main::combo".$bid}]);
124 $bid++;
125 }
126 while ($line =~m/(\{([^}]*)\})/g){ # buttons are made from { <label> }
127 my $ps=length($`);my $label=$2; my $len=length ($label);$label=~s/^(\s+)|(\s+)$//g;
128 $log= "Button with label '$label' calls function &btn$bid\n"; ##
129 if ($verbose){ print $log; }
130 if ($auto){ $autoGen.=makeSub("btn$bid", "button with label $label "); }
131 addWidget(["btn",$bid, $label,[$winScale*($ps*2-1),$winScale*$l*4],[$winScale*($len*2+3),$winScale*4], \&{"main::btn".$bid}]);
132 $bid++;
133 $line=~s/(\{([^}]*)\})/" " x length($1)/e; #remove buttons replacing with spaces
134 }
135 while ($line=~m/(\[([^\]]+)\])/g){ # text ctrls are made from [ default text ]
136 my ($ps,$all,$content)=(length($`),$1,$2);
137 $log= "Text Control with default text ' $content ', calls function &textctrl$bid \n"; ##
138 if ($verbose){ print $log; }
139 if ($auto){ $autoGen.=makeSub("textctrl$bid","Text Control with default text ' $content '" ); }
140 my $trimmed=$content; $trimmed=~s/(\s+)|(\s+)$//g;
141 addWidget(["textctrl",$bid, $trimmed,[$winScale*($ps*2-1),$winScale*$l*4],[$winScale*(length($all)*2-1),$winScale*4], \&{"main::textctrl".$bid}]);
142 $bid++;
143 $line=~s/(\[([^\]]+)\])/" " x length($1)/e; #remove text controls replacing with spaces
144 }
145 if ($line !~ m/^\+/){
146 my $tmp=$line;
147 $tmp=~s/(\[([^\]]+)\])|(\{([^}]*)\})/" " x length $1/ge;
148 $tmp=~s/^(\|)|(\|)$//g; #remove starting and ending border
149 $tmp=~s/^(\s+)|(\s+)$//g; #remove spaces
150 if (length $tmp){
151 $log= "Static text '".$tmp."' with id stattext$bid\n"; ##
152 if ($verbose){ print $log; }
153 if ($auto){ $autoGen.="#".$log; }
154 $line=~m/\Q$tmp\E/;my $ps=length($`);
155 addWidget(["stattext",$bid++, $tmp,[$winScale*($ps*2-1),$winScale*$l*4]]);
156 }
157 }
158 $l++;
159 }
160 if(!$winHeight) {$winHeight=$winScale*4*($l-1)};
161
162 my $mode="";
163 while ($l++<=scalar(@lines)){
164 my $line=$lines[$l];
165 if ((!$line) || ($line eq "")||($line=~/^#/)){
166 $mode="";next;
167 }
168 elsif ($line=~/menu/i){
169 $log="Menu found\n"; ##
170 if ($verbose){ print $log; }
171 if ($auto){ $autoGen.="#".$log; }
172 $mode="menu";
173 next;}
174 elsif($line=~/^([A-z]+=)/){
175 chomp $line;
176 my ($varName,$value)=split(/=/,$line,2);
177 $log="var ' $varName ' has value ' $value '\n"; ##
178 addVar($varName,$value);
179 }
180
181 if($mode eq "menu"){
182 if ($line=~/^\-([A-z0-9].*)/i){
183 $log= "Menuhead $1 found\n"; ##
184 if ($verbose){ print $log; }
185 if ($auto){ $autoGen.="#".$log; }
186 addWidget(["mb",$bid, $1, "menuhead", undef]);
187 $bid++;
188 }
189 elsif($line=~/^\-{2}([A-z0-9].*)\;radio/i){
190 $log= "Menu $1 as radio found, calls function &menu$bid \n";##
191 if ($verbose){ print $log; }
192 if ($auto){ $autoGen.="#".$log.makeSub("menu$bid","Menu with label $1"); }
193 addWidget(["mb",$bid, $1, "radio", \&{"main::menu".$bid}]);
194 }
195 elsif($line=~/^\-{2}([A-z0-9].*)\;check/i){
196 $log= "Menu $1 as check found, calls function &menu$bid \n";##
197 if ($verbose){ print $log; }
198 if ($auto){ $autoGen.="#".$log.makeSub("menu$bid","Menu with label $1"); }
199 addWidget(["mb",$bid, $1, "check", \&{"main::menu".$bid}]);
200 }
201 elsif($lines[$l]=~/^\-{6}/){
202 $log= "Separator found,\n"; ##
203 if ($verbose){ print $log; }
204 if ($auto){ $autoGen.="#".$log.makeSub("menu$bid","Menu with label $1"); }
205 addWidget(["mb",$bid, "", "separator",""]);
206 }
207 elsif($line=~/^\-{2}([A-z0-9].*)/i){
208 $log= "Menu $1 found, calls function &menu$bid \n";##
209 if ($verbose){ print $log; }
210 if ($auto){ $autoGen.="#".$log.makeSub("menu$bid","Menu with label $1"); }
211 }
212 addWidget(["mb",$bid, $1, "normal", \&{"main::menu".$bid}]);
213 }
214 elsif($line=~/^\-{3}([A-z0-9].*)/i){
215 $log= "SubMenu $1 found\n";##
216 }
217 $bid++;
218 }
219
220 if ($auto){
221 open(my $fh, '>', 'autogen.txt');
222 print $fh $autoGen;
223 close $fh;
224 }
225
226 sub makeSub{
227 my ($subName,$trigger)=@_;
228 return "sub $subName {#called using $trigger\n # subroutione code goes here\n };\n\n";
229 }
230  
231  
232 sub debugGui{ # for debugging parsing...insert after deletion of discovered content
233 my @gui=shift;
234 foreach my $deb (@gui){
235 last if ($deb eq "");
236 print $deb."\n";
237 }
238 }
239 }
240  
241 1;
242  
243 =head1 GUIDeFATE
244  
245 GUIDeFATE - Graphical User Interface Design From A Text Editor
246  
247 =head1 SYNOPSIS
248  
249 use GUIDeFATE qw<$frame>;
250  
251 my $window=<<END;
252 +------------------------+
253 |T Calculator |
254 +M-----------------------+
255 | [ ] |
256 | { V }{ % }{ C }{AC } |
257 | { 1 }{ 2 }{ 3 }{ + } |
258 | { 4 }{ 5 }{ 6 }{ - } |
259 | { 7 }{ 8 }{ 9 }{ * } |
260 | { . }{ 0 }{ = }{ / } |
261 | made with GUIdeFATE |
262 | and happy things |
263 +------------------------+
264  
265 END
266  
267 my $gui=GUIDeFATE->new($window,[$backend],[$assist]); # API changed at version 0.06
268 # $backend is one of Wx(Default), Tk or Gtk
269 # $assist is one or "q" (quiet, default), "v" (verbose) or "a" for Autogenerate
270
271 $frame=$gui->getFrame||$gui;
272 $gui->MainLoop;
273  
274 =head1 REQUIRES
275  
276 Perl5.8.8, Exporter, Wx, Wx::Perl::Imagick (for Wx interface)
277 Perl5.8.8, Exporter, Tk, Image::Imagick, Tk::JPEG, MIME::Base64 (for Tk interface)
278 Perl5.8.8, Exporter, Glib, Gtk (for Gtk interface)
279 Perl5.8.8, Exporter, QtCore4, QtGui4 (for Qt interface)
280  
281  
282 =head1 EXPORTS
283  
284 getFrame()
285 returns an object containing Widgets (referencesd by id) and
286 GUI interaction functions. This is actually provided by the middle-man
287 ((GFwx, GFtk etc)
288  
289 =head1 DESCRIPTION
290  
291 GUIDeFATE enables the user to convert a textual representtaion into a
292 Graphical user Interfac. It attempts to abstract out the underlying
293 framework. A visually recognisable pattern is passed as a string to
294 GUIDeFATE and this is transformed into an Interactive Interface.
295  
296 =head1 METHODS
297  
298 =head2 Creation
299  
300 =over 4
301  
302 =item my $gui=GUIDeFATE->new($window, $backend, $options);
303  
304 Extracts dimensions and wdigets in a window from the textual
305 representation.
306 If $backend not provided, defaults to "Wx"; options are Wx and Tk,
307 Gtk and Qt.
308 If $options contains "v", then a verbose output is sent to console,
309 if it contains "a", and autogenerated file is produced with all the
310 called functions
311  
312 =item my $frame=$gui->getFrame || $gui;
313  
314 Returns reference to the frame for both abstracted and backend
315 specific functions.
316  
317 =back
318  
319 =head1 AUTHOR
320  
321 Saif Ahmed, SAIFTYNET { at } gmail.com
322  
323 =head1 SEE ALSO
324  
325 L<Wx>, L<Tk>, L<Image::Magick>, L<Wx::Perl::Imagick>, L<GLib>, L<Gtk3>
326  
327 =cut