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

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

ajout de l'include algorithm

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