source: PSPA/Interface_Web/trunk/pspaWT/sources/userInterface/src/GWt_softwarePanel.cc @ 413

Last change on this file since 413 was 413, checked in by lemeur, 11 years ago

vérification (dataManager) de la consécution des sections

File size: 16.8 KB
Line 
1//  GWt_softwarePanel.cpp
2//  PSPA
3//
4//  Created by Garnier Laurent on 30/01/13.
5//  Copyright (c) 2013 Garnier Laurent. All rights reserved.
6//
7
8#include <Wt/WLineEdit>
9#include <Wt/WText>
10#include <Wt/WComboBox>
11#include <Wt/WPanel>
12#include <Wt/WBreak>
13#include <Wt/WApplication>
14#include <Wt/WVBoxLayout>
15
16#include "GWt_softwarePanel.h"
17#include "GWt_dialog.h"
18#include "GWt_console.h"
19#include "GWt_globalParameters.h"
20#include "GWt_sectionToExecute.h"
21
22
23GWt_softwarePanel::GWt_softwarePanel(dataManager* dataManager,PspaApplication* pspa)
24  : WContainerWidget() 
25{
26  dtmanage_ = dataManager;
27  pspa_ = pspa;
28 
29  // bouton execute
30  exec_go_ = new WPushButton("execute!");
31  //    exec_go_->setMinimumSize(300,300);
32  //  exec_go_->setDisabled(true);
33  exec_go_->clicked().connect(this, &GWt_softwarePanel::executer);
34 
35   
36  // preparation du bouton push_ok
37  //  WPushButton* exec_ok = new WPushButton("ok");
38  //  exec_ok->clicked().connect(this, &GWt_softwarePanel::updateSectionSelection);
39 
40  // le panel
41  WPanel *panelLogiciels = new WPanel(this);
42  panelLogiciels->setTitle(" sections of beam line for executing softwares ");
43 
44  contenuSections_ = new WContainerWidget();
45  //  contenuSections_->addWidget(exec_ok);
46  contenuSections_->addWidget(exec_go_);
47  contenuSections_->addWidget(new WBreak());
48  contenuSections_->addWidget(new WBreak());
49  addSectionToExecuteW();
50 
51  panelLogiciels->setCentralWidget(contenuSections_);
52}
53
54GWt_softwarePanel::~GWt_softwarePanel() {
55}
56
57void GWt_softwarePanel::addSectionToExecuteW()
58{   
59 
60  abstractElement* premierElement;
61  abstractElement* dernierElement;
62
63  int premierIndex = 0;
64  int dernierIndex = 0;
65
66    // if there is no section
67  if(sections_.size() == 0) {
68      premierElement = pspa_->getBeamLine()->getAbstractElement(0);
69      premierIndex = 0;
70
71  // if this is not the first :
72  // - first element will be the last of the previous section
73  // (or the same if it is the last of the beam line)
74  // - lastElement will be the last of the beam line
75  // - software will be the first of the list
76     
77  } else {
78    dernierElement = pspa_->getBeamLine()->getAbstractElement(pspa_->getBeamLine()->getBeamLineSize()-1);
79    dernierIndex = pspa_->getBeamLine()->getBeamLineSize()-1;
80
81    premierElement = dernierElement;
82    premierIndex = dernierIndex;
83   
84/*    int indexOfLastElementOfLastSection = dtmanage_->getJobListAt(dtmanage_->getJobListSize()-1)->getLastElementNumberInSection();
85
86   
87    // if there are still elements at the end
88    if (pspa_->getBeamLine()->getAbstractElement(indexOfLastElementOfLastSection+1) != NULL ) {
89      premierIndex = indexOfLastElementOfLastSection+1;
90      premierElement = pspa_->getBeamLine()->getAbstractElement(indexOfLastElementOfLastSection+1);
91
92      // if this is the last element
93    } else {
94      premierIndex = indexOfLastElementOfLastSection;
95      premierElement = pspa_->getBeamLine()->getAbstractElement(indexOfLastElementOfLastSection);
96       
97    }
98 */
99  }
100 
101  dernierElement = pspa_->getBeamLine()->getAbstractElement(pspa_->getBeamLine()->getBeamLineSize()-1);
102  dernierIndex = pspa_->getBeamLine()->getBeamLineSize()-1;
103  //  cout << "PspaApplication::addSectionToExecute() : " << premierText << " à  " << dernierText << endl;
104 
105 
106  abstractSoftware* soft = NULL;
107  dtmanage_->addSectionToExecute(premierElement,premierIndex, dernierElement, dernierIndex, soft);
108
109  std::string premierElementLabel = "";
110  std::string dernierElementLabel = "";
111  if (premierElement) {
112    premierElementLabel = premierElement->getLabel();
113  }
114
115  if (dernierElement) {
116    dernierElementLabel = dernierElement->getLabel();
117  }
118
119  WComboBox* lineFromCombo = new WComboBox();
120  WComboBox* lineToCombo = new WComboBox();
121  fillComboWithElements(lineFromCombo);
122  fillComboWithElements(lineToCombo);
123
124  // set selection
125  lineFromCombo->setCurrentIndex(premierIndex);
126  lineToCombo->setCurrentIndex(dernierIndex);
127
128  WComboBox* softCombo = new WComboBox();
129  fillComboWithSoftwares(softCombo);
130
131  GWt_sectionToExecute* newSection = new GWt_sectionToExecute(lineFromCombo, lineToCombo, softCombo,createAddDeletePushButtons(sections_.size()),this);
132
133  stringstream st;
134  st << sections_.size();
135
136  // this is the mean to identify this section!
137  newSection->setObjectName(st.str());
138
139  // push back on sections vector
140  sections_.push_back(newSection);
141
142  contenuSections_->addWidget(newSection);
143 
144  updateSections();
145}
146
147
148// void GWt_softwarePanel::disableSectionExecute()
149// {
150//   exec_go_->setDisabled(true);
151// }
152
153
154// void GWt_softwarePanel::checkSectionSelection()
155// {
156//   if ( selectedSections_.empty() ) return;
157 
158//   // traitement de la premiere ligne
159//   // on impose le depart du calcul au premier element
160//   string premier = dtmanage_->getLabelFromElementNumero(1);
161//   (*selectedSections_.begin())->debut->setText(premier);
162 
163//   string currentString =  (*selectedSections_.begin())->fin->text().toUTF8();
164//   int current = dtmanage_->getNumeroFromElementLabel(currentString);
165   
166//   // si la fin est mal definie on prend toute la config par defaut
167//   if ( current <= 0 || current > dtmanage_->getBeamLineSize() )
168//     {
169//       current = dtmanage_->getBeamLineSize();
170//       currentString =  dtmanage_->getLabelFromElementNumero(current);
171//       (*selectedSections_.begin())->fin->setText(currentString);
172//     }
173//   current++;
174//   currentString = dtmanage_->getLabelFromElementNumero(current);
175 
176//   // traitement des suivantes (on avance d'un cran dans la liste)
177//   list<GWt_sectionToExecute*>::iterator itr, itr0;
178//   itr0 = selectedSections_.begin();
179//   itr0++;
180//   for (itr = itr0; itr != selectedSections_.end(); itr++)
181//     {
182//       // debut
183//       if ( current > dtmanage_->getBeamLineSize() )
184//         {
185//        GWt_dialog warningDialog("PSPA :: verification des sections", " bad section definition !", GWt_dialog::Error,true,true);
186//        warningDialog.exec();
187//        return;
188//         }
189     
190//       (*itr)->debut->setText(currentString);
191//       // fin
192//       string finString =  (*itr)->fin->text().toUTF8();
193     
194//       int numeroFin = dtmanage_->getNumeroFromElementLabel( finString);
195     
196//       if ( numeroFin < current || numeroFin > dtmanage_->getBeamLineSize())
197//         {
198//        GWt_dialog warningDialog("PSPA : Checking of sections", " bad section definition !", GWt_dialog::Error, true,true);
199//        warningDialog.exec();
200//        return;
201//         }
202     
203//       // preparation de la ligne suivante
204//       current = numeroFin +1;
205//       currentString = dtmanage_->getLabelFromElementNumero(current);
206//     }
207 
208//   if (!areDataCoherent()) {
209//     GWt_dialog warningDialog("PSPA : verification des sections", " donnees incoherentes !", GWt_dialog::Error,true,true);
210//     warningDialog.exec();
211//   }
212//   else
213//     {
214//       exec_go_->setDisabled(false);
215       
216//         // All ok, then put colors on beamLine
217//         list<GWt_sectionToExecute*>::iterator itr2;
218//         for(itr2 = selectedSections_.begin();itr2 != selectedSections_.end(); itr2++)
219//         {
220//             string debString = (*itr2)->debut->text().toUTF8();
221//             string finString = (*itr2)->fin->text().toUTF8();
222           
223           
224//             int debut = dtmanage_->getNumeroFromElementLabel(debString);
225//             int fin = dtmanage_->getNumeroFromElementLabel(finString);
226// /**
227//  for (int i=debut; i=<fin; i++) {
228//                 getBeamLine()->getAbstractElement(i)->setBGColor(
229               
230//             }
231//             nomDeLogiciel prog = nomDeLogiciel ( (*itr)->selection->currentIndex() );
232//             dtmanage_->addSectionToExecute(debut,fin,prog);
233// */
234//         }
235//     }
236   
237
238// }
239
240bool GWt_softwarePanel::updateSectionSelection()
241{
242  if ( dtmanage_->getJobListSize() == 0 ) return false;
243 
244  // traitement de la premiere ligne
245  // on impose le depart du calcul au premier element
246  string premier = dtmanage_->getLabelFromElementNumero(1);
247  if (sections_.size() > 0) {
248    sections_[0]->setFirstElementCurrentSelection(premier);
249  }
250 
251  Wt::WString currentString =  sections_[0]->getLastElementCurrentText();
252  int current = dtmanage_->getNumeroFromElementLabel(currentString.toUTF8());
253 
254  // si la fin est mal definie on prend toute la config par defaut
255  if ( current <= 0 || current > dtmanage_->getBeamLineSize() )
256  {
257    current = dtmanage_->getBeamLineSize();
258    currentString =  dtmanage_->getLabelFromElementNumero(current);
259    if (sections_.size() > 0) {
260      sections_[0]->setLastElementCurrentSelection(currentString);
261//...
262    }
263  }
264  current++;
265  currentString = dtmanage_->getLabelFromElementNumero(current);
266 
267  // traitement des suivantes (on avance d'un cran dans la liste)
268  for (int a = 1; a< sections_.size(); a++)
269    {
270      // debut
271      if ( current > dtmanage_->getBeamLineSize() )
272        {
273          sections_[a]->setErrors("This section element could not be after previous session last element");
274          exec_go_->disable();
275          return false;
276        }
277     
278      sections_[a]->setFirstElementCurrentSelection(currentString);
279
280      // fin
281      string finString =  sections_[a]->getLastElementCurrentText().toUTF8();
282     
283      int numeroFin = dtmanage_->getNumeroFromElementLabel( finString);
284     
285      if ( numeroFin < current)
286      {
287        sections_[a]->setErrors("Last section element should be after first section element");
288        exec_go_->disable();
289        return false;
290      }
291
292      if (numeroFin > dtmanage_->getBeamLineSize())
293      {
294        sections_[a]->setErrors("Last section element number is greater than the beam line size");
295        exec_go_->disable();
296        return false;
297      }
298     
299      // preparation de la ligne suivante
300      current = numeroFin +1;
301      currentString = dtmanage_->getLabelFromElementNumero(current);
302    }
303  //  exec_go_->setDisabled(false);
304
305  if (!areDataCoherent()) {
306    exec_go_->disable();
307  } else {
308    exec_go_->enable();
309  }
310
311  return true;
312}
313
314
315bool GWt_softwarePanel::areDataCoherent()
316{
317  bool caMarche = true;
318
319  // initialize dataManager
320  dtmanage_->initializeExecution();
321 
322  // intialize User Interface
323  if (pspa_->getBeamLine()) {
324    pspa_->getBeamLine()->initializeSoftwares();
325  }
326 
327  string diagnosticErrors;
328  string diagnosticWarnings;
329 
330  for (int a = 0; a< sections_.size(); a++)
331  {
332    string debString = sections_[a]->getFirstElementCurrentText().toUTF8();
333    string finString = sections_[a]->getLastElementCurrentText().toUTF8();
334   
335    int debut = pspa_->getBeamLine()->getAbstractElementIndexFromLabel(debString)+1; // FIXME : Should NOT be +1
336    int fin = pspa_->getBeamLine()->getAbstractElementIndexFromLabel(finString)+1; // FIXME : Should NOT be +1
337   
338    nomDeLogiciel prog = nomDeLogiciel ( sections_[a]->getSoftwareCurrentText().toUTF8());
339    dtmanage_->addSectionToExecute(pspa_->getBeamLine()->getAbstractElement(debut),
340                                   debut,
341                                   pspa_->getBeamLine()->getAbstractElement(fin),
342                                   fin,
343                                   dtmanage_->createSoftwareConnexion(prog));
344   
345    diagnosticErrors = "";
346    diagnosticWarnings = "";
347
348    // check sections
349    for (int i=debut-1; i<fin; i++) {
350      if (pspa_->getBeamLine()) {
351        if (pspa_->getBeamLine()->getAbstractElement(i)) {
352          trivaluedBool tb = pspa_->getBeamLine()->getAbstractElement(i)->setSoftware(prog.getString());
353          if (tb  == TBoolError ) {
354            // j'insiste pour remettre le nom generique de l'element refuse (glm / 30/03/2013)
355            diagnosticErrors += pspa_->getBeamLine()->getAbstractElement(i)->getNomdElement().getElementName() + " is not allowed with "+ prog.getString()+"<br /> ";
356
357            caMarche = false;
358
359            // intialize thissoftware
360            pspa_->getBeamLine()->getAbstractElement(i)->setSoftware(nomDeLogiciel::unknownSoftware);
361
362          } else if ( tb == TBoolIgnore ) {
363            diagnosticWarnings += pspa_->getBeamLine()->getAbstractElement(i)->getLabel() + " will be ignored by  "+ prog.getString()+"<br /> ";
364           
365
366          }
367          pspa_->getBeamLine()->update(i);
368        }
369      }
370    }
371    // set errors and warnings
372    sections_[a]->setErrors(diagnosticErrors);
373    sections_[a]->setWarnings(diagnosticWarnings);
374  }
375   
376  return caMarche;
377}
378
379void GWt_softwarePanel::deleteSectionToExecuteW(int sectionLabel)
380{
381  stringstream st;
382  st << sectionLabel;
383  std::string sectionName = st.str();
384 
385  if ( dtmanage_->getJobListSize() == 0 ) return;
386  for (unsigned int sectionIndex = 0; sectionIndex< sections_.size(); sectionIndex++) {
387    if (sections_[sectionIndex]->objectName() == sectionName) {
388
389      // delete from dataManager
390      dtmanage_->clearSectionToExecute(sectionIndex);
391
392      // delete from User Interface
393      contenuSections_->removeWidget(sections_[sectionIndex]);
394      delete sections_[sectionIndex];
395      sections_.erase (sections_.begin()+sectionIndex);
396    }
397  } 
398}
399
400
401void GWt_softwarePanel::updateSections()
402{
403  // update all sections in order to manage new/deleted items
404  for (int a = 0; a< sections_.size(); a++) {
405    fillComboWithElements(sections_[a]->getFirstElement());
406    fillComboWithElements(sections_[a]->getLastElement());
407
408    sections_[a]->getFirstElement()->setEnabled (true);
409    sections_[a]->getLastElement()->setEnabled (true);
410  }
411  if (sections_.size() == 0) {
412    return;
413  }
414  // the first element will always be the first element of the beamLine
415  sections_[0]->getFirstElement()->setCurrentIndex(0);
416  sections_[0]->getFirstElement()->setEnabled (false);
417
418  // the last element will always be the last element of the beamLine
419  sections_[sections_.size()-1]->getLastElement()->setCurrentIndex(sections_[sections_.size()-1]->getLastElement()->count());
420  sections_[sections_.size()-1]->getLastElement()->setEnabled (false);
421
422  // set default values
423
424  // update
425  updateSectionSelection();
426}
427
428
429
430/*
431 string GWt_softwarePanel::getSelection()
432{
433  list<GWt_sectionToExecute*>::iterator itr = selectedSections_.begin();
434  string str = (*itr)->fin->text().toUTF8();
435  return str;
436}
437*/
438
439void GWt_softwarePanel::executer()
440{
441
442  if (!areDataCoherent()) {
443    //    GWt_dialog warningDialog("PSPA : verification des sections", " donnees incoherentes !", GWt_dialog::Error,true,true);
444    //    warningDialog.exec();
445    //    exec_go_->setDisabled(true);
446    return;
447  }
448 
449  // GWt_console* console = NULL;
450  // if (static_cast<GWt_console*> (wApp->findWidget ("console"))) {
451  //   console = static_cast<GWt_console*> (wApp->findWidget ("console"));
452  //   console->addConsoleMessage(string("on va peut etre y arriver"));
453  // }
454 
455  static_cast<GWt_globalParameters*>(pspa_->getGlobalParam())->updateGlobals();
456 
457  GWt_dialog calculDialog("Calcul en cours", "Veuillez patienter...", GWt_dialog::Wait, true,false);
458  calculDialog.show();
459   
460  wApp->processEvents();
461 
462  if (!dtmanage_->executeAll()) {
463    GWt_dialog warningDialog("PSPA : Echec", " echec lors de l'exÈcution !", GWt_dialog::Error, true,true);
464    warningDialog.exec();
465  }
466     
467  //  exec_go_->setDisabled(true);
468  calculDialog.hide(); 
469  pspa_->faireDessin();
470}
471
472
473void GWt_softwarePanel::fillComboWithElements(Wt::WComboBox* cBox) {
474  if (cBox == NULL) return;
475
476  // get the last item selected
477  WString selectedString = cBox->currentText ();
478  cBox->clear();
479
480  for (int a=0; a< pspa_->getBeamLine()->getBeamLineSize(); a++) {
481    abstractElement* abs = pspa_->getBeamLine()->getAbstractElement(a);
482    if (abs != NULL){
483      cBox->addItem(abs->getLabel());
484    }
485  }
486 
487  for (int a=0; a<cBox->count(); a++) {
488    if (cBox->itemText (a) == selectedString) {
489      cBox->setCurrentIndex(a);
490    }
491  }
492  cBox->refresh();
493}
494
495
496void GWt_softwarePanel::fillComboWithSoftwares(Wt::WComboBox* cBox) {
497  if (cBox == NULL) return;
498  cBox->clear();
499
500  unsigned nb = nomDeLogiciel::getNumberOfSoftwares();
501  unsigned k;
502  for(k = 0; k < nb; k++) {
503    cBox->addItem(nomDeLogiciel(k).getString());
504  }
505  cBox->refresh();
506}
507
508
509Wt::WContainerWidget* GWt_softwarePanel::createAddDeletePushButtons(int sectionNumber) {
510
511  WContainerWidget* buttonContainer= new WContainerWidget();
512 
513  Wt::WHBoxLayout* buttonContainerLayout = new Wt::WHBoxLayout();
514  buttonContainerLayout->setContentsMargins(0,0,0,0);
515  // preparation du bouton add
516  WPushButton* exec_add = new WPushButton("+");
517  exec_add->clicked().connect(this, &GWt_softwarePanel::addSectionToExecuteW);
518  exec_add->setStyleClass("roundButton");
519  exec_add->setMaximumSize(20,20);
520  exec_add->setToolTip("Add new section");
521 
522  // preparation du bouton delete
523  WPushButton* exec_delete = new WPushButton("-");
524  //  warningsContainer_->setStyleClass("warningsContainer");
525
526  exec_delete->clicked().connect(boost::bind(&GWt_softwarePanel::deleteSectionToExecuteW, this, sectionNumber));
527  exec_delete->setStyleClass("roundButton");
528  exec_delete->setMaximumSize(20,20);
529  exec_delete->setToolTip("Remove this section");
530
531  buttonContainerLayout->addWidget(exec_add);
532  buttonContainerLayout->addWidget(exec_delete);
533
534  buttonContainer->setLayout(buttonContainerLayout);
535
536  return buttonContainer;
537}
Note: See TracBrowser for help on using the repository browser.