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

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

merge avec la branche 12_03_12-managerComboBox

File size: 38.0 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) : 
48  WApplication(env),
49  applicationDefaultTitle_("portail PSPA"),
50  beamLine_(NULL),
51  extensionFile_(0)
52{
53  workingDir_ = WApplication::docRoot()+ "/"+WORKINGAREA;
54  nameOfCase_ = "pspa"; // default
55   
56  setTitle(applicationDefaultTitle_);  // application title
57  if (!wApp->environment().javaScript()) {
58    new WText("<i>This examples requires that javascript support is enabled.</i>",root());
59  }
60   
61  // register the absractElementFactory
62  abstractElementFactory_ = new GWt_abstractElementFactory();
63  abstractElementFactory_->setRFGunEnable(true);
64  abstractElementFactory_->setDriftEnable(true);
65  abstractElementFactory_->setCellEnable(true);
66  abstractElementFactory_->setBendEnable(true);
67  abstractElementFactory_->setSolenoEnable(true);
68  abstractElementFactory_->setBeamEnable(true);
69  abstractElementFactory_->setFitEnable(true);
70  abstractElementFactory_->setSnapshotEnable(true);
71 
72  // include the styleSheet
73  WContainerWidget *widroot = root();
74  useStyleSheet("htdocs/pspa.css");
75  dtmanage_ = new dataManager(this);
76  //    dtmanage_->setWorkingDir(workingDir_);
77 
78  /*
79   * The main layout is a 4x3 grid layout.
80   */
81  WGridLayout *mainGridLayout = new WGridLayout();
82  //  layout->addWidget(createTitle("<a href='workingArea/parmin'> Menu (In future)</a>"), 0, 0, 1, 2);
83  //  layout->addWidget(createTitle("Menu (In future)"), 0, 0, 1, 2);
84 
85  WHBoxLayout *toolbarLayout = new WHBoxLayout();
86 
87  WPushButton* boutonSauve = new WPushButton();
88  WPushButton* boutonRestaure = new WPushButton();
89  WPushButton* boutonLoadNew = new WPushButton();
90  WPushButton* boutongraphicalAnalysis = new WPushButton();
91  WPushButton* boutonTrash = new WPushButton();
92 
93  boutonSauve->setStyleClass("saveButton");
94  boutonRestaure->setStyleClass("restoreButton");
95  boutonLoadNew->setStyleClass("loadNewButton");
96  boutongraphicalAnalysis->setStyleClass("histoButton");
97  boutonTrash->setStyleClass("trashButton");
98 
99  boutonSauve->setToolTip ("sauvegarder votre configuration");
100  boutonRestaure->setToolTip (" restaurer la configuration depuis le serveur");
101  boutonLoadNew->setToolTip ("charger une configuration");
102  boutongraphicalAnalysis->setToolTip ("analyse graphique");
103  boutonTrash->setToolTip ("tout effacer");
104 
105  boutonSauve->setMaximumSize(38,38);
106  boutonSauve->setMinimumSize(38,38);
107  boutonRestaure->setMaximumSize(38,38);
108  boutonRestaure->setMinimumSize(38,38);
109  boutonLoadNew->setMaximumSize(38,38);
110  boutonLoadNew->setMinimumSize(38,38);
111  boutongraphicalAnalysis->setMaximumSize(38,38);
112  boutongraphicalAnalysis->setMinimumSize(38,38);
113  boutonTrash->setMaximumSize(38,38);
114  boutonTrash->setMinimumSize(38,38);
115 
116  boutonSauve->clicked().connect(this, &PspaApplication::sauver);
117  boutonRestaure->clicked().connect(this, &PspaApplication::restaurer);
118  // Upload when the button is clicked.
119  // React to a succesfull upload.
120  boutonLoadNew->clicked().connect(this, &PspaApplication::openFileSelector);
121 
122  boutongraphicalAnalysis->clicked().connect(this, &PspaApplication::dialogOnGraphics);
123 
124  boutonTrash->clicked().connect(this, &PspaApplication::removeBeamLine);
125 
126  toolbarLayout->addWidget(boutonSauve , 0,Wt::AlignMiddle);
127  toolbarLayout->addWidget(boutonRestaure , 0,Wt::AlignMiddle);
128  toolbarLayout->addWidget(boutonLoadNew , 0,Wt::AlignMiddle);
129  toolbarLayout->addWidget(boutongraphicalAnalysis , 0,Wt::AlignMiddle);
130  toolbarLayout->addWidget(boutonTrash , 0,Wt::AlignMiddle);
131  toolbarLayout->addWidget(new WText("") , 1,Wt::AlignMiddle);
132 
133  WContainerWidget * toolbarLayoutContainer = new WContainerWidget();
134  toolbarLayoutContainer->setLayout(toolbarLayout);
135  toolbarLayoutContainer->decorationStyle().setBackgroundImage ("/htdocs/fond_toolbar.png");
136   
137  mainGridLayout->addWidget(toolbarLayoutContainer, 1, 0, 1, 3);
138  mainGridLayout->addWidget(createPalette(), 2, 0, 4, 1);
139
140 
141  WScrollArea* scroll = new  WScrollArea();
142  scroll->setWidget(createBeamLine());
143  scroll->setMinimumSize(300,150);
144
145  mainGridLayout->addWidget(scroll, 2, 1, 1, 2);
146 
147 
148  // console_ = new WContainerWidget();
149  // console_->decorationStyle().setBackgroundColor (WColor("lightgray"));
150  // console_->setMaximumSize(600,200);
151  // layout->addWidget(console_, 3, 1);
152  // console_->setMinimumSize(300,100);
153  // console_->setOverflow(WContainerWidget::OverflowAuto);
154 
155 
156  //-----------
157  // A supprimer et a mettre en fenetre
158  globalParam_ = createGlobalParamWidget();
159  //  leDessin_ = new WContainerWidget();
160  //  leDessin_ = createDrawingWidget();
161  graphicsDialog_ = new GWt_dialog("graphical analysis",createDrawingWidget(),false);
162  graphicsDialog_->setMinimumSize(400,400);
163  graphicsDialog_->setClosable(true);
164 
165  executeWidget_ = new GWt_softwarePanel(dtmanage_,this);
166 
167  mainGridLayout->addWidget( globalParam_, 3, 1);
168  //  layout->addWidget( leDessin_, 3, 2);
169  mainGridLayout->addWidget( executeWidget_ , 4, 1);
170  //-----------
171 
172  console_ = new GWt_console();
173
174  // manage sizes
175  console_->setMinimumSize(300,100);
176  executeWidget_->setMinimumSize(400,100);
177
178  mainGridLayout->addWidget(console_, 3, 2,2,1);
179 
180 
181  /*
182   * Let row 2 and column 1 take the excess space.
183   */
184  mainGridLayout->setRowStretch(4, 1);
185  mainGridLayout->setColumnStretch(2, 1);
186 
187  widroot->setLayout(mainGridLayout);
188
189  // set auto scrollbar if needed
190  widroot->setOverflow(WContainerWidget::OverflowAuto);
191
192}
193
194WWidget* PspaApplication::createPalette()
195{
196  WContainerWidget* palette = new WContainerWidget();
197  vector <GWt_abstractElement*> elems = abstractElementFactory_->getAllElements();
198  for (unsigned a = 0; a < elems.size(); a++) {
199    elems[a]->createDragImage(palette);
200    new WBreak(palette);
201  }
202
203  return palette; 
204}
205
206void PspaApplication::createDragImage(const char *url,const char *smallurl,const char *mimeType,WContainerWidget *p,WString())
207{
208  GWt_draggableImage *result = new GWt_draggableImage(url,p);
209 
210  /*
211   * Set the image to be draggable, showing the other image (dragImage)
212   * to be used as the widget that is visually dragged.
213   */
214  result->setDraggable(mimeType,new WImage(smallurl,p),true);
215}
216
217Wt::WWidget* PspaApplication::createBeamLine()
218{
219  extensionFile_= 0;
220  if (!beamLine_) {
221    beamLine_ = new GWt_LigneFaisceau(this);
222  } else {
223    beamLine_->clear();
224  }
225  beamLine_->setMinimumSize(300,100);
226  setTitle(applicationDefaultTitle_);
227
228  return beamLine_;
229}
230
231WWidget* PspaApplication::createGlobalParamWidget()
232{
233    WContainerWidget* globalParam = new GWt_globalParameters(this);
234    return globalParam;
235}
236
237
238void PspaApplication::dialogOnGraphics()
239{
240  graphicsDialog_->show();
241}
242
243WContainerWidget* PspaApplication::createDrawingWidget()
244{
245  WGridLayout *glayout= new WGridLayout();
246  WContainerWidget* dessin = new WContainerWidget();
247  dessin->setLayout(glayout);
248
249  group_ = new Wt::WButtonGroup(this);
250 
251  /////////////////////////////////////////////////////////////////////
252       
253  choixEnveloppeDessin_ = new WComboBox();
254  choixEnveloppeDessin_->addItem("x");
255  // choixEnveloppeDessin_->addItem("y");
256  Wt::WRadioButton *button1= new WRadioButton(" enveloppe");
257  group_->addButton(button1,1);
258  glayout->addWidget(button1,0,1);
259  glayout->addWidget(choixEnveloppeDessin_,0,2);
260  /////////////////////////////////////////////////////////////////////
261
262  choixElementDessin_ = new WComboBox();
263  choixElementDessin_->setMinimumSize(50,10);
264  choixTypeDessinFaisceau_ = new WComboBox();
265  choixTypeDessinFaisceau_->addItem("courant_snyder");
266  choixTypeDessinFaisceau_->addItem("macroparticles");
267  Wt::WRadioButton *button2= new WRadioButton(" phase space after element");
268
269  choixAbsPhase_ = new WComboBox();
270  choixAbsPhase_->addItem("x");
271  choixAbsPhase_->addItem("y");
272  choixAbsPhase_->addItem("dz");
273  choixAbsPhase_->addItem("xp");
274  choixAbsPhase_->addItem("yp");
275  choixAbsPhase_->addItem("dE/E");
276  choixAbsPhase_->setCurrentIndex(0);
277 
278  choixOrdPhase_ = new WComboBox();
279  choixOrdPhase_->addItem("x");
280  choixOrdPhase_->addItem("y");
281  choixOrdPhase_->addItem("dz");
282  choixOrdPhase_->addItem("xp");
283  choixOrdPhase_->addItem("yp");
284  choixOrdPhase_->addItem("dE/E");
285  choixOrdPhase_->setCurrentIndex(3);
286 
287  group_->addButton(button2,2);
288  glayout->addWidget(button2,1,1);
289  glayout->addWidget(choixElementDessin_,1,2);
290  glayout->addWidget(choixTypeDessinFaisceau_,1,3);
291  //  glayout->addWidget(new WText("abs. :"),2,2);
292  glayout->addWidget(choixAbsPhase_,2,2);
293  //  glayout->addWidget(new WText("ord. :"),2,4);
294  glayout->addWidget(choixOrdPhase_,2,3);
295  /////////////////////////////////////////////////////////////////////
296
297  choixVariableHisto_= new WComboBox();
298  choixVariableHisto_->addItem("x");
299  choixVariableHisto_->addItem("y");
300  choixVariableHisto_->addItem("z");
301  choixVariableHisto_->addItem("xp");
302  choixVariableHisto_->addItem("yp");
303  choixVariableHisto_->addItem("dE/E");
304  choixVariableHisto_->setCurrentIndex(5);
305
306  choixHistoDessin_ = new WComboBox();
307  Wt::WRadioButton *button3= new WRadioButton(" histogram after element ");
308  group_->addButton(button3,3);
309
310  glayout->addWidget(button3,3,1);
311  glayout->addWidget(choixHistoDessin_,3,2);
312  glayout->addWidget(choixVariableHisto_,3,3);
313  /////////////////////////////////////////////////////////////////////
314 
315  WPushButton *close= new WPushButton("close"); 
316  glayout->addWidget(close,4,1);
317  close->clicked().connect(this,&PspaApplication::closeGraphicDialog);
318 
319  WPushButton *draw= new WPushButton("draw"); 
320  glayout->addWidget(draw,4,2,2,1);
321  draw->clicked().connect(this,&PspaApplication::dessiner);
322  /////////////////////////////////////////////////////////////////////
323
324  toto_ = new WContainerWidget();
325  dessin->addWidget(toto_);
326  return dessin;
327}
328
329void PspaApplication::closeGraphicDialog()
330{
331  cout << " close graphicsDialog_" << endl;
332  for(unsigned i = 0; i < eDialog_.size(); i++) eDialog_[i]->done(WDialog::Rejected);
333  eDialog_.clear();
334  graphicsDialog_->done(WDialog::Rejected);
335}
336
337void PspaApplication::dessiner()
338{
339  int k= group_->checkedId();
340  cout << "dessiner k= " << k << endl;
341
342  switch(k) {
343  case  1: 
344    dessinerEnveloppe();
345    break;
346  case 2:
347    dessinerPhaseSpace();
348    break;
349  case 3:
350    dessinerHistogramme();
351    break;
352  default:
353    GWt_dialog graphicalWarning("graphical analysis","select a button",GWt_dialog::Warning,false,true);
354    graphicalWarning.exec();
355    break;
356  }
357}
358
359
360void PspaApplication::sauver()
361{
362  cout << " on sauve " << endl;
363  console_->addConsoleMessage("sauvegarde");
364 
365  dialogSave_ = new WDialog("save");
366  new WText("name of case : ",dialogSave_->contents());
367  saveNameEdit_ = new WLineEdit(nameOfCase_.c_str(), dialogSave_->contents());
368  WPushButton *annule = new WPushButton("cancel",dialogSave_->contents());
369  WPushButton *submit = new WPushButton("OK",dialogSave_->contents());
370  annule->clicked().connect(dialogSave_, &Wt::WDialog::reject);
371  submit->clicked().connect(dialogSave_, &Wt::WDialog::accept);
372  dialogSave_->finished().connect(this, &PspaApplication::dialogSaveDone);
373  dialogSave_->show();
374}
375
376
377void PspaApplication::dialogSaveDone(WDialog::DialogCode code)
378{
379    if ( code != Wt::WDialog::Accepted ) {
380        return;
381    }
382    nameOfCase_ = saveNameEdit_->text().toUTF8();
383    delete dialogSave_;
384    dialogSave_ = NULL;
385
386    GWt_globalParameters* bibi = static_cast<GWt_globalParameters*>(globalParam_);
387    bibi->updateGlobals();
388    //  dtmanage_->saveConfiguration(nameOfCase_);
389    dtmanage_->saveConfiguration(sessionId(),nameOfCase_);
390}
391
392
393void PspaApplication::restaurer()
394{
395  GWt_serverFileSelector * fs = new GWt_serverFileSelector("Select a configuration file",workingDir_);
396  string fileName = fs->exec();
397  nameOfCase_ = fileName;
398  if (fileName == "") {
399    return;
400  }
401   
402  removePathFromConfigName(nameOfCase_ );
403  cout << " nom sans path " << nameOfCase_ << endl;
404  removeExtensionFromConfigName(nameOfCase_);
405  cout << " nom sans extension " << nameOfCase_ << endl;
406  if (nameOfCase_ == "")
407    return;
408  cout << " je restaure le fichier " << fileName << endl;
409  dialogSave_ = NULL;
410 
411  bool test = dtmanage_->restoreElements(fileName);
412  if ( !test ) {
413    GWt_dialog restoreWarning(" element restoring", "failure in restoring elements from file : " + fileName , GWt_dialog::Error, false,true);
414    restoreWarning.exec();
415  } else {
416    // Change the window title
417    unsigned long found = fileName.find_last_of("/");
418    setTitle(applicationDefaultTitle_+" : "+fileName.substr(found+1));
419  }
420 
421  GWt_globalParameters* bibi = static_cast<GWt_globalParameters*>(globalParam_);
422  bibi->renew();
423 
424  console_->addConsoleMessage(string("restauration terminee"));
425}
426
427void PspaApplication::openFileSelector()
428{
429   
430  WContainerWidget *result = new WContainerWidget();
431  WVBoxLayout* myLayout = new WVBoxLayout();
432   
433  uploadFileSelectorWidget_ = new WFileUpload();
434   
435  uploadFileSelectorWidget_->setFileTextSize(40);
436   
437  myLayout->addWidget(new WText("Select the configuration file for pspa : "));
438  myLayout->addWidget(uploadFileSelectorWidget_);
439   
440  result->setLayout (myLayout);
441   
442  // Upload automatically when the user entered a file.
443  uploadFileSelectorWidget_->changed().connect(uploadFileSelectorWidget_, &WFileUpload::upload);
444 
445  // React to a succesfull upload.
446  uploadFileSelectorWidget_->uploaded().connect(this, &PspaApplication::chargerConfig);
447 
448  // React to a fileupload problem.
449  uploadFileSelectorWidget_->fileTooLarge().connect(this, &PspaApplication::fileTooLarge);
450 
451 
452  GWt_dialog* fileSelectorDialog = new GWt_dialog("Load a file",result,false);
453 
454  fileSelectorDialog->exec();
455}
456
457void PspaApplication::chargerConfig()
458{
459  GWt_dialog*  message= new GWt_dialog("File successfully upload","The file has been correctly upload to" + uploadFileSelectorWidget_->spoolFileName(),GWt_dialog::Warning,false,true);
460   
461  string nomDuFichier = (uploadFileSelectorWidget_->clientFileName()).toUTF8();
462  cout << " fichier client : " << nomDuFichier << endl;
463  bool test = removeExtensionFromConfigName(nomDuFichier);
464  cout << " fichier client sans extension : " << nomDuFichier << endl;
465 
466  if ( test )
467    {
468      nameOfCase_ = nomDuFichier;
469      console_->addConsoleMessage(string("restauration..."));
470     
471      if ( !dtmanage_->restoreElements(uploadFileSelectorWidget_->spoolFileName()) ) {
472        GWt_dialog restoreWarning(" element restoring", "failure in restoring elements !", GWt_dialog::Error, false,true);
473        restoreWarning.exec();
474      }
475     
476      GWt_globalParameters* bibi = static_cast<GWt_globalParameters*>(globalParam_);
477      bibi->renew();
478     
479      console_->addConsoleMessage(string("rechargement de config termine"));
480      message->show();
481    }
482}
483
484void PspaApplication::fileTooLarge()
485{
486  std::stringstream stream;
487  stream << maximumRequestSize ();
488  std::string maxRequestSize(stream.str());
489 
490  std::string message = "This file is too large, please select a one\n";
491  message += "Maximum file size is "+ maxRequestSize;
492  message += " bytes\n";
493   
494  GWt_dialog*  messageBox= new GWt_dialog("Error during upload file" ,message ,GWt_dialog::Error,false,true);
495 
496  messageBox->show();
497}
498
499void PspaApplication::faireDessin()
500{
501  choixElementDessin_->clear();
502  choixHistoDessin_->clear();
503
504  int nombre = dtmanage_->getBeamLineSize(); 
505  for(int i = 1; i <= nombre; i++) {
506    abstractElement* elPtr = dtmanage_->getElementPointerFromNumero(i);
507    //    if(elPtr->getNomdElement().getElementType() == snapshot) continue;
508
509    choixElementDessin_->addItem(elPtr->getLabel());
510    choixHistoDessin_->addItem(elPtr->getLabel());
511  } 
512}
513
514void PspaApplication::dessinerPhaseSpace()
515{   
516   
517  int typeFaisceau = choixTypeDessinFaisceau_->currentIndex();
518  //  int index = choixElementDessin_->currentIndex();
519  string nameAbs = choixAbsPhase_->currentText().toUTF8();
520  string nameOrd = choixOrdPhase_->currentText().toUTF8();
521  if ( nameAbs == nameOrd ) {
522        GWt_dialog phaseSpaceError(" graphical analysis", "the beam coordinates must be different !", GWt_dialog::Error, false,true);
523        phaseSpaceError.exec();
524        return;
525  }
526  string elementLabel = choixElementDessin_->currentText().toUTF8();
527  particleBeam* beam = dtmanage_->getDiagnosticBeam(elementLabel);
528  //  particleBeam* beam = dtmanage_->getDiagnosticBeam(index);
529   
530  if ( beam == NULL ) {
531    GWt_dialog warningBeamState(" graphical analysis", "the beam does not exist at the end of this element !", GWt_dialog::Error, false,true);
532    warningBeamState.exec();
533    return;
534  }
535   
536    toto_->clear();
537    faireDessinPhaseSpace(toto_, beam, nameAbs, nameOrd, typeFaisceau);
538
539  // if ( typeFaisceau == 0 )
540  //   {
541  //     if ( !beam->momentRepresentationOk() ) beam->buildMomentRepresentation();
542  //     faireDessinTransport(toto_, beam, nameAbs, nameOrd );
543  //   }
544  // else if ( typeFaisceau == 1 )
545  //   {
546  //     if ( beam->particleRepresentationOk() ) faireDessinParmela(toto_, beam, nameAbs, nameOrd );
547  //     else {
548  //    GWt_dialog warningBeamState(" graphical analysis", "the beam state does not allow providing a drawing with macroparticles !", GWt_dialog::Error, false,true);
549  //    warningBeamState.exec();
550  //     }
551  //   }
552  // else {
553  //   GWt_dialog warningTypeDrawing(" graphical analysis", "type of  drawing not programmed !", GWt_dialog::Error, false,true);
554  //   warningTypeDrawing.exec();
555  // }
556}
557
558void PspaApplication::dessinerEnveloppe()
559{
560  toto_->clear();
561 
562  int typeEnveloppe = choixEnveloppeDessin_->currentIndex();
563  if ( typeEnveloppe == 0 )
564    {
565      faireDessinEnveloppe(toto_, "x");
566    }
567  else {
568    GWt_dialog warningTypeEnveloppe(" graphical analysis", "type of enveloppe drawing not programmed !", GWt_dialog::Error, false,true);
569    warningTypeEnveloppe.exec();
570  }
571}
572
573void PspaApplication::faireDessinEnveloppe(WContainerWidget* toto, string type)
574{
575 
576  GWt_dialog* envDialog = new GWt_dialog("enveloppe",toto,false);
577  eDialog_.push_back(envDialog);
578  envDialog->setMinimumSize(400,400);
579  envDialog->setClosable(true);
580  envDialog->show();
581     
582  //  unsigned nbel = dtmanage_->getBeamLineSize();
583  vector<double> xcor;
584  vector<double> ycor;
585  string titre;
586  string legendx;
587  string legendy;
588  dtmanage_->donneesRmsEnveloppe(type,xcor,ycor,titre,legendx,legendy);
589
590  Wt::Chart::SeriesType seriesType = LineSeries;
591  Wt::Chart::FillRangeType fillRange = NoFill;
592  Wt::Chart::AxisValue location = ZeroValue;
593  bool isGridLinesEnabled = false; 
594  chartPlot2vec(envDialog->contents(),xcor,ycor,seriesType,fillRange,location,isGridLinesEnabled,titre,legendx,legendy,500,300);
595}
596
597void PspaApplication::faireDessinPhaseSpace(WContainerWidget* toto, particleBeam* beam, string namex, string namey, int typeFaisceau)
598{
599
600  vector<double> xcor;
601  vector<double> ycor;
602  vector<string> legende;
603  string titre;
604  Wt::Chart::SeriesType seriesType;
605 
606  if ( typeFaisceau == 0 ) {
607    seriesType = LineSeries;
608    titre = "phase space rms";
609    beam->donneesDessinEllipse(xcor,ycor,legende,namex,namey);
610  } else if (  typeFaisceau == 1 ) {
611    seriesType = PointSeries;
612    titre = " phase space ";
613    beam->particlesPhaseSpaceData(xcor,ycor,legende,namex,namey);
614  } else {
615    GWt_dialog warningBeamState(" graphical analysis", "the beam state does not allow providing a drawing with macroparticles !", GWt_dialog::Error, false,true);
616    warningBeamState.exec();
617    return;
618  }
619
620  GWt_dialog* phaseSpaceDialog = new GWt_dialog("phase space",toto,false);
621  eDialog_.push_back(phaseSpaceDialog);
622  phaseSpaceDialog->setMinimumSize(400,400);
623  phaseSpaceDialog->setClosable(true);
624  new WText(nameOfCase_, phaseSpaceDialog->contents());
625
626  for (int k=0 ; k < legende.size(); k++) {
627    new WBreak(phaseSpaceDialog->contents());
628    new WText(legende.at(k), phaseSpaceDialog->contents());
629  }
630
631  Wt::Chart::FillRangeType fillRange = NoFill;
632  Wt::Chart::AxisValue location = ZeroValue;
633  bool isGridLinesEnabled = false; 
634  chartPlot2vec(phaseSpaceDialog->contents(),xcor,ycor,seriesType,fillRange,location,isGridLinesEnabled,titre,namex,namey,500,300);
635 
636  phaseSpaceDialog->show();
637}
638
639
640
641// void PspaApplication::faireDessinParmela(WContainerWidget* toto, particleBeam* beam, string namex, string namey )
642// {
643//   cout << " faireDessinParmela " << endl;
644//   GWt_dialog* pointsDialog = new GWt_dialog("space phase",toto,false);
645//   eDialog_.push_back(pointsDialog);
646//   pointsDialog->setMinimumSize(400,400);
647//   pointsDialog->setClosable(true);
648//   // pointsDialog->show();
649     
650//   new WText(nameOfCase_, pointsDialog->contents());
651
652
653//   vector<double> xcor;
654//   vector<double> ycor;
655//   vector<string> legende;
656//   beam->particlesPhaseSpaceData(xcor, ycor, legende, namex, namey);
657//   for (int k=0 ; k < legende.size(); k++) {
658//     new WBreak(pointsDialog->contents());
659//     new WText(legende.at(k), pointsDialog->contents());
660//   }
661//   string titre = " phase space ";
662//   chartPlot2vec(pointsDialog->contents(), xcor, ycor, true,titre,namex, namey,400,400);
663
664
665   
666//   pointsDialog->show();
667   
668//   //  chart->setMargin(10, Top | Bottom);            // add margin vertically
669//   //  chart->setMargin(WLength::Auto, Left | Right); // center horizontally
670// }
671
672// void PspaApplication::faireDessinTransport(WContainerWidget* toto, particleBeam* beam, string namex, string namey)
673// {
674//   GWt_dialog* ellipseDialog = new GWt_dialog("ellipse",toto,false);
675//   eDialog_.push_back(ellipseDialog);
676//   ellipseDialog->setMinimumSize(400,400);
677//   ellipseDialog->setClosable(true);
678//   ellipseDialog->show();
679//   new WText(nameOfCase_, ellipseDialog->contents());
680 
681//   vector<double> xcor;
682//   vector<double> ycor;
683//   vector<string> legende;
684//   beam->donneesDessinEllipse(xcor,ycor,legende, namex, namey);
685
686//   for (int k=0 ; k < legende.size(); k++) {
687//     new WBreak(ellipseDialog->contents());
688//     new WText(legende.at(k), ellipseDialog->contents());
689//   }
690//   string titre = "phase space rms";
691//   chartPlot2vec(ellipseDialog->contents(), xcor, ycor, false,titre, namex, namey,500,300);
692// }
693
694
695// parametre drawPoints : true = on trace des points (phase space) ; false = on trace des lignes (enveloppes...)
696void PspaApplication::chartPlot2vec(WContainerWidget* toto, vector<double>& xcor, vector<double>& ycor, bool drawPoints,string title,string legendx, string legendy,int width, int height, bool makeIcon)
697{
698    int nbpts = xcor.size();
699    cout << " PspaApplication::chartPlot2vec nbpts = " << nbpts << endl;
700    WStandardItemModel *model = new WStandardItemModel(nbpts, 2, toto);
701    for (int i = 0; i < nbpts; ++i) {
702        model->setData(i, 0, xcor.at(i));
703        model->setData(i, 1, ycor.at(i));
704        //    cout << " PspaApplication::chartPlot2vec el= " << i+1 << " x= " << xcor.at(i) << " y= " << ycor.at(i) << endl;
705    }
706   
707    WCartesianChart *chart = new WCartesianChart(toto);
708    if (!makeIcon) {
709        chart->setTitle(title);
710    }
711    chart->initLayout();
712   
713    chart->setModel(model);        // set the model
714    chart->setXSeriesColumn(0);    // set the column that holds the X data
715    if (!makeIcon) {
716        chart->setLegendEnabled(true); // enable the legend
717    } else {
718        chart->setLegendEnabled(false); // enable the legend
719    }
720   
721    chart->setType(ScatterPlot);   // set type to ScatterPlot
722   
723    // Typically, for mathematical functions, you want the axes to cross
724    // at the 0 mark:
725    chart->axis(XAxis).setLocation(ZeroValue);
726    chart->axis(YAxis).setLocation(ZeroValue);
727
728    // Provide space for the X and Y axis and title.
729    chart->setPlotAreaPadding(80, Left);  // ?
730    chart->setPlotAreaPadding(40, Bottom);
731    chart->setPlotAreaPadding(60, Top);
732    if ( drawPoints ) {
733      WDataSeries s(1, PointSeries, Y1Axis);
734      chart->addSeries(s);   
735    } else {
736      WDataSeries s(1, LineSeries, Y1Axis);
737      chart->addSeries(s);   
738    }
739    chart->resize(width, height); // WPaintedWidget must be given explicit size
740   
741   
742    WAxis& axis = chart->axis(XAxis);
743    axis.setLabelFormat("%.3f");
744    //       axis.setGridLinesEnabled(true);
745    axis.setTitle(legendx);
746   
747    WAxis& axey = chart->axis(YAxis);
748    axey.setTitle(legendy);
749    // axis = chart->axis(YAxis);
750    // axis.setLabelFormat("%.3f");
751    // axis.setGridLinesEnabled(true);
752    // axis.setTitle(legendy);
753   
754    if (makeIcon) {
755      chart->setPlotAreaPadding(0);
756      chart->setAxisPadding(0);
757      WFont xAxisFont = chart->axis(XAxis).labelFont();
758      xAxisFont.setSize(8);
759      WFont yAxisFont = chart->axis(YAxis).labelFont();
760      yAxisFont.setSize(8);
761      chart->axis(XAxis).setLabelFont(xAxisFont);
762      chart->axis(YAxis).setLabelFont(yAxisFont);
763    }
764
765#ifdef HAS_IMAGEMAGIC
766    Wt::WRasterImage pngImage("png", 600, 600);
767    Wt::WPainter p(&pngImage);
768    chart->paint(p);
769    std::string name;
770    name = workingDir_ + "/chart-"+sessionId ()+".png";
771    std::ofstream f(name.c_str(), std::ios::out |std::ios::trunc | std::ios::binary);
772    pngImage.write(f);   
773    new WText("<a href='workingArea/chart-"+sessionId ()+".png' target='_blank'>Afficher l'image</a>",toto);
774   
775    Wt::WPdfImage pdfImage("30cm", "30cm");
776    Wt::WPainter p1(&pdfImage);
777    pdfImage.init();
778    chart->paint(p1);
779    name = workingDir_ + "/chart-"+sessionId ()+".pdf";
780    std::ofstream f1(name.c_str(), std::ios::out |std::ios::trunc | std::ios::binary);
781    pdfImage.write(f1);
782#endif
783}
784
785
786WText* PspaApplication::createTitle(const WString& title)
787{
788  WText *result = new WText(title);
789  result->setInline(false);
790  result->setStyleClass("title");
791  result->setMinimumSize(30,30);
792 
793  return result;
794}
795
796// void PspaApplication::addConsoleMessage(WString msg) {
797//   WText *w = new WText(console_);
798//   w->setTextFormat(PlainText);
799//   w->setText(msg);
800//   w->setInline(false);
801
802//   /*
803//    * Little javascript trick to make sure we scroll along with new content
804//    */
805//   WApplication *app = WApplication::instance();
806//   app->doJavaScript(console_->jsRef() + ".scrollTop += "
807//                     + console_->jsRef() + ".scrollHeight;");
808
809// }
810
811bool PspaApplication::removePathFromConfigName(string& config) {
812  string toExtract = workingDir_ + "/";
813  string::size_type nn = config.find(toExtract);
814  if ( nn == string::npos ) {
815        GWt_dialog checkremovePath(" checking config file name", " failed to recognize path name for file  " + config, GWt_dialog::Error,true,true);
816        checkremovePath.exec();
817    return false;
818  }
819  config.replace(nn, toExtract.size(), "");
820  return true;
821}
822
823bool PspaApplication::removeExtensionFromConfigName(string& config)
824{
825    //  string configName;
826    string extension(".save");
827    bool test = true;
828    string::size_type nn = config.rfind('.');
829    if ( nn == string::npos )
830    {
831        // pas de point
832        test = false;
833    }
834    string fin = config.substr(nn);
835    if ( fin != extension )
836    {
837        // l'extension n'est pas la bonne
838        test = false;
839    }
840    if ( test )
841    {
842        string::size_type count = config.length() - extension.length();
843        config = config.substr(0, count);
844    }
845    else
846    {
847        GWt_dialog checkConfigNameDialog(" checking config file name", " the file must have the extension " + extension, GWt_dialog::Error,true,true);
848        checkConfigNameDialog.exec();
849    }
850    return test;
851}
852
853void PspaApplication::old_dessinerHistogramme()
854{
855  // pour mon test : histo en energie
856  toto_->clear();
857 
858  GWt_dialog* histoDialog = new GWt_dialog("histogramme",toto_,false);
859  eDialog_.push_back(histoDialog);
860  histoDialog->setMinimumSize(400,400);
861  histoDialog->setClosable(true);
862 
863  // int index = choixHistoDessin_->currentIndex();   
864  // particleBeam* beam = dtmanage_->getDiagnosticBeam(index);
865   string elementLabel = choixHistoDessin_->currentText().toUTF8();   
866   particleBeam* beam = dtmanage_->getDiagnosticBeam(elementLabel);
867  if (beam == NULL) {
868    GWt_dialog warningBeamState("histogram","the beam does not exist at the end of this element !", GWt_dialog::Error, false,true);
869    warningBeamState.exec();
870    return;
871  }
872 
873  if (!beam->particleRepresentationOk()) {
874    GWt_dialog warningBeamState("histogram", "the beam state does not allow providing a drawing with macroparticles !", GWt_dialog::Error, false,true);
875    warningBeamState.exec();
876    return;
877  }
878
879  histoDialog->show();
880  vector<double> xcor;
881  vector<int> hist;
882  double out[3]= {0.0};
883
884  unsigned iabs= choixVariableHisto_->currentIndex(); 
885  beam->histogramme(iabs,xcor,hist,out);
886
887  cout<<"xcor.size()= "<<xcor.size()<<", hist.size()= "<<hist.size()<<endl;
888  //////////////////////////////////////////////////////////////////////////////////
889
890  WContainerWidget *w= histoDialog->contents();
891  int n = hist.size();
892  int nbpts= 2*(n+1);
893  WStandardItemModel *model = new WStandardItemModel(nbpts,2,w);
894
895  int j= 0;
896  model->setData(j,0,xcor[0]);
897  model->setData(j,1,0.0);
898  j++;
899  model->setData(j,0,xcor[0]);
900  model->setData(j,1,hist[0]);
901  j++;
902
903  for (int i = 1; i < n; ++i) {
904    model->setData(j,0,xcor[i]);
905    model->setData(j,1,hist[i-1]);
906    j++;
907    model->setData(j,0,xcor[i]);
908    model->setData(j,1,hist[i]);
909    j++;
910  }
911
912  model->setData(j,0,xcor[n]);
913  model->setData(j,1,hist[n-1]);
914  j++;
915  model->setData(j,0,xcor[n]);
916  model->setData(j,1,0.0);
917
918  // legendes
919  string unites[2];
920  if(iabs == 0 || iabs == 1 || iabs == 2) {
921    unites[0]= unites[1]= " mm";
922  }
923  if(iabs == 3 || iabs == 4) {
924    unites[0]= unites[1]= " mrad";
925  }
926  if(iabs == 5) {
927    unites[0]= " MeV"; unites[1]= " KeV";
928  }
929
930  new WText(" entries : "+ mixedTools::intToString((int)out[0]),w);
931  new WBreak(w);
932  new WText(" mean : "+ mixedTools::doubleToString(out[1])+unites[0],w);
933  new WBreak(w);
934  new WText(" sigma rms : "+ mixedTools::doubleToString(out[2])+unites[1],w);
935   
936  WCartesianChart *chart = new WCartesianChart(w);
937
938  if(iabs == 0) chart->setTitle("x-coordinate");
939  if(iabs == 1) chart->setTitle("y-coordinate");
940  if(iabs == 2) chart->setTitle("z-coordinate");
941  if(iabs == 3) chart->setTitle("xp-divergence");
942  if(iabs == 4) chart->setTitle("yp-devergence");
943  if(iabs == 5) chart->setTitle("kinetic energy");
944 
945  chart->setModel(model);        // set the model
946  chart->setXSeriesColumn(0);    // set the column that holds the categories
947  chart->setLegendEnabled(false); // enable the legend
948   
949  // Provide space for the X and Y axis and title.
950  chart->setPlotAreaPadding(200, Right);
951  chart->setPlotAreaPadding(80, Left);
952  chart->setPlotAreaPadding(40, Top | Bottom);
953   
954  chart->setType(ScatterPlot);
955   
956  WAxis& axis = chart->axis(XAxis);
957  axis.setLabelFormat("%.3f");
958  axis.setGridLinesEnabled(true);
959
960  if(iabs == 0) axis.setTitle(WString(" x (mm)"));
961  if(iabs == 1) axis.setTitle(WString(" y (mm)"));
962  if(iabs == 2) axis.setTitle(WString(" z (mm)"));
963  if(iabs == 3) axis.setTitle(WString(" xp (mrad)"));
964  if(iabs == 4) axis.setTitle(WString(" yp (mrad)"));
965  if(iabs == 5) axis.setTitle(WString(" dEcin/Ecin (%)"));
966 
967  chart->axis(Y1Axis).setGridLinesEnabled(true);
968  //chart->axis(Y1Axis).setTitle(WString("legende y"));
969   
970  WDataSeries s(1, LineSeries);
971  s.setFillRange(ZeroValueFill);
972  chart->addSeries(s);
973   
974  chart->resize(800, 400);
975  chart->setMargin(10, Top | Bottom);
976  chart->setMargin(WLength::Auto, Left | Right);
977   
978  LegendLocation location = LegendOutside;
979  Side side = Right;
980  AlignmentFlag alignment = AlignMiddle;
981  chart->setLegendLocation(location, side, alignment);
982  chart->setLegendStyle(chart->legendFont(), WPen(black), WBrush(WColor(0xFF, 0xFA, 0xE5)));
983}
984
985void PspaApplication::removeBeamLine() {
986  createBeamLine();
987  executeWidget_->updateSections();
988}
989
990void PspaApplication::chartPlot2vec(WContainerWidget* toto,vector<double>& xcor,vector<double>& ycor,SeriesType seriesType,FillRangeType fillRange,AxisValue value,bool isGridLinesEnables,string title,string legendx,string legendy,int width,int height,bool makeIcon)
991{
992  int nbpts = xcor.size();
993  cout << " PspaApplication::chartPlot2vec nbpts = " << nbpts << endl;
994
995  WStandardItemModel *model = new WStandardItemModel(nbpts, 2, toto);
996  for (int i = 0; i < nbpts; ++i) {
997    model->setData(i, 0, xcor.at(i));
998    model->setData(i, 1, ycor.at(i));
999    //    cout << " PspaApplication::chartPlot2vec el= " << i+1 << " x= " << xcor.at(i) << " y= " << ycor.at(i) << endl;
1000  }
1001   
1002  WCartesianChart *chart = new WCartesianChart(toto);
1003  if (!makeIcon) chart->setTitle(title);
1004
1005  chart->initLayout(); 
1006  chart->setModel(model);        // set the model
1007  chart->setXSeriesColumn(0);    // set the column that holds the X data
1008 
1009  chart->setType(ScatterPlot);   // set type to ScatterPlot
1010  chart->setLegendEnabled(false); // enable the legend 
1011
1012  // Provide space for the X and Y axis and title.
1013  chart->setPlotAreaPadding(80, Left); 
1014  chart->setPlotAreaPadding(40, Bottom);
1015  chart->setPlotAreaPadding(60, Top);
1016 
1017  // Typically, for mathematical functions, you want the axes to cross
1018  // at the 0 mark (value= ZeroValue)
1019  chart->axis(XAxis).setLocation(value);
1020  chart->axis(YAxis).setLocation(value);
1021 
1022  WDataSeries s(1,seriesType);
1023  s.setFillRange(fillRange);
1024  chart->addSeries(s);
1025 
1026  WAxis& axis = chart->axis(XAxis);
1027  axis.setLabelFormat("%.3f");
1028  axis.setTitle(legendx);
1029   
1030  chart->axis(Y1Axis).setTitle(legendy);
1031
1032  if(isGridLinesEnables) {
1033    axis.setGridLinesEnabled(true);
1034    chart->axis(Y1Axis).setGridLinesEnabled(true);
1035  }
1036 
1037  chart->resize(width,height); // WPaintedWidget must be given explicit size
1038  chart->setMargin(10, Top | Bottom); // ?
1039  chart->setMargin(WLength::Auto, Left | Right); //?
1040
1041  if (makeIcon) {
1042    chart->setLegendEnabled(false); // enable the legend
1043    chart->setPlotAreaPadding(0);
1044    chart->setAxisPadding(0);
1045    WFont xAxisFont = chart->axis(XAxis).labelFont();
1046    xAxisFont.setSize(8);
1047    WFont yAxisFont = chart->axis(YAxis).labelFont();
1048    yAxisFont.setSize(8);
1049    chart->axis(XAxis).setLabelFont(xAxisFont);
1050    chart->axis(YAxis).setLabelFont(yAxisFont);
1051  }
1052 
1053#ifdef HAS_IMAGEMAGIC
1054  Wt::WRasterImage pngImage("png", 600, 600);
1055  Wt::WPainter p(&pngImage);
1056  chart->paint(p);
1057  std::string name;
1058  name = workingDir_ + "/chart-"+sessionId ()+".png";
1059  std::ofstream f(name.c_str(), std::ios::out |std::ios::trunc | std::ios::binary);
1060  pngImage.write(f);   
1061  new WText("<a href='workingArea/chart-"+sessionId ()+".png' target='_blank'>Afficher l'image</a>",toto);
1062 
1063  Wt::WPdfImage pdfImage("30cm", "30cm");
1064  Wt::WPainter p1(&pdfImage);
1065  pdfImage.init();
1066  chart->paint(p1);
1067  name = workingDir_ + "/chart-"+sessionId ()+".pdf";
1068  std::ofstream f1(name.c_str(), std::ios::out |std::ios::trunc | std::ios::binary);
1069  pdfImage.write(f1);
1070#endif
1071}
1072
1073void PspaApplication::dessinerHistogramme()
1074{
1075  toto_->clear();
1076 
1077  GWt_dialog* histoDialog = new GWt_dialog("histogramme",toto_,false);
1078  eDialog_.push_back(histoDialog);
1079  histoDialog->setMinimumSize(400,400);
1080  histoDialog->setClosable(true);
1081
1082  string elementLabel = choixHistoDessin_->currentText().toUTF8();   
1083  particleBeam* beam = dtmanage_->getDiagnosticBeam(elementLabel);
1084  if (beam == NULL) {
1085    GWt_dialog warningBeamState("histogram","the beam does not exist at the end of this element !", GWt_dialog::Error, false,true);
1086    warningBeamState.exec();
1087    return;
1088  }
1089 
1090  if (!beam->particleRepresentationOk()) {
1091    GWt_dialog warningBeamState("histogram", "the beam state does not allow providing a drawing with macroparticles !", GWt_dialog::Error, false,true);
1092    warningBeamState.exec();
1093    return;
1094  }
1095
1096  histoDialog->show();
1097  vector<double> xcor;
1098  vector<int> hist;
1099  double out[3]= {0.0};
1100  unsigned iabs= choixVariableHisto_->currentIndex(); 
1101  beam->histogramme(iabs,xcor,hist,out);
1102
1103  cout<<"xcor.size()= "<<xcor.size()<<", hist.size()= "<<hist.size()<<endl;
1104  //////////////////////////////////////////////////////////////////////////////////
1105 
1106  int n = hist.size();
1107  vector<double> x;
1108  x.resize(2*n+2);
1109  vector<double> y;
1110  y.resize(2*n+2);
1111
1112  int j= 0;
1113  x.at(j)= xcor[0];
1114  y.at(j)= 0.0;
1115  j++;
1116  x.at(j)= xcor[0];
1117  y.at(j)= hist[0];
1118  j++;
1119 
1120  for (int i = 1; i < n; ++i) {
1121    x.at(j)= xcor[i];
1122    y.at(j)= hist[i-1];
1123    j++;
1124    x.at(j)= xcor[i];
1125    y.at(j)= hist[i];
1126    j++;
1127  }
1128
1129  x.at(j)= xcor[n];
1130  y.at(j)= hist[n-1];
1131  j++;
1132  x.at(j)= xcor[n];
1133  y.at(j)= 0.0;
1134  //////////////////////////////////////////////////////////////////////////////////
1135
1136  WContainerWidget *w= histoDialog->contents();
1137
1138  // legendes
1139  string unites[2];
1140  if(iabs == 0 || iabs == 1 || iabs == 2) {
1141    unites[0]= unites[1]= " mm";
1142  }
1143  if(iabs == 3 || iabs == 4) {
1144    unites[0]= unites[1]= " mrad";
1145  }
1146  if(iabs == 5) {
1147    unites[0]= " MeV"; unites[1]= " KeV";
1148  }
1149
1150  new WText(" entries : "+ mixedTools::intToString((int)out[0]),w);
1151  new WBreak(w);
1152  new WText(" mean : "+ mixedTools::doubleToString(out[1])+unites[0],w);
1153  new WBreak(w);
1154  new WText(" sigma rms : "+ mixedTools::doubleToString(out[2])+unites[1],w);
1155  //////////////////////////////////////////////////////////////////////////////////
1156
1157  string titre;
1158  if(iabs == 0) titre="x-coordinate";
1159  if(iabs == 1) titre="y-coordinate";
1160  if(iabs == 2) titre="z-coordinate";
1161  if(iabs == 3) titre="xp-divergence";
1162  if(iabs == 4) titre="yp-devergence";
1163  if(iabs == 5) titre="kinetic energy";
1164
1165  string xname;
1166  if(iabs == 0) xname=" x (mm)";
1167  if(iabs == 1) xname=" y (mm)";
1168  if(iabs == 2) xname=" z (mm)";
1169  if(iabs == 3) xname=" xp (mrad)";
1170  if(iabs == 4) xname=" yp (mrad)";
1171  if(iabs == 5) xname=" dEcin/Ecin (%)";
1172
1173  string yname;
1174  Wt::Chart::SeriesType seriesType = LineSeries;
1175  Wt::Chart::FillRangeType fillRange = ZeroValueFill;
1176  Wt::Chart::AxisValue location = MinimumValue;
1177  bool isGridLinesEnabled = true; 
1178  chartPlot2vec(w,x,y,seriesType,fillRange,location,isGridLinesEnabled,titre,xname,yname,500,300);
1179}
Note: See TracBrowser for help on using the repository browser.