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

Last change on this file since 436 was 436, checked in by touze, 11 years ago

ajout de secteurs

File size: 13.1 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#include "GWt_pspaApplication.h"
22
23#define BAVARD 0
24
25GWt_softwarePanel::GWt_softwarePanel(dataManager* dataManager,PspaApplication* pspa)
26  : WContainerWidget() 
27{
28  dtmanage_ = dataManager;
29  pspa_ = pspa;
30 
31  // bouton execute
32  exec_go_ = new WPushButton("execute!");
33  exec_go_->clicked().connect(this, &GWt_softwarePanel::executer);
34
35  // le panel
36  WPanel *panelLogiciels = new WPanel(this);
37  panelLogiciels->setTitle(" sections of beam line for executing softwares ");
38 
39  contenuSections_ = new WContainerWidget();
40  contenuSections_->addWidget(exec_go_);
41  contenuSections_->addWidget(new WBreak());
42  contenuSections_->addWidget(new WBreak());
43  addSectionToExecuteW();
44 
45  panelLogiciels->setCentralWidget(contenuSections_);
46}
47
48GWt_softwarePanel::~GWt_softwarePanel() {
49}
50
51void GWt_softwarePanel::addSectionToExecuteW()
52{     
53#if BAVARD > 0
54  cout << "***********************************" << endl;
55  cout << " GWt_softwarePanel::addSectionToExecuteW() " << endl<<endl;
56#endif
57
58  abstractElement* premierElement;
59  abstractElement* dernierElement;
60
61  int premierIndex = 0;
62  int dernierIndex = 0;
63 
64  cout << "sections_.size() = " << sections_.size() << endl;
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 
85  dernierElement = pspa_->getBeamLine()->getAbstractElement(pspa_->getBeamLine()->getBeamLineSize()-1);
86  dernierIndex = pspa_->getBeamLine()->getBeamLineSize()-1;
87 
88  dtmanage_->addSectionToExecute(premierElement,premierIndex,dernierElement,dernierIndex,nomDeLogiciel());
89
90  std::string premierElementLabel = "";
91  std::string dernierElementLabel = "";
92  if (premierElement) {
93    premierElementLabel = premierElement->getLabel();
94  }
95  if (dernierElement) {
96    dernierElementLabel = dernierElement->getLabel();
97  }
98
99  WComboBox* lineFromCombo = new WComboBox();
100  WComboBox* lineToCombo = new WComboBox();
101  fillComboWithElements(lineFromCombo);
102  fillComboWithElements(lineToCombo);
103
104  // set selection
105  lineFromCombo->setCurrentIndex(premierIndex);
106  lineToCombo->setCurrentIndex(dernierIndex);
107
108  WComboBox* softCombo = new WComboBox();
109  fillComboWithSoftwares(softCombo);
110
111  GWt_sectionToExecute* newSection = new GWt_sectionToExecute(lineFromCombo,lineToCombo,softCombo,createAddDeletePushButtons(sections_.size()),this);
112
113  stringstream st;
114  st << sections_.size();
115
116  // this is the mean to identify this section!
117  newSection->setObjectName(st.str());
118
119  // push back on sections vector
120  sections_.push_back(newSection);
121  contenuSections_->addWidget(newSection);
122  updateSections();
123}
124
125void GWt_softwarePanel::updateSections()
126{
127#if BAVARD > 0
128  cout << "***********************************" << endl;
129  cout << " GWt_softwarePanel::updateSections() " << endl<<endl;
130#endif
131
132  // update all sections in order to manage new/deleted items
133  for (int a = 0; a < sections_.size(); a++) {
134    fillComboWithElements(sections_[a]->getFirstElement());
135    fillComboWithElements(sections_[a]->getLastElement());
136    sections_[a]->getFirstElement()->setEnabled (true);
137    sections_[a]->getLastElement()->setEnabled (true);
138  }
139 
140  if (sections_.size() == 0) {
141    return;
142  }
143 
144  // the first element will always be the first element of the beamLine
145  sections_[0]->getFirstElement()->setCurrentIndex(0);
146  sections_[0]->getFirstElement()->setEnabled (false);
147
148  // the last element will always be the last element of the beamLine
149  sections_[sections_.size()-1]->getLastElement()->setCurrentIndex(sections_[sections_.size()-1]->getLastElement()->count());
150  sections_[sections_.size()-1]->getLastElement()->setEnabled(false);
151
152  // set default values
153  updateSectionSelection();
154}
155
156bool GWt_softwarePanel::updateSectionSelection()
157{
158#if BAVARD > 0
159  cout << "***********************************" << endl;
160  cout << " GWt_softwarePanel::updateSectionSelection() " << endl<<endl;
161#endif
162
163  if ( dtmanage_->getJobListSize() == 0 ) return false;
164 
165  // traitement de la premiere ligne
166  // on impose le depart du calcul au premier element
167  string premier = dtmanage_->getLabelFromElementNumero(1);
168  if (sections_.size() > 0) {
169    sections_[0]->setFirstElementCurrentSelection(premier);
170  }
171 
172  Wt::WString currentString =  sections_[0]->getLastElementCurrentText();
173  int current = dtmanage_->getNumeroFromElementLabel(currentString.toUTF8());
174 
175  // si la fin est mal definie on prend toute la config par defaut
176  if ( current <= 0 || current > dtmanage_->getBeamLineSize() )
177    {
178      current = dtmanage_->getBeamLineSize();
179      currentString =  dtmanage_->getLabelFromElementNumero(current);
180      if (sections_.size() > 0) {
181        sections_[0]->setLastElementCurrentSelection(currentString);
182        //...
183      }
184    }
185  current++;
186  currentString = dtmanage_->getLabelFromElementNumero(current);
187 
188  // traitement des suivantes (on avance d'un cran dans la liste)
189  for (int a = 1; a< sections_.size(); a++)
190    {
191      // debut
192      if ( current > dtmanage_->getBeamLineSize() )
193        {
194          sections_[a]->setErrors("This section element could not be after previous session last element");
195          exec_go_->disable();
196          return false;
197        }
198     
199      sections_[a]->setFirstElementCurrentSelection(currentString);
200
201      // fin
202      string finString =  sections_[a]->getLastElementCurrentText().toUTF8();
203     
204      int numeroFin = dtmanage_->getNumeroFromElementLabel( finString);
205     
206      if ( numeroFin < current)
207      {
208        sections_[a]->setErrors("Last section element should be after first section element");
209        exec_go_->disable();
210        return false;
211      }
212
213      if (numeroFin > dtmanage_->getBeamLineSize())
214      {
215        sections_[a]->setErrors("Last section element number is greater than the beam line size");
216        exec_go_->disable();
217        return false;
218      }
219     
220      // preparation de la ligne suivante
221      current = numeroFin +1;
222      currentString = dtmanage_->getLabelFromElementNumero(current);
223    }
224  //  exec_go_->setDisabled(false);
225
226  if (!areDataCoherent()) {
227#if BAVARD > 0
228    cout << " GWt_softwarePanel::addSectionToExecuteW() DISABLE" << endl;
229#endif
230    exec_go_->disable();
231  } else {
232#if BAVARD > 0
233    cout << " GWt_softwarePanel::addSectionToExecuteW() ENABLE" << endl;
234#endif
235    exec_go_->enable();
236  }
237
238  return true;
239}
240
241bool GWt_softwarePanel::areDataCoherent()
242{
243#if BAVARD > 0
244  cout << "***********************************" << endl;
245  cout << " GWt_softwarePanel::areDataCoherent() " << endl<<endl;
246#endif
247
248  bool caMarche = true;
249
250  // initialize dataManager
251  dtmanage_->initializeExecution();
252 
253  // intialize User Interface
254  if (pspa_->getBeamLine()) {
255    pspa_->getBeamLine()->initializeSoftwares();
256  }
257       
258  string diagnosticErrors;
259  string diagnosticWarnings;
260  for (int a = 0; a < sections_.size(); a++) 
261    {
262      string debString= sections_[a]->getFirstElementCurrentText().toUTF8();
263      string finString= sections_[a]->getLastElementCurrentText().toUTF8();
264      int debut = pspa_->getBeamLine()->getAbstractElementNumeroFromLabel(debString);
265      int fin = pspa_->getBeamLine()->getAbstractElementNumeroFromLabel(finString);
266
267      string currentSoft= sections_[a]->getSoftwareCurrentText().toUTF8();
268      nomDeLogiciel prog = nomDeLogiciel(currentSoft);
269      sectionToExecute* sectToExec = dtmanage_->addSectionToExecute(pspa_->getBeamLine()->getAbstractElement(debut),debut,pspa_->getBeamLine()->getAbstractElement(fin),fin,prog);
270
271      abstractSoftware* softToExec = sectToExec->getSoftware(); 
272      diagnosticErrors = "";
273      diagnosticWarnings = "";
274      for(int i = debut-1; i < fin; i++) // check sections
275        {
276          if (!pspa_->getBeamLine()) continue;
277          abstractElement* elPtr= pspa_->getBeamLine()->getAbstractElement(i);   
278          if (!elPtr) continue; 
279
280          trivaluedBool tb = softToExec->doAcceptElement(elPtr->getNomdElement().getElementType());
281          if (tb  == TBoolOk ) {
282            elPtr->setSoftware(softToExec);
283          } else {
284            elPtr->setSoftware(NULL);
285            if (tb == TBoolIgnore) {
286              if(prog.getString() != "unknownSoftware") diagnosticWarnings += elPtr->getLabel() + " will be ignored by  "+ prog.getString() + "<br /> ";           
287            } else {
288              // j'insiste pour remettre le nom generique de l'element refuse (glm / 30/03/2013)
289              diagnosticErrors += elPtr->getNomdElement().getExpandedName() + " is not allowed with " + prog.getString() + "<br /> ";
290              caMarche = false;
291            }
292          }
293          pspa_->getBeamLine()->update(i);
294        } //i
295     
296      // set errors and warnings
297      sections_[a]->setErrors(diagnosticErrors);
298      sections_[a]->setWarnings(diagnosticWarnings);
299    }//a
300   
301  return caMarche;
302}
303
304void GWt_softwarePanel::deleteSectionToExecuteW(int sectionLabel)
305{
306#if BAVARD > 0
307  cout << "***********************************" << endl;
308  cout << " GWt_softwarePanel::deleteSectionToExecuteW()" << endl<<endl;
309#endif
310
311  stringstream st;
312  st << sectionLabel;
313  std::string sectionName = st.str();
314 
315  if ( dtmanage_->getJobListSize() == 0 ) return;
316  for (unsigned int sectionIndex = 0; sectionIndex< sections_.size(); sectionIndex++) {
317    if (sections_[sectionIndex]->objectName() == sectionName) {
318
319      // delete from dataManager
320      dtmanage_->clearSectionToExecute(sectionIndex);
321
322      // delete from User Interface
323      contenuSections_->removeWidget(sections_[sectionIndex]);
324      delete sections_[sectionIndex];
325      sections_.erase (sections_.begin()+sectionIndex);
326    }
327  } 
328}
329
330void GWt_softwarePanel::executer()
331{
332  cout << "***********************************" << endl;
333  cout << " GWt_softwarePanel::executer() " << endl<<endl;
334
335  if (!areDataCoherent()) {
336    return;
337  } 
338 
339  //static_cast<GWt_globalParameters*>(pspa_->getGlobalParam())->updateGlobals();
340 
341  GWt_dialog calculDialog("Calcul en cours", "Veuillez patienter...", GWt_dialog::Wait, true,false);
342  calculDialog.show();
343   
344  wApp->processEvents();
345 
346  if (!dtmanage_->executeAll()) {
347    GWt_dialog warningDialog("PSPA : Echec", " echec lors de l'exécution !", GWt_dialog::Error, true,true);
348    warningDialog.exec();
349  }
350     
351  //  exec_go_->setDisabled(true);
352  calculDialog.hide(); 
353  pspa_->faireDessin();
354}
355
356void GWt_softwarePanel::fillComboWithElements(Wt::WComboBox* cBox) 
357{
358  if (cBox == NULL) return;
359
360  // get the last item selected
361  WString selectedString = cBox->currentText();
362  cBox->clear();
363
364  for(int a = 0; a < pspa_->getBeamLine()->getBeamLineSize(); a++) {
365    abstractElement* abs = pspa_->getBeamLine()->getAbstractElement(a);
366    if (abs != NULL) {
367      cBox->addItem(abs->getLabel());
368    }
369  }
370 
371  for(int a = 0; a < cBox->count(); a++) {
372    if (cBox->itemText (a) == selectedString) {
373      cBox->setCurrentIndex(a);
374    }
375  }
376  cBox->refresh();
377}
378
379void GWt_softwarePanel::fillComboWithSoftwares(Wt::WComboBox* cBox) 
380{
381#if BAVARD > 0
382  cout << "***********************************" << endl;
383  cout << " GWt_softwarePanel::fillComboWithSoftwares " << endl<<endl;
384#endif
385
386  if (cBox == NULL) return;
387  cBox->clear();
388
389  unsigned nb = nomDeLogiciel::getNumberOfSoftwares();
390  unsigned k;
391  for(k = 0; k <= nb; k++) { //xx
392    cBox->addItem(nomDeLogiciel(k).getString());
393  }
394  cBox->setCurrentIndex(nb); // xx
395  cBox->refresh();
396}
397
398Wt::WContainerWidget* GWt_softwarePanel::createAddDeletePushButtons(int sectionNumber) 
399{
400  WContainerWidget* buttonContainer= new WContainerWidget();
401 
402  Wt::WHBoxLayout* buttonContainerLayout = new Wt::WHBoxLayout();
403  buttonContainerLayout->setContentsMargins(0,0,0,0);
404  // preparation du bouton add
405  WPushButton* exec_add = new WPushButton("+");
406  exec_add->clicked().connect(this, &GWt_softwarePanel::addSectionToExecuteW);
407  exec_add->setStyleClass("roundButton");
408  exec_add->setMaximumSize(20,20);
409  exec_add->setToolTip("Add new section");
410 
411  // preparation du bouton delete
412  WPushButton* exec_delete = new WPushButton("-");
413  //  warningsContainer_->setStyleClass("warningsContainer");
414
415  exec_delete->clicked().connect(boost::bind(&GWt_softwarePanel::deleteSectionToExecuteW, this, sectionNumber));
416  exec_delete->setStyleClass("roundButton");
417  exec_delete->setMaximumSize(20,20);
418  exec_delete->setToolTip("Remove this section");
419
420  buttonContainerLayout->addWidget(exec_add);
421  buttonContainerLayout->addWidget(exec_delete);
422  buttonContainer->setLayout(buttonContainerLayout);
423
424  return buttonContainer;
425}
Note: See TracBrowser for help on using the repository browser.