#!/usr/bin/perl -w
package LCDRange;

use Qt qw(%Orientation);

@ISA = qw(QWidget);

sub new {
    my $self = shift->SUPER::new(@_);

    my $lcd = QLCDNumber->new(2, $self, 'lcd');
    $lcd->move(0, 0);
    my $sBar =
	QScrollBar->new(0, 99,				# range
		        1, 10,				# line/page steps
		        0,				# initial value
		        $Orientation{Horizontal},	# orientation
		        $self, 'scrollbar');
    $lcd->connect($sBar, 'valueChanged(int)', 'display(int)');

    @$self{'sBar', 'lcd'} = ($sBar, $lcd);
    return $self;
}

sub resizeEvent {
    my $self = shift;
    my($sBar, $lcd) = @$self{'sBar', 'lcd'};

    $sBar->setGeometry(0, $self->height() - 16, $self->width(), 16);
    $lcd->resize($self->width(), $sBar->y() - 5);
}

package MyWidget;

use Qt qw($app %Weight);

@ISA = qw(QWidget);

sub new {
    my $self = shift->SUPER::new(@_);
    my $value;

    $self->setMinimumSize(200, 300);

    my $quit = QPushButton->new('Quit', $self, 'quit');
    $quit->setGeometry(10, 10, 75, 30);
    $quit->setFont(QFont->new('Times', 18, $Weight{Bold}));

    $app->connect($quit, 'clicked()', 'quit()');

    for my $i (0..15) {    # $_ is contaminated, find and fix
	$value->[$i] = LCDRange->new($self);
    }

    @$self{'quit', 'value'} = ($quit, $value);
    return $self;
}

sub resizeEvent {
    my $self = shift;
    my($quit, $value) = @$self{'quit', 'value'};
    my $startx      = 10;
    my $starty      = $quit->y() + $quit->height() + 10;
    my $valueWidth  = ($self->width()  - $startx - 10 - 3*5)/4;
    my $valueHeight = ($self->height() - $starty - 10 - 3*5)/4;
    for(0..15) {
	$value->[$_]->setGeometry($startx + int($_%4) * (5+$valueWidth),
				  $starty + int($_/4) * (5+$valueHeight),
				  $valueWidth, $valueHeight);
    }
}

package main;

use Qt 2.0;

$w = MyWidget->new;
$w->setGeometry(100, 100, 400, 400);
$app->setMainWidget($w);
$w->show();
exit $app->exec();
