UltraScan III
us_investigator.cpp
Go to the documentation of this file.
1 
3 #include "us_investigator.h"
4 #include "us_gui_settings.h"
5 #include "us_settings.h"
6 #include "us_db2.h"
7 #include "us_crypto.h"
8 #include "us_passwd.h"
9 
13 US_Investigator::US_Investigator( bool signal, int inv )
14  : US_WidgetsDialog( 0, 0 )
15 {
16  signal_wanted = signal;
17 
18  setPalette( US_GuiSettings::frameColor() );
19  setWindowTitle( tr( "Manage Investigators" ) );
20  setAttribute( Qt::WA_DeleteOnClose );
21 
22  int row = 0;
23  QGridLayout* main = new QGridLayout( this );
24  main->setContentsMargins( 2, 2, 2, 2 );
25  main->setSpacing( 2 );
26 
27  // Search last name
28  QLabel* lb_search = us_label( tr( "Search:" ) );
29  main->addWidget( lb_search, row, 0 );
30 
32  connect( le_search, SIGNAL( textChanged( const QString& ) ),
33  SLOT ( limit_names( const QString& ) ) );
34  main->addWidget( le_search, row++, 1 );
35 
36  // List widget
38  connect( lw_names, SIGNAL( itemDoubleClicked( QListWidgetItem* ) ),
39  SLOT ( get_inv_data ( QListWidgetItem* ) ) );
40  main->addWidget( lw_names, row, 0, 4, 2 );
41  row += 4;
42 
43  QStringList DB = US_Settings::defaultDB();
44  if ( DB.isEmpty() ) DB << "Undefined";
45  QLabel* lb_DB = us_banner( tr( "Database: " ) + DB.at( 0 ) );
46  main->addWidget( lb_DB, row++, 0, 1, 2 );
47 
48  // Investigator ID
49  QLabel* lb_invID = us_label( tr( "Investigator ID:" ) );
50  main->addWidget( lb_invID, row, 0 );
51 
53  le_invID->setReadOnly( true );
54  main->addWidget( le_invID, row++, 1 );
55 
56  // Investigator ID
57  QLabel* lb_invGuid = us_label( tr( "Global Identifier:" ) );
58  main->addWidget( lb_invGuid, row, 0 );
59 
60  QPalette gray = US_GuiSettings::editColor();
61  gray.setColor( QPalette::Base, QColor( 0xe0, 0xe0, 0xe0 ) );
62 
64  le_invGuid->setReadOnly( true );
65  le_invGuid->setPalette ( gray );
66  main->addWidget( le_invGuid, row++, 1 );
67 
68  if ( US_Settings::us_debug() == 0 )
69  {
70  lb_invGuid->setVisible( false );
71  le_invGuid->setVisible( false );
72  }
73 
74  // Last Name
75  QLabel* lb_lname = us_label( tr( "Last Name:" ) );
76  main->addWidget( lb_lname, row, 0 );
77 
79  main->addWidget( le_lname, row++, 1 );
80 
81  // First Name
82  QLabel* lb_fname = us_label( tr( "First Name:" ) );
83  main->addWidget( lb_fname, row, 0 );
84 
86  main->addWidget( le_fname, row++, 1 );
87 
88  // Address
89  QLabel* lb_address = us_label( tr( "Address:" ) );
90  main->addWidget( lb_address, row, 0 );
91 
93  main->addWidget( le_address, row++, 1 );
94 
95  // City
96  QLabel* lb_city = us_label( tr( "City:" ) );
97  main->addWidget( lb_city, row, 0 );
98 
99  le_city = us_lineedit();
100  main->addWidget( le_city, row++, 1 );
101 
102  // State
103  QLabel* lb_state = us_label( tr( "State:" ) );
104  main->addWidget( lb_state, row, 0 );
105 
106  le_state = us_lineedit();
107  main->addWidget( le_state, row++, 1 );
108 
109  // Zip
110  QLabel* lb_zip = us_label( tr( "Zip:" ) );
111  main->addWidget( lb_zip, row, 0 );
112 
113  le_zip = us_lineedit();
114  main->addWidget( le_zip, row++, 1 );
115 
116  // Phone
117  QLabel* lb_phone = us_label( tr( "Phone:" ) );
118  main->addWidget( lb_phone, row, 0 );
119 
120  le_phone = us_lineedit();
121  main->addWidget( le_phone, row++, 1 );
122 
123  // Email
124  QLabel* lb_email = us_label( tr( "Email:" ) );
125  main->addWidget( lb_email, row, 0 );
126 
127  le_email = us_lineedit();
128 
129  // Make the line edit entries a little wider than the default
130  QFontMetrics fm( le_email->font() );
131  le_email->setMinimumWidth( fm.maxWidth() * 10 );
132 
133  main->addWidget( le_email, row++, 1 );
134 
135  // Organization
136  QLabel* lb_org = us_label( tr( "Organization:" ) );
137  main->addWidget( lb_org, row, 0 );
138 
139  le_org = us_lineedit();
140  main->addWidget( le_org, row++, 1 );
141 
142  // Pushbuttons
143  QHBoxLayout* buttons1 = new QHBoxLayout;
144  QPushButton* pb_queryDB = us_pushbutton( tr( "Query DB" ) );
145  connect( pb_queryDB, SIGNAL( clicked() ), SLOT( queryDB() ) );
146  buttons1->addWidget( pb_queryDB, row );
147 
148  pb_update = us_pushbutton( tr( "Update DB" ), false );
149  connect( pb_update, SIGNAL( clicked() ), SLOT( update() ) );
150  buttons1->addWidget( pb_update, row++ );
151  main->addLayout( buttons1, row++, 0, 1, 2 );
152 
153  // Button row
154  QBoxLayout* buttons2 = new QHBoxLayout;
155 
156  QPushButton* pb_reset = us_pushbutton( tr( "Reset" ) );
157  connect( pb_reset, SIGNAL( clicked() ), SLOT( reset() ) );
158  buttons2->addWidget( pb_reset );
159 
160  QPushButton* pb_help = us_pushbutton( tr( "Help" ) );
161  connect( pb_help, SIGNAL( clicked() ), SLOT( help() ) );
162  buttons2->addWidget( pb_help );
163 
164  QPushButton* pb_close;
165 
166  if ( signal_wanted )
167  {
168  QPushButton* pb_cancel = us_pushbutton( tr( "Cancel" ) );
169  connect( pb_cancel, SIGNAL( clicked() ), SLOT( reject() ) );
170  buttons2->addWidget( pb_cancel );
171 
172  pb_close = us_pushbutton( tr( "Accept" ) );
173  }
174  else
175  pb_close = us_pushbutton( tr( "Close" ) );
176 
177  connect( pb_close, SIGNAL( clicked() ), SLOT( close() ) );
178  buttons2->addWidget( pb_close );
179 
180  main->addLayout( buttons2, row++, 0, 1, 2 );
181  reset();
182  queryDB();
183 
184  // Get last investigator ID if it is not passed
185  // Will return -1 if it has not been set
186  if ( inv < 0 ) inv = US_Settings::us_inv_ID();
187 
188  if ( inv > -1 )
189  {
190  for ( int i = 0; i < investigators.size(); i++ )
191  {
192  if ( investigators[ i ].invID == inv )
193  {
194  get_inv_data( lw_names->item( i ) );
195  break;
196  }
197  }
198  }
199 }
200 
202 {
203  US_Passwd pw;
204  QString masterPW = pw.getPasswd();
205  US_DB2 db( masterPW ); // New constructor
206 
207  if ( db.lastErrno() != US_DB2::OK )
208  {
209  // Error message here
210  QMessageBox::information( this,
211  tr( "DB Connection Problem" ),
212  tr( "There was an error connecting to the database:\n" )
213  + db.lastError() );
214  return;
215  }
216 
217  lw_names->clear();
218 
219  QStringList query;
220  query << "get_people" << "%" + le_search->text() + "%";
221 
222  db.query( query );
223 
224  US_InvestigatorData data;
225 
226  while ( db.next() )
227  {
228  data.invID = db.value( 0 ).toInt();
229  data.lastName = db.value( 1 ).toString();
230  data.firstName = db.value( 2 ).toString();
231 
232  investigators << data;
233 
234  lw_names->addItem( new QListWidgetItem(
235  "InvID: (" + QString::number( data.invID ) + "), " +
236  data.lastName + ", " + data.firstName ) );
237  }
238 }
239 
241 {
242  if ( le_invID->text().isEmpty() )
243  {
244  QMessageBox::information( this,
245  tr( "Attention" ),
246  tr( "You must select an Investigator from the DB to update." ) );
247  return;
248  }
249 
250  if ( ! check_fields() ) return;
251 
252  if ( ! changed() ) return;
253 
254  int response = QMessageBox::question( this,
255  tr( "Update the entry?" ),
256  tr( "Clicking 'OK' will update the information\n"
257  "for this investigator in the database." ),
258  QMessageBox::Ok, QMessageBox::Cancel );
259 
260  if ( response == QMessageBox::Ok )
261  {
262  US_Passwd pw;
263  QString masterPW = pw.getPasswd();
264  US_DB2 db( masterPW ); // New constructor
265 
266  if ( db.lastErrno() != US_DB2::OK )
267  {
268  QMessageBox::warning( this,
269  tr( "Database Error" ),
270  tr( "US_Investigator::connectDB: Could not open DB\n" ) +
271  db.lastError() );
272  return;
273  }
274 
275  QStringList info = US_Settings::defaultDB();
276 
277  // DB Internal PW
278  QString cipher = info.at( 7 );
279  QString iv = info.at( 8 );
280  QString userPW = US_Crypto::decrypt( cipher, masterPW, iv );
281 
282  QStringList query;
283  query << "update_person";
284  query << le_invID ->text();
285  query << le_fname ->text();
286  query << le_lname ->text();
287  query << le_address->text();
288  query << le_city ->text();
289  query << le_state ->text();
290  query << le_zip ->text();
291  query << le_phone ->text();
292  query << le_email ->text();
293  query << le_org ->text();
294  query << userPW;
295 
296  // Error check
297  int status = db.statusQuery( query );
298 
299  if ( status == US_DB2::OK )
300  {
301  QMessageBox::information( this,
302  tr( "Success" ),
303  tr( "The entry was updated.\n" ) );
304  }
305  else
306  {
307  QMessageBox::warning( this,
308  tr( "Database Error" ),
309  tr( "US_Investigator::update_person: Could not update DB\n" ) +
310  db.lastError() );
311  }
312  }
313 }
314 
315 void US_Investigator::limit_names( const QString& s )
316 {
317  lw_names->clear();
318 
319  for ( int i = 0; i < investigators.size(); i++ )
320  {
321  if ( investigators[ i ].lastName.contains(
322  QRegExp( ".*" + s + ".*", Qt::CaseInsensitive ) ) ||
323  investigators[ i ].firstName.contains(
324  QRegExp( ".*" + s + ".*", Qt::CaseInsensitive ) ) )
325  lw_names->addItem( new QListWidgetItem(
326  "InvID: (" + QString::number( investigators[ i ].invID ) + "), " +
327  investigators[ i ].lastName + ", " +
328  investigators[ i ].firstName ) );
329  }
330 }
331 
332 void US_Investigator::get_inv_data( QListWidgetItem* item )
333 {
334  QString entry = item->text();
335 
336 
337  int left = entry.indexOf( '(' ) + 1;
338  int right = entry.indexOf( ')' );
339  QString invID = entry.mid( left, right - left );
340 
341  US_Passwd pw;
342  QString masterPW = pw.getPasswd();
343  US_DB2 db( masterPW );
344 
345  if ( db.lastErrno() != US_DB2::OK )
346  {
347  QMessageBox::information( this,
348  tr( "DB Connection Problem" ),
349  tr( "There was an error connecting to the database:\n" )
350  + db.lastError() );
351  return;
352  }
353 
354  QStringList query;
355  query << "get_person_info" << invID;
356 
357  db.query( query );
358  db.next();
359 
360  info.invID = invID.toInt();
361  info.firstName = db.value( 0 ).toString();
362  info.lastName = db.value( 1 ).toString();
363  info.address = db.value( 2 ).toString();
364  info.city = db.value( 3 ).toString();
365  info.state = db.value( 4 ).toString();
366  info.zip = db.value( 5 ).toString();
367  info.phone = db.value( 6 ).toString();
368  info.organization = db.value( 7 ).toString();
369  info.email = db.value( 8 ).toString();
370  info.invGuid = db.value( 9 ).toString();
371 
372  //le_lname ->disconnect();
373  le_lname ->setText( info.lastName );
374  //connect( le_search, SIGNAL( textChanged( const QString& ) ),
375  // SLOT ( limit_names( const QString& ) ) );
376 
377  le_invID ->setText( invID );
378  le_fname ->setText( info.firstName );
379  le_address->setText( info.address );
380  le_city ->setText( info.city );
381  le_state ->setText( info.state );
382  le_zip ->setText( info.zip );
383  le_phone ->setText( info.phone );
384  le_email ->setText( info.email );
385  le_org ->setText( info.organization );
386  le_invGuid->setText( info.invGuid );
387 
388  pb_update ->setEnabled( true );
389 }
390 
392 {
393  if ( signal_wanted )
394  {
395  if ( le_invID->text() == "" )
396  {
397  QMessageBox::information( this,
398  tr( "Attention" ),
399  tr( "Select an investigator before 'Accept':\n" ) );
400 
401  return;
402  }
403  }
404 
405  if ( le_invID->text().toInt() > 0 )
406  {
407  QString s = le_lname->text() + ", " + le_fname->text();
408 
410  US_Settings::set_us_inv_ID( le_invID->text().toInt() );
411  }
412 
413  // Send signal *after* updating settings.
414  if ( signal_wanted )
415  emit investigator_accepted( le_invID->text().toInt() );
416 
417  US_WidgetsDialog::close();
418 }
419 
421 {
422  le_invID ->setText( "" );
423  le_invGuid->setText( "" );
424  le_fname ->setText( "" );
425  le_lname ->setText( "" );
426  le_address->setText( "" );
427  le_city ->setText( "" );
428  le_state ->setText( "" );
429  le_zip ->setText( "" );
430  le_phone ->setText( "" );
431  le_email ->setText( "" );
432  le_org ->setText( "" );
433 
434  lw_names ->clear();
435  investigators.clear();
436 
437  //pb_delete ->setEnabled( false );
438  pb_update ->setEnabled( false );
439 
440  info.invID = -1;
441  info.lastName = "";
442  info.firstName = "";
443  info.address = "";
444  info.city = "";
445  info.state = "";
446  info.zip = "";
447  info.phone = "";
448  info.email = "";
449  info.organization = "";
450 }
451 
453 {
454  QString missing = "";
455  QRegExp strip( "^\\s*(.*)\\s*$" );
456 
457  if ( le_lname->text().replace( strip, "\\1" ).isEmpty() )
458  missing += tr( "\nLast Name" );
459 
460  if ( le_fname->text().replace( strip, "\\1" ).isEmpty() )
461  missing += tr( "\\First Name" );
462 
463  if ( le_address->text().replace( strip, "\\1" ).isEmpty() )
464  missing += tr( "\nAddress" );
465 
466  if ( le_city->text().replace( strip, "\\1" ).isEmpty() )
467  missing += tr( "\nCity" );
468 
469  if ( le_state->text().replace( strip, "\\1" ).isEmpty() )
470  missing += tr( "\nState" );
471 
472  if ( le_zip->text().replace( strip, "\\1" ).isEmpty() )
473  missing += tr( "\nZip code" );
474 
475  if ( le_phone->text().replace( strip, "\\1" ).isEmpty() )
476  missing += tr( "\nPhone" );
477 
478  if ( le_email->text().replace( strip, "\\1" ).isEmpty() )
479  missing += tr( "\nEmail" );
480 
481  if ( le_org->text().replace( strip, "\\1" ).isEmpty() )
482  missing += tr( "\nOfganization" );
483 
484 
485  if ( missing != "" )
486  {
487  QMessageBox::information( this,
488  tr( "Attention" ),
489  tr( "Update the following fields before committing data:\n" ) +
490  missing );
491 
492  return false;
493  }
494 
495  return true;
496 }
497 
499 {
500  if ( le_lname ->text() != info.lastName ) return true;
501  if ( le_fname ->text() != info.firstName ) return true;
502  if ( le_address->text() != info.address ) return true;
503  if ( le_city ->text() != info.city ) return true;
504  if ( le_state ->text() != info.state ) return true;
505  if ( le_zip ->text() != info.zip ) return true;
506  if ( le_phone ->text() != info.phone ) return true;
507  if ( le_email ->text() != info.email ) return true;
508  if ( le_org ->text() != info.organization ) return true;
509 
510  QMessageBox::information( this,
511  tr( "Attention" ),
512  tr( "Nothing has changed." ) );
513 
514  return false;
515 }