|
1 #include <qpushbutton.h> |
|
2 #include <qapplication.h> |
|
3 #include <qvgroupbox.h> |
|
4 #include <qlayout.h> |
|
5 #include <qlineedit.h> |
|
6 #include <qlabel.h> |
|
7 #include <qregexp.h> |
|
8 #include "dbconfigureform.h" |
|
9 #include "inputfield.h" |
|
10 #include "inputcombo.h" |
|
11 #include <qvalidator.h> |
|
12 #include <qsqldatabase.h> |
|
13 #include <qcombobox.h> |
|
14 |
|
15 DBConfigureForm :: DBConfigureForm( Tdb_config *mydb_config, |
|
16 QWidget *parent, const char *name ) |
|
17 : QDialog( parent, name, TRUE) |
|
18 { |
|
19 QVBoxLayout *mainlayout = new QVBoxLayout( this ,10 ); |
|
20 |
|
21 // We store the pointer to the data we should set |
|
22 db_config = mydb_config; |
|
23 |
|
24 // Data InputFields |
|
25 QVGroupBox *fields = new QVGroupBox( "Data", this ); |
|
26 |
|
27 host = new InputField( tr("Host:"), db_config->host, fields); |
|
28 database = new InputField( tr("Database:"), db_config->database, |
|
29 fields); |
|
30 username = new InputField( tr("Username:"), |
|
31 db_config->username, fields); |
|
32 password = new InputField( tr("Password:"), db_config->password, |
|
33 fields); |
|
34 driver = new InputCombo( tr("Driver:"), QSqlDatabase::drivers(), |
|
35 fields); |
|
36 |
|
37 // Password echo mode for password input field. |
|
38 password->qLineEdit()->setEchoMode(QLineEdit::Password); |
|
39 // Set the default setting in driver combo |
|
40 driver->qComboBox()->setCurrentText(db_config->driver); |
|
41 |
|
42 mainlayout->addWidget( fields ); |
|
43 |
|
44 |
|
45 // Apply button |
|
46 apply_button = new QPushButton( tr("Apply"), this, |
|
47 "ApplyButton"); |
|
48 // Accept button |
|
49 QPushButton *accept_button = new QPushButton( tr("Accept"), this, |
|
50 "AcceptButton"); |
|
51 // Cancel button |
|
52 QPushButton *cancel_button = new QPushButton( tr("Cancel"), |
|
53 this, "CancelButton"); |
|
54 |
|
55 // Main form buttons |
|
56 QHBoxLayout *buttons = new QHBoxLayout( mainlayout, 10 ); |
|
57 buttons->addWidget(apply_button); |
|
58 buttons->addWidget(accept_button); |
|
59 buttons->addWidget(cancel_button); |
|
60 |
|
61 accept_button->setDefault(TRUE); |
|
62 |
|
63 setCaption( tr("DB Configure Form") ); |
|
64 |
|
65 // Connects: buttons |
|
66 connect( apply_button, SIGNAL(clicked()), this, |
|
67 SLOT(updateVariables()) ); |
|
68 connect( accept_button, SIGNAL(clicked()), this, |
|
69 SLOT(acceptChanges()) ); |
|
70 connect( cancel_button, SIGNAL(clicked()), this, SLOT(reject()) ); |
|
71 |
|
72 // Connects: textboxes |
|
73 connect( host->qLineEdit(), SIGNAL(textChanged(const QString&)), this, |
|
74 SLOT(setFormChanged()) ); |
|
75 connect( database->qLineEdit(), SIGNAL(textChanged(const QString&)), |
|
76 this, SLOT(setFormChanged()) ); |
|
77 connect( username->qLineEdit(), SIGNAL(textChanged(const QString&)), |
|
78 this, SLOT(setFormChanged()) ); |
|
79 connect( password->qLineEdit(), SIGNAL(textChanged(const QString&)), |
|
80 this, SLOT(setFormChanged()) ); |
|
81 connect( driver->qComboBox(), SIGNAL(activated(int)), |
|
82 this, SLOT(setFormChanged()) ); |
|
83 |
|
84 // Initial state of the form |
|
85 setFormNoChanged(); |
|
86 |
|
87 // Validators for InputFields |
|
88 host->qLineEdit()->setValidator( |
|
89 new QRegExpValidator( QRegExp( "[0-9a-z_\\-\\.]*" ), |
|
90 host->qLineEdit() )); |
|
91 username->qLineEdit()->setValidator( |
|
92 new QRegExpValidator( QRegExp( "[0-9a-zA-Z_\\-\\.]+" ), |
|
93 host->qLineEdit() )); |
|
94 password->qLineEdit()->setValidator( |
|
95 new QRegExpValidator( QRegExp( "[0-9a-zA-Z_\\-\\.]*" ), |
|
96 host->qLineEdit() )); |
|
97 database->qLineEdit()->setValidator( |
|
98 new QRegExpValidator( QRegExp( "[0-9a-zA-Z_\\-\\.]+" ), |
|
99 host->qLineEdit() )); |
|
100 |
|
101 } |
|
102 |
|
103 |
|
104 int DBConfigureForm :: updateVariables( ) |
|
105 { |
|
106 /* |
|
107 // ** Check if data is correct. Don't let it end if sth's wrong. |
|
108 if (host->text().find( QRegExp( "[^a-z_\\-\\.]" ) ) != -1 ) |
|
109 { |
|
110 // *** A Message Box should be shown |
|
111 qWarning("Host isn't good."); |
|
112 return 1; // ERROR |
|
113 } |
|
114 */ |
|
115 |
|
116 // Save data |
|
117 db_config->host = host->text(); |
|
118 db_config->username = username->text(); |
|
119 db_config->database = database->text(); |
|
120 db_config->password = password->text(); |
|
121 db_config->driver = driver->text(); |
|
122 |
|
123 setFormNoChanged(); |
|
124 |
|
125 return 0; // OK |
|
126 } |
|
127 |
|
128 void DBConfigureForm :: acceptChanges( ) |
|
129 { |
|
130 if (!updateVariables()) |
|
131 accept(); |
|
132 } |
|
133 |
|
134 void DBConfigureForm :: setFormChanged() |
|
135 { |
|
136 apply_button->setEnabled(true); |
|
137 } |
|
138 void DBConfigureForm :: setFormNoChanged() |
|
139 { |
|
140 apply_button->setEnabled(false); |
|
141 } |