#!/usr/bin/perl -w

use strict;
use utf8;


package SelectCover;
my $image0;
my @files = ();
my $file  = "";

use File::stat;

use Qt;
use Qt::isa qw(Qt::Widget);
use Qt::slots
    updatePreview => [],
    updateData => [],
    selectCover => [];
use Qt::attributes qw(
    TL_AvailCovers
    PL_Preview
    TL_Preview
    PB_Select
    PB_Cancel
    TL_FileSize
    TL_FileDate
    TL_Size
    TL_ImageSizeValue
    TL_FileSizeValue
    TL_FileDateValue
    LB_AvailCovers
);



sub NEW
{
    shift->SUPER::NEW(@_[0..2]);

    if ( name() eq "unnamed" )
    {
        setName("SelectCover" );
    }
    setSizePolicy(Qt::SizePolicy(1, 0, 0, 0, this->sizePolicy()->hasHeightForWidth()) );
    setMinimumSize(Qt::Size(600, 320) );
    setMaximumSize(Qt::Size(600, 320) );


    TL_AvailCovers = Qt::Label(this, "TL_AvailCovers");
    TL_AvailCovers->setGeometry( Qt::Rect(20, 20, 100, 20) );

    PL_Preview = Qt::Label(this, "PL_Preview");
    PL_Preview->setGeometry( Qt::Rect(340, 50, 240, 240) );
    PL_Preview->setSizePolicy( Qt::SizePolicy(2, 2, 0, 0, PL_Preview->sizePolicy()->hasHeightForWidth()) );
    PL_Preview->setMinimumSize( Qt::Size(240, 240) );
    PL_Preview->setScaledContents( 1 );

    TL_Preview = Qt::Label(this, "TL_Preview");
    TL_Preview->setGeometry( Qt::Rect(330, 20, 60, 20) );

    PB_Select = Qt::PushButton(this, "PB_Select");
    PB_Select->setGeometry( Qt::Rect(20, 270, 80, 30) );
    PB_Select->setDefault( 1 );

    PB_Cancel = Qt::PushButton(this, "PB_Cancel");
    PB_Cancel->setGeometry( Qt::Rect(120, 270, 80, 30) );

    TL_FileSize = Qt::Label(this, "TL_FileSize");
    TL_FileSize->setGeometry( Qt::Rect(20, 190, 60, 20) );

    TL_FileDate = Qt::Label(this, "TL_FileDate");
    TL_FileDate->setGeometry( Qt::Rect(20, 210, 60, 20) );

    TL_Size = Qt::Label(this, "TL_Size");
    TL_Size->setGeometry( Qt::Rect(20, 230, 60, 20) );

    TL_ImageSizeValue = Qt::Label(this, "TL_ImageSizeValue");
    TL_ImageSizeValue->setGeometry( Qt::Rect(80, 230, 80, 20) );

    TL_FileSizeValue = Qt::Label(this, "TL_FileSizeValue");
    TL_FileSizeValue->setGeometry( Qt::Rect(80, 190, 80, 20) );

    TL_FileDateValue = Qt::Label(this, "TL_FileDateValue");
    TL_FileDateValue->setGeometry( Qt::Rect(80, 210, 220, 20) );

    LB_AvailCovers = Qt::ListBox(this, "LB_AvailCovers");
    LB_AvailCovers->setGeometry( Qt::Rect(20, 50, 290, 130) );
    languageChange();
    my $resize = Qt::Size(600, 320);
    $resize = $resize->expandedTo(minimumSizeHint());
    resize( $resize );
    clearWState( &Qt::WState_Polished );

    Qt::Object::connect(PB_Cancel, SIGNAL "clicked()", this, SLOT "close()");
    Qt::Object::connect(PB_Select, SIGNAL "clicked()", this, SLOT "selectCover()");
    Qt::Object::connect(LB_AvailCovers, SIGNAL "currentChanged(QListBoxItem*)", this, SLOT "updatePreview()");
    Qt::Object::connect(LB_AvailCovers, SIGNAL "currentChanged(QListBoxItem*)", this, SLOT "updateData()");
    Qt::Object::connect(PB_Select, SIGNAL "clicked()", this, SLOT "close()");

    setTabOrder(LB_AvailCovers, PB_Select);
    setTabOrder(PB_Select, PB_Cancel);

    init();
}


#  Sets the strings of the subwidgets using the current
#  language.

sub languageChange
{
    setCaption(trUtf8("Select Cover") );
    TL_AvailCovers->setText( trUtf8("Available Covers:") );
    TL_Preview->setText( trUtf8("Preview:") );
    PB_Select->setText( trUtf8("&Select") );
    PB_Select->setAccel( Qt::KeySequence( trUtf8("Alt+S") ) );
    PB_Cancel->setText( trUtf8("&Cancel") );
    PB_Cancel->setAccel( Qt::KeySequence( trUtf8("Alt+C") ) );
    TL_FileSize->setText( trUtf8("File Size:") );
    TL_FileDate->setText( trUtf8("File Date:") );
    TL_Size->setText( trUtf8("Image Size:") );
    TL_ImageSizeValue->setText( trUtf8("---") );
    TL_FileSizeValue->setText( trUtf8("---") );
    TL_FileDateValue->setText( trUtf8("---") );
}


sub init
{

  opendir(DIR, ".");
  @files = grep(/\.jpg$/,readdir(DIR));
  closedir(DIR);

  foreach $file (@files) {
      LB_AvailCovers->insertItem( $file );
  }
  TL_AvailCovers->setText( trUtf8("Available Covers:") );

}

sub updatePreview
{

  if( LB_AvailCovers->currentText() ) {
    $image0 = Qt::Pixmap(LB_AvailCovers->currentText());
    PL_Preview->setPixmap( $image0 );
  }

}

sub updateData
{

  if( $image0 ) {
    my $tmp = $image0->width()." x ".$image0->height();
    TL_ImageSizeValue->setText($tmp);
  }
  my $sb = stat(LB_AvailCovers->currentText());
  TL_FileSizeValue->setText(($sb->size)." bytes");
  TL_FileDateValue->setText(scalar localtime $sb->mtime);

}

sub selectCover
{

  if( LB_AvailCovers->currentText() ) {
    if( LB_AvailCovers->currentText() ne "found.jpg" ) {
      system ("cp \"".LB_AvailCovers->currentText()."\" \"found.jpg\"");
    }
    close();
  }

}

1;


package main;

use Qt;
use SelectCover;

my $a = Qt::Application(\@ARGV);
my $w = SelectCover;
$a->setMainWidget($w);
$w->show;
exit $a->exec;
