ProgressStatusBar.pm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # Status bar at the bottom of the main screen.
  2. package Slic3r::GUI::ProgressStatusBar;
  3. use strict;
  4. use warnings;
  5. use Wx qw(:gauge :misc);
  6. use base 'Wx::StatusBar';
  7. sub new {
  8. my $class = shift;
  9. my $self = $class->SUPER::new(@_);
  10. $self->{busy} = 0;
  11. $self->{timer} = Wx::Timer->new($self);
  12. $self->{prog} = Wx::Gauge->new($self, wxGA_HORIZONTAL, 100, wxDefaultPosition, wxDefaultSize);
  13. $self->{prog}->Hide;
  14. $self->{cancelbutton} = Wx::Button->new($self, -1, "Cancel", wxDefaultPosition, wxDefaultSize);
  15. $self->{cancelbutton}->Hide;
  16. $self->SetFieldsCount(3);
  17. $self->SetStatusWidths(-1, 150, 155);
  18. Wx::Event::EVT_TIMER($self, \&OnTimer, $self->{timer});
  19. Wx::Event::EVT_SIZE($self, \&OnSize);
  20. Wx::Event::EVT_BUTTON($self, $self->{cancelbutton}, sub {
  21. $self->{cancel_cb}->();
  22. $self->{cancelbutton}->Hide;
  23. });
  24. return $self;
  25. }
  26. sub DESTROY {
  27. my $self = shift;
  28. $self->{timer}->Stop if $self->{timer} && $self->{timer}->IsRunning;
  29. }
  30. sub OnSize {
  31. my ($self, $event) = @_;
  32. my %fields = (
  33. # 0 is reserved for status text
  34. 1 => $self->{cancelbutton},
  35. 2 => $self->{prog},
  36. );
  37. foreach (keys %fields) {
  38. my $rect = $self->GetFieldRect($_);
  39. my $offset = &Wx::wxGTK ? 1 : 0; # add a cosmetic 1 pixel offset on wxGTK
  40. my $pos = [$rect->GetX + $offset, $rect->GetY + $offset];
  41. $fields{$_}->Move($pos);
  42. $fields{$_}->SetSize($rect->GetWidth - $offset, $rect->GetHeight);
  43. }
  44. $event->Skip;
  45. }
  46. sub OnTimer {
  47. my ($self, $event) = @_;
  48. if ($self->{prog}->IsShown) {
  49. $self->{timer}->Stop;
  50. }
  51. $self->{prog}->Pulse if $self->{_busy};
  52. }
  53. sub SetCancelCallback {
  54. my $self = shift;
  55. my ($cb) = @_;
  56. $self->{cancel_cb} = $cb;
  57. $cb ? $self->{cancelbutton}->Show : $self->{cancelbutton}->Hide;
  58. }
  59. sub Run {
  60. my $self = shift;
  61. my $rate = shift || 100;
  62. if (!$self->{timer}->IsRunning) {
  63. $self->{timer}->Start($rate);
  64. }
  65. }
  66. sub GetProgress {
  67. my $self = shift;
  68. return $self->{prog}->GetValue;
  69. }
  70. sub SetProgress {
  71. my $self = shift;
  72. my ($val) = @_;
  73. if (!$self->{prog}->IsShown) {
  74. $self->ShowProgress(1);
  75. }
  76. if ($val == $self->{prog}->GetRange) {
  77. $self->{prog}->SetValue(0);
  78. $self->ShowProgress(0);
  79. } else {
  80. $self->{prog}->SetValue($val);
  81. }
  82. }
  83. sub SetRange {
  84. my $self = shift;
  85. my ($val) = @_;
  86. if ($val != $self->{prog}->GetRange) {
  87. $self->{prog}->SetRange($val);
  88. }
  89. }
  90. sub ShowProgress {
  91. my $self = shift;
  92. my ($show) = @_;
  93. $self->{prog}->Show($show);
  94. $self->{prog}->Pulse;
  95. }
  96. sub StartBusy {
  97. my $self = shift;
  98. my $rate = shift || 100;
  99. $self->{_busy} = 1;
  100. $self->ShowProgress(1);
  101. if (!$self->{timer}->IsRunning) {
  102. $self->{timer}->Start($rate);
  103. }
  104. }
  105. sub StopBusy {
  106. my $self = shift;
  107. $self->{timer}->Stop;
  108. $self->ShowProgress(0);
  109. $self->{prog}->SetValue(0);
  110. $self->{_busy} = 0;
  111. }
  112. sub IsBusy {
  113. my $self = shift;
  114. return $self->{_busy};
  115. }
  116. 1;