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

Last change on this file since 102 was 102, checked in by lemeur, 12 years ago

creation d'un diagnostic de coherence

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