App-PLab

 view release on metacpan or  search on metacpan

lib/App/PLab/ImageApp.pm  view on Meta::CPAN

      $extname =~ s/\.[^.]*$/\.$ext/
   } else {
      $extname .= ".$ext";
   }
   my $extsave = length($w-> {ini}-> {extSaveDir}) ? $w-> {ini}-> {extSaveDir} : $w-> {ini}-> {path};
   return "$extsave/$extname";
}

sub win_extwarn
{
   my $w = $_[0];
   return unless length $w->{ini}->{extSaveDir};
   return if $w->{ini}->{silentExtDir};
   my $ret = Prima::MsgBox::message_box( $::application-> name, ".".$w-> {dataExt}." save path is ".
      $w->{ini}->{extSaveDir}.".\nDo you want to set it to the current directory, as default?",
      mb::YesNo | mb::Abort | mb::Warning, {
      buttons => { mb::Abort, { text => 'Ne~ver' }},
    });
   if ( $ret == mb::Yes) {
      $w->{ini}->{extSaveDir} = '';
      $w-> win_extpathchanged;
   } elsif ( $ret == mb::Abort) {
      Prima::MsgBox::message("You will be never asked again, until you manually re-set the save path to the default", mb::Information);
      $w->{ini}->{silentExtDir} = 1;
   }
}


sub win_extpathchanged
{
}

sub win_newframe
{
   $_[0]-> modified( 0);
}


sub win_saveframe
{
   return 1;
}

sub win_closeframe
{
}

sub win_framechanged
{
   my $w = $_[0];
   $w-> menu-> FileCloseImage-> enabled( defined $w-> {file});
}

sub win_newextras
{
   my $w = $_[0];
   my $num = $w-> {cypherMask};
   my $file = $w->{file};
   return unless $file;
   my ($fileNum, $fileBeg, $fileEnd, $ff);
   if ( $file =~ /(.*)(\d{$num})(\.(?:tif|gif|jpg|bmp|pcx|png))$/i) {
      ($fileBeg,$fileNum,$fileEnd) = ($1,$2,$3);
      $w-> {prevFile} = $ff
         if ( $fileNum > 0) && ( -f ($ff = sprintf("%s%0${num}d%s",$fileBeg,$fileNum-1,$fileEnd)));
      $w-> {nextFile} = $ff
         if ( $fileNum < 10 ** $num - 1) && ( -f ($ff = sprintf("%s%0${num}d%s",$fileBeg,$fileNum+1,$fileEnd)));
      $w-> {fileBeg} = $fileBeg;
      $w-> {fileEnd} = $fileEnd;
      $w-> {fileNum} = int($fileNum);
   }
}

sub win_closeextras
{
   my $w = $_[0];
   $w-> {$_} = undef for qw( nextFile prevFile fileNum fileBeg fileEnd);
}

sub win_extraschanged
{
   my $w = $_[0];
   my $tb = $w-> ToolBar;
   $w-> menu-> FileNextImage-> enabled( defined $w-> {nextFile});
   $tb-> NextImage-> enabled( defined $w-> {nextFile});
   $w-> menu-> FilePrevImage-> enabled( defined $w-> {prevFile});
   $tb-> PrevImage-> enabled( defined $w-> {prevFile});
   $w-> menu-> FileNext5Image-> enabled( defined $w-> {nextFile});
   $w-> menu-> FilePrev5Image-> enabled( defined $w-> {prevFile});
   $w-> menu-> FileLastImage -> enabled( defined $w-> {nextFile});
   $w-> menu-> FileFirstImage-> enabled( defined $w-> {prevFile});
}

sub win_openfile
{
   my $w   = $_[0];
   my $d   = $w-> dlg_file(
      cwd         => 1,
      directory   => $w->{ini}->{path},
      filterIndex => 0,
      multiSelect => 0,
      filter      => [
         ['Images' => '*.bmp;*.pcx;*.gif;*.jpg;*.png;*.tif'],
         ['All files' => '*.*'],
      ]
   );
   if ( defined $w->{file} && $w-> {file} =~ /([^\\\/]*)$/) {
      my $fname = $1;
      my $i = 0;
      my @items = @{$d-> Files-> items};
      for ( @items) {
         last if $fname eq $items[ $i];
         $i++;
      }
      if ( $i <= @items) {
         $d-> Files-> focusedItem( $i);
         $d-> fileName( $fname);
      }
   }
   $w-> iv_cancelmagnify( $w-> IV);
   return $w-> win_loadfile( $d->fileName) if $d-> execute;
   return 0;
}

sub win_openserfile
{
   my $w = $_[0];
   my $dir = eval { Cwd::abs_path( $w-> {ini}-> {path})};
   $dir = '.' if $@;
   $dir = '' unless -d $dir;
   my $d = $w->{fileserDlg} ? $w->{fileserDlg} : App::PLab::SerOpenDialog-> create(
      owner     => $w,
      directory => $dir,
      filter    => [
        ['Images' => '*.bmp;*.pcx;*.gif;*.jpg;*.png;*.tif'],
        ['All files' => '*.*'],
      ]
   );
   $w-> iv_cancelmode( $w-> IV);
   if ( $d-> execute) {
        $w-> win_loadfile( $d-> fileName);
   }
   $w->{fileserDlg} = $d;
}


sub win_loadfile
{
   my ($w,$file) = @_;
   return 0 unless defined $file;
   my $path = $file;
   $path =~ s{[/\\][^/\\]*$}{};
   $w-> {preloadfile} = $file;

   if ( defined $w-> {file}) {
      return unless $w-> win_saveframe;
   }

   my $self = $w-> IV;
   $w-> iv_cancelmode( $self);
   $self-> {savePointer} = $self->pointer;
   $self-> pointer(cr::Wait);
   my $i = Prima::Image-> create;
   unless ( $i-> load( $file)) {
      $self->pointer($self-> {savePointer});
      Prima::MsgBox::message_box( $::application-> name, "Error loading file $file", mb::OK|mb::Error);
      return 0;
   }
   $w-> {preloadfile} = undef;

   $w-> win_closeextras;
   $w-> win_closeframe;
   $file =~ m{[/\\]([^/\\]*)$};
   $w-> text( $::application-> name . " - [$1]");
   $w-> {ini}-> {path} = $path;
   $w-> {file} = $file;
   $w-> IV-> image( $i);
   ($w-> {IVx}, $w->{IVy}) = $w-> IV-> image-> size;

   $w-> win_newframe;
   $w-> win_newextras;
   $w-> win_framechanged;
   $w-> win_extraschanged;

   $w-> IV-> repaint;

   $w-> sb_text( "$file loaded OK");
   $self-> pointer($self-> {savePointer});

   return 1;
}

sub win_closefile
{
   my $w = $_[0];
   return 1 unless defined $w->{file};
   return 0 if !$w-> win_saveframe;
   $w-> win_closeframe;
   $w-> win_closeextras;

   $w-> text( $::application-> name);
   $w-> {file} = undef;
   $w-> IV-> image( undef);
   $w-> {IVx} = $w-> {IVy} = 0;
   $w-> IV-> repaint;
   $w-> win_framechanged;
   $w-> win_extraschanged;
   return 1;
}


sub win_nextfile
{
   $_[0]-> win_loadfile( $_[0]-> {nextFile}) if defined $_[0]-> {nextFile};
}

sub win_getseriesrange
{
   my $w = $_[0];

   return (0,0) unless defined $w->{fileNum};
   my $path = $w-> {ini}-> {path};
   my @d;

   if ( exists $w-> {cachedDir} && ( $w-> {cachedDir} eq $path) &&
      (( time - $w-> {cachedDirTime}) < $w-> {ini}-> {dirTimeout})) {
      @d = @{$w-> {cachedDirContent}};
   } else {
      warn("Cannot read directory $path:$!"), return (0,0) unless opendir DIR, $path;
      @d = readdir DIR;
      closedir DIR;
      $w-> {cachedDir}        = $path;
      $w-> {cachedDirTime}    = time;
      $w-> {cachedDirContent} = \@d;
   }

   my ( $min, $max) = ( 0, 10 ** $w-> {cypherMask} - 1);
   my ( $fmin, $fmax, $i) = ( $w->{fileNum}, $w->{fileNum});
   my @bix = ();
   my $num = $w->{cypherMask};
   my $fbeg = $w->{fileBeg};
   $fbeg =~ s{.*[/\\]([^/\\]*)$}{$1};

   for (@d) {
      next unless /(.*)(\d{$num})(\.(?:tif|gif|jpg|bmp|pcx|png))$/i;
      next unless $1 eq $fbeg and $3 eq $w->{fileEnd};
      $bix[ $2] = 1;
   }

   for ( $i = $w->{fileNum}; $i >= $min; $i--) {
       last unless $bix[ $i];
       $fmin = $i;
   }

   for ( $i = $w->{fileNum}; $i <= $max; $i++) {
       last unless $bix[ $i];
       $fmax = $i;
   }

   return $fmin, $fmax;
}

sub win_formfilename
{
   my ( $w, $fnum) = @_;
   my $num = $w-> {cypherMask};
   return sprintf("%s%0${num}d%s",$w->{fileBeg}, $fnum, $w->{fileEnd});
}

sub win_next5file
{
   my $w = $_[0];
   return unless defined $w-> {nextFile};
   my ( $min, $max) = $w-> win_getseriesrange;
   $w-> win_loadfile( $w-> win_formfilename(( $w-> {fileNum} + 5 > $max) ? $max : ( $w-> {fileNum} + 5)));
}

sub win_lastfile
{
   my $w = $_[0];
   return unless defined $w-> {nextFile};
   my ( $min, $max) = $w-> win_getseriesrange;
   $w-> win_loadfile( $w-> win_formfilename( $max));
}

sub win_prevfile
{
   $_[0]-> win_loadfile( $_[0]-> {prevFile}) if defined $_[0]-> {prevFile};
}

sub win_prev5file
{
   my $w = $_[0];
   return unless defined $w-> {prevFile};
   my ( $min, $max) = $w-> win_getseriesrange;
   $w-> win_loadfile( $w-> win_formfilename(( $w-> {fileNum} - 5 < $min) ? $min : ( $w-> {fileNum} - 5)));
}

sub win_firstfile
{
   my $w = $_[0];
   return unless defined $w-> {prevFile};
   my ( $min, $max) = $w-> win_getseriesrange;
   $w-> win_loadfile( $w-> win_formfilename( $min));
}



( run in 2.584 seconds using v1.01-cache-2.11-cpan-df04353d9ac )