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

Last change on this file since 407 was 407, checked in by garnier, 11 years ago

Correction dans les comboBox

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