source: PSPA/Interface_Web/trunk/pspaWT/src/GWt_pspaApplication.cc @ 161

Last change on this file since 161 was 161, checked in by garnier, 12 years ago

mise a jour du veuillez patienter

File size: 19.9 KB
Line 
1#include <stdio.h>
2#include <string.h>
3#include <vector>
4
5#include <Wt/WLineEdit>
6#include <Wt/WText>
7#include <Wt/WGridLayout>
8#include <Wt/WVBoxLayout>
9#include <Wt/WHBoxLayout>
10#include <Wt/WImage>
11#include <Wt/WMenu>
12#include <Wt/WStackedWidget>
13#include <Wt/WBreak>
14#include <Wt/WDialog>
15#include <Wt/WStandardItemModel>
16#include <Wt/Chart/WCartesianChart>
17
18#include "GWt_pspaApplication.h"
19#include "GWt_LigneFaisceau.h"
20#include "GWt_globalParameters.h"
21#include "GWt_dialog.h"
22#include "particleBeam.h"
23#include "bareParticle.h"
24#include "nomDeLogiciel.h"
25#include "mixedTools.h"
26#include "nomdElements.h"
27
28using namespace Wt::Chart;
29
30/*
31 * The env argument contains information about the new session, and
32 * the initial request. It must be passed to the WApplication
33 * constructor so it is typically also an argument for your custom
34 * application constructor.
35*/
36
37PspaApplication::PspaApplication(const WEnvironment& env) : WApplication(env)
38{
39  setTitle("portail PSPA");                               // application title
40 
41  if (!wApp->environment().javaScript()) {
42    new WText("<i>This examples requires that javascript support is enabled.</i>",root());
43  }
44 
45  WContainerWidget *w = root();
46  w->setStyleClass("PSPA");
47  dtmanage_ = new dataManager();
48
49  /*
50   * The main layout is a 3x2 grid layout.
51   */
52  WGridLayout *layout = new WGridLayout();
53  layout->addWidget(createTitle("Menu (In future)"), 0, 0, 1, 2);
54
55  WHBoxLayout *toolbarLayout = new WHBoxLayout();
56  WPushButton* boutonSauve = new WPushButton(" sauvegarder la config");
57  WPushButton* boutonRestaure = new WPushButton(" restaurer la config");
58  boutonSauve->setMinimumSize(30,30);
59  boutonRestaure->setMinimumSize(30,30);
60 
61  toolbarLayout->addWidget(boutonSauve , 1);
62  toolbarLayout->addWidget(boutonRestaure , 1);
63  boutonSauve->clicked().connect(this, &PspaApplication::sauver);
64  boutonRestaure->clicked().connect(this, &PspaApplication::restaurer);
65  layout->addLayout(toolbarLayout, 1, 0, 1, 2);
66  layout->addWidget(createPalette(), 2, 0, 4, 1);
67 
68  beamLine_ = createBeamLine();
69  layout->addWidget(beamLine_, 2, 1, 1, 1);
70 
71  console_ = new WTextArea();
72  console_->setMaximumSize(600,200);
73  layout->addWidget(console_, 3, 1);
74  console_->setMinimumSize(300,100);
75 
76  //-----------
77  // A supprimer et a mettre en fenetre
78  globalParam_ = createGlobalParamWidget();
79  //  leDessin_ = new WContainerWidget();
80  leDessin_ = createDrawingWidget();
81
82
83
84  WWidget* executeWidget = createExecuteWidget();
85
86  layout->addWidget( globalParam_, 4, 1);
87  layout->addWidget( leDessin_, 3, 2);
88  layout->addWidget( executeWidget , 5, 1);
89  //-----------
90 
91  layout->setColumnResizable(1);
92  layout->setRowResizable(2);
93  /*
94   * Let row 2 and column 1 take the excess space.
95   */
96  layout->setRowStretch(2, 1);
97  layout->setColumnStretch(1, 1);
98 
99  w->setLayout(layout); 
100}
101
102WWidget* PspaApplication::createPalette()
103{
104  WContainerWidget* palette=new WContainerWidget();
105  //  palette->setLayout(new WVBoxLayout());
106
107  // nomdElements *e= new nomdElements();
108  // int nElts= e->getNumberOfElements();
109  // delete e;
110  //  nomdElements bidon;
111  int nElts= nomdElements::getNumberOfElements(); 
112
113  for(int k = 0; k < nElts; k++) {
114    typedElement eType= (typedElement)k;
115    string image = nomdElements::getImageFromType(eType);
116    createDragImage(image.c_str(),
117                    image.c_str(),image.c_str(), palette,nomdElements::getLabelFromType(eType));
118  new WBreak(palette);
119  }
120
121
122  palette->setMinimumSize(100,300);
123  return palette;
124}
125
126void PspaApplication::createDragImage(const char *url,const char *smallurl,const char *mimeType,WContainerWidget *p,WString name)
127{
128  WImage *result = new WImage(url,p);
129  WImage *dragImage = new WImage(smallurl,p);
130
131  /*
132   * Set the image to be draggable, showing the other image (dragImage)
133   * to be used as the widget that is visually dragged.
134   */
135  result->setDraggable(mimeType,dragImage,true);
136}
137
138WWidget* PspaApplication::createBeamLine()
139{
140  WContainerWidget* beamLine = new GWt_LigneFaisceau(this);
141  beamLine->setMinimumSize(300,100);
142  return beamLine;
143}
144
145WWidget* PspaApplication::createGlobalParamWidget()
146{
147  WContainerWidget* globalParam = new GWt_globalParameters(this);
148  globalParam->setMaximumSize(600,150);
149  globalParam->setMinimumSize(600,150);
150  return globalParam;
151}
152
153WWidget* PspaApplication::createExecuteWidget()
154{
155
156  WContainerWidget* executeW = new WContainerWidget();
157  executeW->setMaximumSize(600,150);
158  executeW->setMinimumSize(600,150);
159
160  // bouton execute
161  exec_go_ = new WPushButton("execute!");
162  //    exec_go_->setMinimumSize(300,300);
163  exec_go_->setDisabled(true);
164  exec_go_->clicked().connect(this, &PspaApplication::executer);
165
166  // preparation du bouton add
167  WPushButton* exec_add = new WPushButton("add");
168  exec_add->clicked().connect(this, &PspaApplication::addSectionToExecuteW);
169 
170  // preparation du bouton delete
171  WPushButton* exec_delete = new WPushButton("delete");
172  exec_delete->clicked().connect(this, &PspaApplication::deleteSectionToExecuteW);
173
174  // preparation du bouton push_ok
175  WPushButton* exec_ok = new WPushButton("check/ok");
176  exec_ok->clicked().connect(this, &PspaApplication::checkSectionSelection);
177 
178  // le panel
179  WPanel *panelLogiciels = new WPanel(executeW);
180  panelLogiciels->setTitle(" sections of beam Line for executing softwares ");
181
182  contenuSections_ = new WContainerWidget(); 
183  contenuSections_->addWidget(exec_add);
184  contenuSections_->addWidget(exec_delete);
185  contenuSections_->addWidget(exec_ok);
186  contenuSections_->addWidget(exec_go_);
187  contenuSections_->addWidget(new WBreak());
188  contenuSections_->addWidget(new WBreak());
189  addSectionToExecuteW();
190 
191  panelLogiciels->setCentralWidget(contenuSections_);
192  return executeW;
193}
194
195
196WContainerWidget* PspaApplication::createDrawingWidget()
197{
198  WContainerWidget* dessin = new WContainerWidget();
199
200  dessin->addWidget(new WText(" graphic analysis :  "));
201  dessin->addWidget(new WBreak());
202  dessin->addWidget(new WText(" drawing after element :  "));
203
204
205  choixElementDessin_ = new WComboBox();
206
207  choixTypeDessinFaisceau_ = new WComboBox();
208  choixTypeDessinFaisceau_->addItem("courant_snyder");
209  choixTypeDessinFaisceau_->addItem("macroparticles");
210
211  dessin->addWidget(choixElementDessin_);
212  dessin->addWidget(choixTypeDessinFaisceau_);
213  WPushButton* caroule = new WPushButton("dessiner");
214 dessin->addWidget(caroule);
215 caroule->clicked().connect(this, &PspaApplication::dessiner);
216  dessin->addWidget(new WBreak());
217
218  WPushButton* okEnv = new WPushButton("draw enveloppe");
219
220  dessin->addWidget(new WText(" drawing enveloppe :  "));
221  choixEnveloppeDessin_ = new WComboBox();
222  choixEnveloppeDessin_->addItem("x");
223  choixEnveloppeDessin_->addItem("y");
224
225  dessin->addWidget(okEnv);
226 okEnv->clicked().connect(this, &PspaApplication::dessinerEnveloppe);
227
228  toto_ = new WContainerWidget();
229  dessin->addWidget(toto_);
230  return dessin;
231}
232
233void PspaApplication::addSectionToExecuteW()
234{
235
236  disableSectionExecute();
237
238  string premierText, dernierText;
239  if(selectedSections_.size() == 0)
240    {
241      premierText = dtmanage_->getLabelFromElementNumero(1);
242
243      dernierText = dtmanage_->getLabelFromElementNumero(dtmanage_->beamLineSize());
244    }
245  else 
246    {
247      dernierText = selectedSections_.back()->fin->text().toUTF8();
248      int dernierNumero = dtmanage_->getCollection()->getNumeroFromLabel(dernierText);
249      dernierNumero++;
250      if ( dernierNumero <= dtmanage_->beamLineSize() )
251        {
252          premierText = dtmanage_->getLabelFromElementNumero(dernierNumero);
253        }
254      else 
255        {
256          premierText = dtmanage_->getLabelFromElementNumero(dtmanage_->beamLineSize());
257        }
258      dernierText = premierText;
259    }
260 
261  //  cout << "PspaApplication::addSectionToExecute() : " << premierText << " à  " << dernierText << endl;
262
263  WContainerWidget* newSection = new WContainerWidget;
264
265  selectedSections_.push_back(new GWt_sectionToExecute);
266  selectedSections_.back()->debut = new WLineEdit();
267  selectedSections_.back()->debut->setDisabled(true);
268  selectedSections_.back()->debut->setText(premierText);
269  selectedSections_.back()->fin = new WLineEdit();
270  selectedSections_.back()->fin->changed().connect(this,&PspaApplication::disableSectionExecute);
271  selectedSections_.back()->fin->setText(dernierText);
272  selectedSections_.back()->selection = new WComboBox();
273  selectedSections_.back()->ligneDeWidget = newSection;
274  newSection->addWidget(new WText(" from : "));
275  newSection->addWidget(selectedSections_.back()->debut);
276  newSection->addWidget(new WText(" to : "));
277  newSection->addWidget(selectedSections_.back()->fin);
278  newSection->addWidget(selectedSections_.back()->selection);
279
280  contenuSections_->addWidget(newSection);
281  unsigned nb = nomDeLogiciel::getNumberOfSoftwares();
282  unsigned k;
283  for(k = 0; k < nb; k++) {
284    selectedSections_.back()->selection->addItem(nomDeLogiciel(k).getString());
285  }
286}
287
288void PspaApplication::disableSectionExecute()
289{
290  exec_go_->setDisabled(true);
291}
292
293void PspaApplication::checkSectionSelection()
294{
295  if ( selectedSections_.empty() ) return;
296
297  // traitement de la premiere ligne
298  // on impose le depart du calcul au premier element
299  string premier = dtmanage_->getLabelFromElementNumero(1);
300  (*selectedSections_.begin())->debut->setText(premier);
301
302  string currentString =  (*selectedSections_.begin())->fin->text().toUTF8();
303  int current = dtmanage_->getCollection()->getNumeroFromLabel( currentString);
304
305  cout << " numero " << current << endl;
306  // si la fin est mal definie on prend toute la config par defaut
307  if ( current <= 0 || current > dtmanage_->beamLineSize() ) 
308    {
309      current = dtmanage_->beamLineSize();
310      currentString =  dtmanage_->getLabelFromElementNumero(current);
311      (*selectedSections_.begin())->fin->setText(currentString);
312    }
313  current++;
314  currentString = dtmanage_->getLabelFromElementNumero(current);
315   
316  // traitement des suivantes (on avance d'un cran dans la liste)
317  list<GWt_sectionToExecute*>::iterator itr, itr0;
318  itr0 = selectedSections_.begin();
319  itr0++;
320  for (itr = itr0; itr != selectedSections_.end(); itr++)
321    {
322      // debut
323      if ( current >= dtmanage_->beamLineSize() ) 
324        {
325          addConsoleMessage(" bad section definition !  \n ");
326          GWt_dialog warningDialog("PSPA : Vérification des sections", " bad section definition !", GWt_dialog::Error,true,true);
327          warningDialog.exec();
328          return;
329        }
330
331      (*itr)->debut->setText(currentString);
332      // fin
333      string finString =  (*itr)->fin->text().toUTF8();
334
335      int numeroFin = dtmanage_->getCollection()->getNumeroFromLabel( finString);
336
337      if ( numeroFin <= current || numeroFin > dtmanage_->beamLineSize())
338        {
339          addConsoleMessage(" bad section definition !  \n ");
340          GWt_dialog warningDialog("PSPA : Vérification des sections", " bad section definition !", GWt_dialog::Error, true,true);
341          warningDialog.exec();
342          return;
343        }
344
345      // preparation de la ligne suivante
346      current = numeroFin +1;
347      currentString = dtmanage_->getLabelFromElementNumero(current);
348    }
349
350  if (!areDataCoherent()) {
351    GWt_dialog warningDialog("PSPA : Vérification des sections", " données incohérentes !", GWt_dialog::Error,true,true);
352    warningDialog.exec();
353  }
354  else 
355    {
356      exec_go_->setDisabled(false);
357    }
358}
359
360bool PspaApplication::areDataCoherent() 
361{
362  bool caMarche = true;
363  dtmanage_->initializeExecution();
364 
365  list<GWt_sectionToExecute*>::iterator itr;
366  for(itr = selectedSections_.begin(); itr != selectedSections_.end(); itr++)
367    {
368      string debString = (*itr)->debut->text().toUTF8();
369      string finString = (*itr)->fin->text().toUTF8();
370
371
372      int debut = dtmanage_->getCollection()->getNumeroFromLabel(debString);
373      int fin = dtmanage_->getCollection()->getNumeroFromLabel(finString);
374      nomDeLogiciel prog = nomDeLogiciel ( (*itr)->selection->currentIndex() );
375      dtmanage_->addSectionToExecute(debut,fin,prog);
376    }
377
378  string diagnostic = dtmanage_->checkExecute();
379  if ( !diagnostic.empty() )
380    {   
381      caMarche = false; 
382      addConsoleMessage(diagnostic.c_str());
383      GWt_dialog calculDialog("PSPA : Erreur lors de check execute", diagnostic , GWt_dialog::Error,true,true);
384      calculDialog.exec();
385    }
386
387  return caMarche;
388}
389
390void PspaApplication::deleteSectionToExecuteW()
391{
392  if ( selectedSections_.empty() ) return;
393  disableSectionExecute();
394  selectedSections_.back()->ligneDeWidget->clear();
395  delete selectedSections_.back()->ligneDeWidget;
396  selectedSections_.pop_back();
397}
398
399void PspaApplication::executer()
400{
401  addConsoleMessage(string("on va peut etre y arriver"));
402  static_cast<GWt_globalParameters*>(globalParam_)->updateGlobals();
403
404  GWt_dialog calculDialog("Calcul en cours", "Veuillez patienter...", GWt_dialog::Wait, true,false);
405  calculDialog.show();
406
407  processEvents();     
408
409  string resultat;
410  if ( !dtmanage_->executeAll(resultat)) {
411    GWt_dialog warningDialog("PSPA : Echec", " echec lors de l'exécution !", GWt_dialog::Error, true,true);
412    warningDialog.exec();
413  }
414  //  cout << " PspaApplication : retour d'execution resultat =  " << resultat << endl;
415  addConsoleMessage(resultat);
416  //  cout << " PspaApplication : affichage console termine  " << endl;
417
418  exec_go_->setDisabled(true);
419
420  calculDialog.hide();
421
422  faireDessin();
423}
424
425void PspaApplication::sauver()
426{
427  cout << " on sauve " << endl;
428  addConsoleMessage("sauvegarde");
429  GWt_globalParameters* bibi = static_cast<GWt_globalParameters*>(globalParam_);
430  bibi->updateGlobals();
431  dtmanage_->saveAll();
432
433}
434
435void PspaApplication::restaurer()
436{
437  addConsoleMessage(string("restauration..."));
438
439  dtmanage_->restoreElements();
440
441  GWt_globalParameters* bibi = static_cast<GWt_globalParameters*>(globalParam_);
442  bibi->renew();
443
444  GWt_LigneFaisceau* bobo = static_cast<GWt_LigneFaisceau*>(beamLine_);
445  bobo->restoreElementCollection();
446
447  addConsoleMessage(string("...terminee"));
448}
449
450void PspaApplication::faireDessin()
451{
452  choixElementDessin_->clear();
453  int nombre = dtmanage_->beamLineSize();
454  for ( int numero =1; numero <= nombre; numero++)
455    {
456      choixElementDessin_->addItem(dtmanage_->getLabelFromElementNumero(numero));
457    }
458
459}
460
461void PspaApplication::dessiner()
462{
463 
464  toto_->clear();
465
466  int typeFaisceau = choixTypeDessinFaisceau_->currentIndex();
467
468  int index = choixElementDessin_->currentIndex();
469  particleBeam* beam = dtmanage_->getDiagnosticBeam(index);
470  if ( typeFaisceau == 0 ) 
471    {
472      if ( !beam->momentRepresentationOk() ) beam->buildMomentRepresentation();
473      faireDessinTransport(toto_, beam);
474    }
475  else if ( typeFaisceau == 1 ) 
476    {
477      if ( beam->particleRepresentationOk() ) faireDessinParmela(toto_, beam);
478      else addConsoleMessage("the beam state does not allow providing a drawing"); 
479    }
480  else addConsoleMessage("type of  drawing not programmed");
481  //////////////////////////////////////////
482}
483
484void PspaApplication::dessinerEnveloppe()
485{
486 
487  toto_->clear();
488
489  int typeEnveloppe = choixEnveloppeDessin_->currentIndex();
490  if ( typeEnveloppe == 0 ) 
491    {
492      faireDessinEnveloppe(toto_, "x");
493    }
494  else addConsoleMessage("type of enveloppe drawing not programmed");
495  //////////////////////////////////////////
496}
497
498void PspaApplication::faireDessinEnveloppe(WContainerWidget* toto, string type)
499{
500       new WText("enveloppe", toto);
501       unsigned nbel = dtmanage_->beamLineSize();
502
503
504  vector<double> xcor;
505  vector<double> ycor;
506
507
508  dtmanage_->donneesRmsEnveloppe(type,1, nbel, xcor,ycor);
509
510    scatterPlot1D(toto,xcor,ycor);
511
512}
513
514void PspaApplication::faireDessinParmela(WContainerWidget* toto, particleBeam* beam)
515{
516  // WContainerWidget* toto = static_cast<WContainerWidget*>(leDessin_);
517  // //  toto->clear();
518  //      new WText("emittance", toto);
519  // vector<bareParticle>& partic = dtmanage_->getCurrentBeam()->getParticleVector();
520       new WText("emittance parmela", toto);
521  vector<bareParticle>& partic = beam->getParticleVector();
522  WStandardItemModel *model = new WStandardItemModel(partic.size(), 3, toto);
523  //    model->setHeaderData(0, WString("X"));
524  //   model->setHeaderData(1, WString("Y = sin(X)"));
525
526
527  for (unsigned i = 0; i < partic.size(); ++i) {
528    double x= partic.at(i).getPosition().getComponent(0);
529    double begamz = partic.at(i).getBetaGamma().getComponent(2);
530    double xp = partic.at(i).getBetaGamma().getComponent(0)/begamz;
531    //    cout << "x = " << x << " xp= " << xp << endl;
532    model->setData(i, 0, 10*x);
533    model->setData(i, 1,1.e3*xp);
534    model->setData(i, 2,2.e3*xp);
535  }
536
537  WCartesianChart *chart = new WCartesianChart(toto);
538  chart->setModel(model);        // set the model
539  chart->setXSeriesColumn(0);    // set the column that holds the X data
540  chart->setLegendEnabled(true); // enable the legend
541
542  chart->setType(ScatterPlot);   // set type to ScatterPlot
543
544  // Typically, for mathematical functions, you want the axes to cross
545  // at the 0 mark:
546   chart->axis(XAxis).setLocation(ZeroValue);
547   chart->axis(YAxis).setLocation(ZeroValue);
548
549  // Provide space for the X and Y axis and title.
550  chart->setPlotAreaPadding(80, Left);
551  chart->setPlotAreaPadding(40, Top | Bottom);
552  // Add the curves
553  WDataSeries s(1, PointSeries, Y1Axis);
554  //  s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
555  //    s.setMarker(SquareMarker);
556  //  s.setMarkerSize(600.);
557  //  cout << "le marker est : " << s.marker() << endl;
558     chart->addSeries(s);
559
560  chart->resize(300, 300); // WPaintedWidget must be given explicit size
561
562  //  chart->setMargin(10, Top | Bottom);            // add margin vertically
563  //  chart->setMargin(WLength::Auto, Left | Right); // center horizontally
564}
565
566
567// void PspaApplication::addElemToGlobals()
568// {
569//   static_cast<GWt_globalParameters*>(globalParam_)->addElem();
570// }
571
572void PspaApplication::faireDessinTransport(WContainerWidget* toto, particleBeam* beam)
573{
574  //  WContainerWidget* toto = static_cast<WContainerWidget*>(leDessin_);
575  //  toto->clear();
576  new WText("emittance transport", toto);
577  vector<double> xcor;
578  vector<double> ycor;
579  //  dtmanage_->getCurrentBeam()->donneesDessinEllipseXxp(xcor,ycor);
580  beam->donneesDessinEllipseXxp(xcor,ycor);
581    scatterPlot1D(toto,xcor,ycor);
582
583}
584
585void PspaApplication::scatterPlot1D(WContainerWidget* toto, vector<double>& xcor, vector<double>& ycor)
586{
587  int nbpts = xcor.size();
588  cout << " PspaApplication::scatterPlot1D nbpts = " << nbpts << endl;
589  WStandardItemModel *model = new WStandardItemModel(nbpts, 2, toto);
590  for (int i = 0; i < nbpts; ++i) {
591    model->setData(i, 0, xcor.at(i));
592    model->setData(i, 1, ycor.at(i));
593    cout << " PspaApplication::scatterPlot1D el= " << i+1 << " x= " << xcor.at(i) << " y= " << ycor.at(i) << endl;
594  }
595
596  WCartesianChart *chart = new WCartesianChart(toto);
597  chart->setModel(model);        // set the model
598  chart->setXSeriesColumn(0);    // set the column that holds the X data
599  chart->setLegendEnabled(true); // enable the legend
600
601  chart->setType(ScatterPlot);   // set type to ScatterPlot
602
603  // Typically, for mathematical functions, you want the axes to cross
604  // at the 0 mark:
605   chart->axis(XAxis).setLocation(ZeroValue);
606   chart->axis(YAxis).setLocation(ZeroValue);
607
608  // Provide space for the X and Y axis and title.
609  chart->setPlotAreaPadding(80, Left);
610  chart->setPlotAreaPadding(40, Top | Bottom);
611  // Add the curves
612  WDataSeries s(1, CurveSeries, Y1Axis);
613     chart->addSeries(s);
614
615  chart->resize(300, 300); // WPaintedWidget must be given explicit size
616}
617
618void PspaApplication::updateSelections()
619{
620  string premierText, dernierText;
621  if ( selectedSections_.size() > 0 )
622    {
623      premierText = dtmanage_->getLabelFromElementNumero(1);
624      dernierText = dtmanage_->getLabelFromElementNumero( dtmanage_->beamLineSize() );
625      (*selectedSections_.begin())->debut->setText(premierText);
626      (*selectedSections_.begin())->fin->setText(dernierText);
627    }
628
629  cout << "PspaApplication::updateSelections(): " << premierText << " à  " << dernierText << endl;
630}
631
632string PspaApplication::getSelection()
633{
634  list<GWt_sectionToExecute*>::iterator itr = selectedSections_.begin();
635  string str = (*itr)->fin->text().toUTF8();
636  return str;
637}
638
639WText* PspaApplication::createTitle(const WString& title) 
640{
641  WText *result = new WText(title);
642  result->setInline(false);
643  result->setStyleClass("title");
644  result->setMinimumSize(30,30);
645 
646  return result;
647}
Note: See TracBrowser for help on using the repository browser.