| 1 | package GFqt; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.09'; | ||
| 6 | |||
| 7 | use QtCore4; | ||
| 8 | use QtGui4; | ||
| 9 | use QtCore4::isa qw( Qt::MainWindow ); | ||
| 10 | use QtCore4::slots | ||
| 11 | mapAction=>['QString']; | ||
| 12 | |||
| 13 | |||
| 14 | use Exporter 'import'; | ||
| 15 | our @EXPORT = qw<addWidget addVar setScale getFrame $frame $winScale $winWidth $winHeight $winTitle>; | ||
| 16 | our $frame; | ||
| 17 | our $app; | ||
| 18 | |||
| 19 | our $winX=30; | ||
| 20 | our $winY=30; | ||
| 21 | our $winWidth; | ||
| 22 | our $winHeight; | ||
| 23 | our $winTitle; | ||
| 24 | our $winScale=6.5; | ||
| 25 | |||
| 26 | # these arrays will contain the widgets each as an arrayref of the parameters | ||
| 27 | my @widgets=(); | ||
| 28 | my %iVars=(); #vars for interface operation (e.g. | ||
| 29 | my %oVars=(); #vars for interface creation (e.g. list of options) | ||
| 30 | my %styles; | ||
| 31 | # my @menus; | ||
| 32 | my $lastMenuLabel; #bug workaround in menu generator may be needed for submenus | ||
| 33 | |||
| 34 | sub new | ||
| 35 | { | ||
| 36 | my $class = shift; | ||
| 37 | $app = Qt::Application(\@ARGV); | ||
| 38 | my $self = $class->SUPER::new(); # call the superclass' constructor | ||
| 39 | $self->{canvas}=Qt::Widget; | ||
| 40 | $frame= $self->{canvas}; | ||
| 41 | $self->setWindowTitle ( $winTitle ); | ||
| 42 | $self->{canvas}->setGeometry(0, 0, $winWidth, $winHeight); | ||
| 43 | $self->{canvas}->setParent($self); | ||
| 44 | $app->setMainWidget($self->{canvas}); | ||
| 45 | $self->{SigMan} = Qt::SignalMapper($self); | ||
| 46 | $self->connect($self->{SigMan}, SIGNAL 'mapped(QString)', $self, SLOT 'mapAction(QString)');# | ||
| 47 | setupContent($self,$self->{canvas}); | ||
| 48 | |||
| 49 | $self->resize($winWidth, $winHeight); | ||
| 50 | |||
| 51 | $self->show; | ||
| 52 | return $self; | ||
| 53 | |||
| 54 | }; | ||
| 55 | |||
| 56 | sub MainLoop{ | ||
| 57 | my $self=shift; | ||
| 58 | $app->exec(); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub mapAction{ | ||
| 62 | my $item=shift; | ||
| 63 | my $widgetIndex=getItem($item); | ||
| 64 | my @widget=@{$widgets[$widgetIndex]}; | ||
| 65 | my $wType=shift @widget; | ||
| 66 | if ($widgetIndex !=-1){ | ||
| 67 | if ($wType eq "mb") { &{$widget[3]};} | ||
| 68 | elsif ($wType eq "btn") { &{$widget[4]};} | ||
| 69 | elsif ($wType eq "combo") { &{$widget[4]};} | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | # setupContent sets up the initial content before Mainloop can be run. | ||
| 74 | sub setupContent{ | ||
| 75 | my ($self, $canvas)=@_; | ||
| 76 | my $currentMenu; | ||
| 77 | foreach my $widget (@widgets){ | ||
| 78 | my @params=@$widget; my $menuStarted=0; | ||
| 79 | my $wtype=shift @params; | ||
| 80 | if ($wtype eq "btn") {aBt($self, $canvas, @params);} | ||
| 81 | elsif ($wtype eq "textctrl") {aTC($self, $canvas, @params);} | ||
| 82 | elsif ($wtype eq "stattext") {aST($self, $canvas, @params);} | ||
| 83 | elsif ($wtype eq "sp") {aSP($self, $canvas, @params);} | ||
| 84 | elsif ($wtype eq "combo") {aCB($self, $canvas, @params);} | ||
| 85 | elsif ($wtype eq "sp") {aSP($self, $canvas, @params);} | ||
| 86 | elsif ($wtype eq "mb") {$currentMenu=aMB($self,$canvas,$currentMenu,@params) } | ||
| 87 | } | ||
| 88 | if (defined $currentMenu){ $self->menuBar()->addMenu($self->{$currentMenu}) } | ||
| 89 | |||
| 90 | sub aBt{ | ||
| 91 | my ($self,$canvas, $id, $label, $location, $size, $action)=@_; | ||
| 92 | $canvas->{"btn$id"}=Qt::PushButton($label); | ||
| 93 | $canvas->{"btn$id"}->setParent($canvas); | ||
| 94 | $canvas->{"btn$id"}->setGeometry(${$location}[0],${$location}[1],${$size}[0],${$size}[1]); | ||
| 95 | $self->connect($canvas->{"btn".$id}, SIGNAL 'clicked()', $self->{SigMan}, SLOT 'map()'); | ||
| 96 | $self->{SigMan}->setMapping($canvas->{"btn".$id}, "btn".$id); | ||
| 97 | } | ||
| 98 | sub aTC{ | ||
| 99 | my ($self,$canvas, $id, $text, $location, $size, $action)=@_; | ||
| 100 | $canvas->{"textctrl$id"}=Qt::LineEdit($text); | ||
| 101 | $canvas->{"textctrl$id"}->setParent($canvas); | ||
| 102 | $canvas->{"textctrl$id"}->setGeometry(${$location}[0],${$location}[1],${$size}[0],${$size}[1]); | ||
| 103 | } | ||
| 104 | sub aST{ | ||
| 105 | my ($self,$canvas, $id, $text, $location)=@_; | ||
| 106 | $canvas->{"stattext$id"}=Qt::Label($text); | ||
| 107 | $canvas->{"stattext$id"}->setStyleSheet("font-size:18px"); | ||
| 108 | $canvas->{"stattext$id"}->setParent($canvas); | ||
| 109 | $canvas->{"stattext$id"}->setGeometry(${$location}[0],${$location}[1],32*length $text,24); | ||
| 110 | } | ||
| 111 | sub aCB{ | ||
| 112 | my ($self,$canvas, $id, $label, $location,$size,$action)=@_; | ||
| 113 | $canvas->{"combo$id"}=Qt::ComboBox; | ||
| 114 | if (defined $oVars{$label}){ | ||
| 115 | my @strings2 = split(",",$oVars{$label}); | ||
| 116 | foreach (@strings2){ | ||
| 117 | $canvas->{"combo$id"}->addItem($_); | ||
| 118 | } | ||
| 119 | $canvas->{"combo$id"}->setParent($canvas); | ||
| 120 | $canvas->{"combo$id"}->setGeometry(${$location}[0],${$location}[1],${$size}[0],${$size}[1]); | ||
| 121 | $self->connect($canvas->{"combo".$id}, SIGNAL 'currentIndexChanged(int)', $self->{SigMan}, SLOT 'map()'); | ||
| 122 | $self->{SigMan}->setMapping($canvas->{"combo".$id}, "combo".$id); | ||
| 123 | |||
| 124 | } | ||
| 125 | else {print "Combo options not defined for 'combo$id' with label $label\n"} | ||
| 126 | |||
| 127 | } | ||
| 128 | |||
| 129 | sub aMB{ | ||
| 130 | my ($self,$canvas,$currentMenu, $id, $label, $type, $action)=@_; | ||
| 131 | if (($lastMenuLabel) &&($label eq $lastMenuLabel)){return $currentMenu} # bug workaround | ||
| 132 | else {$lastMenuLabel=$label}; # in menu generator | ||
| 133 | if ($type eq "menuhead"){ | ||
| 134 | if (defined $currentMenu){ $self->menuBar()->addMenu($self->{$currentMenu}) } | ||
| 135 | $currentMenu="menu".$id; | ||
| 136 | $self->{$currentMenu} = Qt::Menu($self->tr($label),$self); | ||
| 137 | |||
| 138 | # push (@menus, $currentMenu) | ||
| 139 | } | ||
| 140 | elsif ($type eq "radio"){ | ||
| 141 | } | ||
| 142 | elsif ($type eq "check"){ | ||
| 143 | } | ||
| 144 | elsif ($type eq "separator"){ | ||
| 145 | $canvas->{$currentMenu}->addSeparator(); | ||
| 146 | } | ||
| 147 | else{ | ||
| 148 | #print "MenuItem $label \n"; | ||
| 149 | $self->{"menu".$id."Act"}=Qt::Action($self->tr($label), $self); | ||
| 150 | $self->connect($self->{"menu".$id."Act"}, SIGNAL 'triggered()', $self->{SigMan}, SLOT 'map()'); | ||
| 151 | $self->{SigMan}->setMapping($self->{"menu".$id."Act"}, "menu".$id); | ||
| 152 | $self->{$currentMenu}->addAction($self->{"menu".$id."Act"}); | ||
| 153 | } | ||
| 154 | |||
| 155 | # logging menu generator print "$currentMenu---$id----$label---$type\n"; | ||
| 156 | return $currentMenu; | ||
| 157 | } | ||
| 158 | sub aSP{ | ||
| 159 | my ($self,$canvas, $id, $panelType, $content, $location, $size)=@_; | ||
| 160 | |||
| 161 | if ($panelType eq "I"){ # Image panels start with I | ||
| 162 | $canvas->{"Image$id"}=Qt::Label(); | ||
| 163 | $canvas->{"Image$id"}->setParent($canvas); | ||
| 164 | $canvas->{"Image$id"}->setGeometry(${$location}[0],${$location}[1],${$size}[0],${$size}[1]); | ||
| 165 | if (! -e $content){ return; } | ||
| 166 | my $image = Qt::Image(Qt::String($content)); | ||
| 167 | $canvas->{"Image$id"}->setPixmap(Qt::Pixmap::fromImage( $image )->scaled(${$size}[0],${$size}[1]) ); | ||
| 168 | |||
| 169 | } | ||
| 170 | if ($panelType eq "T"){ | ||
| 171 | $canvas->{"TextCtrl".($id+1)}=Qt::PlainTextEdit; | ||
| 172 | $canvas->{"TextCtrl".($id+1)}->setPlainText($content); | ||
| 173 | $canvas->{"TextCtrl".($id+1)}->setParent($canvas); | ||
| 174 | $canvas->{"TextCtrl".($id+1)}->setGeometry(${$location}[0],${$location}[1],${$size}[0],${$size}[1]); | ||
| 175 | } | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | #functions for GUIDeFATE to load the widgets into the backend | ||
| 180 | sub addWidget{ | ||
| 181 | push (@widgets,shift ); | ||
| 182 | } | ||
| 183 | sub addStyle{ | ||
| 184 | my ($name,$style)=@_; | ||
| 185 | $styles{$name}=$style; | ||
| 186 | } | ||
| 187 | sub addVar{ | ||
| 188 | my ($varName,$value)=@_; | ||
| 189 | $oVars{$varName}=$value; | ||
| 190 | } | ||
| 191 | |||
| 192 | # Functions for internal use | ||
| 193 | sub getSize{ | ||
| 194 | my ($id)=@_; | ||
| 195 | my $found=getItem($id); | ||
| 196 | return ( $found!=-1) ? $widgets[$found][5]:0; | ||
| 197 | |||
| 198 | } | ||
| 199 | sub getLocation{ | ||
| 200 | my ($id)=@_; | ||
| 201 | my $found=getItem($id); | ||
| 202 | return ( $found!=-1) ? $widgets[$found][4]:0; | ||
| 203 | |||
| 204 | } | ||
| 205 | sub getItem{ | ||
| 206 | my ($id)=@_; | ||
| 207 | $id=~s/[^\d]//g; | ||
| 208 | my $i=0; my $found=-1; | ||
| 209 | while ($i<@widgets){ | ||
| 210 | if ($widgets[$i][1]==$id) { | ||
| 211 | $found=$i; | ||
| 212 | } | ||
| 213 | $i++; | ||
| 214 | } | ||
| 215 | return $found; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub setScale{ | ||
| 219 | $winScale=shift; | ||
| 220 | }; | ||
| 221 | |||
| 222 | |||
| 223 | # doesnt work for GFqt | ||
| 224 | sub getFrame{ | ||
| 225 | my $self=shift; | ||
| 226 | return $self; | ||
| 227 | }; | ||
| 228 | |||
| 229 | # The functions for GUI Interactions | ||
| 230 | #Static Text functions | ||
| 231 | sub setLabel{ | ||
| 232 | my ($id,$text)=@_; | ||
| 233 | $frame->{$id}->setText($text); | ||
| 234 | } | ||
| 235 | |||
| 236 | |||
| 237 | #Image functions | ||
| 238 | sub setImage{ | ||
| 239 | my ($id,$imageFile)=@_; | ||
| 240 | my @widget=@{$widgets[getItem($id)]}; | ||
| 241 | my $size= $widget[5]; | ||
| 242 | my $image = Qt::Image(Qt::String($imageFile)) or die "could not load Image $!"; | ||
| 243 | $frame->{$id}->setPixmap(Qt::Pixmap::fromImage( $image )->scaled(${$size}[0],${$size}[1]) ); | ||
| 244 | |||
| 245 | } | ||
| 246 | |||
| 247 | #Text input functions | ||
| 248 | sub getValue{ | ||
| 249 | my ($id)=@_; | ||
| 250 | if ($id=~/TextCtrl/){ return $frame->{$id}->toPlainText() } | ||
| 251 | elsif ($id=~/textctrl/){ return $frame->{$id}->text();} | ||
| 252 | elsif ($id=~/combo/){ return $frame->{$id}->currentText();} | ||
| 253 | |||
| 254 | } | ||
| 255 | sub setValue{ | ||
| 256 | my ($id,$text)=@_; | ||
| 257 | if ($id=~/TextCtrl/){ $frame->{$id}->setPlainText($text) } | ||
| 258 | elsif ($id=~/textctrl/){ return $frame->{$id}->setText($text)} | ||
| 259 | } | ||
| 260 | sub appendValue{ | ||
| 261 | my ($id,$text)=@_; | ||
| 262 | if ($id=~/TextCtrl/){ $frame->{$id}->appendPlainText($text) } | ||
| 263 | elsif ($id=~/textctrl/){ return $frame->{$id}->insert($text)} | ||
| 264 | } | ||
| 265 | |||
| 266 | #Message box, Fileselector and Dialog Boxes | ||
| 267 | sub showFileSelectorDialog{ | ||
| 268 | my ($message,$load,$filter) = @_; | ||
| 269 | my $dialog=Qt::Dialog; my $fileName; | ||
| 270 | if ($load){ | ||
| 271 | $fileName = Qt::FileDialog::getOpenFileName($dialog, | ||
| 272 | $dialog->tr("Open file"), | ||
| 273 | $message, | ||
| 274 | $dialog->tr('All Files (*)') ); | ||
| 275 | } | ||
| 276 | |||
| 277 | else{ | ||
| 278 | $fileName = Qt::FileDialog::getSaveFileName($dialog, | ||
| 279 | $dialog->tr("Save file"), | ||
| 280 | $message, | ||
| 281 | $dialog->tr('All Files (*)') ); | ||
| 282 | } | ||
| 283 | return $fileName; | ||
| 284 | }; | ||
| 285 | |||
| 286 | sub showDialog{ #unblessed so $self not passed | ||
| 287 | my ( $title, $message, $response, $icon) = @_; | ||
| 288 | my $dialog=Qt::Dialog; my $reply; | ||
| 289 | |||
| 290 | my %responses=( YNC => Qt::MessageBox::Yes() | Qt::MessageBox::No() | Qt::MessageBox::Cancel(), | ||
| 291 | YN => Qt::MessageBox::Yes() | Qt::MessageBox::No(), | ||
| 292 | OK => Qt::MessageBox::Ok(), | ||
| 293 | OKC => Qt::MessageBox::Ok() | Qt::MessageBox::Cancel() ); | ||
| 294 | |||
| 295 | $response=$response?$responses{$response}:Qt::MessageBox::Ok(); | ||
| 296 | if ($icon =~ /W|E|H/){ | ||
| 297 | $reply = Qt::MessageBox::critical($dialog, $dialog->tr($title), | ||
| 298 | $message, | ||
| 299 | $response); | ||
| 300 | } | ||
| 301 | elsif ($icon =~ /Q/){ | ||
| 302 | $reply = Qt::MessageBox::question($dialog, $dialog->tr($title), | ||
| 303 | $message, | ||
| 304 | $response); | ||
| 305 | } | ||
| 306 | else{ | ||
| 307 | $reply = Qt::MessageBox::information($dialog, $dialog->tr($title), | ||
| 308 | $message, | ||
| 309 | $response); | ||
| 310 | } | ||
| 311 | return (($reply==Qt::MessageBox::Yes() )||($reply==Qt::MessageBox::Ok())); | ||
| 312 | }; | ||
| 313 | |||
| 314 | # Quit | ||
| 315 | sub quit{ | ||
| 316 | $app ->quit(); | ||
| 317 | } | ||
| 318 | |||
| 319 | |||
| 320 | 1; |