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

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

ajout d'un fond gris sur l output

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