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

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

mise a jour des manipulations de faisceau

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