/****************************************************************************
** Form implementation generated from reading ui file 'book.ui'
**
** Created: Wed Aug 8 03:34:02 2001
**      by:  The User Interface Compiler (uic)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/
import org.trinitydesktop.qt.*;

public class BookForm extends TQWidget { 
    TQDataTable AuthorDataTable;
    TQDataTable BookDataTable;
    TQPushButton connectButton;
    TQPushButton editButton;

    TQGridLayout BookFormLayout;
	TQSizePolicy policy_1;
	TQSizePolicy policy_2;
	TQSpacerItem spacer;
	TQSpacerItem spacer_2;

	TQSqlCursor authorCursor;
	TQSqlCursor bookCursor;

/* 
 *  Constructs a BookForm which is a child of 'parent', with the 
 *  name 'name' and widget flags set to 'f' 
 */
BookForm( TQWidget parent, String name, int fl )
{
    super( parent, name, fl );
    if ( name == null )
	setName( "BookForm" );
    resize( 478, 498 ); 
    setCaption( trUtf8( "Book" ) );
    BookFormLayout = new TQGridLayout( this ); 
    BookFormLayout.setSpacing( 6 );
    BookFormLayout.setMargin( 11 );

    AuthorDataTable = new TQDataTable( this, "AuthorDataTable" );
    AuthorDataTable.addColumn( "surname", trUtf8( "Surname", "" ) );
    AuthorDataTable.addColumn( "forename", trUtf8( "Forename", "" ) );
    AuthorDataTable.setSorting( true );
    AuthorDataTable.setConfirmDelete( true );
    AuthorDataTable.setSort( new String[] { "surname ASC", "forename ASC" } );

    BookFormLayout.addMultiCellWidget( AuthorDataTable, 1, 1, 0, 2 );
    spacer = new TQSpacerItem( 20, 20, TQSizePolicy.Expanding, TQSizePolicy.Minimum );
    BookFormLayout.addMultiCell( spacer, 0, 0, 1, 2 );

    BookDataTable = new TQDataTable( this, "BookDataTable" );
    BookDataTable.addColumn( "title", trUtf8( "Title", "" ) );
    BookDataTable.addColumn( "price", trUtf8( "Price", "" ) );
    BookDataTable.addColumn( "notes", trUtf8( "Notes", "" ) );
    BookDataTable.setReadOnly( true );
    BookDataTable.setSorting( true );
    BookDataTable.setSort( new String[] { "title ASC" } );

    BookFormLayout.addMultiCellWidget( BookDataTable, 2, 2, 0, 2 );
    spacer_2 = new TQSpacerItem( 20, 20, TQSizePolicy.Expanding, TQSizePolicy.Minimum );
    BookFormLayout.addMultiCell( spacer_2, 3, 3, 0, 1 );

    connectButton = new TQPushButton( this, "connectButton" );
	policy_1 = new TQSizePolicy( (int)0, (int)0, connectButton.sizePolicy().hasHeightForWidth() );
    connectButton.setSizePolicy( policy_1 );
    connectButton.setText( trUtf8( "&Connect..." ) );

    BookFormLayout.addWidget( connectButton, 0, 0 );

    editButton = new TQPushButton( this, "editButton" );
    editButton.setEnabled( false );
	policy_2 = new TQSizePolicy( (int)0, (int)0, editButton.sizePolicy().hasHeightForWidth() );
    editButton.setSizePolicy( policy_2 );
    editButton.setText( trUtf8( "&Edit Books" ) );

    BookFormLayout.addWidget( editButton, 3, 2 );

    // database support





    // signals and slots connections
    connect( editButton, TQ_SIGNAL( "clicked()" ), this, TQ_SLOT( "editClicked()" ) );
    connect( AuthorDataTable, TQ_SIGNAL( "primeInsert(TQSqlRecord)" ), this, TQ_SLOT( "primeInsertAuthor(TQSqlRecord)" ) );
    connect( AuthorDataTable, TQ_SIGNAL( "currentChanged(TQSqlRecord)" ), this, TQ_SLOT( "newCurrentAuthor(TQSqlRecord)" ) );
    connect( connectButton, TQ_SIGNAL( "clicked()" ), this, TQ_SLOT( "connectClicked()" ) );

    // tab order
    setTabOrder( connectButton, editButton );
    init();
}

BookForm( TQWidget parent, String name )
{
	this(parent, name, 0);
}

BookForm( TQWidget parent )
{
	this(parent, null, 0);
}

BookForm( )
{
	this(null, null, 0);
}

void init()
{
}

public void destroy()
{
}

void editClicked()
{
    EditBookForm dialog = new EditBookForm( this, "Edit Book Form", true, 0 );
    TQSqlCursor cur = new TQSqlCursor( "book" );
    dialog.BookDataBrowser.setSqlCursor( cur );
    dialog.BookDataBrowser.setFilter( BookDataTable.filter() );
	String[] sort = new String[BookDataTable.sort().size()];
	sort = (String[]) BookDataTable.sort().toArray(sort);
    dialog.BookDataBrowser.setSort(TQSqlIndex.fromStringList(
	sort, cur ) );
    dialog.BookDataBrowser.refresh();
    int i = BookDataTable.currentRow();
    if ( i == -1 ) i = 0; // Always use the first row
    dialog.BookDataBrowser.seek( i );
    dialog.exec();
    dialog = null;
    BookDataTable.refresh();
}

void connectClicked()
{
    boolean ok = false;
    ConnectDialog dialog = new ConnectDialog( this, "Connect", true, 0 );
	String[] drivers = new String[TQSqlDatabase.drivers().size()];;
	drivers = (String[]) TQSqlDatabase.drivers().toArray(drivers);
    dialog.comboDriver.insertStringList( drivers  );
    dialog.editDatabase.setText( "book" );
    if ( dialog.exec() == TQDialog.Accepted ) {
	// TQSqlDatabase.removeDatabase( TQSqlDatabase.defaultConnection() );
	TQSqlDatabase db = TQSqlDatabase.addDatabase( dialog.comboDriver.currentText() );
	db.setDatabaseName( dialog.editDatabase.text() );
	db.setUserName( dialog.editUsername.text() );
	db.setPassword( dialog.editPassword.text() );
	db.setHostName( dialog.editHostname.text() );
	if ( !db.open() ) {
	    //## warning?
	    ok= false;
	} else
	    ok = true;
    } 
    if ( !ok ) {
	editButton.setEnabled( false );
	BookDataTable.setSqlCursor( (TQSqlCursor) null );
	AuthorDataTable.setSqlCursor( (TQSqlCursor) null );
    }  else {
	editButton.setEnabled( true );
	authorCursor = new TQSqlCursor( "author" );
	AuthorDataTable.setSqlCursor( authorCursor, false, true );
	bookCursor = new TQSqlCursor( "book" );
	BookDataTable.setSqlCursor( bookCursor, false, true );
	AuthorDataTable.refresh( TQDataTable.RefreshAll );
	BookDataTable.refresh( TQDataTable.RefreshAll );
    }
    dialog = null;
}

void newCurrentAuthor(TQSqlRecord author)
{
    BookDataTable.setFilter( "authorid=" + author.value( "id" ).toString() );
    BookDataTable.refresh();
}

void primeInsertAuthor(TQSqlRecord buffer)
{
    TQSqlQuery q = new TQSqlQuery();
    q.exec( "UPDATE sequence SET sequence = sequence + 1 WHERE tablename='author';" );
    q.exec( "SELECT sequence FROM sequence WHERE tablename='author';" );
    if ( q.next() ) {
	buffer.setValue( "id", q.value( 0 ) );
    }
}

}
