source: trunk/tools/designer/plugins/wizards/sqlformwizardimpl.cpp

Last change on this file was 197, checked in by rudi, 14 years ago

Added QtDesigner

File size: 20.1 KB
Line 
1 /**********************************************************************
2** Copyright (C) 2005-2007 Trolltech ASA. All rights reserved.
3**
4** This file is part of Qt Designer.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
12** licenses may use this file in accordance with the Qt Commercial License
13** Agreement provided with the Software.
14**
15** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17**
18** See http://www.trolltech.com/gpl/ for GPL licensing information.
19** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
20** information about Qt Commercial License Agreements.
21**
22** Contact info@trolltech.com if any conditions of this licensing are
23** not clear to you.
24**
25**********************************************************************/
26
27#include "sqlformwizardimpl.h"
28
29#include <qlistbox.h>
30#include <qwidget.h>
31#include <qcheckbox.h>
32#include <qlineedit.h>
33
34#include <qlabel.h>
35#include <qgroupbox.h>
36#include <qlayout.h>
37#include <qregexp.h>
38#include <qpushbutton.h>
39#include <qmultilineedit.h>
40#include <qlistview.h>
41#include <qfeatures.h>
42#include <qradiobutton.h>
43#include <qspinbox.h>
44#include <limits.h>
45
46#ifndef QT_NO_SQL
47#include <qdatatable.h>
48#include <qdatabrowser.h>
49#include <qdataview.h>
50#include <qsqleditorfactory.h>
51#include <qsqlindex.h>
52#include <qsqlcursor.h>
53#endif
54
55SqlFormWizard::SqlFormWizard( QUnknownInterface *aIface, QWidget *w,
56 QWidget* parent, DesignerFormWindow *fw, const char* name, bool modal, WFlags fl )
57 : SqlFormWizardBase( parent, name, modal, fl ), widget( w ), appIface( aIface ),
58 mode( None )
59{
60 appIface->addRef();
61 formWindow = fw;
62 setFinishEnabled( finishPage, TRUE );
63
64 /* set mode of operation */
65 if ( ::qt_cast<QDataTable*>(widget) ) {
66 setCaption( "Data Table Wizard" );
67 mode = Table;
68 setAppropriate( navigPage, FALSE );
69 setAppropriate( layoutPage, FALSE );
70 checkBoxAutoEdit->setChecked( FALSE );
71 } else if ( ::qt_cast<QDataBrowser*>(widget) ) {
72 setCaption( "Data Browser Wizard" );
73 setAppropriate( tablePropertiesPage, FALSE );
74 mode = Browser;
75 checkBoxAutoEdit->setChecked( TRUE );
76 } else if ( ::qt_cast<QDataView*>(widget) ) {
77 setCaption( "Data View Wizard" );
78 setAppropriate( tablePropertiesPage, FALSE );
79 setAppropriate( navigPage, FALSE );
80 setAppropriate( sqlPage, FALSE);
81 checkCreateFieldLayout->hide();
82 checkCreateButtonLayout->hide();
83 checkBoxAutoEdit->hide();
84 mode = View;
85 }
86
87 connect( nextButton(), SIGNAL(clicked()), SLOT(nextPageClicked()) );
88 setupPage1();
89}
90
91
92SqlFormWizard::~SqlFormWizard()
93{
94 appIface->release();
95}
96
97void SqlFormWizard::nextPageClicked()
98{
99 if ( currentPage() == populatePage ) {
100 autoPopulate( TRUE );
101 }
102}
103
104void SqlFormWizard::connectionSelected( const QString &c )
105{
106 if ( !appIface )
107 return;
108
109 DesignerProject *proIface = (DesignerProject*)( (DesignerInterface*)appIface )->currentProject();
110 if ( !proIface )
111 return;
112
113 listBoxTable->clear();
114 QPtrList<DesignerDatabase> databases = proIface->databaseConnections();
115 for ( DesignerDatabase *d = databases.first(); d; d = databases.next() ) {
116 if ( d->name() == c || ( d->name() == "(default)" || d->name().isEmpty() ) && c == "(default)")
117 listBoxTable->insertStringList( d->tables() );
118 }
119 setNextEnabled( databasePage, ( listBoxTable->currentItem() >= 0 ) );
120}
121
122void SqlFormWizard::tableSelected( const QString & )
123{
124 if ( listBoxTable->currentItem() >= 0 ) {
125 setNextEnabled( databasePage, TRUE );
126 } else {
127 setNextEnabled( databasePage, FALSE );
128 }
129
130}
131
132void SqlFormWizard::autoPopulate( bool populate )
133{
134 DesignerProject *proIface = (DesignerProject*)( (DesignerInterface*)appIface )->currentProject();
135 if ( !proIface )
136 return;
137 QPtrList<DesignerDatabase> databases = proIface->databaseConnections();
138 listBoxField->clear();
139 listBoxSortField->clear();
140 listBoxSelectedField->clear();
141 if ( populate ) {
142 for ( DesignerDatabase *d = databases.first(); d; d = databases.next() ) {
143 if ( d->name() == listBoxConnection->currentText() ||
144 ( ( d->name() == "(default)" || d->name().isEmpty() ) &&
145 listBoxConnection->currentText() == "(default)" ) ) {
146 QStringList lst = *d->fields().find( listBoxTable->currentText() );
147 // remove primary index fields, if any
148 listBoxSortField->insertStringList( lst );
149 d->open( FALSE );
150#ifndef QT_NO_SQL
151 QSqlCursor tab( listBoxTable->currentText(), TRUE, d->connection() );
152 QSqlIndex pIdx = tab.primaryIndex();
153 for ( uint i = 0; i < pIdx.count(); i++ ) {
154 listBoxField->insertItem( pIdx.field( i )->name() );
155 lst.remove( pIdx.field( i )->name() );
156 }
157#endif
158 d->close();
159 listBoxSelectedField->insertStringList( lst );
160 }
161 }
162 }
163}
164
165void SqlFormWizard::fieldDown()
166{
167 if ( listBoxSelectedField->currentItem() == -1 ||
168 listBoxSelectedField->currentItem() == (int)listBoxSelectedField->count() - 1 ||
169 listBoxSelectedField->count() < 2 )
170 return;
171 int index = listBoxSelectedField->currentItem() + 1;
172 QListBoxItem *i = listBoxSelectedField->item( listBoxSelectedField->currentItem() );
173 listBoxSelectedField->takeItem( i );
174 listBoxSelectedField->insertItem( i, index );
175 listBoxSelectedField->setCurrentItem( i );
176}
177
178void SqlFormWizard::fieldUp()
179{
180 if ( listBoxSelectedField->currentItem() <= 0 ||
181 listBoxSelectedField->count() < 2 )
182 return;
183 int index = listBoxSelectedField->currentItem() - 1;
184 QListBoxItem *i = listBoxSelectedField->item( listBoxSelectedField->currentItem() );
185 listBoxSelectedField->takeItem( i );
186 listBoxSelectedField->insertItem( i, index );
187 listBoxSelectedField->setCurrentItem( i );
188}
189
190void SqlFormWizard::removeField()
191{
192 int i = listBoxSelectedField->currentItem();
193 if ( i != -1 ) {
194 listBoxField->insertItem( listBoxSelectedField->currentText() );
195 listBoxSelectedField->removeItem( i );
196 }
197}
198
199void SqlFormWizard::addField()
200{
201 int i = listBoxField->currentItem();
202 if ( i == -1 )
203 return;
204 QString f = listBoxField->currentText();
205 if ( !f.isEmpty() )
206 listBoxSelectedField->insertItem( f );
207 listBoxField->removeItem( i );
208}
209
210void SqlFormWizard::addSortField()
211{
212 int i = listBoxSortField->currentItem();
213 if ( i == -1 )
214 return;
215 QString f = listBoxSortField->currentText();
216 if ( !f.isEmpty() )
217 listBoxSortedField->insertItem( f + " ASC" );
218}
219
220void SqlFormWizard::reSortSortField()
221{
222 int i = listBoxSortedField->currentItem();
223 if ( i != -1 ) {
224 QString text = listBoxSortedField->currentText();
225 if ( text.mid( text.length() - 3 ) == "ASC" )
226 text = text.mid( 0, text.length() - 3 ) + "DESC";
227 else if ( text.mid( text.length() - 4 ) == "DESC" )
228 text = text.mid( 0, text.length() - 4 ) + "ASC";
229 listBoxSortedField->removeItem( i );
230 listBoxSortedField->insertItem( text, i );
231 listBoxSortedField->setCurrentItem( i );
232 }
233}
234
235void SqlFormWizard::removeSortField()
236{
237 int i = listBoxSortedField->currentItem();
238 if ( i != -1 ) {
239 listBoxSortedField->removeItem( i );
240 }
241}
242
243void SqlFormWizard::sortFieldUp()
244{
245 if ( listBoxSortedField->currentItem() <= 0 ||
246 listBoxSortedField->count() < 2 )
247 return;
248 int index = listBoxSortedField->currentItem() - 1;
249 QListBoxItem *i = listBoxSortedField->item( listBoxSortedField->currentItem() );
250 listBoxSortedField->takeItem( i );
251 listBoxSortedField->insertItem( i, index );
252 listBoxSortedField->setCurrentItem( i );
253}
254
255void SqlFormWizard::sortFieldDown()
256{
257 if ( listBoxSortedField->currentItem() == -1 ||
258 listBoxSortedField->currentItem() == (int)listBoxSortedField->count() - 1 ||
259 listBoxSortedField->count() < 2 )
260 return;
261 int index = listBoxSortedField->currentItem() + 1;
262 QListBoxItem *i = listBoxSortedField->item( listBoxSortedField->currentItem() );
263 listBoxSortedField->takeItem( i );
264 listBoxSortedField->insertItem( i, index );
265 listBoxSortedField->setCurrentItem( i );
266}
267
268void SqlFormWizard::setupDatabaseConnections()
269{
270 if ( !appIface )
271 return;
272
273 DesignerProject *proIface = (DesignerProject*)( (DesignerInterface*)appIface )->currentProject();
274 if ( !proIface )
275 return;
276 proIface->setupDatabases();
277 raise();
278 setupPage1();
279}
280
281void SqlFormWizard::setupPage1()
282{
283 if ( !appIface )
284 return;
285
286 DesignerProject *proIface = (DesignerProject*)( (DesignerInterface*)appIface )->currentProject();
287 if ( !proIface )
288 return;
289
290 listBoxTable->clear();
291 listBoxConnection->clear();
292 QPtrList<DesignerDatabase> databases = proIface->databaseConnections();
293 QStringList lst;
294 for ( DesignerDatabase *d = databases.first(); d; d = databases.next() )
295 lst << d->name();
296 listBoxConnection->insertStringList( lst );
297 if ( lst.count() )
298 listBoxConnection->setCurrentItem( 0 );
299
300 setNextEnabled( databasePage, FALSE );
301}
302
303static QPushButton *create_widget( QWidget *parent, const char *name,
304 const QString &txt, const QRect &r, DesignerFormWindow *fw )
305{
306 QPushButton *pb = (QPushButton*)fw->create( "QPushButton", parent, name );
307 pb->setText( txt );
308 pb->setGeometry( r );
309 fw->setPropertyChanged( pb, "text", TRUE );
310 fw->setPropertyChanged( pb, "geometry", TRUE );
311 return pb;
312}
313
314void SqlFormWizard::accept()
315{
316 if ( !appIface || mode == None )
317 return;
318
319#ifndef QT_NO_SQL
320 DesignerProject *proIface = (DesignerProject*)( (DesignerInterface*)appIface )->currentProject();
321 if ( !widget || !proIface ) {
322 SqlFormWizardBase::accept();
323 return;
324 }
325
326 QString conn = listBoxConnection->currentText();
327 QString table = listBoxTable->currentText();
328 QStringList lst;
329 lst << conn << table;
330
331 if ( !conn.isEmpty() && !table.isEmpty() ) {
332 formWindow->setProperty( widget, "database", lst );
333 formWindow->setPropertyChanged( widget, "database", TRUE );
334 }
335
336 if ( !editFilter->text().isEmpty() ) {
337 widget->setProperty( "filter", editFilter->text() );
338 formWindow->setPropertyChanged( widget, "filter", TRUE );
339 }
340
341 if ( listBoxSortedField->count() ) {
342 QStringList lst;
343 for ( uint i = 0; i < listBoxSortedField->count(); ++i )
344 lst << listBoxSortedField->text( i );
345 widget->setProperty( "sort", lst );
346 formWindow->setPropertyChanged( widget, "sort", TRUE );
347 }
348
349 QPtrList<DesignerDatabase> databases = proIface->databaseConnections();
350 DesignerDatabase *database = 0;
351 for ( DesignerDatabase *d = databases.first(); d; d = databases.next() ) {
352 if ( d->name() == listBoxConnection->currentText() || ( d->name() == "(default)" || d->name().isEmpty() ) && listBoxConnection->currentText() == "(default)" ) {
353 database = d;
354 d->open( FALSE );
355 break;
356 }
357 }
358
359 if (!database) {
360 return;
361 }
362 QSqlCursor tab( listBoxTable->currentText(), TRUE, database->connection() );
363 int columns = 2;
364
365 QSqlEditorFactory * f = QSqlEditorFactory::defaultFactory();
366
367 QWidget * editorDummy;
368 QWidget * editor;
369 QLabel * label;
370
371 int visibleFields = listBoxSelectedField->count();
372 int numPerColumn = visibleFields / columns;
373 if( (visibleFields % columns) > 0)
374 numPerColumn++;
375
376 int row = 0;
377 const int SPACING = 25;
378 const int COL_SPACING = SPACING*9;
379
380 uint j;
381 switch ( mode ) {
382 case None:
383 break;
384 case View:
385 case Browser: {
386
387 if ( mode == Browser && !checkBoxAutoEdit->isChecked() ) {
388 ((QDataBrowser*)widget)->setAutoEdit( FALSE );
389 formWindow->setPropertyChanged( widget, "autoEdit", TRUE );
390 }
391
392 formWindow->clearSelection();
393 bool createFieldLayout = checkCreateFieldLayout->isChecked();
394 bool createButtonLayout = checkCreateButtonLayout->isChecked();
395 bool createLayouts = checkCreateLayouts->isChecked();
396 bool labelAbove = radioLabelsTop->isOn();
397 uint numCols = spinNumberOfColumns->text().toInt();
398 uint currentCol = 0;
399 uint fieldsPerCol = listBoxSelectedField->count();
400 uint fieldsInCol = 0;
401 if ( listBoxSelectedField->count() )
402 fieldsPerCol = listBoxSelectedField->count() / numCols;
403 /* labels and data field editors */
404 for( j = 0; j < listBoxSelectedField->count(); j++ ){
405
406 QSqlField* field = tab.field( listBoxSelectedField->text( j ) );
407 if ( !field )
408 continue;
409
410 /* label */
411 QString labelName = field->name();
412 labelName = labelName.mid(0,1).upper() + labelName.mid(1);
413 label = (QLabel*)formWindow->create( "QLabel", widget,
414 QString( "label" + labelName ) );
415 label->setText( labelName );
416 label->setGeometry( SPACING + currentCol*COL_SPACING, row+SPACING,
417 SPACING*3, SPACING );
418
419 formWindow->setPropertyChanged( label, "geometry", TRUE );
420 formWindow->setPropertyChanged( label, "text", TRUE );
421
422 /* editor */
423 editorDummy = f->createEditor( widget, field );
424 editor = formWindow->create( editorDummy->className(), widget,
425 QString( QString( editorDummy->className() )
426 + labelName) );
427 delete editorDummy;
428 if ( labelAbove ) {
429 row += SPACING;
430 editor->setGeometry(SPACING + currentCol*COL_SPACING, row+SPACING,
431 SPACING*3, SPACING );
432 } else {
433 editor->setGeometry(SPACING * 5 + currentCol*COL_SPACING, row+SPACING,
434 SPACING*3, SPACING );
435 }
436 formWindow->setPropertyChanged( editor, "geometry", TRUE );
437 if ( QString(editor->className()) == "QLineEdit" &&
438 (field->type() == QVariant::Double ||
439 field->type() == QVariant::Int ||
440 field->type() == QVariant::UInt ) ) {
441 /* default right-align numerics */
442 //##
443 ((QLineEdit*)editor)->setAlignment( Qt::AlignRight );
444 formWindow->setPropertyChanged( editor, "alignment", TRUE );
445 }
446 if ( ::qt_cast<QSpinBox*>(editor) ) {
447 ( (QSpinBox*)editor )->setMaxValue( INT_MAX );
448 formWindow->setPropertyChanged( editor, "maxValue", TRUE );
449 }
450 QStringList lst;
451 lst << conn << table << field->name();
452 formWindow->setProperty( editor, "database", lst );
453 formWindow->setPropertyChanged( editor, "database", TRUE );
454
455 /* geometry */
456 if ( createFieldLayout ) {
457 formWindow->selectWidget( label );
458 formWindow->selectWidget( editor );
459 }
460
461 row += SPACING + 5;
462 fieldsInCol++;
463 if ( ( fieldsInCol >= fieldsPerCol ) && ( currentCol < numCols-1 ) ) {
464 currentCol++;
465 fieldsInCol = 0;
466 row = 0;
467 }
468 }
469
470 if ( listBoxSelectedField->count() ) {
471 if ( createFieldLayout )
472 formWindow->layoutG();
473 row += SPACING;
474 }
475
476 if ( mode == Browser ) {
477 if ( checkBoxNavig->isChecked() ) {
478 formWindow->clearSelection();
479 currentCol = 0;
480 if ( checkBoxFirst->isChecked() ) {
481 QPushButton *pb = create_widget( widget, "PushButtonFirst",
482 "|< &First",
483 QRect( 3 * SPACING * currentCol, row+SPACING, SPACING * 3, SPACING ),
484 formWindow );
485 formWindow->addConnection( pb, "clicked()", widget, "first()" );
486 formWindow->addConnection( widget, "firstRecordAvailable( bool )",
487 pb, "setEnabled( bool )" );
488 currentCol++;
489 formWindow->selectWidget( pb );
490 }
491 if ( checkBoxPrev->isChecked() ) {
492 QPushButton *pb = create_widget( widget, "PushButtonPrev",
493 "<< &Prev",
494 QRect( 3 * SPACING * currentCol, row+SPACING, SPACING * 3, SPACING ),
495 formWindow );
496 formWindow->addConnection( pb, "clicked()", widget, "prev()" );
497 formWindow->addConnection( widget, "prevRecordAvailable( bool )",
498 pb, "setEnabled( bool )" );
499 currentCol++;
500 formWindow->selectWidget( pb );
501 }
502 if ( checkBoxNext->isChecked() ) {
503 QPushButton *pb = create_widget( widget, "PushButtonNext",
504 "&Next >>",
505 QRect( 3 * SPACING * currentCol, row+SPACING, SPACING * 3, SPACING ),
506 formWindow );
507 formWindow->addConnection( pb, "clicked()", widget, "next()" );
508 formWindow->addConnection( widget, "nextRecordAvailable( bool )", pb,
509 "setEnabled( bool )" );
510 currentCol++;
511 formWindow->selectWidget( pb );
512 }
513 if ( checkBoxLast->isChecked() ) {
514 QPushButton *pb = create_widget( widget, "PushButtonLast", "&Last >|",
515 QRect( 3 * SPACING * currentCol, row+SPACING, SPACING*3, SPACING ), formWindow );
516 formWindow->addConnection( pb, "clicked()", widget, "last()" );
517 formWindow->addConnection( widget, "lastRecordAvailable( bool )", pb,
518 "setEnabled( bool )" );
519 currentCol++;
520 formWindow->selectWidget( pb );
521 }
522 if ( createButtonLayout )
523 formWindow->layoutH();
524 }
525 if ( checkBoxEdit->isChecked() ) {
526 formWindow->clearSelection();
527 row += SPACING;
528 currentCol = 0;
529 if ( checkBoxInsert->isChecked() ) {
530 QPushButton *pb = create_widget( widget, "PushButtonInsert", "&Insert",
531 QRect( 3 * SPACING * currentCol, row+SPACING, SPACING * 3, SPACING ), formWindow );
532 formWindow->addConnection( pb, "clicked()", widget, "insert()" );
533 currentCol++;
534 formWindow->selectWidget( pb );
535 }
536 if ( checkBoxUpdate->isChecked() ) {
537 QPushButton *pb = create_widget( widget, "PushButtonUpdate", "&Update",
538 QRect( 3 * SPACING * currentCol, row+SPACING, SPACING * 3, SPACING ), formWindow );
539 formWindow->addConnection( pb, "clicked()", widget, "update()" );
540 currentCol++;
541 formWindow->selectWidget( pb );
542 }
543 if ( checkBoxDelete->isChecked() ) {
544 QPushButton *pb = create_widget( widget, "PushButtonDelete", "&Delete",
545 QRect( 3 * SPACING * currentCol, row+SPACING, SPACING * 3, SPACING ), formWindow );
546 formWindow->addConnection( pb, "clicked()", widget, "del()" );
547 currentCol++;
548 formWindow->selectWidget( pb );
549 }
550 if ( createButtonLayout )
551 formWindow->layoutH();
552 }
553 }
554 if ( createLayouts )
555 formWindow->layoutGContainer( widget );
556 formWindow->clearSelection();
557 break;
558 }
559 case Table:
560 {
561 QDataTable* sqlTable = ((QDataTable*)widget);
562 if ( checkBoxAutoEdit->isChecked() ) {
563 sqlTable->setAutoEdit( TRUE );
564 formWindow->setPropertyChanged( sqlTable, "autoEdit", TRUE );
565 }
566
567 if ( checkBoxReadOnly->isChecked() ) {
568 sqlTable->setReadOnly( TRUE );
569 formWindow->setPropertyChanged( sqlTable, "readOnly", TRUE );
570 } else {
571 if ( checkBoxConfirmInserts->isChecked() ) {
572 sqlTable->setConfirmInsert( TRUE );
573 formWindow->setPropertyChanged( sqlTable, "confirmInsert", TRUE );
574 }
575 if ( checkBoxConfirmUpdates->isChecked() ) {
576 sqlTable->setConfirmUpdate( TRUE );
577 formWindow->setPropertyChanged( sqlTable, "confirmUpdate", TRUE );
578 }
579 if ( checkBoxConfirmDeletes->isChecked() ) {
580 sqlTable->setConfirmDelete( TRUE );
581 formWindow->setPropertyChanged( sqlTable, "confirmDelete", TRUE );
582 }
583 if ( checkBoxConfirmCancels->isChecked() ) {
584 sqlTable->setConfirmCancels( TRUE );
585 formWindow->setPropertyChanged( sqlTable, "confirmCancels", TRUE );
586 }
587 }
588 if ( checkBoxSorting->isChecked() ) {
589 sqlTable->setSorting( TRUE );
590 formWindow->setPropertyChanged( sqlTable, "sorting", TRUE );
591 }
592
593 QMap<QString, QString> columnFields;
594 sqlTable->setNumCols( listBoxSelectedField->count() ); // no need to change property through mdbIface here, since QDataTable doesn't offer that through Designer
595 for( j = 0; j < listBoxSelectedField->count(); j++ ){
596
597 QSqlField* field = tab.field( listBoxSelectedField->text( j ) );
598 if ( !field )
599 continue;
600
601 QString labelName = field->name();
602 labelName = labelName.mid(0,1).upper() + labelName.mid(1);
603
604 ((QTable*)widget)->horizontalHeader()->setLabel( j, labelName );
605
606 columnFields.insert( labelName, field->name() );
607 }
608 formWindow->setColumnFields( widget, columnFields );
609 break;
610 }
611 }
612
613 database->close();
614#endif
615
616 SqlFormWizardBase::accept();
617}
Note: See TracBrowser for help on using the repository browser.