@@ -12,29 +12,11 @@ OpenConnectionDialog::OpenConnectionDialog(QWidget *parent) :
12
12
{
13
13
ui->setupUi (this );
14
14
15
- // Load saved connections
16
- QSettings settings;
17
- int size = settings.beginReadArray (" connections" );
18
- for (int i = 0 ; i < size; i++) {
19
- settings.setArrayIndex (i);
15
+ // Add a new connection by default
16
+ ui->connectionsListWidget ->addItem (" -- New Connection --" );
20
17
21
- // Add connection from settings to list
22
- SavedConnection *connection = new SavedConnection;
23
- connection->name = settings.value (" name" ).toString ();
24
- connection->database = settings.value (" database" ).toString ();
25
- connection->driver = settings.value (" driver" ).toString ();
26
- connection->hostname = settings.value (" hostname" ).toString ();
27
- connection->username = settings.value (" username" ).toString ();
28
- connection->password = settings.value (" password" ).toString ();
29
- connection->port = settings.value (" port" ).toInt ();
30
-
31
- // Append to saved connections
32
- savedConnections.append (connection);
33
-
34
- // Add item to combo box
35
- ui->connectionsComboBox ->addItem (connection->name , savedConnections.length ());
36
- }
37
- settings.endArray ();
18
+ // Load saved connections
19
+ reloadConnections ();
38
20
39
21
// Setup list of drivers for selection
40
22
foreach (QString driver, QSqlDatabase::drivers ()) {
@@ -88,11 +70,6 @@ int OpenConnectionDialog::getPort()
88
70
return ui->portSpinBox ->text ().toInt ();
89
71
}
90
72
91
- bool OpenConnectionDialog::getSaveConnection ()
92
- {
93
- return ui->saveCheckBox ->isChecked ();
94
- }
95
-
96
73
void OpenConnectionDialog::on_driverCombo_currentIndexChanged (const QString &arg1)
97
74
{
98
75
// Disable fields if using SQLite
@@ -123,50 +100,63 @@ void OpenConnectionDialog::on_fileButton_clicked()
123
100
ui->databaseEdit ->setText (fileName);
124
101
}
125
102
126
- void OpenConnectionDialog::on_okButton_clicked ()
103
+ void OpenConnectionDialog::on_addButton_clicked ()
127
104
{
128
- if (ui->saveCheckBox ->isChecked ()) {
105
+ // Add connection from settings to list
106
+ SavedConnection *newConnection = new SavedConnection;
107
+ newConnection->name = getName ();
108
+ newConnection->database = getDatabase ();
109
+ newConnection->driver = getDriver ();
110
+ newConnection->hostname = getHostname ();
111
+ newConnection->username = getUsername ();
112
+ newConnection->password = getPassword ();
113
+ newConnection->port = getPort ();
114
+
115
+ // Append to saved connections
116
+ savedConnections.append (newConnection);
117
+
118
+ // Save connections
119
+ QSettings settings;
120
+ settings.beginWriteArray (" connections" );
129
121
130
- // Add connection from settings to list
131
- SavedConnection *newConnection = new SavedConnection;
132
- newConnection->name = getName ();
133
- newConnection->database = getDatabase ();
134
- newConnection->driver = getDriver ();
135
- newConnection->hostname = getHostname ();
136
- newConnection->username = getUsername ();
137
- newConnection->password = getPassword ();
138
- newConnection->port = getPort ();
122
+ int i = 0 ;
123
+ foreach (SavedConnection* connection, savedConnections)
124
+ {
125
+ settings.setArrayIndex (i);
139
126
140
- // Append to saved connections
141
- savedConnections.append (newConnection);
142
-
143
- // Save connections
144
- QSettings settings;
145
- settings.beginWriteArray (" connections" );
146
-
147
- int i = 0 ;
148
- foreach (SavedConnection* connection, savedConnections)
149
- {
150
- settings.setArrayIndex (i);
151
-
152
- // Add connection from settings to list
153
- settings.setValue (" name" , connection->name );
154
- settings.setValue (" database" , connection->database );
155
- settings.setValue (" driver" , connection->driver );
156
- settings.setValue (" hostname" , connection->hostname );
157
- settings.setValue (" username" , connection->username );
158
- settings.setValue (" password" , connection->password );
159
- settings.setValue (" port" , connection->port );
160
-
161
- i++;
162
- }
163
- settings.endArray ();
127
+ // Add connection from settings to list
128
+ settings.setValue (" name" , connection->name );
129
+ settings.setValue (" database" , connection->database );
130
+ settings.setValue (" driver" , connection->driver );
131
+ settings.setValue (" hostname" , connection->hostname );
132
+ settings.setValue (" username" , connection->username );
133
+ settings.setValue (" password" , connection->password );
134
+ settings.setValue (" port" , connection->port );
135
+
136
+ i++;
164
137
}
138
+ settings.endArray ();
139
+
140
+ // Reload connections widget
141
+ reloadConnections ();
165
142
}
166
143
167
- void OpenConnectionDialog::on_connectionsComboBox_currentIndexChanged ( int index )
144
+ void OpenConnectionDialog::on_connectionsListWidget_itemActivated (QListWidgetItem *item )
168
145
{
169
- SavedConnection *connection = savedConnections.at (index );
146
+ SavedConnection *connection;
147
+
148
+ QList<SavedConnection*>::iterator i;
149
+ for (i = savedConnections.begin (); i != savedConnections.end (); ++i) {
150
+ if (item->text () == " -- New Connection --" ) {
151
+ connection = new SavedConnection;
152
+ break ;
153
+ }
154
+
155
+ if ((*i)->name == item->text ()) {
156
+ connection = *i;
157
+ break ;
158
+ }
159
+ }
170
160
171
161
int driver = ui->driverCombo ->findText (connection->driver );
172
162
ui->driverCombo ->setCurrentIndex (driver);
@@ -176,3 +166,79 @@ void OpenConnectionDialog::on_connectionsComboBox_currentIndexChanged(int index)
176
166
ui->hostnameEdit ->setText (connection->hostname );
177
167
ui->portSpinBox ->setValue (connection->port );
178
168
}
169
+
170
+ void OpenConnectionDialog::reloadConnections ()
171
+ {
172
+ // Clear out the list widget
173
+ ui->connectionsListWidget ->clear ();
174
+
175
+ // Add a new connection by default
176
+ ui->connectionsListWidget ->addItem (" -- New Connection --" );
177
+
178
+ // Load saved connections
179
+ QSettings settings;
180
+ int size = settings.beginReadArray (" connections" );
181
+ for (int i = 0 ; i < size; i++) {
182
+ settings.setArrayIndex (i);
183
+
184
+ // Add connection from settings to list
185
+ SavedConnection *connection = new SavedConnection;
186
+ connection->name = settings.value (" name" ).toString ();
187
+ connection->database = settings.value (" database" ).toString ();
188
+ connection->driver = settings.value (" driver" ).toString ();
189
+ connection->hostname = settings.value (" hostname" ).toString ();
190
+ connection->username = settings.value (" username" ).toString ();
191
+ connection->password = settings.value (" password" ).toString ();
192
+ connection->port = settings.value (" port" ).toInt ();
193
+
194
+ // Append to saved connections
195
+ savedConnections.append (connection);
196
+
197
+ // Add item to combo box
198
+ ui->connectionsListWidget ->addItem (connection->name );
199
+ }
200
+ settings.endArray ();
201
+ }
202
+
203
+ void OpenConnectionDialog::on_removeButton_clicked ()
204
+ {
205
+ // Get current item
206
+ QListWidgetItem *item = ui->connectionsListWidget ->currentItem ();
207
+
208
+ QList<SavedConnection*>::iterator i;
209
+ for (i = savedConnections.begin (); i != savedConnections.end (); ++i) {
210
+ if (item->text () == " -- New Connection --" ) {
211
+ break ;
212
+ }
213
+
214
+ if ((*i)->name == item->text ()) {
215
+ savedConnections.removeOne (*i);
216
+ break ;
217
+ }
218
+ }
219
+
220
+ // Save connections
221
+ QSettings settings;
222
+ settings.beginWriteArray (" connections" );
223
+
224
+ int j = 0 ;
225
+ foreach (SavedConnection* connection, savedConnections)
226
+ {
227
+ settings.setArrayIndex (j);
228
+
229
+ // Add connection from settings to list
230
+ settings.setValue (" name" , connection->name );
231
+ settings.setValue (" database" , connection->database );
232
+ settings.setValue (" driver" , connection->driver );
233
+ settings.setValue (" hostname" , connection->hostname );
234
+ settings.setValue (" username" , connection->username );
235
+ settings.setValue (" password" , connection->password );
236
+ settings.setValue (" port" , connection->port );
237
+
238
+ j++;
239
+ }
240
+ settings.endArray ();
241
+
242
+ // Reload connections widget
243
+ reloadConnections ();
244
+ }
0 commit comments