| 1 | package App::financeta::gui::progress_bar; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use 5.10.0; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use App::financeta::mo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Log::Any '$log', filter => \&App::financeta::utils::log_filter; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use Prima qw(Application sys::GUIException Utils ); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use POSIX qw(floor); | ||
| 10 | |||
| 11 | $|=1; | ||
| 12 | |||
| 13 | has owner => undef; | ||
| 14 | has bar => ( builder => '_build_bar' ); | ||
| 15 | has title => 'Loading...'; | ||
| 16 | has bar_width => 100; | ||
| 17 | has bar_height => 40; | ||
| 18 | |||
| 19 | sub _build_bar { | ||
| 20 | my $self = shift; | ||
| 21 | $log->debug("Creating progress bar"); | ||
| 22 | my $bar = Prima::Window->create( | ||
| 23 | name => 'progress_bar', | ||
| 24 | text => $self->title, | ||
| 25 | size => [$self->bar_width, $self->bar_height], | ||
| 26 | origin => [0, 0], | ||
| 27 | widgetClass => wc::Dialog, | ||
| 28 | borderStyle => bs::Dialog, | ||
| 29 | borderIcons => 0, | ||
| 30 | hint => $self->title, | ||
| 31 | showHint => 1, | ||
| 32 | centered => 1, | ||
| 33 | owner => $self->owner, | ||
| 34 | visible => 1, | ||
| 35 | pointerType => cr::Wait, | ||
| 36 | onPaint => sub { | ||
| 37 | my ($w, $canvas) = @_; | ||
| 38 | $canvas->color(cl::Blue); | ||
| 39 | $canvas->bar(0, 0, $w->{-progress}, $w->height); | ||
| 40 | $canvas->color(cl::Back); | ||
| 41 | $canvas->bar($w->{-progress}, 0, $w->size); | ||
| 42 | my $pct = floor(100 * $w->{-progress} / $w->width); | ||
| 43 | if ($pct > 0) { | ||
| 44 | $canvas->color(cl::Yellow); | ||
| 45 | $canvas->font(size => 10, style => fs::Bold); | ||
| 46 | $canvas->text_out(sprintf("%d%%", $pct), 0, 10) if $pct > 0; | ||
| 47 | } | ||
| 48 | }, | ||
| 49 | syncPaint => 1, | ||
| 50 | onTop => 1, | ||
| 51 | ); | ||
| 52 | $bar->{-progress} = 0; | ||
| 53 | $bar->repaint; | ||
| 54 | if (defined $bar->owner) { | ||
| 55 | $bar->owner->pointerType(cr::Wait); | ||
| 56 | $bar->owner->repaint; | ||
| 57 | } | ||
| 58 | return $bar; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub update { | ||
| 62 | my ($self, $val) = @_; | ||
| 63 | ## is percentage | ||
| 64 | if (defined $val and ($val > 0 and $val < 1)) { | ||
| 65 | $self->bar->{-progress} = ($val * $self->bar_width); | ||
| 66 | } elsif (defined $val) {#is absolute | ||
| 67 | $self->bar->{-progress} = $val; | ||
| 68 | } else { | ||
| 69 | $self->bar->{-progress} += 5; | ||
| 70 | } | ||
| 71 | $self->bar->repaint; | ||
| 72 | if (defined $self->bar->owner) { | ||
| 73 | $self->bar->owner->repaint; | ||
| 74 | } | ||
| 75 | return $self->bar->{-progress}; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub close { | ||
| 79 | my $self = shift; | ||
| 80 | if (defined $self and defined $self->bar) { | ||
| 81 | if (defined $self->bar->owner) { | ||
| 82 | $self->bar->owner->pointerType(cr::Default); | ||
| 83 | $self->bar->owner->repaint; | ||
| 84 | } | ||
| 85 | $self->bar->close; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | sub progress { | ||
| 90 | return shift->bar->{-progress}; | ||
| 91 | } | ||
| 92 | |||
| 93 | 1; | ||
| 94 | __END__ | ||