UltraScan III
us_select_runid.cpp
Go to the documentation of this file.
1 
3 #include "us_select_runid.h"
4 #include "us_settings.h"
5 #include "us_gui_settings.h"
6 #include "us_matrix.h"
7 #include "us_investigator.h"
8 #include "us_passwd.h"
9 #include "us_db2.h"
10 #include "us_util.h"
11 #include "us_editor.h"
12 #include "us_constants.h"
13 #include "us_report.h"
14 
15 // Main constructor with flags for edit, latest-edit and local-data
16 
17 US_SelectRunid::US_SelectRunid( bool dbase, QStringList& runIDs )
18  : US_WidgetsDialog( 0, 0 ),
19  runIDs ( runIDs )
20 {
21  sel_db = dbase;
23 
24  setWindowTitle( tr( "Select Run ID for vHW Distributions (%1)" )
25  .arg( sel_db ? "DB" : "Local" ) );
26  setPalette ( US_GuiSettings::frameColor() );
27  setMinimumSize( 480, 300 );
28 DbgLv(1) << "SE:sel_db" << sel_db;
29 
30  // Main layout
31  QVBoxLayout* main = new QVBoxLayout( this );
32  main->setContentsMargins( 2, 2, 2, 2 );
33  main->setSpacing ( 2 );
34 
35  // Top layout: buttons and fields above list widget
36  QGridLayout* top = new QGridLayout;
37 
40  pb_invest = us_pushbutton( tr( "Select Investigator" ) );
41  QString invnum = QString::number( US_Settings::us_inv_ID() ) + ": ";
42  QString invusr = US_Settings::us_inv_name();
43  le_invest = us_lineedit( invnum + invusr, 0, true );
44  pb_invest->setEnabled( sel_db );
45 
46  // Search line
47  QLabel* lb_filtdata = us_label( tr( "Search" ) );
48 
50 
51  connect( dkdb_cntrls, SIGNAL( changed( bool ) ),
52  this, SLOT( update_disk_db( bool ) ) );
53  connect( pb_invest, SIGNAL( clicked() ),
54  SLOT ( get_person() ) );
55  connect( le_dfilter, SIGNAL( textChanged( const QString& ) ),
56  SLOT ( search ( const QString& ) ) );
57 
58  int row = 0;
59  top->addLayout( dkdb_cntrls, row++, 0, 1, 3 );
60  top->addWidget( pb_invest, row, 0, 1, 1 );
61  top->addWidget( le_invest, row++, 1, 1, 2 );
62  top->addWidget( lb_filtdata, row, 0, 1, 1 );
63  top->addWidget( le_dfilter, row++, 1, 1, 2 );
64 
65  main->addLayout( top );
66 
68 
69  // List widget to show data choices
70  lw_data = new QListWidget( this );
71  lw_data->setFrameStyle ( QFrame::NoFrame );
72  lw_data->setPalette ( US_GuiSettings::editColor() );
73  lw_data->setFont ( font );
74  lw_data->setSelectionMode( QAbstractItemView::ExtendedSelection );
75  //lw_data->setSelectionMode( QAbstractItemView::SingleSelection );
76  connect( lw_data, SIGNAL( itemSelectionChanged() ),
77  this, SLOT ( selectionChanged() ) );
78 
79  main->addWidget( lw_data );
80 
81  // Button Row
82  QHBoxLayout* buttons = new QHBoxLayout;
83 
84  QPushButton* pb_cancel = us_pushbutton( tr( "Cancel" ) );
85  QPushButton* pb_accept = us_pushbutton( tr( "Accept" ) );
86 
87  connect( pb_cancel, SIGNAL( clicked() ), SLOT( cancelled() ) );
88  connect( pb_accept, SIGNAL( clicked() ), SLOT( accepted() ) );
89 
90  buttons->addWidget( pb_cancel );
91  buttons->addWidget( pb_accept );
92 
93  main->addLayout( buttons );
94 
95  // Status Row
96  QFontMetrics fm( font );
97  int fhigh = fm.lineSpacing();
98  int fwide = fm.width( QChar( '6' ) );
99  int lhigh = fhigh * 3 + 12;
100  int lwide = fwide * 32;
101 
103  te_status->setMaximumHeight( lhigh );
104  te_status->resize( lwide, lhigh );
105  us_setReadOnly( te_status, true );
106  te_status->setTextColor( Qt::blue );
107 
108  main->addWidget( te_status );
109 
110  // List from disk or db source
111  list_data();
112 }
113 
114 void US_SelectRunid::search( const QString& search_string )
115 {
116  lw_data->setCurrentItem( NULL );
117 
118  for ( int ii = 0; ii < lw_data->count(); ii++ )
119  {
120  QListWidgetItem* lwi = lw_data->item( ii );
121  bool hide = ! lwi->text().contains( search_string, Qt::CaseInsensitive );
122  lwi->setHidden( hide );
123  }
124 }
125 
126 // List data choices (from db or disk)
128 {
129  rlabels.clear();
130 
131  if ( sel_db ) // Scan database data
132  {
133  scan_dbase_runs();
134  }
135  else // Scan local disk data
136  {
137  scan_local_runs();
138  }
139 
140  lw_data->clear();
141 
142  if ( rlabels.size() == 0 )
143  {
144  QString clabel = tr( "No data found." );
145  lw_data->addItem( new QListWidgetItem( clabel ) );
146  return;
147  }
148 
149 DbgLv(1) << "LD:sel_db" << sel_db << "rlsize" << rlabels.size();
150  for ( int ii = 0; ii < rlabels.size(); ii++ )
151  { // Propagate list widget with labels
152  QString clabel = rlabels.at( ii );
153 
154  lw_data->addItem( new QListWidgetItem( clabel ) );
155  }
156 
157  count_list = lw_data->count();
158  count_seld = lw_data->selectedItems().size();
159  te_status->setText(
160  tr( "The list derives from %1 scanned run IDs.\n"
161  "Of these, %2 have associated vHW distribution data.\n"
162  "%3 runs are currently selected for combination plot components." )
163  .arg( count_allr ).arg( count_list ).arg( count_seld ) );
164 }
165 
166 // Cancel button: no editIDs returned
168 {
169  reject();
170  close();
171 }
172 
173 // Accept button: set up to return editID pre-filter information
175 {
176 DbgLv(1) << "SE:accepted()";
177  QList< QListWidgetItem* > selitems = lw_data->selectedItems();
178 
179  if ( selitems.size() == 0 )
180  {
181  QMessageBox::information( this,
182  tr( "No Data Selected" ),
183  tr( "You have not selected any data.\nSelect or Cancel" ) );
184  return;
185  }
186 
187  // Get and return runIDs from selected edit items
188  for ( int ii = 0; ii < selitems.size(); ii++ )
189  {
190  QListWidgetItem* lwi_data = selitems.at( ii );
191  QString clabel = lwi_data->text();
192 DbgLv(1) << "SE: ii clabel" << ii << clabel;
193 
194  runIDs << clabel;
195  }
196 DbgLv(1) << "SE: runID" << runIDs[0];
197 
198  accept(); // Signal that selection was accepted
199  close();
200 }
201 
202 // Scan database for edit sets
204 {
205  US_Passwd pw;
206  US_DB2 db( pw.getPasswd() );
207  count_allr = 0;
208  count_list = 0;
209  count_seld = 0;
210 
211  if ( db.lastErrno() != US_DB2::OK )
212  {
213  QMessageBox::information( this,
214  tr( "DB Connection Problem" ),
215  tr( "There was an error connecting to the database:\n" )
216  + db.lastError() );
217  return;
218  }
219 
220  QStringList docruns;
221  QString runid;
222 
223  QStringList query;
224  QString invID = QString::number( US_Settings::us_inv_ID() );
225 
226  query.clear();
227  query << "get_report_desc" << invID;
228  db.query( query );
229 
230  while ( db.next() )
231  {
232  runid = db.value( 4 ).toString();
233 DbgLv(1) << "ScDB: report runid" << runid;
234 
235  if ( ! docruns.contains( runid ) )
236  {
237  count_allr++;
238  docruns << runid;
239  }
240  }
241 
242  int nreps = docruns.count();
243 DbgLv(1) << "ScDB: nreps" << nreps;
244  for ( int ii = 0; ii < nreps; ii++ )
245  {
246  runid = docruns[ ii ];
247  US_Report freport;
248  freport.readDB( runid, &db );
249  int ntrip = freport.triples.count();
250 DbgLv(1) << "ScDB: ntrip" << ntrip;
251  int ndats = 0;
252 
253  for ( int jj = 0; jj < ntrip; jj++ )
254  {
255  US_Report::ReportTriple* tripl = &freport.triples[ jj ];
256  int ndocs = tripl->docs.count();
257 
258  for ( int kk = 0; kk < ndocs; kk++ )
259  {
260  US_Report::ReportDocument* doc = &tripl->docs[ kk ];
261  QString fname = doc->filename;
262 
263 DbgLv(1) << "ScDB: kk fname" << kk << fname;
264  if ( fname.contains( "distrib.csv" ) )
265  ndats++;
266  }
267  }
268 
269 DbgLv(1) << "ScDB: ndats" << ndats;
270  if ( ndats > 0 )
271  {
272  count_list++;
273  rlabels << runid;
274  }
275  }
276 DbgLv(1) << "ScDB:count_list" << count_list;
277 }
278 
279 
280 // Scan local disk for edit sets
282 {
283  QString rdir = US_Settings::resultDir();
284  QStringList aucdirs = QDir( rdir ).entryList(
285  QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name );
286 DbgLv(1) << "ScLo:rdir" << rdir << "aucdir count" << aucdirs.count();
287 
288  QStringList aucfilt( "*.auc" );
289  QStringList datfilt;
290  count_allr = 0;
291  count_list = 0;
292  count_seld = 0;
293 
294  for ( int ii = 0; ii < aucdirs.count(); ii++ )
295  {
296  QString subdir = rdir + "/" + aucdirs.at( ii );
297  QStringList aucfiles = QDir( subdir ).entryList(
298  aucfilt, QDir::Files, QDir::Name );
299 DbgLv(1) << "ScLo: subdir" << subdir << "aucfiles count" << aucfiles.count();
300 
301  if ( aucfiles.count() < 1 )
302  continue;
303 
304  QString aucfbase = aucfiles.at( 0 );
305  QString aucfname = subdir + "/" + aucfbase;
306  QString runID = aucfbase.section( ".", 0, -6 );
307  QString tripl = aucfbase.section( ".", -4, -2 )
308  .replace( ".", "" );
309  count_allr++;
310 
311  datfilt.clear();
312  datfilt << "vHW." + tripl + ".*distrib.csv"
313  << "vHW." + tripl + ".*envelope.csv";
314  QStringList datfiles = QDir( subdir ).entryList(
315  datfilt, QDir::Files, QDir::Name );
316 DbgLv(1) << "ScLo: datfilt0" << datfilt[0];
317 DbgLv(1) << "ScLo: datfiles count" << datfiles.count();
318 
319  if ( datfiles.count() < 1 )
320  continue;
321 
322  count_list++;
323  rlabels << runID;
324  }
325 DbgLv(1) << "ScLo:rlabels count" << count_list << rlabels.count();
326 }
327 
328 
329 // Investigator button clicked: get investigator from dialog
331 {
332  int invID = US_Settings::us_inv_ID();
333  US_Investigator* dialog = new US_Investigator( true, invID );
334 
335  connect( dialog, SIGNAL( investigator_accepted( int ) ),
336  SLOT( update_person( int ) ) );
337 
338  dialog->exec();
339 }
340 
341 // Slot to handle accept in investigator dialog
343 {
344  QString number = ( ID > 0 ) ? QString::number( ID ) + ": " : "";
345  le_invest->setText( number + US_Settings::us_inv_name() );
346 
347  list_data();
348 }
349 
350 // Slot to update disk/db selection
352 {
353  emit changed( isDB );
354 
355  sel_db = isDB;
356  list_data();
357 
358  pb_invest->setEnabled( isDB );
359  setWindowTitle( tr( "Select Run ID for vHW Distributions (%1)" )
360  .arg( sel_db ? "DB" : "Local" ) );
361 }
362 
363 // Slot to record a change in list item selection
365 {
366  count_seld = lw_data->selectedItems().size();
367 
368  te_status->setText(
369  tr( "The list derives from %1 scanned run IDs.\n"
370  "Of these, %2 have associated vHW distribution data.\n"
371  "%3 %4 currently selected for combination plot components." )
372  .arg( count_allr ).arg( count_list ).arg( count_seld )
373  .arg( count_seld > 1 ? tr( "runs are" ) : tr( "run is" ) ) );
374 }
375