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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
|
/**********************************************************************
** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved.
**
** This file is part of TQt Designer.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing retquirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** Licensees holding valid TQt Commercial licenses may use this file in
** accordance with the TQt Commercial License Agreement provided with
** the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/
#include "sqlformwizardimpl.h"
#include <qlistbox.h>
#include <qwidget.h>
#include <qcheckbox.h>
#include <qlineedit.h>
#include <qlabel.h>
#include <qgroupbox.h>
#include <qlayout.h>
#include <qregexp.h>
#include <qpushbutton.h>
#include <qmultilineedit.h>
#include <qlistview.h>
#include <qfeatures.h>
#include <qradiobutton.h>
#include <qspinbox.h>
#include <limits.h>
#ifndef QT_NO_SQL
#include <qdatatable.h>
#include <qdatabrowser.h>
#include <qdataview.h>
#include <qsqleditorfactory.h>
#include <qsqlindex.h>
#include <qsqlcursor.h>
#endif
SqlFormWizard::SqlFormWizard( TQUnknownInterface *aIface, TQWidget *w,
TQWidget* parent, DesignerFormWindow *fw, const char* name, bool modal, WFlags fl )
: SqlFormWizardBase( parent, name, modal, fl ), widget( w ), appIface( aIface ),
mode( None )
{
appIface->addRef();
formWindow = fw;
setFinishEnabled( finishPage, TRUE );
/* set mode of operation */
if ( ::qt_cast<TQDataTable*>(widget) ) {
setCaption( "Data Table Wizard" );
mode = Table;
setAppropriate( navigPage, FALSE );
setAppropriate( layoutPage, FALSE );
checkBoxAutoEdit->setChecked( FALSE );
} else if ( ::qt_cast<TQDataBrowser*>(widget) ) {
setCaption( "Data Browser Wizard" );
setAppropriate( tablePropertiesPage, FALSE );
mode = Browser;
checkBoxAutoEdit->setChecked( TRUE );
} else if ( ::qt_cast<TQDataView*>(widget) ) {
setCaption( "Data View Wizard" );
setAppropriate( tablePropertiesPage, FALSE );
setAppropriate( navigPage, FALSE );
setAppropriate( sqlPage, FALSE);
checkCreateFieldLayout->hide();
checkCreateButtonLayout->hide();
checkBoxAutoEdit->hide();
mode = View;
}
connect( nextButton(), SIGNAL(clicked()), SLOT(nextPageClicked()) );
setupPage1();
}
SqlFormWizard::~SqlFormWizard()
{
appIface->release();
}
void SqlFormWizard::nextPageClicked()
{
if ( currentPage() == populatePage ) {
autoPopulate( TRUE );
}
}
void SqlFormWizard::connectionSelected( const TQString &c )
{
if ( !appIface )
return;
DesignerProject *proIface = (DesignerProject*)( (DesignerInterface*)appIface )->currentProject();
if ( !proIface )
return;
listBoxTable->clear();
TQPtrList<DesignerDatabase> databases = proIface->databaseConnections();
for ( DesignerDatabase *d = databases.first(); d; d = databases.next() ) {
if ( d->name() == c || ( d->name() == "(default)" || d->name().isEmpty() ) && c == "(default)")
listBoxTable->insertStringList( d->tables() );
}
setNextEnabled( databasePage, ( listBoxTable->currentItem() >= 0 ) );
}
void SqlFormWizard::tableSelected( const TQString & )
{
if ( listBoxTable->currentItem() >= 0 ) {
setNextEnabled( databasePage, TRUE );
} else {
setNextEnabled( databasePage, FALSE );
}
}
void SqlFormWizard::autoPopulate( bool populate )
{
DesignerProject *proIface = (DesignerProject*)( (DesignerInterface*)appIface )->currentProject();
if ( !proIface )
return;
TQPtrList<DesignerDatabase> databases = proIface->databaseConnections();
listBoxField->clear();
listBoxSortField->clear();
listBoxSelectedField->clear();
if ( populate ) {
for ( DesignerDatabase *d = databases.first(); d; d = databases.next() ) {
if ( d->name() == listBoxConnection->currentText() ||
( ( d->name() == "(default)" || d->name().isEmpty() ) &&
listBoxConnection->currentText() == "(default)" ) ) {
TQStringList lst = *d->fields().find( listBoxTable->currentText() );
// remove primary index fields, if any
listBoxSortField->insertStringList( lst );
d->open( FALSE );
#ifndef QT_NO_SQL
TQSqlCursor tab( listBoxTable->currentText(), TRUE, d->connection() );
TQSqlIndex pIdx = tab.primaryIndex();
for ( uint i = 0; i < pIdx.count(); i++ ) {
listBoxField->insertItem( pIdx.field( i )->name() );
lst.remove( pIdx.field( i )->name() );
}
#endif
d->close();
listBoxSelectedField->insertStringList( lst );
}
}
}
}
void SqlFormWizard::fieldDown()
{
if ( listBoxSelectedField->currentItem() == -1 ||
listBoxSelectedField->currentItem() == (int)listBoxSelectedField->count() - 1 ||
listBoxSelectedField->count() < 2 )
return;
int index = listBoxSelectedField->currentItem() + 1;
TQListBoxItem *i = listBoxSelectedField->item( listBoxSelectedField->currentItem() );
listBoxSelectedField->takeItem( i );
listBoxSelectedField->insertItem( i, index );
listBoxSelectedField->setCurrentItem( i );
}
void SqlFormWizard::fieldUp()
{
if ( listBoxSelectedField->currentItem() <= 0 ||
listBoxSelectedField->count() < 2 )
return;
int index = listBoxSelectedField->currentItem() - 1;
TQListBoxItem *i = listBoxSelectedField->item( listBoxSelectedField->currentItem() );
listBoxSelectedField->takeItem( i );
listBoxSelectedField->insertItem( i, index );
listBoxSelectedField->setCurrentItem( i );
}
void SqlFormWizard::removeField()
{
int i = listBoxSelectedField->currentItem();
if ( i != -1 ) {
listBoxField->insertItem( listBoxSelectedField->currentText() );
listBoxSelectedField->removeItem( i );
}
}
void SqlFormWizard::addField()
{
int i = listBoxField->currentItem();
if ( i == -1 )
return;
TQString f = listBoxField->currentText();
if ( !f.isEmpty() )
listBoxSelectedField->insertItem( f );
listBoxField->removeItem( i );
}
void SqlFormWizard::addSortField()
{
int i = listBoxSortField->currentItem();
if ( i == -1 )
return;
TQString f = listBoxSortField->currentText();
if ( !f.isEmpty() )
listBoxSortedField->insertItem( f + " ASC" );
}
void SqlFormWizard::reSortSortField()
{
int i = listBoxSortedField->currentItem();
if ( i != -1 ) {
TQString text = listBoxSortedField->currentText();
if ( text.mid( text.length() - 3 ) == "ASC" )
text = text.mid( 0, text.length() - 3 ) + "DESC";
else if ( text.mid( text.length() - 4 ) == "DESC" )
text = text.mid( 0, text.length() - 4 ) + "ASC";
listBoxSortedField->removeItem( i );
listBoxSortedField->insertItem( text, i );
listBoxSortedField->setCurrentItem( i );
}
}
void SqlFormWizard::removeSortField()
{
int i = listBoxSortedField->currentItem();
if ( i != -1 ) {
listBoxSortedField->removeItem( i );
}
}
void SqlFormWizard::sortFieldUp()
{
if ( listBoxSortedField->currentItem() <= 0 ||
listBoxSortedField->count() < 2 )
return;
int index = listBoxSortedField->currentItem() - 1;
TQListBoxItem *i = listBoxSortedField->item( listBoxSortedField->currentItem() );
listBoxSortedField->takeItem( i );
listBoxSortedField->insertItem( i, index );
listBoxSortedField->setCurrentItem( i );
}
void SqlFormWizard::sortFieldDown()
{
if ( listBoxSortedField->currentItem() == -1 ||
listBoxSortedField->currentItem() == (int)listBoxSortedField->count() - 1 ||
listBoxSortedField->count() < 2 )
return;
int index = listBoxSortedField->currentItem() + 1;
TQListBoxItem *i = listBoxSortedField->item( listBoxSortedField->currentItem() );
listBoxSortedField->takeItem( i );
listBoxSortedField->insertItem( i, index );
listBoxSortedField->setCurrentItem( i );
}
void SqlFormWizard::setupDatabaseConnections()
{
if ( !appIface )
return;
DesignerProject *proIface = (DesignerProject*)( (DesignerInterface*)appIface )->currentProject();
if ( !proIface )
return;
proIface->setupDatabases();
raise();
setupPage1();
}
void SqlFormWizard::setupPage1()
{
if ( !appIface )
return;
DesignerProject *proIface = (DesignerProject*)( (DesignerInterface*)appIface )->currentProject();
if ( !proIface )
return;
listBoxTable->clear();
listBoxConnection->clear();
TQPtrList<DesignerDatabase> databases = proIface->databaseConnections();
TQStringList lst;
for ( DesignerDatabase *d = databases.first(); d; d = databases.next() )
lst << d->name();
listBoxConnection->insertStringList( lst );
if ( lst.count() )
listBoxConnection->setCurrentItem( 0 );
setNextEnabled( databasePage, FALSE );
}
static TQPushButton *create_widget( TQWidget *parent, const char *name,
const TQString &txt, const TQRect &r, DesignerFormWindow *fw )
{
TQPushButton *pb = (TQPushButton*)fw->create( "TQPushButton", parent, name );
pb->setText( txt );
pb->setGeometry( r );
fw->setPropertyChanged( pb, "text", TRUE );
fw->setPropertyChanged( pb, "geometry", TRUE );
return pb;
}
void SqlFormWizard::accept()
{
if ( !appIface || mode == None )
return;
#ifndef QT_NO_SQL
DesignerProject *proIface = (DesignerProject*)( (DesignerInterface*)appIface )->currentProject();
if ( !widget || !proIface ) {
SqlFormWizardBase::accept();
return;
}
TQString conn = listBoxConnection->currentText();
TQString table = listBoxTable->currentText();
TQStringList lst;
lst << conn << table;
if ( !conn.isEmpty() && !table.isEmpty() ) {
formWindow->setProperty( widget, "database", lst );
formWindow->setPropertyChanged( widget, "database", TRUE );
}
if ( !editFilter->text().isEmpty() ) {
widget->setProperty( "filter", editFilter->text() );
formWindow->setPropertyChanged( widget, "filter", TRUE );
}
if ( listBoxSortedField->count() ) {
TQStringList lst;
for ( uint i = 0; i < listBoxSortedField->count(); ++i )
lst << listBoxSortedField->text( i );
widget->setProperty( "sort", lst );
formWindow->setPropertyChanged( widget, "sort", TRUE );
}
TQPtrList<DesignerDatabase> databases = proIface->databaseConnections();
DesignerDatabase *database = 0;
for ( DesignerDatabase *d = databases.first(); d; d = databases.next() ) {
if ( d->name() == listBoxConnection->currentText() || ( d->name() == "(default)" || d->name().isEmpty() ) && listBoxConnection->currentText() == "(default)" ) {
database = d;
d->open( FALSE );
break;
}
}
if (!database) {
return;
}
TQSqlCursor tab( listBoxTable->currentText(), TRUE, database->connection() );
int columns = 2;
TQSqlEditorFactory * f = TQSqlEditorFactory::defaultFactory();
TQWidget * editorDummy;
TQWidget * editor;
TQLabel * label;
int visibleFields = listBoxSelectedField->count();
int numPerColumn = visibleFields / columns;
if( (visibleFields % columns) > 0)
numPerColumn++;
int row = 0;
const int SPACING = 25;
const int COL_SPACING = SPACING*9;
uint j;
switch ( mode ) {
case None:
break;
case View:
case Browser: {
if ( mode == Browser && !checkBoxAutoEdit->isChecked() ) {
((TQDataBrowser*)widget)->setAutoEdit( FALSE );
formWindow->setPropertyChanged( widget, "autoEdit", TRUE );
}
formWindow->clearSelection();
bool createFieldLayout = checkCreateFieldLayout->isChecked();
bool createButtonLayout = checkCreateButtonLayout->isChecked();
bool createLayouts = checkCreateLayouts->isChecked();
bool labelAbove = radioLabelsTop->isOn();
uint numCols = spinNumberOfColumns->text().toInt();
uint currentCol = 0;
uint fieldsPerCol = listBoxSelectedField->count();
uint fieldsInCol = 0;
if ( listBoxSelectedField->count() )
fieldsPerCol = listBoxSelectedField->count() / numCols;
/* labels and data field editors */
for( j = 0; j < listBoxSelectedField->count(); j++ ){
TQSqlField* field = tab.field( listBoxSelectedField->text( j ) );
if ( !field )
continue;
/* label */
TQString labelName = field->name();
labelName = labelName.mid(0,1).upper() + labelName.mid(1);
label = (TQLabel*)formWindow->create( "TQLabel", widget,
TQString( "label" + labelName ) );
label->setText( labelName );
label->setGeometry( SPACING + currentCol*COL_SPACING, row+SPACING,
SPACING*3, SPACING );
formWindow->setPropertyChanged( label, "geometry", TRUE );
formWindow->setPropertyChanged( label, "text", TRUE );
/* editor */
editorDummy = f->createEditor( widget, field );
editor = formWindow->create( editorDummy->className(), widget,
TQString( TQString( editorDummy->className() )
+ labelName) );
delete editorDummy;
if ( labelAbove ) {
row += SPACING;
editor->setGeometry(SPACING + currentCol*COL_SPACING, row+SPACING,
SPACING*3, SPACING );
} else {
editor->setGeometry(SPACING * 5 + currentCol*COL_SPACING, row+SPACING,
SPACING*3, SPACING );
}
formWindow->setPropertyChanged( editor, "geometry", TRUE );
if ( TQString(editor->className()) == "TQLineEdit" &&
(field->type() == TQVariant::Double ||
field->type() == TQVariant::Int ||
field->type() == TQVariant::UInt ) ) {
/* default right-align numerics */
//##
((TQLineEdit*)editor)->setAlignment( TQt::AlignRight );
formWindow->setPropertyChanged( editor, "alignment", TRUE );
}
if ( ::qt_cast<TQSpinBox*>(editor) ) {
( (TQSpinBox*)editor )->setMaxValue( INT_MAX );
formWindow->setPropertyChanged( editor, "maxValue", TRUE );
}
TQStringList lst;
lst << conn << table << field->name();
formWindow->setProperty( editor, "database", lst );
formWindow->setPropertyChanged( editor, "database", TRUE );
/* geometry */
if ( createFieldLayout ) {
formWindow->selectWidget( label );
formWindow->selectWidget( editor );
}
row += SPACING + 5;
fieldsInCol++;
if ( ( fieldsInCol >= fieldsPerCol ) && ( currentCol < numCols-1 ) ) {
currentCol++;
fieldsInCol = 0;
row = 0;
}
}
if ( listBoxSelectedField->count() ) {
if ( createFieldLayout )
formWindow->layoutG();
row += SPACING;
}
if ( mode == Browser ) {
if ( checkBoxNavig->isChecked() ) {
formWindow->clearSelection();
currentCol = 0;
if ( checkBoxFirst->isChecked() ) {
TQPushButton *pb = create_widget( widget, "PushButtonFirst",
"|< &First",
TQRect( 3 * SPACING * currentCol, row+SPACING, SPACING * 3, SPACING ),
formWindow );
formWindow->addConnection( pb, "clicked()", widget, "first()" );
formWindow->addConnection( widget, "firstRecordAvailable( bool )",
pb, "setEnabled( bool )" );
currentCol++;
formWindow->selectWidget( pb );
}
if ( checkBoxPrev->isChecked() ) {
TQPushButton *pb = create_widget( widget, "PushButtonPrev",
"<< &Prev",
TQRect( 3 * SPACING * currentCol, row+SPACING, SPACING * 3, SPACING ),
formWindow );
formWindow->addConnection( pb, "clicked()", widget, "prev()" );
formWindow->addConnection( widget, "prevRecordAvailable( bool )",
pb, "setEnabled( bool )" );
currentCol++;
formWindow->selectWidget( pb );
}
if ( checkBoxNext->isChecked() ) {
TQPushButton *pb = create_widget( widget, "PushButtonNext",
"&Next >>",
TQRect( 3 * SPACING * currentCol, row+SPACING, SPACING * 3, SPACING ),
formWindow );
formWindow->addConnection( pb, "clicked()", widget, "next()" );
formWindow->addConnection( widget, "nextRecordAvailable( bool )", pb,
"setEnabled( bool )" );
currentCol++;
formWindow->selectWidget( pb );
}
if ( checkBoxLast->isChecked() ) {
TQPushButton *pb = create_widget( widget, "PushButtonLast", "&Last >|",
TQRect( 3 * SPACING * currentCol, row+SPACING, SPACING*3, SPACING ), formWindow );
formWindow->addConnection( pb, "clicked()", widget, "last()" );
formWindow->addConnection( widget, "lastRecordAvailable( bool )", pb,
"setEnabled( bool )" );
currentCol++;
formWindow->selectWidget( pb );
}
if ( createButtonLayout )
formWindow->layoutH();
}
if ( checkBoxEdit->isChecked() ) {
formWindow->clearSelection();
row += SPACING;
currentCol = 0;
if ( checkBoxInsert->isChecked() ) {
TQPushButton *pb = create_widget( widget, "PushButtonInsert", "&Insert",
TQRect( 3 * SPACING * currentCol, row+SPACING, SPACING * 3, SPACING ), formWindow );
formWindow->addConnection( pb, "clicked()", widget, "insert()" );
currentCol++;
formWindow->selectWidget( pb );
}
if ( checkBoxUpdate->isChecked() ) {
TQPushButton *pb = create_widget( widget, "PushButtonUpdate", "&Update",
TQRect( 3 * SPACING * currentCol, row+SPACING, SPACING * 3, SPACING ), formWindow );
formWindow->addConnection( pb, "clicked()", widget, "update()" );
currentCol++;
formWindow->selectWidget( pb );
}
if ( checkBoxDelete->isChecked() ) {
TQPushButton *pb = create_widget( widget, "PushButtonDelete", "&Delete",
TQRect( 3 * SPACING * currentCol, row+SPACING, SPACING * 3, SPACING ), formWindow );
formWindow->addConnection( pb, "clicked()", widget, "del()" );
currentCol++;
formWindow->selectWidget( pb );
}
if ( createButtonLayout )
formWindow->layoutH();
}
}
if ( createLayouts )
formWindow->layoutGContainer( widget );
formWindow->clearSelection();
break;
}
case Table:
{
TQDataTable* sqlTable = ((TQDataTable*)widget);
if ( checkBoxAutoEdit->isChecked() ) {
sqlTable->setAutoEdit( TRUE );
formWindow->setPropertyChanged( sqlTable, "autoEdit", TRUE );
}
if ( checkBoxReadOnly->isChecked() ) {
sqlTable->setReadOnly( TRUE );
formWindow->setPropertyChanged( sqlTable, "readOnly", TRUE );
} else {
if ( checkBoxConfirmInserts->isChecked() ) {
sqlTable->setConfirmInsert( TRUE );
formWindow->setPropertyChanged( sqlTable, "confirmInsert", TRUE );
}
if ( checkBoxConfirmUpdates->isChecked() ) {
sqlTable->setConfirmUpdate( TRUE );
formWindow->setPropertyChanged( sqlTable, "confirmUpdate", TRUE );
}
if ( checkBoxConfirmDeletes->isChecked() ) {
sqlTable->setConfirmDelete( TRUE );
formWindow->setPropertyChanged( sqlTable, "confirmDelete", TRUE );
}
if ( checkBoxConfirmCancels->isChecked() ) {
sqlTable->setConfirmCancels( TRUE );
formWindow->setPropertyChanged( sqlTable, "confirmCancels", TRUE );
}
}
if ( checkBoxSorting->isChecked() ) {
sqlTable->setSorting( TRUE );
formWindow->setPropertyChanged( sqlTable, "sorting", TRUE );
}
TQMap<TQString, TQString> columnFields;
sqlTable->setNumCols( listBoxSelectedField->count() ); // no need to change property through mdbIface here, since TQDataTable doesn't offer that through Designer
for( j = 0; j < listBoxSelectedField->count(); j++ ){
TQSqlField* field = tab.field( listBoxSelectedField->text( j ) );
if ( !field )
continue;
TQString labelName = field->name();
labelName = labelName.mid(0,1).upper() + labelName.mid(1);
((TQTable*)widget)->horizontalHeader()->setLabel( j, labelName );
columnFields.insert( labelName, field->name() );
}
formWindow->setColumnFields( widget, columnFields );
break;
}
}
database->close();
#endif
SqlFormWizardBase::accept();
}
|