source: PSPA/Interface_Web/trunk/pspaWT/sources/userInterface/src/GWt_pspaApplication.cc @ 339

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

complement graphique (fin)

File size: 31.9 KB
Line 
1#include <stdio.h>
2#include <string.h>
3#include <vector>
4
5#include "GWt_pspaApplication.h"
6#include "GWt_ligneFaisceau.h"
7#include "GWt_softwarePanel.h"
8#include "GWt_globalParameters.h"
9// #include "GWt_dialog.h"
10#include "GWt_draggableImage.h"
11#include "particleBeam.h"
12#include "bareParticle.h"
13#include "nomDeLogiciel.h"
14#include "mixedTools.h"
15#include "nomdElements.h"
16#include "environmentVariables.h"
17#include "GWt_console.h"
18#include "GWt_serverFileSelector.h"
19#include "trivaluedBool.h"
20
21#include <Wt/WGridLayout>
22#include <Wt/WVBoxLayout>
23#include <Wt/WHBoxLayout>
24#include <Wt/WImage>
25#include <Wt/WMenu>
26#include <Wt/WStackedWidget>
27#include <Wt/WBreak>
28#include <Wt/WDialog>
29#include <Wt/WStandardItemModel>
30#include <Wt/WFileUpload>
31#include <Wt/WPainter>
32#include <Wt/WPdfImage>
33#include <Wt/WRasterImage>
34#include <Wt/WScrollArea>
35#include <Wt/WLineEdit>
36#include <Wt/WRadioButton>
37
38using namespace Wt::Chart;
39
40/*
41 * The env argument contains information about the new session, and
42 * the initial request. It must be passed to the WApplication
43 * constructor so it is typically also an argument for your custom
44 * application constructor.
45 */
46
47PspaApplication::PspaApplication(const WEnvironment& env) : WApplication(env)
48{
49   
50    workingDir_ = WApplication::docRoot()+ "/"+WORKINGAREA;
51    nameOfCase_ = "pspa"; // default
52   
53    setTitle("portail PSPA");                               // application title
54    if (!wApp->environment().javaScript()) {
55        new WText("<i>This examples requires that javascript support is enabled.</i>",root());
56    }
57   
58    // register the absractElementFactory
59    abstractElementFactory_ = new GWt_abstractElementFactory();
60    abstractElementFactory_->setRFGunEnable(true);
61    abstractElementFactory_->setDriftEnable(true);
62    abstractElementFactory_->setCellEnable(true);
63    abstractElementFactory_->setBendEnable(true);
64    abstractElementFactory_->setSolenoEnable(true);
65    abstractElementFactory_->setBeamEnable(true);
66    abstractElementFactory_->setFitEnable(true);
67   
68    // include the styleSheet
69    WContainerWidget *widroot = root();
70    useStyleSheet       ("htdocs/pspa.css");
71    dtmanage_ = new dataManager(this);
72    dtmanage_->setWorkingDir(workingDir_);
73    /*
74     * The main layout is a 3x2 grid layout.
75     */
76    WGridLayout *layout = new WGridLayout();
77    //  layout->addWidget(createTitle("<a href='workingArea/parmin'> Menu (In future)</a>"), 0, 0, 1, 2);
78    //  layout->addWidget(createTitle("Menu (In future)"), 0, 0, 1, 2);
79   
80    WHBoxLayout *toolbarLayout = new WHBoxLayout();
81   
82    WPushButton* boutonSauve = new WPushButton();
83    WPushButton* boutonRestaure = new WPushButton();
84    WPushButton* boutonLoadNew = new WPushButton();
85    WPushButton* boutongraphicalAnalysis = new WPushButton();
86   
87    boutonSauve->setStyleClass("saveButton");
88    boutonRestaure->setStyleClass("restoreButton");
89    boutonLoadNew->setStyleClass("loadNewButton");
90    boutongraphicalAnalysis->setStyleClass("histoButton");
91   
92    boutonSauve->setToolTip ("sauvegarder la config");
93    boutonRestaure->setToolTip (" restaurer la config");
94    boutonLoadNew->setToolTip ("upload config");
95    boutongraphicalAnalysis->setToolTip ("analyse graphique");
96   
97    boutonSauve->setMaximumSize(38,38);
98    boutonSauve->setMinimumSize(38,38);
99    boutonRestaure->setMaximumSize(38,38);
100    boutonRestaure->setMinimumSize(38,38);
101    boutonLoadNew->setMaximumSize(38,38);
102    boutonLoadNew->setMinimumSize(38,38);
103    boutongraphicalAnalysis->setMaximumSize(38,38);
104    boutongraphicalAnalysis->setMinimumSize(38,38);
105   
106    boutonSauve->clicked().connect(this, &PspaApplication::sauver);
107    boutonRestaure->clicked().connect(this, &PspaApplication::restaurer);
108    // Upload when the button is clicked.
109    // React to a succesfull upload.
110    boutonLoadNew->clicked().connect(this, &PspaApplication::openFileSelector);
111   
112    boutongraphicalAnalysis->clicked().connect(this, &PspaApplication::dialogOnGraphics);
113   
114    toolbarLayout->addWidget(boutonSauve , 0,Wt::AlignMiddle);
115    toolbarLayout->addWidget(boutonRestaure , 0,Wt::AlignMiddle);
116    toolbarLayout->addWidget(boutonLoadNew , 0,Wt::AlignMiddle);
117    toolbarLayout->addWidget(boutongraphicalAnalysis , 0,Wt::AlignMiddle);
118    toolbarLayout->addWidget(new WText("") , 1,Wt::AlignMiddle);
119   
120    WContainerWidget * layoutContainer = new WContainerWidget();
121    layoutContainer->setLayout(toolbarLayout);
122    layoutContainer->decorationStyle().setBackgroundImage ("/htdocs/fond_toolbar.png");
123    layout->addWidget(layoutContainer, 1, 0, 1, 2);
124    layout->addWidget(createPalette(), 2, 0, 4, 1);
125   
126    createBeamLine();
127
128    WScrollArea* scroll = new  WScrollArea();
129    scroll->setWidget(beamLine_);
130    layout->addWidget(scroll, 2, 1, 1, 1);
131   
132   
133    // console_ = new WContainerWidget();
134    // console_->decorationStyle().setBackgroundColor (WColor("lightgray"));
135    // console_->setMaximumSize(600,200);
136    // layout->addWidget(console_, 3, 1);
137    // console_->setMinimumSize(300,100);
138    // console_->setOverflow(WContainerWidget::OverflowAuto);
139   
140   
141    console_ = new GWt_console();
142    console_->clear();
143    console_->decorationStyle().setBackgroundColor (WColor("lightgray"));
144    console_->setMaximumSize(600,200);
145    console_->setMinimumSize(300,100);
146    console_->setOverflow(WContainerWidget::OverflowAuto);
147    layout->addWidget(console_, 3, 2);
148   
149    //-----------
150    // A supprimer et a mettre en fenetre
151    globalParam_ = createGlobalParamWidget();
152    //  leDessin_ = new WContainerWidget();
153    //  leDessin_ = createDrawingWidget();
154    graphicsDialog_ = new GWt_dialog("graphical analysis",createDrawingWidget(),false);
155    graphicsDialog_->setMinimumSize(400,400);
156    graphicsDialog_->setClosable(true);
157   
158    executeWidget_ = new GWt_softwarePanel(dtmanage_,this);
159   
160    layout->addWidget( globalParam_, 3, 1);
161    //  layout->addWidget( leDessin_, 3, 2);
162    layout->addWidget( executeWidget_ , 4, 1);
163    //-----------
164     
165    layout->setColumnResizable(1);
166    layout->setRowResizable(2);
167    /*
168     * Let row 2 and column 1 take the excess space.
169     */
170    layout->setRowStretch(2, 1);
171    layout->setColumnStretch(1, 1);
172   
173    widroot->setLayout(layout);
174}
175
176WWidget* PspaApplication::createPalette()
177{
178  WContainerWidget* palette = new WContainerWidget();
179  vector <GWt_abstractElement*> elems = abstractElementFactory_->getAllElements();
180  for (unsigned a = 0; a < elems.size(); a++) {
181    elems[a]->createDragImage(palette);
182    new WBreak(palette);
183  }
184
185  return palette; 
186}
187
188void PspaApplication::createDragImage(const char *url,const char *smallurl,const char *mimeType,WContainerWidget *p,WString())
189{
190  GWt_draggableImage *result = new GWt_draggableImage(url,p);
191 
192  /*
193   * Set the image to be draggable, showing the other image (dragImage)
194   * to be used as the widget that is visually dragged.
195   */
196  result->setDraggable(mimeType,new WImage(smallurl,p),true);
197}
198
199void PspaApplication::createBeamLine()
200{
201  beamLine_ = new GWt_LigneFaisceau(this);
202  beamLine_->setMinimumSize(300,100);
203}
204
205WWidget* PspaApplication::createGlobalParamWidget()
206{
207    WContainerWidget* globalParam = new GWt_globalParameters(this);
208    globalParam->setMaximumSize(600,150);
209    globalParam->setMinimumSize(600,150);
210    return globalParam;
211}
212
213
214void PspaApplication::dialogOnGraphics()
215{
216  graphicsDialog_->show();
217}
218
219WContainerWidget* PspaApplication::createDrawingWidget()
220{
221  WGridLayout *glayout= new WGridLayout();
222  WContainerWidget* dessin = new WContainerWidget();
223  dessin->setLayout(glayout);
224
225  group_ = new Wt::WButtonGroup(this);
226 
227  /////////////////////////////////////////////////////////////////////
228       
229  choixEnveloppeDessin_ = new WComboBox();
230  choixEnveloppeDessin_->addItem("x");
231  // choixEnveloppeDessin_->addItem("y");
232  Wt::WRadioButton *button1= new WRadioButton(" enveloppe");
233  group_->addButton(button1,1);
234  glayout->addWidget(button1,0,1);
235  glayout->addWidget(choixEnveloppeDessin_,0,2);
236  /////////////////////////////////////////////////////////////////////
237
238  choixElementDessin_ = new WComboBox();
239  choixElementDessin_->setMinimumSize(50,10);
240  choixTypeDessinFaisceau_ = new WComboBox();
241  choixTypeDessinFaisceau_->addItem("courant_snyder");
242  choixTypeDessinFaisceau_->addItem("macroparticles");
243  Wt::WRadioButton *button2= new WRadioButton(" phase space after element");
244
245    choixAbsPhase_ = new WComboBox();
246    choixAbsPhase_->addItem("x");
247    choixAbsPhase_->addItem("y");
248    choixAbsPhase_->addItem("z");
249    choixAbsPhase_->addItem("xp");
250    choixAbsPhase_->addItem("yp");
251    choixAbsPhase_->addItem("dE/E");
252    choixAbsPhase_->setCurrentIndex(0);
253
254    choixOrdPhase_ = new WComboBox();
255    choixOrdPhase_->addItem("x");
256    choixOrdPhase_->addItem("y");
257    choixOrdPhase_->addItem("z");
258    choixOrdPhase_->addItem("xp");
259    choixOrdPhase_->addItem("yp");
260    choixOrdPhase_->addItem("dE/E");
261    choixOrdPhase_->setCurrentIndex(3);
262
263
264
265  group_->addButton(button2,2);
266  glayout->addWidget(button2,1,1);
267  glayout->addWidget(choixElementDessin_,1,2);
268  glayout->addWidget(choixTypeDessinFaisceau_,1,3);
269  //  glayout->addWidget(new WText("abs. :"),2,2);
270  glayout->addWidget(choixAbsPhase_,2,2);
271  //  glayout->addWidget(new WText("ord. :"),2,4);
272  glayout->addWidget(choixOrdPhase_,2,3);
273  /////////////////////////////////////////////////////////////////////
274
275  choixHistoDessin_ = new WComboBox();
276  Wt::WRadioButton *button3= new WRadioButton(" histogram after element");
277  group_->addButton(button3,3);
278  glayout->addWidget(button3,3,1);
279  glayout->addWidget(choixHistoDessin_,3,2);
280  /////////////////////////////////////////////////////////////////////
281 
282  WPushButton *close= new WPushButton("close"); 
283  glayout->addWidget(close,4,1);
284  close->clicked().connect(this,&PspaApplication::closeGraphicDialog);
285 
286  WPushButton *draw= new WPushButton("draw"); 
287  glayout->addWidget(draw,4,2,2,1);
288  draw->clicked().connect(this,&PspaApplication::dessiner);
289  /////////////////////////////////////////////////////////////////////
290
291  toto_ = new WContainerWidget();
292  dessin->addWidget(toto_);
293  return dessin;
294}
295
296void PspaApplication::closeGraphicDialog()
297{
298  cout << " close graphicsDialog_" << endl;
299  graphicsDialog_->done(WDialog::Rejected);
300}
301
302void PspaApplication::dessiner()
303{
304  int k= group_->checkedId();
305  cout << "dessiner k= " << k << endl;
306
307  switch(k) {
308  case  1: 
309    dessinerEnveloppe();
310    break;
311  case 2:
312    dessinerPhaseSpace();
313    break;
314  case 3:
315    dessinerHistogramme();
316    break;
317  default:
318    GWt_dialog graphicalWarning("graphical analysis","select a button",GWt_dialog::Warning,false,true);
319    graphicalWarning.exec();
320    break;
321  }
322}
323
324void PspaApplication::sauver()
325{
326  cout << " on sauve " << endl;
327  console_->addConsoleMessage("sauvegarde");
328 
329  dialogSave_ = new WDialog("save");
330  new WText("name of case : ",dialogSave_->contents());
331  saveNameEdit_ = new WLineEdit(nameOfCase_.c_str(), dialogSave_->contents());
332  WPushButton *annule = new WPushButton("cancel",dialogSave_->contents());
333  WPushButton *submit = new WPushButton("OK",dialogSave_->contents());
334  annule->clicked().connect(dialogSave_, &Wt::WDialog::reject);
335  submit->clicked().connect(dialogSave_, &Wt::WDialog::accept);
336  dialogSave_->finished().connect(this, &PspaApplication::dialogSaveDone);
337  dialogSave_->show();
338}
339
340void PspaApplication::dialogSaveDone(WDialog::DialogCode code)
341{
342 
343  if ( code != Wt::WDialog::Accepted ) { console_->addConsoleMessage(" pas de sauvegarde"); return;}
344  else { console_->addConsoleMessage("sauvegarde sur repertoire : " + workingDir_);}
345  nameOfCase_ = saveNameEdit_->text().toUTF8();
346  cout << " PspaApplication::dialogSaveDone() nameOfCase_= " << nameOfCase_ << endl;
347  delete dialogSave_;
348  dialogSave_ = NULL;
349  GWt_globalParameters* bibi = static_cast<GWt_globalParameters*>(globalParam_);
350  bibi->updateGlobals();
351  //  dtmanage_->saveConfiguration(nameOfCase_);
352  dtmanage_->saveConfiguration( nameOfCase_);
353}
354
355
356void PspaApplication::restaurer()
357{
358    GWt_serverFileSelector * fs = new GWt_serverFileSelector("Select a configuration file",workingDir_);
359    nameOfCase_ = fs->exec();
360   
361    if (nameOfCase_ == "")
362        return;
363   
364    dialogSave_ = NULL;
365   
366    bool test = dtmanage_->restoreElements(nameOfCase_);
367    if ( !test ) {
368        GWt_dialog restoreWarning(" element restoring", "failure in restoring elements !", GWt_dialog::Error, false,true);
369        restoreWarning.exec();
370    }
371   
372    GWt_globalParameters* bibi = static_cast<GWt_globalParameters*>(globalParam_);
373    bibi->renew();
374   
375    console_->addConsoleMessage(string("...terminee"));
376}
377
378
379void PspaApplication::openFileSelector()
380{
381   
382  WContainerWidget *result = new WContainerWidget();
383  WVBoxLayout* myLayout = new WVBoxLayout();
384   
385  uploadFileSelectorWidget_ = new WFileUpload();
386   
387  uploadFileSelectorWidget_->setFileTextSize(40);
388   
389  myLayout->addWidget(new WText("Select the configuration file for pspa : "));
390  myLayout->addWidget(uploadFileSelectorWidget_);
391   
392  result->setLayout (myLayout);
393   
394  // Upload automatically when the user entered a file.
395  uploadFileSelectorWidget_->changed().connect(uploadFileSelectorWidget_, &WFileUpload::upload);
396 
397  // React to a succesfull upload.
398  uploadFileSelectorWidget_->uploaded().connect(this, &PspaApplication::chargerConfig);
399 
400  // React to a fileupload problem.
401  uploadFileSelectorWidget_->fileTooLarge().connect(this, &PspaApplication::fileTooLarge);
402 
403 
404  GWt_dialog* fileSelectorDialog = new GWt_dialog("Load a file",result,false);
405 
406  fileSelectorDialog->exec();
407}
408
409void PspaApplication::chargerConfig()
410{
411    GWt_dialog*  message= new GWt_dialog("File successfully upload","The file has been correctly upload to" + uploadFileSelectorWidget_->spoolFileName(),GWt_dialog::Warning,false,true);
412   
413    string nomDuFichier = (uploadFileSelectorWidget_->clientFileName()).toUTF8();
414    cout << " fichier client : " << nomDuFichier << endl;
415    bool test = removeExtensionFromConfigName(nomDuFichier);
416    cout << " fichier client sans extension : " << nomDuFichier << endl;
417   
418    if ( test )
419    {
420        nameOfCase_ = nomDuFichier;
421        console_->addConsoleMessage(string("restauration..."));
422       
423        if ( !dtmanage_->restoreElements(uploadFileSelectorWidget_->spoolFileName()) ) {
424            GWt_dialog restoreWarning(" element restoring", "failure in restoring elements !", GWt_dialog::Error, false,true);
425            restoreWarning.exec();
426        }
427       
428        GWt_globalParameters* bibi = static_cast<GWt_globalParameters*>(globalParam_);
429        bibi->renew();
430               
431        console_->addConsoleMessage(string("...terminee"));
432        message->show();
433    }
434}
435
436
437void PspaApplication::fileTooLarge()
438{
439    std::stringstream stream;
440    stream << maximumRequestSize ();
441    std::string maxRequestSize(stream.str());
442   
443    std::string message = "This file is too large, please select a one\n";
444    message += "Maximum file size is "+ maxRequestSize;
445    message += " bytes\n";
446   
447    GWt_dialog*  messageBox= new GWt_dialog("Error during upload file" ,message ,GWt_dialog::Error,false,true);
448   
449    messageBox->show();
450}
451
452void PspaApplication::faireDessin()
453{
454  choixElementDessin_->clear();
455  choixHistoDessin_->clear();
456  int nombre = dtmanage_->getBeamLineSize();
457  cout << " PspaApplication::faireDessin() nb elements : " << nombre << endl;
458  for(int i = 1; i <= nombre; i++) {
459    choixElementDessin_->addItem(dtmanage_->getLabelFromElementNumero(i));
460    choixHistoDessin_->addItem(dtmanage_->getLabelFromElementNumero(i));
461  } 
462}
463
464void PspaApplication::dessinerPhaseSpace()
465{   
466  toto_->clear();
467   
468  int typeFaisceau = choixTypeDessinFaisceau_->currentIndex();
469  int index = choixElementDessin_->currentIndex();
470  unsigned xabs = choixAbsPhase_->currentIndex();
471  unsigned yord = choixOrdPhase_->currentIndex();
472       
473  particleBeam* beam = dtmanage_->getDiagnosticBeam(index);
474   
475  if ( beam == NULL ) {
476    GWt_dialog warningBeamState(" graphical analysis", "the beam does not exist at the end of this element !", GWt_dialog::Error, false,true);
477    warningBeamState.exec();
478    return;
479  }
480   
481   
482  if ( typeFaisceau == 0 )
483    {
484      if ( !beam->momentRepresentationOk() ) beam->buildMomentRepresentation();
485      faireDessinTransport(toto_, beam);
486    }
487  else if ( typeFaisceau == 1 )
488    {
489      if ( beam->particleRepresentationOk() ) faireDessinParmela(toto_, beam, xabs, yord);
490      else {
491        GWt_dialog warningBeamState(" graphical analysis", "the beam state does not allow providing a drawing with macroparticles !", GWt_dialog::Error, false,true);
492        warningBeamState.exec();
493      }
494    }
495  else {
496    GWt_dialog warningTypeDrawing(" graphical analysis", "type of  drawing not programmed !", GWt_dialog::Error, false,true);
497    warningTypeDrawing.exec();
498  }
499}
500
501void PspaApplication::dessinerEnveloppe()
502{
503  toto_->clear();
504 
505  int typeEnveloppe = choixEnveloppeDessin_->currentIndex();
506  if ( typeEnveloppe == 0 )
507    {
508      faireDessinEnveloppe(toto_, "x");
509    }
510  else {
511    GWt_dialog warningTypeEnveloppe(" graphical analysis", "type of enveloppe drawing not programmed !", GWt_dialog::Error, false,true);
512    warningTypeEnveloppe.exec();
513  }
514}
515
516void PspaApplication::faireDessinEnveloppe(WContainerWidget* toto, string type)
517{
518   
519  GWt_dialog* envDialog = new GWt_dialog("enveloppe",toto,false);
520  envDialog->setMinimumSize(400,400);
521  envDialog->setClosable(true);
522  envDialog->show();
523     
524  //       new WText(nameOfCase_ + " : enveloppe", toto);
525  unsigned nbel = dtmanage_->getBeamLineSize();
526  vector<double> xcor;
527  vector<double> ycor;
528  dtmanage_->donneesRmsEnveloppe(type,1, nbel, xcor,ycor);
529  scatterPlot1D(envDialog->contents(),xcor,ycor); 
530}
531
532
533void PspaApplication::faireDessinParmela(WContainerWidget* toto, particleBeam* beam, unsigned indexAbs, unsigned indexOrd )
534{
535  cout << " faireDessinParmela " << endl;
536    GWt_dialog* pointsDialog = new GWt_dialog("space phase",toto,false);
537    pointsDialog->setMinimumSize(400,400);
538    pointsDialog->setClosable(true);
539    // pointsDialog->show();
540   
541   
542   
543    vector<bareParticle>& partic = beam->getParticleVector();
544    new WText(nameOfCase_ + " : espace de phase x,x' "+ " nb partic. "+ mixedTools::intToString(partic.size()), pointsDialog->contents());
545    WStandardItemModel *model = new WStandardItemModel(partic.size(), 2, pointsDialog->contents());
546    //    model->setHeaderData(0, WString("X"));
547    //   model->setHeaderData(1, WString("Y = sin(X)"));
548   
549    if ( indexAbs > 5 || indexOrd > 5 ) return;
550
551    for (unsigned i = 0; i < partic.size(); ++i) {
552      double valAbs, valOrd;
553      double begamz = partic.at(i).getBetaGamma().getComponent(2);
554      if ( indexAbs <= 2 ) {
555        valAbs = partic.at(i).getPosition().getComponent(indexAbs);
556        cout << " i= " << i << " valabs = " << valAbs << endl;
557      } else {
558        if ( indexAbs == 5 ) {
559          cout << " PspaApplication::faireDessinParmela : deltap non programme " << endl;
560          return;
561        } else {
562          if ( begamz != 0.0) {
563            valAbs = 1.e3*partic.at(i).getBetaGamma().getComponent(indexAbs-3)/begamz;
564          } else {
565            valAbs = 0.0;
566          }
567        }
568      }
569
570      if ( indexOrd <= 2 ) {
571        valOrd = partic.at(i).getPosition().getComponent(indexOrd);
572      } else {
573        if ( indexOrd == 5 ) {
574          cout << " PspaApplication::faireDessinParmela : deltap non programme " << endl;
575          return;
576        } else {
577
578
579          if ( begamz != 0.0) {
580            valOrd = 1.e3*partic.at(i).getBetaGamma().getComponent(indexOrd-3)/begamz;
581          } else {
582            valOrd = 0.0;
583          }
584        cout << " i= " << i << " valord = " << valOrd << endl;
585        }
586      }
587     
588
589        // double x= partic.at(i).getPosition().getComponent(0);
590        // double begamz = partic.at(i).getBetaGamma().getComponent(2);
591        // double xp;
592        // if ( begamz != 0.0) {
593        //   xp = partic.at(i).getBetaGamma().getComponent(0)/begamz;
594        // } else {
595        //   xp = 0.0;
596        // }
597        // //    cout << "x = " << x << " xp= " << xp << endl;
598        // model->setData(i, 0, x);
599        // model->setData(i, 1,1.e3*xp);
600        // model->setData(i, 2,2.e3*xp);
601        model->setData(i, 0, valAbs);
602        model->setData(i, 1,  valOrd);
603    }
604      cout << " faireDessinParmela : FAISCEAU VU  " << endl;
605
606    WCartesianChart *chart = new WCartesianChart(pointsDialog->contents());
607    chart->setModel(model);        // set the model
608    chart->setXSeriesColumn(0);    // set the column that holds the X data
609    chart->setLegendEnabled(true); // enable the legend
610   
611    chart->setType(ScatterPlot);   // set type to ScatterPlot
612   
613    // Typically, for mathematical functions, you want the axes to cross
614    // at the 0 mark:
615    chart->axis(XAxis).setLocation(ZeroValue);
616    chart->axis(YAxis).setLocation(ZeroValue);
617   
618    // Provide space for the X and Y axis and title.
619    chart->setPlotAreaPadding(80, Left);
620    chart->setPlotAreaPadding(40, Top | Bottom);
621    // Add the curves
622    WDataSeries s(1, PointSeries, Y1Axis);
623    //  s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
624    //    s.setMarker(SquareMarker);
625    //  s.setMarkerSize(600.);
626    //  cout << "le marker est : " << s.marker() << endl;
627    chart->addSeries(s);
628   
629    chart->resize(400, 400); // WPaintedWidget must be given explicit size
630   
631   
632#ifdef HAS_IMAGEMAGIC
633    Wt::WRasterImage pngImage("png", 600, 600);
634    Wt::WPainter p(&pngImage);
635    chart->paint(p);
636    std::string name;
637    name = workingDir_ + "/chart-"+sessionId ()+".png";
638    std::ofstream f(name.c_str(), std::ios::out |std::ios::trunc | std::ios::binary);
639    pngImage.write(f);
640    new WText("<a href='workingArea/chart-"+sessionId ()+".png' target='_blank'>Afficher l'image</a>",toto);
641#endif
642   
643    pointsDialog->show();
644   
645    //  chart->setMargin(10, Top | Bottom);            // add margin vertically
646    //  chart->setMargin(WLength::Auto, Left | Right); // center horizontally
647}
648
649
650// void PspaApplication::addElemToGlobals()
651// {
652//   static_cast<GWt_globalParameters*>(globalParam_)->addElem();
653// }
654
655void PspaApplication::faireDessinTransport(WContainerWidget* toto, particleBeam* beam)
656{
657    //  WContainerWidget* toto = static_cast<WContainerWidget*>(leDessin_);
658    //  toto->clear();
659   
660    //  new WText(nameOfCase_ + " : espace de phase x,x' "+ " nb partic. "+ mixedTools::intToString(partic.size()), toto);
661   
662    GWt_dialog* ellipseDialog = new GWt_dialog("ellipse",toto,false);
663    ellipseDialog->setMinimumSize(400,400);
664    ellipseDialog->setClosable(true);
665    ellipseDialog->show();
666   
667   
668    new WText(nameOfCase_ + " : emittance x,x' " + mixedTools::doubleToString(beam->getUnnormalizedEmittanceX()) + " pi.mm.mrad" , ellipseDialog->contents());
669    new WBreak(ellipseDialog->contents());
670    new WText(" xmax = " + mixedTools::doubleToString(beam->getSigmaTransportij(1,1)) + " cm ", ellipseDialog->contents());
671    new WBreak(ellipseDialog->contents());
672    new WText(" x' max = " + mixedTools::doubleToString(beam->getSigmaTransportij(2,2)) + " mrad ", ellipseDialog->contents());
673    new WBreak(ellipseDialog->contents());
674    new WText(" corr. 12 = " + mixedTools::doubleToString(beam->getSigmaTransportij(2,1))  + " sqrt(cm.rad) ", ellipseDialog->contents());
675   
676    vector<double> xcor;
677    vector<double> ycor;
678    //  dtmanage_->getCurrentBeam()->donneesDessinEllipseXxp(xcor,ycor);
679    beam->donneesDessinEllipseXxp(xcor,ycor);
680    scatterPlot1D(ellipseDialog->contents(),xcor,ycor);
681   
682}
683
684// void PspaApplication::scatterPlot1D(WContainerWidget* toto, vector<double>& xcor, vector<double>& ycor)
685// {
686//   int nbpts = xcor.size();
687//   cout << " PspaApplication::scatterPlot1D nbpts = " << nbpts << endl;
688//   WStandardItemModel *model = new WStandardItemModel(nbpts, 2, toto);
689//   for (int i = 0; i < nbpts; ++i) {
690//     model->setData(i, 0, xcor.at(i));
691//     model->setData(i, 1, ycor.at(i));
692//     //    cout << " PspaApplication::scatterPlot1D el= " << i+1 << " x= " << xcor.at(i) << " y= " << ycor.at(i) << endl;
693//   }
694
695//   WCartesianChart *chart = new WCartesianChart(toto);
696
697//   chart->initLayout();
698
699//   chart->setModel(model);        // set the model
700//   chart->setXSeriesColumn(0);    // set the column that holds the X data
701//   chart->setLegendEnabled(true); // enable the legend
702
703//   chart->setType(ScatterPlot);   // set type to ScatterPlot
704
705//   // Typically, for mathematical functions, you want the axes to cross
706//   // at the 0 mark:
707//    chart->axis(XAxis).setLocation(ZeroValue);
708//    chart->axis(YAxis).setLocation(ZeroValue);
709
710//   // Provide space for the X and Y axis and title.
711// //   chart->setPlotAreaPadding(80, Left);
712// //   chart->setPlotAreaPadding(40, Top | Bottom);
713//   // Add the curves
714//   WDataSeries s(1, CurveSeries, Y1Axis);
715//   chart->addSeries(s);
716
717//   // chart->setBackground (WBrush(WColor("blue")));
718//   chart->resize(300, 300); // WPaintedWidget must be given explicit size
719
720// #ifdef HAS_IMAGEMAGIC
721//  Wt::WRasterImage pngImage("png", 600, 600);
722//  Wt::WPainter p(&pngImage);
723
724//  chart->paint(p);
725//  std::string name;
726//  name = workingDir_ + "/chart-"+sessionId ()+".png";
727//  std::ofstream f(name.c_str(), std::ios::out |std::ios::trunc | std::ios::binary);
728//  pngImage.write(f);
729
730//  new WText("<a href='workingArea/chart-"+sessionId ()+".png' target='_blank'>Afficher l'image</a>",toto);
731
732//  /*
733//  Wt::WPdfImage pdfImage("30cm", "30cm");
734//  Wt::WPainter p1(&pdfImage);
735//  chart->paint(p1);
736//  std::ofstream f1("chart.pdf", std::ios::out | std::ios::binary);
737//  pdfImage.write(f1);
738//  */
739// #endif
740// }
741
742
743void PspaApplication::scatterPlot1D(WContainerWidget* toto, vector<double>& xcor, vector<double>& ycor)
744{
745    int nbpts = xcor.size();
746    cout << " PspaApplication::scatterPlot1D nbpts = " << nbpts << endl;
747    WStandardItemModel *model = new WStandardItemModel(nbpts, 2, toto);
748    for (int i = 0; i < nbpts; ++i) {
749        model->setData(i, 0, xcor.at(i));
750        model->setData(i, 1, ycor.at(i));
751        //    cout << " PspaApplication::scatterPlot1D el= " << i+1 << " x= " << xcor.at(i) << " y= " << ycor.at(i) << endl;
752    }
753   
754    WCartesianChart *chart = new WCartesianChart(toto);
755    chart->setTitle("titre");
756   
757    //  chart->initLayout();
758   
759    chart->setModel(model);        // set the model
760    chart->setXSeriesColumn(0);    // set the column that holds the X data
761    chart->setLegendEnabled(true); // enable the legend
762   
763    chart->setType(ScatterPlot);   // set type to ScatterPlot
764   
765    // Typically, for mathematical functions, you want the axes to cross
766    // at the 0 mark:
767    chart->axis(XAxis).setLocation(ZeroValue);
768    chart->axis(YAxis).setLocation(ZeroValue);
769   
770    // Provide space for the X and Y axis and title.
771    //   chart->setPlotAreaPadding(80, Left);
772    //   chart->setPlotAreaPadding(40, Top | Bottom);
773    // Add the curves
774    WDataSeries s(1, LineSeries, Y1Axis);
775    chart->addSeries(s);
776   
777    // chart->setBackground (WBrush(WColor("blue")));
778    chart->resize(500, 300); // WPaintedWidget must be given explicit size
779   
780#ifdef HAS_IMAGEMAGIC
781    Wt::WRasterImage pngImage("png", 600, 600);
782    Wt::WPainter p(&pngImage);
783   
784    chart->paint(p);
785    std::string name;
786    name = workingDir_ + "/chart-"+sessionId ()+".png";
787    std::ofstream f(name.c_str(), std::ios::out |std::ios::trunc | std::ios::binary);
788    pngImage.write(f);
789   
790    new WText("<a href='workingArea/chart-"+sessionId ()+".png' target='_blank'>Afficher l'image</a>",toto);
791   
792    /*
793     Wt::WPdfImage pdfImage("30cm", "30cm");
794     Wt::WPainter p1(&pdfImage);
795     chart->paint(p1);
796     std::ofstream f1("chart.pdf", std::ios::out | std::ios::binary);
797     pdfImage.write(f1);
798     */
799#endif
800}
801
802
803
804WText* PspaApplication::createTitle(const WString& title)
805{
806  WText *result = new WText(title);
807  result->setInline(false);
808  result->setStyleClass("title");
809  result->setMinimumSize(30,30);
810 
811  return result;
812}
813
814// void PspaApplication::addConsoleMessage(WString msg) {
815//   WText *w = new WText(console_);
816//   w->setTextFormat(PlainText);
817//   w->setText(msg);
818//   w->setInline(false);
819
820//   /*
821//    * Little javascript trick to make sure we scroll along with new content
822//    */
823//   WApplication *app = WApplication::instance();
824//   app->doJavaScript(console_->jsRef() + ".scrollTop += "
825//                     + console_->jsRef() + ".scrollHeight;");
826
827// }
828
829bool PspaApplication::removeExtensionFromConfigName(string& config)
830{
831    //  string configName;
832    string extension(".save");
833    bool test = true;
834    string::size_type nn = config.rfind('.');
835    if ( nn == string::npos )
836    {
837        // pas de point
838        test = false;
839    }
840    string fin = config.substr(nn);
841    if ( fin != extension )
842    {
843        // l'extension n'est pas la bonne
844        test = false;
845    }
846    if ( test )
847    {
848        string::size_type count = config.length() - extension.length();
849        config = config.substr(0, count);
850    }
851    else
852    {
853        GWt_dialog checkConfigNameDialog(" checking config file name", " the file must have the extension " + extension, GWt_dialog::Error,true,true);
854        checkConfigNameDialog.exec();
855    }
856    return test;
857}
858
859void PspaApplication::dessinerHistogramme()
860{
861  // pour mon test : histo en energie
862  toto_->clear();
863   
864  GWt_dialog* pourVoir= new GWt_dialog("histogramme",toto_,false);
865  pourVoir->setMinimumSize(400,400);
866  pourVoir->setClosable(true);
867  pourVoir->show();
868 
869  int index = choixHistoDessin_->currentIndex();   
870  particleBeam* beam = dtmanage_->getDiagnosticBeam(index);
871  if (beam == NULL) {
872    GWt_dialog warningBeamState("histogram","the beam does not exist at the end of this element !", GWt_dialog::Error, false,true);
873    warningBeamState.exec();
874    return;
875  }
876 
877  if (!beam->particleRepresentationOk()) {
878    GWt_dialog warningBeamState("histogram", "the beam state does not allow providing a drawing with macroparticles !", GWt_dialog::Error, false,true);
879    warningBeamState.exec();
880    return;
881  }
882
883  vector<double> xcor;
884  vector<int> hist;
885  int cnts;
886  double out[3]= {0.0};
887  beam->histogramme(xcor,hist,cnts,out);
888 
889  cout<<"xcor.size()= "<<xcor.size()<<", hist.size()= "<<hist.size()<<endl;
890  //////////////////////////////////////////////////////////////////////////////////
891   
892  WContainerWidget *w = toto_;
893  int nbpts= xcor.size()+cnts+1;
894  WStandardItemModel *model = new WStandardItemModel(nbpts,2,w);
895  model->setHeaderData(1, WString("energie"));
896 
897  int j= 0;
898  if(hist[0] > 0) {
899    model->setData(j,0,xcor[0]);
900    model->setData(j,1,hist[0]);
901    j++;
902  }
903  model->setData(j,0,xcor[0]);
904  model->setData(j,1,hist[0]);
905  j++;
906  for (int i = 1; i < nbpts; ++i) {
907    if(hist[i] == 0) {
908      if(hist[i-1] > 0) {
909        model->setData(j,0,xcor[i]);
910        model->setData(j,1,hist[i-1]);
911        j++;
912      }
913      model->setData(j,0,xcor[i]);
914      model->setData(j,1,hist[i]);
915      j++;
916    }
917    if(hist[i] > 0) {
918      model->setData(j,0,xcor[i]);
919      model->setData(j,1,hist[i-1]);
920      j++;
921      model->setData(j,0,xcor[i]);
922      model->setData(j,1,hist[i]);
923      j++;
924    }
925  }
926 
927  // legendes
928  new WText(" entries : "+ mixedTools::intToString((int)out[0]),w);
929  new WBreak(w);
930  new WText(" mean : "+ mixedTools::doubleToString(out[1])+" MeV",w);
931  new WBreak(w);
932  new WText(" rms : "+ mixedTools::doubleToString(out[2])+" KeV",w);
933 
934  WCartesianChart *chart = new WCartesianChart(w);
935  chart->setTitle("Histogram of particle energies");
936  chart->setModel(model);        // set the model
937  chart->setXSeriesColumn(0);    // set the column that holds the categories
938  chart->setLegendEnabled(false); // enable the legend
939   
940  // Provide space for the X and Y axis and title.
941  chart->setPlotAreaPadding(200, Right);
942  chart->setPlotAreaPadding(80, Left);
943  chart->setPlotAreaPadding(40, Top | Bottom);
944   
945  chart->setType(ScatterPlot);
946   
947  WAxis& axis = chart->axis(XAxis);
948  axis.setLabelFormat("%.3f");
949  axis.setGridLinesEnabled(true);
950  axis.setTitle(WString("legende x"));
951
952  chart->axis(Y1Axis).setGridLinesEnabled(true);
953  chart->axis(Y1Axis).setTitle(WString("legende y"));
954   
955  WDataSeries s(1, LineSeries);
956  s.setFillRange(ZeroValueFill);
957  chart->addSeries(s);
958   
959  chart->resize(800, 400);
960  chart->setMargin(10, Top | Bottom);
961  chart->setMargin(WLength::Auto, Left | Right);
962   
963  LegendLocation location = LegendOutside;
964  Side side = Right;
965  AlignmentFlag alignment = AlignMiddle;
966  chart->setLegendLocation(location, side, alignment);
967  chart->setLegendStyle(chart->legendFont(), WPen(black), WBrush(WColor(0xFF, 0xFA, 0xE5)));
968}
Note: See TracBrowser for help on using the repository browser.