#!/usr/bin/perl -w

# Form implementation generated from reading ui file 'table_example.ui'
#
# Created: Mon Jul 5 09:38:35 2004
#      by: The PerlQt User Interface Compiler (puic)
#
# WARNING! All changes made in this file will be lost!


use strict;
use utf8;
use Qt::debug qw (virtual);

package table_example;
use Qt;
use Qt::isa qw(Qt::Widget);
use Qt::slots
    tableOneRow => [],
    tableTwoRows => [];
use Qt::attributes qw(
    frame6
    One_Row
    table
    Two_Rows
);

our $filled = "false";

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

    if ( name() eq "unnamed" )
    {
        setName("table_example" );
    }

    my $table_exampleLayout = Qt::GridLayout(this, 1, 1, 11, 6, '$table_exampleLayout');

    frame6 = Qt::Frame(this, "frame6");
    frame6->setFrameShape( &Qt::Frame::StyledPanel() );
    frame6->setFrameShadow( &Qt::Frame::Raised() );
    my $frame6Layout = Qt::GridLayout(frame6, 1, 1, 11, 6, '$frame6Layout');
    my $spacer = Qt::SpacerItem(190, 20, &Qt::SizePolicy::Expanding, &Qt::SizePolicy::Minimum);
    $frame6Layout->addItem($spacer, 1, 3);

    One_Row = Qt::PushButton(frame6, "One_Row");

    $frame6Layout->addWidget(One_Row, 1, 1);

    table = Qt::Table(frame6, "table");
    table->setNumRows( int(0) );
    table->setNumCols( int(0) );
    table->setReadOnly( 1 );
    table->setSelectionMode( &Qt::Table::NoSelection() );
    table->setLeftMargin(0);                               # don't show left vertical header

    $frame6Layout->addMultiCellWidget(table, 0, 0, 0, 3);

    Two_Rows = Qt::PushButton(frame6, "Two_Rows");

    $frame6Layout->addWidget(Two_Rows, 1, 2);
    my $spacer_2 = Qt::SpacerItem(190, 20, &Qt::SizePolicy::Expanding, &Qt::SizePolicy::Minimum);
    $frame6Layout->addItem($spacer_2, 1, 0);

    $table_exampleLayout->addWidget(frame6, 0, 0);
    languageChange();
    my $resize = Qt::Size(586, 482);
    $resize = $resize->expandedTo(minimumSizeHint());
    resize( $resize );
    clearWState( &Qt::WState_Polished );

    Qt::Object::connect(One_Row, SIGNAL "clicked()", this, SLOT "tableOneRow()");
    Qt::Object::connect(Two_Rows, SIGNAL "clicked()", this, SLOT "tableTwoRows()");
}


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

sub languageChange
{
    setCaption(trUtf8("Table Test") );
    One_Row->setText( trUtf8("One Row") );
    Two_Rows->setText( trUtf8("Two Rows") );
}


sub tableOneRow
{
		my @lines = ();
		
		$filled = "true";

    open (FILE,"table_example.one") or die;
    while (defined (my $line = <FILE>))
    {
        chomp($line);
        push (@lines,$line);
        
        clearTable();
        
        table->setNumCols(1);
        table->horizontalHeader()->setStretchEnabled(1);
        table->horizontalHeader()->setClickEnabled(0,-1);
        table->horizontalHeader()->setLabel(0,"Eine Spalte",-1);
        table->setNumRows(scalar(@lines));
				table->setFocusPolicy( &Qt::Table::NoFocus() );
        
        my $row = 0;
        foreach my $element (@lines)
        {
            table->setItem($row,0,Qt::TableItem(table,&Qt::TableItem::Never," $element"));
            $row++;
        }
    }				
}

sub tableTwoRows
{
    my @col1 = ();
		my @col2 = ();

		$filled = "true";

    open (FILE,"table_example.two") or die;
    while (defined (my $line = <FILE>))
    {
        chomp($line);
				my @parts = split/\s+/,$line;
        push (@col1,$parts[0]);
        push (@col2,$parts[1]);
        
        clearTable();
        
        table->setNumCols(2);
        table->horizontalHeader()->setStretchEnabled(1);
        table->horizontalHeader()->setLabel(0,"Erste Spalte",-1);
        table->horizontalHeader()->setLabel(1,"Zweite Spalte",-1);
        table->horizontalHeader()->setClickEnabled(0,-1);
        table->setNumRows(scalar(@col2) > scalar(@col1) ? scalar(@col1) : scalar(@col2));
				table->setFocusPolicy( &Qt::Table::NoFocus() );
        
        my $row = 0;
        foreach my $element (@col1)
        {
            table->setItem($row,0,Qt::TableItem(table,&Qt::TableItem::Never," $element"));
            $row++;
        }

				$row = 0;
        foreach my $element (@col2)
        {
            table->setItem($row,1,Qt::TableItem(table,&Qt::TableItem::Never," $element"));
            $row++;
        }				
    }				
}


sub clearTable 
{
    if (table->numRows() > 0)
    {
        if (table->numCols() > 1)
        {
            for (my $elem = 0; $elem < table->numRows() ;$elem++ )
            {
                table->takeItem(table->item($elem,0));
                table->takeItem(table->item($elem,1));
            }
        }
        else
        {
            for (my $elem = 0; $elem < table->numRows() ;$elem++ )
            {
                table->takeItem(table->item($elem,0));
            }
        }                       
    }
}





1;


package main;

use Qt;
use table_example;

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