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

use Qt 2.0;
use Qt::app;

@ISA = qw(Qt::Widget);

sub new {
    my $self = shift->SUPER::new(@_);
    # Make the top-level layout; a vertical box to contain all widgets
    # and sub-layouts
    my $topLayout = Qt::VBoxLayout->new($self, 5);

    # Create a menubar....
    my $menubar = Qt::MenuBar->new($self);
    $menubar->setSeparator(Qt::MenuBar::InWindowsStyle);
    my $popup;
    $popup = Qt::PopupMenu->new;
    $popup->insertItem('&Quit', $app, 'quit()');
    $menubar->insertItem('&File', $popup);

    # ... and tell the layout about it
    $topLayout->setMenuBar($menubar);

    # Make an hbox that will hold a row of buttons.
    my $buttons = Qt::HBoxLayout->new;
    $topLayout->addLayout($buttons);
    my $i;
    for($i = 1; $i <= 4; $i++) {
	my $but = Qt::PushButton->new($self);
	$but->setText("Button $i");
	$but->setMinimumSize($but->sizeHint());

	# Set horizontal stretch factor to 10 to let the buttons
	# stretch horizontally. The buttons will not stretch
	# vertically, since bigWidget below will take up vertical
	# stretch.
	$buttons->addWidget($but, 10);
	# (Actually, the result would have been the same with a
	# stretch factor of 0; if no items in a layout have non-zero
	# stretch, the space is divided equally between members.)
    }

    # Make another hbox that will hold a left-justified row of buttons.
    my $buttons2 = Qt::HBoxLayout->new;
    $topLayout->addLayout($buttons2);

    my $but = Qt::PushButton->new("Button five", $self);
    $but->setMinimumSize($but->sizeHint());
    $buttons2->addWidget($but);

    $but = Qt::PushButton->new("Button 6", $self);
    $but->setMinimumSize($but->sizeHint());
    $buttons2->addWidget($but);

    # Fill up the rest of the hbox with stretchable space, so that
    # the buttons get their minimum width and are pushed to the left
    $buttons2->addStretch(10);

    # Make a big widget that will grab all the space in the middle.
    my $bigWidget = Qt::Label->new("This widget will get all the " .
                                   "remaining space", $self);
    $bigWidget->setBackgroundColor(Qt::white);
    $bigWidget->setFrameStyle(Qt::Frame::Panel | Qt::Frame::Plain);

    # Set vertical stretch factor to 10 to let the bigWidget stretch
    # vertically. It will stretch horizontally because there are no
    # widgets beside it to take up horizontal stretch.
    $topLayout->addWidget($bigWidget, 10);

    # Make a grid that will hold a vertical table of QLabel/QLineEdit
    # pairs next to a large QMultiLineEdit.

    # Don't use hard-coded row/column numbers in QGridLayout, you'll
    # regret it when you have to change the layout.
    my $numRows = 3;
    my $labelCol = 0;
    my $linedCol = 1;
    my $multiCol = 2;

    # Let the grid-layout have a spacing of 10 pixels between
    # widgets, overriding the default from topLayout.
    my $grid = Qt::GridLayout->new($numRows, 3, 10);
    $topLayout->addLayout($grid);
    my $row;

    for($row = 0; $row < $numRows; $row++) {
	my $label = Qt::Label->new($self);
	$label->setText("Line &" . ($row+1));
	$label->setMinimumSize($label->sizeHint());
	# The label goes in the first column.
	$grid->addWidget($label, $row, $labelCol);
	my $ed = Qt::LineEdit->new($self);
	# No minimum width for the line edit
	$ed->setMinimumHeight($ed->sizeHint()->height());
	# The line edit goes in the second column
	$grid->addWidget($ed, $row, $linedCol);
	# show off the nice new keyboard interface in Qt 1.3
	$label->setBuddy($ed);
    }

    # The multiline edit will cover the entire vertical range of the
    # grid (rows 0 to numRows) and stay in column 2.

    my $med = Qt::MultiLineEdit->new($self);
    $grid->addMultiCellWidget($med, 0, $numRows - 1, $multiCol, $multiCol);

    # The labels will take the space they need. Let the remaining
    # horizontal space be shared so that the multiline edit gets
    # twice as much as the line edit.
    $grid->setColStretch($linedCol, 10);
    $grid->setColStretch($multiCol, 20);

    # Add a widget at the bottom.
    my $sb = Qt::Label->new($self);
    $sb->setText("Let's pretend this is a status bar");
    $sb->setFrameStyle(Qt::Frame::Panel | Qt::Frame::Sunken);
    # This widget will use all horizontal space, and have a fixed height.
    $sb->setFixedHeight( $sb->sizeHint()->height() );
    $sb->setAlignment( Qt::AlignVCenter | Qt::AlignLeft );
    $topLayout->addWidget( $sb );

    $topLayout->activate();

    return $self;
}

sub DESTROY {
    my $self = shift;
    # All child widgets are deleted by Qt.
    # The top-level layout and all its sub-layouts are deleted by Qt.
    $self->SUPER::DESTROY(@_);
}

package main;

use Qt 2.0;
use Qt::app;

$f = ExampleWidget->new;
$f->resize(400, 300);
$f->show();

$app->setMainWidget($f);
exit $app->exec();
