UltraScan III
us_rotor_gui.cpp
Go to the documentation of this file.
1 
3 #include "us_rotor.h"
4 #include "us_rotor_gui.h"
5 #include "us_settings.h"
6 #include "us_util.h"
7 #include "us_db2.h"
8 #include "us_gui_settings.h"
9 #include "us_passwd.h"
10 
12  US_Rotor::RotorCalibration& calibration,
13  bool new_calibration,
14  bool signal_wanted,
15  int select_db_disk
16  ) : US_WidgetsDialog( 0, 0 ), currentCalibration( calibration )
17 {
18  this->savingCalibration = new_calibration;
19  this->signal = signal_wanted;
20 
21  setAttribute( Qt::WA_DeleteOnClose );
22  setupGui( select_db_disk );
23 
24  if ( savingCalibration )
25  {
26  QMessageBox::information( this,
27  tr( "Attention" ),
28  tr( "Please enter the calibration name, omega2t, "
29  "and click Save Calibration Data, or Close" ) );
30 
31  // The rotor calibration data was passed, so let's fill in the rotor data
32  int rotorID = currentCalibration.rotorID;
33  readRotor( select_db_disk, rotorID );
34 
35  // Now select the current rotor
36  QList< QListWidgetItem* > items
37  = lw_rotors->findItems( QString::number( rotorID ), Qt::MatchStartsWith );
38  if ( items.count() == 1 ) // should be exactly 1
39  lw_rotors->setCurrentItem( items[ 0 ] );
40 
41  // Update other rotor information
42  le_name ->setText( currentRotor.name );
44 
45  lw_calibrations ->clear();
46  lw_calibrations ->addItem( currentCalibration.lastUpdated.toString( "d MMMM yyyy" ) );
47  lw_calibrations ->setCurrentRow( 0 );
48 
49  le_calibrationLabel->setText( "" );
50  le_coefficient1 ->setText( QString::number( currentCalibration.coeff1 ) );
51  le_coefficient2 ->setText( QString::number( currentCalibration.coeff2 ) );
52  le_omega2t ->setText( "" );
53 
54  // Also enable line edits for saving calibrations
55  le_calibrationLabel ->setReadOnly( false );
57  le_omega2t ->setReadOnly( false );
58  le_omega2t ->setPalette( US_GuiSettings::editColor() );
59  }
60 
61  reset();
62 }
63 
65  bool signal_wanted,
66  int select_db_disk,
67  const US_Rotor::Rotor& rotorIn,
68  const US_Rotor::RotorCalibration& calibrationIn)
69 : US_WidgetsDialog( 0, 0 ),
70  currentRotor( rotorIn ),
71  currentCalibration( calibrationIn ),
72  signal( signal_wanted )
73 {
74  setAttribute( Qt::WA_DeleteOnClose );
75  this->savingCalibration = false;
76 
77  // Let's see if the user passed a rotorID, and
78  // find out the rest of the rotor info
79  int rotorID = currentRotor.ID; // save for later; they get wiped out
80  int calibrationID = currentCalibration.ID;
81  US_Rotor::Status status;
82 
83  setupGui( select_db_disk );
84 
85  if ( rotorID > 0 && calibrationID > 0 )
86  {
87  // Then this information was passed; fill in with our own data
88  status = readRotor ( select_db_disk, rotorID );
89  status = readCalibration( select_db_disk, calibrationID );
90 
91  // Now select the current rotor
92  QList< QListWidgetItem* > items
93  = lw_rotors->findItems( QString::number( rotorID ), Qt::MatchStartsWith );
94  if ( items.count() == 1 ) // should be exactly 1
95  lw_rotors->setCurrentItem( items[ 0 ] );
96 
97  // And the current calibration
98  items.clear();
99  items = lw_calibrations->findItems( QString::number( calibrationID ), Qt::MatchStartsWith );
100  if ( items.count() == 1 )
101  lw_calibrations->setCurrentItem( items[ 0 ] );
102 
103  }
104 
105  reset();
106 }
107 
108 void US_RotorGui::setupGui( int select_db_disk )
109 {
110  setWindowTitle( tr( "Rotor Management" ) );
111  setPalette( US_GuiSettings::frameColor() );
112 
113  QGridLayout* top = new QGridLayout( this );
114  top->setSpacing ( 2 );
115  top->setContentsMargins ( 2, 2, 2, 2 );
116 
117  // Very light gray, for read-only line edits
118  QPalette gray = US_GuiSettings::editColor();
119  gray.setColor( QPalette::Base, QColor( 0xe0, 0xe0, 0xe0 ) );
120 
121  int row = 0;
122  QStringList DB = US_Settings::defaultDB();
123  if ( DB.isEmpty() ) DB << "Undefined";
124 
125  QLabel* lbl_bannerDB = us_banner( tr( "Current Database: " ) + DB.at( 0 ) );
126  lbl_bannerDB->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed );
127  top->addWidget( lbl_bannerDB, row++, 0, 1, 4 );
128 
129  disk_controls = new US_Disk_DB_Controls( select_db_disk );
130  connect( disk_controls, SIGNAL( changed ( bool ) ),
131  SLOT ( source_changed( bool ) ) );
132  top->addLayout( disk_controls, row++, 0, 1, 4 );
133 
134  // labID
135  QLabel* lbl_lab = us_label( tr( " Please select a Laboratory:" ) );
136  top->addWidget( lbl_lab, row, 0, 1, 2 );
137  cb_lab = new QComboBox( this );
138  connect( cb_lab, SIGNAL( activated( int ) ), // Only if the user has changed it
139  SLOT ( changeLab( int ) ) );
140  top->addWidget( cb_lab, row++, 2, 1, 2 );
141 
142  QLabel* lbl_bannerRotor = us_banner( tr( "Click to select a Rotor: " ), -1 );
143  lbl_bannerRotor->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed );
144  top->addWidget( lbl_bannerRotor, row, 0, 1, 2 );
145 
146  pb_addRotor = us_pushbutton( tr( "Add new Rotor" ) );
147  connect( pb_addRotor, SIGNAL( clicked() ), this, SLOT( addRotor() ) );
148  top->addWidget( pb_addRotor, row++, 2, 1, 2);
149 
150  // List the rotors for selection
152  lw_rotors-> setSortingEnabled( false );
153  connect( lw_rotors, SIGNAL( itemClicked ( QListWidgetItem* ) ),
154  SLOT ( selectRotor ( QListWidgetItem* ) ) );
155  top->addWidget( lw_rotors, row, 0, 3, 2);
156 
157  pb_deleteRotor = us_pushbutton( tr( "Delete Rotor" ) );
158  connect( pb_deleteRotor, SIGNAL( clicked() ), this, SLOT( deleteRotor() ) );
159  top->addWidget( pb_deleteRotor, row++, 2, 1, 2);
160 
161  QLabel *lbl_name = us_label( tr(" Name of Rotor:"), -1 );
162  top->addWidget( lbl_name, row, 2 );
163 
164  le_name = us_lineedit( "", -1 );
165  le_name->setText( tr("< not selected >"));
166  le_name->setPalette ( gray );
167  le_name->setReadOnly( true );
168  connect( le_name, SIGNAL( textEdited ( const QString & ) ),
169  SLOT ( updateName ( const QString & ) ) );
170  top->addWidget( le_name, row++, 3);
171 
172  QLabel *lbl_serialNumber = us_label( tr(" Rotor Serial Number:"), -1 );
173  top->addWidget( lbl_serialNumber, row, 2 );
174 
175  le_serialNumber = us_lineedit( "", -1 );
176  le_serialNumber->setText( tr("< not selected >"));
177  le_serialNumber->setPalette ( gray );
178  le_serialNumber->setReadOnly( true );
179  connect( le_serialNumber, SIGNAL( textEdited ( const QString & ) ),
180  SLOT ( updateSerialNumber ( const QString & ) ) );
181  top->addWidget( le_serialNumber, row++, 3);
182 
183  QLabel* lbl_bannerCalibration = us_banner( tr( "Click to select a Rotor Calibration: " ), -1 );
184  lbl_bannerCalibration->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed );
185  top->addWidget( lbl_bannerCalibration, row, 0, 1, 2);
186 
187  pb_saveCalibration = us_pushbutton( tr( "Save Calibration Data" ) );
188  connect( pb_saveCalibration, SIGNAL( clicked() ), this, SLOT( saveCalibration() ) );
189  top->addWidget( pb_saveCalibration, row++, 2, 1, 2);
190 
192  lw_calibrations-> setSortingEnabled( false ); // comes out of mysql sorted
193  connect( lw_calibrations, SIGNAL( itemClicked ( QListWidgetItem* ) ),
194  SLOT ( selectCalibration( QListWidgetItem* ) ) );
195  top->addWidget( lw_calibrations, row, 0, 6, 2);
196 
197  pb_deleteCalibration = us_pushbutton( tr( "Delete Selected Calibration" ) );
198  connect( pb_deleteCalibration, SIGNAL( clicked() ), this, SLOT( deleteCalibration() ) );
199  top->addWidget( pb_deleteCalibration, row++, 2, 1, 2);
200 
201  pb_viewReport = us_pushbutton( tr( "View Calibration Report" ) );
202  connect( pb_viewReport, SIGNAL( clicked() ), this, SLOT( viewReport() ) );
203  top->addWidget( pb_viewReport, row++, 2, 1, 2);
204 
205  QLabel* lbl_calibName = us_label( tr( " Calibration Name:"), -1 );
206  top->addWidget( lbl_calibName, row, 2 );
207 
208  le_calibrationLabel = us_lineedit( "", -1 );
209  le_calibrationLabel->setText( tr("< not available >"));
210  le_calibrationLabel->setPalette ( gray );
211  le_calibrationLabel->setReadOnly( true );
212  connect( le_calibrationLabel, SIGNAL( textEdited ( const QString& ) ),
213  SLOT ( updateLabel ( const QString& ) ) );
214  top->addWidget( le_calibrationLabel, row++, 3);
215 
216  QLabel *lbl_coefficients = us_label( tr(" Rotor Stretch Coefficient 1:"), -1 );
217  top->addWidget( lbl_coefficients, row, 2 );
218 
219  le_coefficient1 = us_lineedit( "", -1 );
220  le_coefficient1->setText( tr("< not available >"));
221  le_coefficient1->setPalette ( gray );
222  le_coefficient1->setReadOnly( true );
223  top->addWidget( le_coefficient1, row++, 3);
224 
225  QLabel *lbl_date = us_label( tr(" Rotor Stretch Coefficient 2:"), -1 );
226  top->addWidget( lbl_date, row, 2 );
227 
228  le_coefficient2 = us_lineedit( "", -1 );
229  le_coefficient2->setText( tr("< not available >"));
230  le_coefficient2->setPalette ( gray );
231  le_coefficient2->setReadOnly( true );
232  top->addWidget( le_coefficient2, row++, 3);
233 
234  QLabel *lbl_force = us_label( tr(" Rotor omega2t:"), -1 );
235  top->addWidget( lbl_force, row, 2 );
236 
237  le_omega2t = us_lineedit( "", -1 );
238  le_omega2t->setText( tr("< not available >"));
239  le_omega2t->setPalette ( gray );
240  le_omega2t->setReadOnly( true );
241  connect( le_omega2t, SIGNAL( textEdited ( const QString& ) ),
242  SLOT ( updateOmega2t ( const QString& ) ) );
243  top->addWidget( le_omega2t, row++, 3);
244 
245  // some pushbuttons
246  QHBoxLayout* buttons = new QHBoxLayout;
247 
248  pb_reset = us_pushbutton( tr( "Reset" ) );
249  connect( pb_reset, SIGNAL( clicked() ), this, SLOT( load() ) );
250  buttons->addWidget( pb_reset );
251 
252  pb_help = us_pushbutton( tr( "Help" ) );
253  connect( pb_help, SIGNAL( clicked() ), this, SLOT( help() ) );
254  buttons->addWidget( pb_help );
255 
256  pb_accept = us_pushbutton( tr( "Close" ) );
257 
258  if ( signal )
259  {
260  QPushButton* pb_cancel = us_pushbutton( tr( "Cancel" ) );
261  connect( pb_cancel, SIGNAL( clicked() ), SLOT( cancel() ) );
262  buttons->addWidget( pb_cancel );
263 
264  pb_accept -> setText( tr( "Accept" ) );
265  }
266 
267  connect( pb_accept, SIGNAL( clicked() ), SLOT( accept() ) );
268  buttons->addWidget( pb_accept );
269 
270  top->addLayout( buttons, row, 0, 1, 4 );
271 
272  load(); // Loads labs, rotors
273 }
274 
275 void US_RotorGui::reset( void )
276 {
277  lw_rotors ->setEnabled( true );
278  pb_saveCalibration ->setEnabled( false );
279  pb_reset ->setEnabled( true );
280  pb_addRotor ->setEnabled( ( US_Settings::us_inv_level() >= 4 ) &&
281  ( disk_controls->db() ) );
282 
283  // buttons to enable if a rotor is selected
284  bool enabled = lw_rotors->count() > 0 &&
285  lw_rotors->currentIndex().row() != -1;
286  pb_deleteRotor ->setEnabled( enabled );
287 
288  // buttons to enable if a calibration is selected
289  enabled = lw_calibrations->count() > 0 &&
290  lw_calibrations->currentIndex().row() != -1;
291  pb_deleteCalibration->setEnabled( enabled );
292  pb_viewReport ->setEnabled( enabled );
293 
294  // Figure out if the accept button should be enabled
295  if ( ! signal ) // Then it's just a close button
296  pb_accept->setEnabled( true );
297 
298  else if ( lw_rotors->currentIndex().row() == -1 )
299  pb_accept->setEnabled( false ); // rotor is not selected
300 
301  else if ( lw_calibrations->currentIndex().row() == -1 )
302  pb_accept->setEnabled( false ); // calibration is not selected
303 
304  else
305  pb_accept->setEnabled( true );
306 
307  if ( savingCalibration )
308  {
309  // In this case we came from US_RotorCalibration, so disable all
310  // pushbuttons except for that one
311  pb_saveCalibration ->setEnabled( lw_rotors->currentIndex().row() != -1 );
312  pb_accept ->setEnabled( true ); // a close button
313  pb_reset ->setEnabled( false );
314  pb_addRotor ->setEnabled( false );
315  pb_deleteRotor ->setEnabled( false );
316  pb_deleteCalibration->setEnabled( false );
317  lw_rotors ->setEnabled( false );
318  }
319 }
320 
322 {
323  if ( lw_rotors->count() == 0 )
324  {
325  // Couldn't read anything---list widget is empty
326  return 0;
327  }
328 
329  QString itemText = lw_rotors->currentItem()->text();
330  QStringList parts = itemText.split( ":" );
331  int rotorID = parts[ 0 ].toInt(); // The ID in the Rotor class
332 
333  return rotorID;
334 }
335 
337 {
338  int dbdisk = ( disk_controls->db() ) ? US_Disk_DB_Controls::DB
340 
342  US_AbstractRotorGui *arDialog = new US_AbstractRotorGui( true, dbdisk, &currentRotor );
343  connect( arDialog, SIGNAL ( accepted() ), this, SLOT ( newRotor() ) );
344  connect( arDialog, SIGNAL ( use_db ( bool ) ),
345  SLOT ( update_disk_db( bool ) ) );
346  arDialog->show();
347 }
348 
350 {
351  ( db ) ? disk_controls->set_db() : disk_controls->set_disk();
352 }
353 
355 {
358  le_name ->setText( currentRotor.name );
360 
361  // Create a dummy configuration to associate before the real one
362  // is available
365  currentCalibration.label = "Dummy Calibration";
366  currentCalibration.report = "This is a dummy calibration --- please replace.";
367  currentCalibration.calibrationExperimentID = -1; // special value
368 
369  if ( disk_controls->db() )
370  {
371  US_Passwd pw;
372  QString masterPW = pw.getPasswd();
373  US_DB2 db( masterPW );
374 
375  if ( db.lastErrno() != US_DB2::OK )
376  {
377  db_error( db.lastError() );
378  return;
379  }
380 
381  int status = currentRotor.addRotorDB( &db );
382  if ( status != US_DB2::OK )
383  db_error( db.lastError() );
384 
385  else
386  {
387  int rotorID = db.lastInsertID();
388  currentCalibration.rotorID = rotorID;
389  currentCalibration.saveDB( rotorID, &db );
390  }
391 
392  }
393  else
394  {
397  }
398 
399  // now get new rotorID and update the list box.
400  loadRotors( labID );
401 
404 
405  // code to select the new rotor from the list
406 }
407 
408 void US_RotorGui::db_error( const QString &error )
409 {
410  QMessageBox::warning( this, tr( "Database Problem" ),
411  tr( "Database returned the following error: \n" ) + error );
412 }
413 
414 void US_RotorGui::selectRotor( QListWidgetItem *item )
415 {
416  QString selected = item->text();
417  QStringList parts = selected.split( ":" );
418 
419  int rotorID = parts[ 0 ].toInt();
420  US_Rotor::Status status;
421 
422  // These calibration info items are now out of scope
423  le_coefficient1 ->setText( "< not available >" );
424  le_coefficient2 ->setText( "< not available >" );
425  le_omega2t ->setText( "< not available >" );
426 
427  // Find out the rest of the rotor info
428  status = readRotor( disk_controls->db(), rotorID );
429 
430  // Update rotor info on the form, if we can
431  if ( status == US_Rotor::ROTOR_OK )
432  {
433  le_name ->setText( currentRotor.name );
437  }
438 
439  pb_deleteRotor->setEnabled( true );
440 
441  // Get the associated calibration profiles
442  readCalibrationProfiles( rotorID );
443 }
444 
445 US_Rotor::Status US_RotorGui::readRotor( int disk_db, int rotorID )
446 {
447  // Let's see if the user passed a rotorID, and
448  // find out the rest of the rotor info
449  US_Rotor::Status status;
451 
452  if ( rotorID <= 0 )
453  return( US_Rotor::NOT_FOUND );
454 
455  if ( disk_db == US_Disk_DB_Controls::DB )
456  {
457  US_Passwd pw;
458  QString masterPW = pw.getPasswd();
459  US_DB2 db( masterPW );
460 
461  if ( db.lastErrno() != US_DB2::OK )
462  {
463  connect_error( db.lastError() );
464  return( US_Rotor::CONNECT_ERROR );
465  }
466 
467  status = currentRotor.readDB( rotorID, &db );
468 
469  // Since there is no save rotor button, save any that is
470  // selected to disk automatically
472  }
473 
474  else
475  {
476  status = currentRotor.readDisk( rotorID );
477  }
478 
479  return status;
480 }
481 
483 {
484  // let's make sure we know which rotor
485  QString selected = lw_rotors->currentItem()->text();
486  QStringList parts = selected.split( ":" );
487 
488  int rotorID = parts[ 0 ].toInt();
489 
490  if ( disk_controls->db() )
491  {
492  US_Passwd pw;
493  QString masterPW = pw.getPasswd();
494  US_DB2 db( masterPW );
495 
496  if ( db.lastErrno() != US_DB2::OK )
497  {
498  connect_error( db.lastError() );
499  return;
500  }
501 
502  int status = US_Rotor::Rotor::deleteRotorDB( rotorID, &db );
503  if ( status == US_DB2::ROTOR_IN_USE )
504  {
505  QString error = tr( "This rotor is in use, and can't be deleted" );
506  db_error( error );
507  return;
508  }
509 
510  else if ( status != US_DB2::OK )
511  {
512  db_error( db.lastError() );
513  return;
514  }
515  }
516 
517  else
518  {
519  QMessageBox::warning( this,
520  tr( "Attention" ),
521  tr( "Deleting rotors on the local disk is not implemented yet." ) );
522  return;
523  }
524 
525  loadRotors( labID );
526 
529  reset();
530 }
531 
533 {
534  QStringList calibrationDescriptions;
535 
536  if ( disk_controls->db() )
537  {
538  // Find out what rotor calibrations we have
539  US_Passwd pw;
540  QString masterPW = pw.getPasswd();
541  US_DB2 db( masterPW );
542 
543  if ( db.lastErrno() != US_DB2::OK )
544  {
545  connect_error( db.lastError() );
546  return( false );
547  }
548 
549  QStringList q( "get_rotor_calibration_profiles" );
550  q << QString::number( rotorID );
551  db.query( q );
552 
553  lw_calibrations->clear();
554  while ( db.next() )
555  {
556  QString ID = db.value( 0 ).toString();
557  QStringList dateParts = db.value( 1 ).toString().split( " " );
558  QDate dateUpdated = QDate::fromString( dateParts[ 0 ], "yyyy-MM-dd" );
559  QString label = db.value( 2 ).toString();
560  calibrationDescriptions << ( ID + ": " + label + dateUpdated.toString( " (d MMMM yyyy)" ) );
561  }
562 
563  }
564 
565  else
566  {
567  // Get information about all the rotor calibrations in this lab
568  QVector< US_Rotor::RotorCalibration > rc;
569 
571  {
572  QMessageBox::warning( this, tr( "Disk Problem" ),
573  tr( "Could not find rotor calibration profiles \n" ) );
574  return( false );
575  }
576 
577  for ( int i = 0; i < rc.size(); i++ )
578  {
579  if ( rc[ i ].rotorID == rotorID )
580  calibrationDescriptions << ( QString::number( rc[ i ].ID ) + ": " +
581  rc[ i ].lastUpdated.toString( "d MMMM yyyy" ) );
582  }
583  }
584 
585  lw_calibrations->clear();
586  lw_calibrations->addItems( calibrationDescriptions );
587 
588  // Select the first one in the list by default
589  if ( lw_calibrations->count() > 0 )
590  {
591  lw_calibrations->setCurrentRow( 0 );
592  selectCalibration( lw_calibrations->currentItem() );
593  }
594 
595  return( true );
596 }
597 
598 void US_RotorGui::selectCalibration( QListWidgetItem *item )
599 {
600  QString selected = item->text();
601  QStringList parts = selected.split( ":" );
602 
603  int calibrationID = parts[ 0 ].toInt();
604  US_Rotor::Status status;
605 
606  // Find out the rest of the rotor calibration info
607  status = readCalibration( disk_controls->db(), calibrationID );
608 
609  // Now populate what we can on the form
610  if ( status == US_Rotor::ROTOR_OK )
611  {
613  le_coefficient1 ->setText( QString::number( currentCalibration.coeff1 ) );
614  le_coefficient2 ->setText( QString::number( currentCalibration.coeff2 ) );
615  le_omega2t ->setText( QString::number( currentCalibration.omega2t ) );
616  }
617 
618  if ( ! savingCalibration )
619  {
620  pb_deleteCalibration->setEnabled( true );
621  }
622 
623  reset();
624 }
625 
626 US_Rotor::Status US_RotorGui::readCalibration( int disk_db, int calibrationID )
627 {
628  // Let's see if the user passed a calibrationID, and
629  // find out the rest of the calibration info
630  US_Rotor::Status status;
632 
633  if ( calibrationID <= 0 )
634  return( US_Rotor::NOT_FOUND );
635 
636  if ( disk_db == US_Disk_DB_Controls::DB )
637  {
638  US_Passwd pw;
639  QString masterPW = pw.getPasswd();
640  US_DB2 db( masterPW );
641 
642  if ( db.lastErrno() != US_DB2::OK )
643  {
644  connect_error( db.lastError() );
645  return( US_Rotor::CONNECT_ERROR );
646  }
647 
648  status = currentCalibration.readDB( calibrationID, &db );
649 
650  // Since there is no save rotor calibration button, save any that is
651  // selected to disk automatically
653  }
654 
655  else
656  {
657  status = currentCalibration.readDisk( calibrationID );
658  }
659 
660  return status;
661 }
662 
664 {
665  US_EditorGui* edit = new US_EditorGui();
666  edit->setWindowTitle( tr("Rotor Calibration Report") );
667  edit->move( this->pos() + QPoint( 100, 100 ) );
668  edit->resize( 500, 400 );
669  edit->editor->e->setFont( QFont( US_GuiSettings::fontFamily(),
671  edit->editor->e->setText( currentCalibration.report );
672  edit->exec();
673 }
674 
676 {
677  // let's make sure we know which calibration
678  QString selected = lw_calibrations->currentItem()->text();
679  QStringList parts = selected.split( ":" );
680 
681  int calibrationID = parts[ 0 ].toInt();
682 
683  if ( disk_controls->db() )
684  {
685  US_Passwd pw;
686  QString masterPW = pw.getPasswd();
687  US_DB2 db( masterPW );
688 
689  if ( db.lastErrno() != US_DB2::OK )
690  {
691  connect_error( db.lastError() );
692  return;
693  }
694 
695  int status = US_Rotor::RotorCalibration::deleteCalibrationDB( calibrationID, &db );
696  if ( status == US_DB2::CALIB_IN_USE )
697  {
698  QString error = tr( "This rotor calibration is in use, and can't be deleted" );
699  db_error( error );
700  return;
701  }
702 
703  else if ( status != US_DB2::OK )
704  {
705  db_error( QString::number( status ) + ": " + db.lastError() );
706  return;
707  }
708  }
709 
710  else
711  {
712  QMessageBox::warning( this,
713  tr( "Attention" ),
714  tr( "Deleting rotor calibrations on the local disk is not implemented yet." ) );
715  return;
716  }
717 
719  reset();
720 }
721 
722 void US_RotorGui::updateName( const QString &name)
723 {
724  currentRotor.name = name;
725 }
726 
727 void US_RotorGui::updateSerialNumber( const QString &number)
728 {
729  currentRotor.serialNumber = number;
730 }
731 
732 void US_RotorGui::updateLabel( const QString &label)
733 {
734  currentCalibration.label = label;
735 }
736 
737 void US_RotorGui::updateOmega2t( const QString &omega2t)
738 {
739  currentCalibration.omega2t = omega2t.toFloat();
740 }
741 
743 {
744  // Should already have everything we need except for the label
745  int rotorID = currentCalibration.rotorID;
747 
748  // Save the calibration info
749  if ( disk_controls->db() )
750  {
751  US_Passwd pw;
752  QString masterPW = pw.getPasswd();
753  US_DB2 db( masterPW );
754 
755  if ( db.lastErrno() != US_DB2::OK )
756  {
757  connect_error( db.lastError() );
758  return;
759  }
760 
761  int status = currentCalibration.saveDB( rotorID, &db );
762  if ( status == US_DB2::OK )
763  {
765 
766  reset();
767 
768  QMessageBox::information( this,
769  tr( "Attention" ),
770  tr( "Calibration was saved to the DB." ) );
771  }
772  }
773 
774  else
775  {
777 
778  reset();
779 
780  QMessageBox::information( this,
781  tr( "Attention" ),
782  tr( "Calibration was saved to the Disk." ) );
783  }
784 
785  // Now that we've saved the calibration, convert to regular mode
786  savingCalibration = false;
787 
788  // Disable calibration input fields
789  QPalette gray = US_GuiSettings::editColor();
790  gray.setColor( QPalette::Base, QColor( 0xe0, 0xe0, 0xe0 ) );
791  le_calibrationLabel->setPalette ( gray );
792  le_calibrationLabel->setReadOnly( true );
793  le_omega2t->setPalette ( gray );
794  le_omega2t->setReadOnly( true );
795 
796  // Load all the calibration profiles
798  reset();
799 }
800 
801 // A function to find out if the original dummy calibration is still there,
802 // and replace it with the current one if it does. Also, to delete
803 // the dummy.
805 {
806  // This is only valid on the DB
807  if ( ! disk_controls->db() )
808  {
809  QString error = tr( "This is a database-only function." );
810  db_error( error );
811  return;
812  }
813 
814  US_Passwd pw;
815  QString masterPW = pw.getPasswd();
816  US_DB2 db( masterPW );
817 
818  if ( db.lastErrno() != US_DB2::OK )
819  {
820  connect_error( db.lastError() );
821  return;
822  }
823 
824  int oldCalibrationID = -1; // To return the dummy's ID in
825  int status = currentCalibration.replaceDummyDB( oldCalibrationID, &db );
826 
827  if ( status == US_DB2::NO_CALIB )
828  {
829  QString error = tr( "The replacement rotor calibration could not be found." );
830  db_error( error );
831  return;
832  }
833 
834  else if ( db.lastErrno() != US_DB2::OK )
835  {
836  db_error( db.lastError() );
837  return;
838  }
839 
840  // Was a dummy calibration found?
841  else if ( oldCalibrationID == -1 )
842  return;
843 
844  // If we get here then a dummy calibration was found and replaced without error
845  status = US_Rotor::RotorCalibration::deleteCalibrationDB( oldCalibrationID, &db );
846  if ( status == US_DB2::CALIB_IN_USE )
847  {
848  QString error = tr( "The database shows the dummy calibration is still in use, and can't be deleted" );
849  db_error( error );
850  return;
851  }
852 
853  else if ( status != US_DB2::OK )
854  {
855  db_error( QString::number( status ) + ": " + db.lastError() );
856  return;
857  }
858 }
859 
861 {
862  QStringList DB = US_Settings::defaultDB();
863 
864  if ( db && ( DB.size() < 5 ) )
865  {
866  QMessageBox::warning( this,
867  tr( "Attention" ),
868  tr( "There is no default database set." ) );
869  }
870 
871  load();
872 
873  emit use_db( db );
874  qApp->processEvents();
875 
876  reset();
877 }
878 
880 {
881  if ( signal )
882  {
884  }
885 
886  close();
887 }
888 
890 {
891  if ( signal )
892  {
893  emit RotorCalibrationCanceled ();
894  }
895 
896  close();
897 }
898 
899 bool US_RotorGui::load( void )
900 {
901  QVector < US_Rotor::Lab > labList;
902 
903  if ( disk_controls->db() )
904  {
905  // Find out what labs we have
906  US_Passwd pw;
907  QString masterPW = pw.getPasswd();
908  US_DB2 db( masterPW );
909 
910  if ( db.lastErrno() != US_DB2::OK )
911  {
912  connect_error( db.lastError() );
913  return( false );
914  }
915 
916  // Get information about all the labs
917  if ( US_Rotor::readLabsDB( labList, &db ) == US_Rotor::NOT_FOUND )
918  {
919  QMessageBox::warning( this, tr( "Database Problem" ),
920  tr( "Could not read lab information \n" ) );
921 
922  return false;
923  }
924 
925  }
926 
927  else
928  {
929  // Get information about all the labs
930  if ( US_Rotor::readLabsDisk( labList ) == US_Rotor::NOT_FOUND )
931  {
932  QMessageBox::warning( this, tr( "Disk Problem" ),
933  tr( "Could not read lab information \n" ) );
934 
935  return false;
936  }
937 
938  }
939 
940  cb_lab->clear();
941  int index = 0;
942  foreach ( US_Rotor::Lab lab, labList )
943  {
944  cb_lab->addItem( QString::number( lab.ID ) + ": " + lab.name );
945  if ( lab.ID == currentRotor.labID )
946  index = cb_lab->currentIndex();
947  }
948  if ( cb_lab->count() > 0 )
949  cb_lab->setCurrentIndex( index );
950 
951  changeLab( 0 ); // To display labs, rotors
952 
953  reset(); // Redo buttons after all data is loaded
954 
955  return true;
956 }
957 
958 // Function to change the current lab
960 {
961  if ( cb_lab->count() == 0 )
962  {
963  return; // nothing to display
964  }
965 
966  QString itemText = cb_lab->currentText();
967  QStringList parts = itemText.split( ":" );
968  labID = parts[ 0 ].toInt();
969 
970  le_name ->setText( "" );
971  le_serialNumber ->setText( "" );
972  le_calibrationLabel->setText( "" );
973 
974  loadRotors( labID );
975 }
976 
977 bool US_RotorGui::loadRotors( const int labID )
978 {
979  QStringList rotorDescriptions;
980 
981  // Clear out lists, line edits
983  lw_rotors ->clear();
984  lw_calibrations ->clear();
985  le_name ->setText( "< not selected >" );
986  le_serialNumber ->setText( "< not selected >" );
987  le_calibrationLabel->setText( "< not selected >" );
988  le_coefficient1 ->setText( "< not available >" );
989  le_coefficient2 ->setText( "< not available >" );
990  le_omega2t ->setText( "< not available >" );
991 
992  if ( ! savingCalibration )
994 
995  if ( disk_controls->db() )
996  {
997  // Find out what rotors we have
998  US_Passwd pw;
999  QString masterPW = pw.getPasswd();
1000  US_DB2 db( masterPW );
1001 
1002  if ( db.lastErrno() != US_DB2::OK )
1003  {
1004  connect_error( db.lastError() );
1005  return( false );
1006  }
1007 
1008  QStringList q( "get_rotor_names" );
1009  q << QString::number( labID ); // find rotors from this lab
1010  db.query( q );
1011 
1012  while ( db.next() )
1013  {
1014  QString rotorID = db.value( 0 ).toString();
1015  QString rotorDesc = db.value( 1 ).toString();
1016  rotorDescriptions << ( rotorID + ": " + rotorDesc );
1017  }
1018 
1019  }
1020 
1021  else
1022  {
1023  // Get information about all the rotors in this lab
1024  QVector< US_Rotor::Rotor > rotors;
1025 
1026  if ( US_Rotor::readRotorsFromDisk( rotors, labID ) == US_Rotor::NOT_FOUND )
1027  {
1028  QMessageBox::warning( this, tr( "Disk Problem" ),
1029  tr( "Could not read rotor information \n" ) );
1030  return( false );
1031  }
1032 
1033  for ( int i = 0; i < rotors.size(); i++ )
1034  {
1035  if ( rotors[ i ].labID == labID )
1036  rotorDescriptions << ( QString::number( rotors[ i ].ID ) + ": " + rotors[ i ].name );
1037  }
1038  }
1039 
1040  lw_rotors->addItems( rotorDescriptions );
1041 
1042  return( true );
1043 }
1044 
1045 void US_RotorGui::connect_error( const QString& error )
1046 {
1047  QMessageBox::warning( this, tr( "Connection Problem" ),
1048  tr( "Could not connect to database \n" ) + error );
1049 }
1050