summaryrefslogtreecommitdiffstats
path: root/qtjava/javalib/examples/demo/sql/BookForm.java
blob: 676703577c65795aed65f179912875ddbe3176e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/****************************************************************************
** 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.kde.qt.*;

public class BookForm extends QWidget { 
    QDataTable AuthorDataTable;
    QDataTable BookDataTable;
    QPushButton connectButton;
    QPushButton editButton;

    QGridLayout BookFormLayout;
	QSizePolicy policy_1;
	QSizePolicy policy_2;
	QSpacerItem spacer;
	QSpacerItem spacer_2;

	QSqlCursor authorCursor;
	QSqlCursor bookCursor;

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

    AuthorDataTable = new QDataTable( 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 QSpacerItem( 20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum );
    BookFormLayout.addMultiCell( spacer, 0, 0, 1, 2 );

    BookDataTable = new QDataTable( 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 QSpacerItem( 20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum );
    BookFormLayout.addMultiCell( spacer_2, 3, 3, 0, 1 );

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

    BookFormLayout.addWidget( connectButton, 0, 0 );

    editButton = new QPushButton( this, "editButton" );
    editButton.setEnabled( false );
	policy_2 = new QSizePolicy( (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, SIGNAL( "clicked()" ), this, SLOT( "editClicked()" ) );
    connect( AuthorDataTable, SIGNAL( "primeInsert(QSqlRecord)" ), this, SLOT( "primeInsertAuthor(QSqlRecord)" ) );
    connect( AuthorDataTable, SIGNAL( "currentChanged(QSqlRecord)" ), this, SLOT( "newCurrentAuthor(QSqlRecord)" ) );
    connect( connectButton, SIGNAL( "clicked()" ), this, SLOT( "connectClicked()" ) );

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

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

BookForm( QWidget 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 );
    QSqlCursor cur = new QSqlCursor( "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(QSqlIndex.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[QSqlDatabase.drivers().size()];;
	drivers = (String[]) QSqlDatabase.drivers().toArray(drivers);
    dialog.comboDriver.insertStringList( drivers  );
    dialog.editDatabase.setText( "book" );
    if ( dialog.exec() == QDialog.Accepted ) {
	// QSqlDatabase.removeDatabase( QSqlDatabase.defaultConnection() );
	QSqlDatabase db = QSqlDatabase.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( (QSqlCursor) null );
	AuthorDataTable.setSqlCursor( (QSqlCursor) null );
    }  else {
	editButton.setEnabled( true );
	authorCursor = new QSqlCursor( "author" );
	AuthorDataTable.setSqlCursor( authorCursor, false, true );
	bookCursor = new QSqlCursor( "book" );
	BookDataTable.setSqlCursor( bookCursor, false, true );
	AuthorDataTable.refresh( QDataTable.RefreshAll );
	BookDataTable.refresh( QDataTable.RefreshAll );
    }
    dialog = null;
}

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

void primeInsertAuthor(QSqlRecord buffer)
{
    QSqlQuery q = new QSqlQuery();
    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 ) );
    }
}

}