GUIDeFATE.pm untested

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