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

Last change on this file since 431 was 431, checked in by touze, 11 years ago

systeme periodique (mailles) + multipoles + madx

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