| 1 | # | ||
| 2 | # This file is part of App-CPAN2Pkg | ||
| 3 | # | ||
| 4 | # This software is copyright (c) 2009 by Jerome Quelin. | ||
| 5 | # | ||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||
| 8 | # | ||
| 9 | use 5.012; | ||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | package App::CPAN2Pkg::UI::Tk; | ||
| 14 | # ABSTRACT: main cpan2pkg window | ||
| 15 | $App::CPAN2Pkg::UI::Tk::VERSION = '3.004'; | ||
| 16 | use DateTime; | ||
| 17 | use List::Util qw{ first }; | ||
| 18 | use Moose; | ||
| 19 | use MooseX::Has::Sugar; | ||
| 20 | use MooseX::POE; | ||
| 21 | use MooseX::SemiAffordanceAccessor; | ||
| 22 | use Readonly; | ||
| 23 | use Tk; | ||
| 24 | use Tk::Balloon; | ||
| 25 | use Tk::HList; | ||
| 26 | use Tk::NoteBook; | ||
| 27 | use Tk::PNG; | ||
| 28 | use Tk::ROText; | ||
| 29 | use Tk::Sugar; | ||
| 30 | |||
| 31 | with 'Tk::Role::HasWidgets'; | ||
| 32 | |||
| 33 | use App::CPAN2Pkg::Utils qw{ $SHAREDIR }; | ||
| 34 | |||
| 35 | Readonly my $K => $poe_kernel; | ||
| 36 | Readonly my $mw => $poe_main_window; # already created by poe | ||
| 37 | |||
| 38 | |||
| 39 | # -- attributes | ||
| 40 | |||
| 41 | # it's not usually a good idea to retain a reference on a poe session, | ||
| 42 | # since poe is already taking care of the references for us. however, we | ||
| 43 | # need the session to call ->postback() to set the various gui callbacks | ||
| 44 | # that will be fired upon gui events. | ||
| 45 | has _session => ( rw, weak_ref, isa=>'POE::Session' ); | ||
| 46 | |||
| 47 | |||
| 48 | # -- initialization | ||
| 49 | |||
| 50 | # | ||
| 51 | # START() | ||
| 52 | # | ||
| 53 | # called as poe session initialization. | ||
| 54 | # | ||
| 55 | sub START { | ||
| 56 | my ($self, $session) = @_[OBJECT, SESSION]; | ||
| 57 | $K->alias_set('main'); | ||
| 58 | $self->_set_session($session); | ||
| 59 | $self->_build_gui; | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | # -- public logging events | ||
| 64 | |||
| 65 | { | ||
| 66 | |||
| 67 | |||
| 68 | event log_out => sub { | ||
| 69 | my ($self, $module, $line) = @_[OBJECT, ARG0 .. $#_ ]; | ||
| 70 | my $rotext = $self->_w( "rotext_$module" ); | ||
| 71 | $rotext->insert( 'end', "$line\n" ); | ||
| 72 | $rotext->yview( 'end' ); | ||
| 73 | }; | ||
| 74 | event log_err => sub { | ||
| 75 | my ($self, $module, $line) = @_[OBJECT, ARG0 .. $#_ ]; | ||
| 76 | my $rotext = $self->_w( "rotext_$module" ); | ||
| 77 | $rotext->insert( 'end', "$line\n", "error" ); | ||
| 78 | $rotext->yview( 'end' ); | ||
| 79 | }; | ||
| 80 | event log_comment => sub { | ||
| 81 | my ($self, $module, $line) = @_[OBJECT, ARG0 .. $#_ ]; | ||
| 82 | my $rotext = $self->_w( "rotext_$module" ); | ||
| 83 | my $timestamp = DateTime->now(time_zone=>"local")->hms; | ||
| 84 | $rotext->insert( 'end', "* $timestamp $line\n", "comment" ); | ||
| 85 | $rotext->yview( 'end' ); | ||
| 86 | }; | ||
| 87 | event log_result => sub { | ||
| 88 | my ($self, $module, $result) = @_[OBJECT, ARG0 .. $#_ ]; | ||
| 89 | my $rotext = $self->_w( "rotext_$module" ); | ||
| 90 | my $timestamp = DateTime->now(time_zone=>"local")->hms; | ||
| 91 | $rotext->insert( 'end', "* $timestamp $result\n", "result" ); | ||
| 92 | $rotext->yview( 'end' ); | ||
| 93 | }; | ||
| 94 | event log_step => sub { | ||
| 95 | my ($self, $module, $step) = @_[OBJECT, ARG0 .. $#_ ]; | ||
| 96 | my $rotext = $self->_w( "rotext_$module" ); | ||
| 97 | $rotext->insert( 'end', "\n\n** $step\n\n", "step" ); | ||
| 98 | $rotext->yview( 'end' ); | ||
| 99 | }; | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | event module_state => sub { | ||
| 104 | my ($self, $module) = @_[OBJECT, ARG0 .. $#_ ]; | ||
| 105 | my $modname = $module->name; | ||
| 106 | |||
| 107 | # find relevant line in hlist | ||
| 108 | my $hlist = $self->_w( "hlist" ); | ||
| 109 | my @children = $hlist->info( 'children' ); | ||
| 110 | my $elem = first { $hlist->info(data=>$_) eq $modname } @children; | ||
| 111 | |||
| 112 | # get bullet color | ||
| 113 | my %color = ( | ||
| 114 | "not started" => "black", | ||
| 115 | "not available" => "yellow", | ||
| 116 | importing => "purple", | ||
| 117 | building => "orange", | ||
| 118 | installing => "blue", | ||
| 119 | available => "green", | ||
| 120 | error => "red", | ||
| 121 | ); | ||
| 122 | my $colorl = $color{ $module->local->status }; | ||
| 123 | my $coloru = $color{ $module->upstream->status }; | ||
| 124 | |||
| 125 | # update bullets | ||
| 126 | my $bulletl = _image( $SHAREDIR->file("bullets", "$colorl.png") ); | ||
| 127 | my $bulletu = _image( $SHAREDIR->file("bullets", "$coloru.png") ); | ||
| 128 | $hlist->itemConfigure( $elem, 0, -image=>$bulletl ); | ||
| 129 | $hlist->itemConfigure( $elem, 1, -image=>$bulletu ); | ||
| 130 | |||
| 131 | # $self->_w( "btn_close_$modname" )->configure( enabled ) | ||
| 132 | # if $module->local->status eq 'available' | ||
| 133 | # && $module->upstream->status eq 'available'; | ||
| 134 | |||
| 135 | $hlist->update; | ||
| 136 | |||
| 137 | # allow user to mark a module as available | ||
| 138 | return unless $module->local->status eq "error" | ||
| 139 | or $module->upstream->status eq "error"; | ||
| 140 | my $nb = $self->_w('notebook'); | ||
| 141 | my $pane = $nb->page_widget( $modname ); | ||
| 142 | my $button = $pane->Button( | ||
| 143 | -text => 'Mark module as available (locally & upstream)', | ||
| 144 | -command => $self->_session->postback( "_on_btn_mark_available", $module ), | ||
| 145 | )->pack( top, fillx ); | ||
| 146 | $self->_set_w( "mark_available_$modname" => $button ); | ||
| 147 | $nb->update; | ||
| 148 | }; | ||
| 149 | |||
| 150 | # -- public events | ||
| 151 | |||
| 152 | |||
| 153 | event new_module => sub { | ||
| 154 | my ($self, $module) = @_[OBJECT, ARG0]; | ||
| 155 | my $modname = $module->name; | ||
| 156 | |||
| 157 | # calculate module position in the list | ||
| 158 | my $hlist = $self->_w('hlist'); | ||
| 159 | my @children = $hlist->info( 'children' ); | ||
| 160 | my $next = first { $hlist->info(data=>$_) gt $modname } @children; | ||
| 161 | my @pos = defined $next ? ( -before => $next ) : ( -at => -1 ); | ||
| 162 | |||
| 163 | # create module in the list | ||
| 164 | my $bullet = _image( $SHAREDIR->file("bullets", "black.png") ); | ||
| 165 | my $elem = $hlist->addchild( "", -data=>$modname, @pos ); | ||
| 166 | $hlist->itemCreate( $elem, 0, -itemtype => 'image', -image=>$bullet ); | ||
| 167 | $hlist->itemCreate( $elem, 1, -itemtype => 'image', -image=>$bullet ); | ||
| 168 | $hlist->itemCreate( $elem, 2, -itemtype => 'text', -text=>$modname ); | ||
| 169 | $hlist->see( $elem ); | ||
| 170 | |||
| 171 | # create new pane in the notebook | ||
| 172 | my $nb = $self->_w('notebook'); | ||
| 173 | my $pane = $nb->add( $modname, -label=>$modname ); | ||
| 174 | $nb->raise( $modname ); | ||
| 175 | my $rotext = $pane->Scrolled( 'ROText', -scrollbars => 'e' )->pack( top, xfill2 ); | ||
| 176 | $rotext->tag( configure => step => -font => "FNbig" ); | ||
| 177 | $rotext->tag( configure => error => -foreground => "firebrick" ); | ||
| 178 | $rotext->tag( configure => result => -foreground => "steelblue" ); | ||
| 179 | $self->_set_w( "rotext_$modname", $rotext ); | ||
| 180 | |||
| 181 | # close button | ||
| 182 | # my $b = $pane->Button( | ||
| 183 | # -text => "Clean finished module", | ||
| 184 | # -command => $self->_session->postback( "_on_btn_clean", $modname ), | ||
| 185 | # disabled, | ||
| 186 | # )->pack( top, fillx ); | ||
| 187 | # $self->_set_w( "btn_close_$modname", $b ); | ||
| 188 | |||
| 189 | $nb->update; | ||
| 190 | }; | ||
| 191 | |||
| 192 | |||
| 193 | # -- gui events | ||
| 194 | |||
| 195 | #event _on_btn_clean => sub { | ||
| 196 | # my ($self, $args) = @_[OBJECT, ARG0]; | ||
| 197 | # my ($modname) = @$args; | ||
| 198 | # $self->_w('notebook')->delete( $modname ); | ||
| 199 | # | ||
| 200 | # my $hlist = $self->_w( 'hlist' ); | ||
| 201 | # my @children = $hlist->info( 'children' ); | ||
| 202 | # my $elem = first { $hlist->info(data=>$_) eq $modname } @children; | ||
| 203 | # $hlist->delete( entry => $elem ); | ||
| 204 | #}; | ||
| 205 | |||
| 206 | event _on_btn_clean_all => sub { | ||
| 207 | my $self = shift; | ||
| 208 | my $nb = $self->_w( 'notebook' ); | ||
| 209 | my $hlist = $self->_w( 'hlist' ); | ||
| 210 | my $app = App::CPAN2Pkg->instance; | ||
| 211 | |||
| 212 | my %pages; | ||
| 213 | @pages{ $nb->info("pages") } = (); | ||
| 214 | |||
| 215 | my @modnames = $app->all_modules; | ||
| 216 | foreach my $modname ( @modnames ) { | ||
| 217 | my $module = $app->module( $modname ); | ||
| 218 | next unless exists $pages{ $modname }; | ||
| 219 | next if $module->local->status ne 'available'; | ||
| 220 | next if $module->upstream->status ne 'available'; | ||
| 221 | |||
| 222 | $nb->delete( $modname ); | ||
| 223 | my @children = $hlist->info( 'children' ); | ||
| 224 | my $elem = first { $hlist->info(data=>$_) eq $modname } @children; | ||
| 225 | $hlist->delete( entry => $elem ); | ||
| 226 | } | ||
| 227 | }; | ||
| 228 | |||
| 229 | # | ||
| 230 | # event: _on_btn_mark_available( $module ) | ||
| 231 | # | ||
| 232 | # received when user clicked the button to mark a module as available. | ||
| 233 | # | ||
| 234 | event _on_btn_mark_available => sub { | ||
| 235 | my ($self, $args) = @_[ OBJECT, ARG0 ]; | ||
| 236 | my $module = $args->[0]; | ||
| 237 | my $modname = $module->name; | ||
| 238 | |||
| 239 | # change module status and update interface | ||
| 240 | $module->local->set_status( "available" ); | ||
| 241 | $module->upstream->set_status( "available" ); | ||
| 242 | $K->yield( log_result => $modname => "Module marked available manually" ); | ||
| 243 | $K->yield( module_state => $module ); | ||
| 244 | |||
| 245 | # signal controller that module is available | ||
| 246 | $K->post( controller => module_ready_locally => $modname ); | ||
| 247 | $K->post( controller => module_ready_upstream => $modname ); | ||
| 248 | |||
| 249 | # remove the button | ||
| 250 | $self->_w( "mark_available_$modname" )->destroy; | ||
| 251 | $self->_del_w( "mark_available_$modname" ); | ||
| 252 | $self->_w('notebook')->update; | ||
| 253 | }; | ||
| 254 | |||
| 255 | # | ||
| 256 | # event: _on_btn_submit() | ||
| 257 | # | ||
| 258 | # received when user clicked the submit button. | ||
| 259 | # | ||
| 260 | event _on_btn_submit => sub { | ||
| 261 | my $self = shift; | ||
| 262 | my $entry = $self->_w( "ent_module" ); | ||
| 263 | my $module = $entry->get; | ||
| 264 | return unless $module; | ||
| 265 | $K->post( controller => new_module_wanted => $module ); | ||
| 266 | $entry->delete( 0, 'end' ); | ||
| 267 | }; | ||
| 268 | |||
| 269 | event _on_hlist_2click => sub { | ||
| 270 | my $self = shift; | ||
| 271 | my $hlist = $self->_w('hlist'); | ||
| 272 | my ($elem) = $hlist->info('selection'); | ||
| 273 | my $module = $hlist->info( data => $elem ); | ||
| 274 | $self->_w('notebook')->raise($module); | ||
| 275 | }; | ||
| 276 | |||
| 277 | |||
| 278 | # -- gui creation | ||
| 279 | |||
| 280 | # | ||
| 281 | # $main->_build_gui; | ||
| 282 | # | ||
| 283 | # create the various gui elements. | ||
| 284 | # | ||
| 285 | sub _build_gui { | ||
| 286 | my $self = shift; | ||
| 287 | my $s = $self->_session; | ||
| 288 | |||
| 289 | # hide window during its creation to avoid flickering | ||
| 290 | $mw->withdraw; | ||
| 291 | |||
| 292 | # prettyfying tk app. | ||
| 293 | # see http://www.perltk.org/index.php?option=com_content&task=view&id=43&Itemid=37 | ||
| 294 | $mw->optionAdd('*BorderWidth' => 1); | ||
| 295 | |||
| 296 | # set windowtitle | ||
| 297 | $mw->title('cpan2pkg'); | ||
| 298 | $mw->iconimage( _image( $SHAREDIR->file('icon.png') ) ); | ||
| 299 | $mw->iconmask ( '@' . $SHAREDIR->file('icon-mask.xbm') ); | ||
| 300 | |||
| 301 | # make sure window is big enough | ||
| 302 | #my $config = Games::Pandemic::Config->instance; | ||
| 303 | #my $width = $config->get( 'win_width' ); | ||
| 304 | #my $height = $config->get( 'win_height' ); | ||
| 305 | #$mw->geometry($width . 'x' . $height); | ||
| 306 | |||
| 307 | # the tooltip | ||
| 308 | $self->_set_w('tooltip', $mw->Balloon); | ||
| 309 | |||
| 310 | # font used in progression text | ||
| 311 | $mw->fontCreate( "FNbig", -weight => "bold" ); | ||
| 312 | |||
| 313 | # | ||
| 314 | my $ftop = $mw->Frame->pack( top, fillx, pad20 ); | ||
| 315 | $ftop->Label( -text => 'New module wanted:' )->pack( left, pad2 ); | ||
| 316 | my $entry = $ftop->Entry()->pack( left, xfillx, pad2 ); | ||
| 317 | $self->_set_w( ent_module => $entry ); | ||
| 318 | $ftop->Button( -text => 'submit', | ||
| 319 | -command => $s->postback( '_on_btn_submit' ), | ||
| 320 | )->pack( left, pad2 ); | ||
| 321 | $mw->bind( '<Return>', $s->postback( '_on_btn_submit' ) ); | ||
| 322 | |||
| 323 | # | ||
| 324 | my $f = $mw->Frame->pack( top, xfill2 ); | ||
| 325 | my $f1 = $f->Frame->pack( left, filly ); | ||
| 326 | my $f2 = $f->Frame->pack( left, xfill2 ); | ||
| 327 | $self->_build_hlist( $f1 ); | ||
| 328 | $self->_build_close_btn( $f1 ); | ||
| 329 | $self->_build_notebook( $f2 ); | ||
| 330 | |||
| 331 | # center & show the window | ||
| 332 | # FIXME: restore last position saved? | ||
| 333 | $mw->Popup; | ||
| 334 | $mw->minsize($mw->width, $mw->height); | ||
| 335 | $self->_w("ent_module")->focus; | ||
| 336 | } | ||
| 337 | |||
| 338 | |||
| 339 | # | ||
| 340 | # $main->_build_close_btn( $parent ); | ||
| 341 | # | ||
| 342 | # build the button to close all finished modules. | ||
| 343 | # | ||
| 344 | sub _build_close_btn { | ||
| 345 | my ($self, $parent) = @_; | ||
| 346 | |||
| 347 | my $b = $parent->Button( | ||
| 348 | -text => "Clean all finished modules", | ||
| 349 | -command => $self->_session->postback( "_on_btn_clean_all" ), | ||
| 350 | )->pack( bottom, fillx ); | ||
| 351 | } | ||
| 352 | |||
| 353 | # | ||
| 354 | # $main->_build_hlist( $parent ); | ||
| 355 | # | ||
| 356 | # build the hierarchical list holding all the module status. | ||
| 357 | # | ||
| 358 | sub _build_hlist { | ||
| 359 | my ($self, $parent) = @_; | ||
| 360 | |||
| 361 | my $hlist = $parent->Scrolled( 'HList', | ||
| 362 | -scrollbars => 'osoe', | ||
| 363 | -width => 30, | ||
| 364 | -columns => 3, | ||
| 365 | -header => 1, | ||
| 366 | )->pack( top, xfilly ); | ||
| 367 | $self->_set_w( hlist => $hlist ); | ||
| 368 | |||
| 369 | $hlist->header( create => 0, -text => 'local' ); | ||
| 370 | $hlist->header( create => 1, -text => 'bs' ); | ||
| 371 | $hlist->header( create => 2, -text => 'module' ); | ||
| 372 | |||
| 373 | $hlist->bind( '<Double-1>', $self->_session->postback('_on_hlist_2click') ); | ||
| 374 | } | ||
| 375 | |||
| 376 | # | ||
| 377 | # $main->_build_notebook( $parent ); | ||
| 378 | # | ||
| 379 | # build the notebook holding one pane per module being built. first tab | ||
| 380 | # contains the legend. | ||
| 381 | # | ||
| 382 | sub _build_notebook { | ||
| 383 | my ($self, $parent) = @_; | ||
| 384 | |||
| 385 | # create the notebook that will hold module details | ||
| 386 | my $nb = $parent->NoteBook->pack( top, xfill2 ); | ||
| 387 | $self->_set_w('notebook', $nb); | ||
| 388 | |||
| 389 | # create a first tab with the legend | ||
| 390 | my $legend = $nb->add("Legend", -label=>"Legend"); | ||
| 391 | |||
| 392 | #my $legend = $mw->Frame->pack( top, fillx ); | ||
| 393 | my @lab1 = ( 'not started', 'not available', 'building', 'installing', 'available', 'error' ); | ||
| 394 | my @col1 = qw{ black yellow orange blue green red }; | ||
| 395 | my @lab2 = ( 'not started', 'not available', 'importing', 'building', 'available', 'error' ); | ||
| 396 | my @col2 = qw{ black yellow purple orange green red }; | ||
| 397 | $legend->Label( -text => 'Local' ) | ||
| 398 | ->grid( -row => 0, -column => 0, -columnspan=>2, -sticky => 'w' ); | ||
| 399 | $legend->Label( -text => 'Build System' ) | ||
| 400 | ->grid( -row => 0, -column => 2, -columnspan=>2,-sticky => 'w' ); | ||
| 401 | my $buldir = $SHAREDIR->subdir( 'bullets' ); | ||
| 402 | foreach my $i ( 0 .. $#lab1 ) { | ||
| 403 | $legend->Label( -image => _image( $buldir->file( $col1[$i] . ".png" ) ) ) | ||
| 404 | ->grid( -row => $i + 1, -column => 0, ipad5 ); | ||
| 405 | $legend->Label( -image => _image( $buldir->file( $col2[$i] . ".png" ) ) ) | ||
| 406 | ->grid( -row => $i + 1, -column => 2, ipad5 ); | ||
| 407 | $legend->Label( -text => $lab1[$i] )->grid( -row=>$i+1, -column=>1, -sticky => 'w' ); | ||
| 408 | $legend->Label( -text => $lab2[$i] )->grid( -row=>$i+1, -column=>3, -sticky => 'w' ); | ||
| 409 | } | ||
| 410 | |||
| 411 | } | ||
| 412 | |||
| 413 | |||
| 414 | # -- private subs | ||
| 415 | |||
| 416 | # | ||
| 417 | # my $img = _image( $path ); | ||
| 418 | # | ||
| 419 | # Return a tk image loaded from C<$path>. If the photo has already been | ||
| 420 | # loaded, return a handle on it. | ||
| 421 | # | ||
| 422 | sub _image { | ||
| 423 | my $path = shift; | ||
| 424 | my $img = $poe_main_window->Photo($path); | ||
| 425 | return $img if $img->width; | ||
| 426 | return $poe_main_window->Photo($path, -file=>$path); | ||
| 427 | } | ||
| 428 | |||
| 429 | |||
| 430 | |||
| 431 | no Moose; | ||
| 432 | __PACKAGE__->meta->make_immutable; | ||
| 433 | 1; | ||
| 434 | |||
| 435 | __END__ |