UltraScan III
us_get_dbrun_ra.cpp
Go to the documentation of this file.
1 
3 #include "us_get_dbrun_ra.h"
4 #include "us_settings.h"
5 #include "us_gui_settings.h"
6 #include "us_db2.h"
7 #include "us_passwd.h"
8 
9 // Primary constructor to establish the dialog
11 : US_WidgetsDialog( 0, 0 ), runID( r )
12 {
13  setWindowTitle( tr( "Available US3 Runs..." ) );
14  setPalette( US_GuiSettings::frameColor() );
15 
16  QVBoxLayout* main = new QVBoxLayout( this );
17  main->setSpacing ( 2 );
18  main->setContentsMargins( 2, 2, 2, 2 );
19 
21  QFontMetrics* fm = new QFontMetrics( font );
22 
23  // Load the runInfo structure with current data
24  if ( ! loadData() )
25  {
26  // Why doesn't this work?
27  // hide();
28  // close(); // done( -1 );
29  // return;
30  }
31 
32  tw = new QTableWidget( runInfo.size(), 4, this ); // rows, columns, parent
33  tw->setPalette( US_GuiSettings::editColor() );
34 
35  QStringList headers;
36  headers << "ID"
37  << "Date"
38  << "RunID"
39  << "Label";
40 
41  tw->setHorizontalHeaderLabels( headers );
42  tw->verticalHeader()->hide();
43  tw->setShowGrid( false );
44  tw->setSelectionBehavior( QAbstractItemView::SelectRows );
45  tw->setMinimumWidth( 640 );
46  tw->setMinimumHeight( 480 );
47  tw->setRowHeight( 0, fm->height() + 4 );
48  tw->setColumnWidth( 0, 50 );
49  tw->setColumnWidth( 1, 150 );
50  tw->setColumnWidth( 2, 250 );
51  tw->setColumnWidth( 3, 350 );
52 
53  // Now load the table, marking each as not-editable
54  for ( int i = 0; i < runInfo.size(); i++ )
55  {
56  RunInfo r = runInfo[ i ];
57 
58  QTableWidgetItem* item = new QTableWidgetItem( QString::number( r.ID ) );
59  item ->setFlags(item->flags() ^ Qt::ItemIsEditable);
60  tw ->setItem( i, 0, item );
61 
62  item = new QTableWidgetItem( r.date );
63  item ->setFlags(item->flags() ^ Qt::ItemIsEditable);
64  tw ->setItem( i, 1, item );
65 
66  item = new QTableWidgetItem( r.runID );
67  item ->setFlags(item->flags() ^ Qt::ItemIsEditable);
68  tw ->setItem( i, 2, item );
69 
70  item = new QTableWidgetItem( r.label );
71  item ->setFlags(item->flags() ^ Qt::ItemIsEditable);
72  tw ->setItem( i, 3, item );
73  }
74 
75  // Enable sorting by a particular column
76  QHeaderView* qHeader = tw ->horizontalHeader();
77  connect( qHeader, SIGNAL( sectionClicked( int ) ),
78  SLOT ( columnClicked ( int ) ) );
79 
80  main->addWidget( tw );
81 
82  // Button Row
83  QHBoxLayout* buttons = new QHBoxLayout;
84 
85  QPushButton* pb_cancel = us_pushbutton( tr( "Cancel" ) );
86  connect( pb_cancel, SIGNAL( clicked() ), SLOT( reject() ) );
87  buttons->addWidget( pb_cancel );
88 
89  QPushButton* pb_delete = us_pushbutton( tr( "Delete" ) );
90  connect( pb_delete, SIGNAL( clicked() ), SLOT( deleteRun() ) );
91  buttons->addWidget( pb_delete );
92 
93  QPushButton* pb_accept = us_pushbutton( tr( "Select" ) );
94  connect( pb_accept, SIGNAL( clicked() ), SLOT( select() ) );
95  buttons->addWidget( pb_accept );
96 
97  main->addLayout( buttons );
98 }
99 
100 // Function to load the runInfo structure with all runID's in the DB
102 {
103  US_Passwd pw;
104  QString masterPW = pw.getPasswd();
105  US_DB2 db( masterPW );
106 
107  if ( db.lastErrno() != US_DB2::OK )
108  {
109  QMessageBox::information( this,
110  tr( "Error" ),
111  tr( "Error making the DB connection.\n" ) );
112  return false;
113  }
114 
115  // Get all the experiment ID's
116  QStringList expIDs;
117  expIDs.clear();
118  QStringList q( "get_experiment_desc" );
119  q << QString::number( US_Settings::us_inv_ID() );
120  db.query( q );
121  while( db.next() )
122  expIDs << db.value( 0 ).toString();
123 
124  // Now get information we want about each experiment
125  runInfo.clear();
126  foreach ( QString expID, expIDs )
127  {
128  q.clear();
129  q << QString( "get_experiment_info" )
130  << expID;
131  db.query( q );
132  db.next();
133 
134  RunInfo r;
135  r.ID = expID.toInt();
136  r.date = db.value( 13 ).toString();
137  r.runID = db.value( 2 ).toString();
138  r.label = db.value( 10 ).toString();
139 
140  runInfo << r;
141  }
142 
143  if ( runInfo.size() < 1 )
144  {
145  QMessageBox::information( this,
146  tr( "Error" ),
147  tr( "There are no US3 runs in the DB to load.\n" ) );
148  return false;
149  }
150 
151  return true;
152 }
153 
154 // Function to sort rows when column header is clicked
156 {
157  tw -> sortItems( col );
158 }
159 
160 // Function to pass information back when select button is pressed
162 {
163  int ndx = tw ->currentRow();
164 
165  runID = tw ->item( ndx, 2 )->text();
166  accept();
167 }
168 
169 // Function to delete the highlighted run when delete button is pressed
171 {
172  US_Passwd pw;
173  QString masterPW = pw.getPasswd();
174  US_DB2 db( masterPW );
175 
176  if ( db.lastErrno() != US_DB2::OK )
177  {
178  QMessageBox::information( this,
179  tr( "Error" ),
180  tr( "Error making the DB connection.\n" ) );
181  return;
182  }
183 
184  int status = QMessageBox::information( this,
185  tr( "Warning" ),
186  tr( "Are you sure you want to delete this run from the DB? " ) +
187  tr( "This action is not reversible. Proceed? " ),
188  tr( "&OK" ), tr( "&Cancel" ),
189  0, 0, 1 );
190  if ( status != 0 ) return;
191 
192  int ndx = tw ->currentRow();
193  QString expID = tw ->item( ndx, 0 )->text();
194 
195  // Let's make sure it's not a calibration experiment in use
196  QStringList q( "count_calibration_experiments " );
197  q << expID;
198  int count = db.functionQuery( q );
199 
200  if ( count < 0 )
201  {
202  qDebug() << "count_calibration_experiments( "
203  << expID
204  << " ) returned a negative count";
205  return;
206  }
207 
208  else if ( count > 0 )
209  {
210  QMessageBox::information( this,
211  tr( "Error" ),
212  tr( "Cannot delete an experiment that is associated "
213  "with a rotor calibration\n" ) );
214  return;
215  }
216 
217  tw->removeRow( ndx );
218 
219  // Delete links between experiment and solutions
220  q.clear();
221  q << "delete_experiment_solutions"
222  << expID ;
223  status = db.statusQuery( q );
224 
225  // Same with cell table
226  q.clear();
227  q << "delete_cell_experiments"
228  << expID ;
229  status = db.statusQuery( q );
230 
231  // Now delete the experiment and all existing rawData,
232  // because we're starting over
233  q.clear();
234  q << "delete_experiment"
235  << expID ;
236  status = db.statusQuery( q );
237 
238  if ( status != US_DB2::OK )
239  {
240  QMessageBox::information( this,
241  tr( "Error" ),
242  db.lastError() + " (" + status + ")\n" );
243  }
244 }