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

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

Renommage de toutes les classes d’élément dans le GWt_ par GWt_elementXXX

File size: 36.5 KB
Line 
1#include <stdio.h>
2#include <ctime>
3
4#include "GWt_pspaApplication.h"
5#include "GWt_draggableImage.h"
6#include "GWt_abstractElementFactory.h"
7#include "GWt_serverFileSelector.h"
8#include "particleBeam.h"
9#include "bareParticle.h"
10#include "nomDeLogiciel.h"
11#include "mixedTools.h"
12#include "nomdElements.h"
13#include "environmentVariables.h"
14#include "trivaluedBool.h"
15
16#include <Wt/WGridLayout>
17#include <Wt/WVBoxLayout>
18#include <Wt/WHBoxLayout>
19#include <Wt/WImage>
20#include <Wt/WMenu>
21#include <Wt/WStackedWidget>
22#include <Wt/WBreak>
23#include <Wt/WStandardItemModel>
24#include <Wt/WFileUpload>
25#include <Wt/WPainter>
26#include <Wt/WScrollArea>
27#include <Wt/WRadioButton>
28#include <Wt/WMessageBox>
29
30#define HAS_IMAGEMAGIC 1
31
32using namespace Wt::Chart;
33
34/*
35 * The env argument contains information about the new session, and
36 * the initial request. It must be passed to the WApplication
37 * constructor so it is typically also an argument for your custom
38 * application constructor.
39 */
40
41PspaApplication::PspaApplication(const WEnvironment& env) : 
42  WApplication(env),
43  applicationDefaultTitle_("portail PSPA"),
44  beamLine_(NULL),
45  extensionFile_(0)
46{
47  workingDir_ = WApplication::docRoot()+ "/"+WORKINGAREA;
48  nameOfCase_ = "pspa"; // default
49   
50  setTitle(applicationDefaultTitle_);  // application title
51  if (!wApp->environment().javaScript()) {
52    new WText("<i>This examples requires that javascript support is enabled.</i>",root());
53  }
54   
55  // register the absractElementFactory
56  abstractElementFactory_ = new GWt_abstractElementFactory();
57  abstractElementFactory_->setRFGunEnable(true);
58  abstractElementFactory_->setDriftEnable(true);
59  abstractElementFactory_->setCellEnable(true);
60  abstractElementFactory_->setBendEnable(true);
61  abstractElementFactory_->setSolenoEnable(true);
62  abstractElementFactory_->setBeamEnable(true);
63  abstractElementFactory_->setFitEnable(true);
64  abstractElementFactory_->setSnapshotEnable(true);
65  abstractElementFactory_->setMPoleEnable(true);
66 
67  // include the styleSheetcd
68  WContainerWidget *widroot = root();
69  useStyleSheet("htdocs/pspa.css");
70  dtmanage_ = new dataManager(this);
71
72  // The main layout is a 4x3 grid layout.
73  WGridLayout *mainGridLayout = new WGridLayout(); 
74  WHBoxLayout *toolbarLayout = new WHBoxLayout();
75 
76  WPushButton* boutonSauve = new WPushButton();
77  WPushButton* boutonRestaure = new WPushButton();
78  WPushButton* boutonLoadNew = new WPushButton();
79  WPushButton* boutongraphicalAnalysis = new WPushButton();
80  WPushButton* boutonTrash = new WPushButton();
81 
82  boutonSauve->setStyleClass("saveButton");
83  boutonRestaure->setStyleClass("restoreButton");
84  boutonLoadNew->setStyleClass("loadNewButton");
85  boutongraphicalAnalysis->setStyleClass("histoButton");
86  boutonTrash->setStyleClass("trashButton");
87 
88  boutonSauve->setToolTip ("sauvegarder votre configuration");
89  boutonRestaure->setToolTip ("restaurer la configuration depuis le serveur");
90  boutonLoadNew->setToolTip ("charger une configuration");
91  boutongraphicalAnalysis->setToolTip ("analyse graphique");
92  boutonTrash->setToolTip ("tout effacer");
93 
94  boutonSauve->setMaximumSize(38,38);
95  boutonSauve->setMinimumSize(38,38);
96  boutonRestaure->setMaximumSize(38,38);
97  boutonRestaure->setMinimumSize(38,38);
98  boutonLoadNew->setMaximumSize(38,38);
99  boutonLoadNew->setMinimumSize(38,38);
100  boutongraphicalAnalysis->setMaximumSize(38,38);
101  boutongraphicalAnalysis->setMinimumSize(38,38);
102  boutonTrash->setMaximumSize(38,38);
103  boutonTrash->setMinimumSize(38,38);
104 
105  boutonSauve->clicked().connect(this, &PspaApplication::sauver);
106  boutonRestaure->clicked().connect(this, &PspaApplication::restaurer);
107  // Upload when the button is clicked. React to a succesfull upload.
108  boutonLoadNew->clicked().connect(this, &PspaApplication::openFileSelector);
109  boutongraphicalAnalysis->clicked().connect(this, &PspaApplication::dialogOnGraphics);
110  boutonTrash->clicked().connect(this, &PspaApplication::removeBeamLine);
111  toolbarLayout->addWidget(boutonSauve , 0,Wt::AlignMiddle);
112  toolbarLayout->addWidget(boutonRestaure , 0,Wt::AlignMiddle);
113  toolbarLayout->addWidget(boutonLoadNew , 0,Wt::AlignMiddle);
114  toolbarLayout->addWidget(boutongraphicalAnalysis , 0,Wt::AlignMiddle);
115  toolbarLayout->addWidget(boutonTrash , 0,Wt::AlignMiddle);
116  toolbarLayout->addWidget(new WText("") , 1,Wt::AlignMiddle);
117 
118  WContainerWidget * toolbarLayoutContainer = new WContainerWidget();
119  toolbarLayoutContainer->setLayout(toolbarLayout);
120  toolbarLayoutContainer->decorationStyle().setBackgroundImage ("/htdocs/fond_toolbar.png");
121   
122  mainGridLayout->addWidget(toolbarLayoutContainer, 1, 0, 1, 3);
123  mainGridLayout->addWidget(createPalette(), 2, 0, 4, 1);
124
125  WScrollArea* scroll = new  WScrollArea();
126  scroll->setWidget(createBeamLine());
127  scroll->setMinimumSize(300,150);
128  mainGridLayout->addWidget(scroll, 2, 1, 1, 2);
129
130  //-----------
131
132  // A supprimer et a mettre en fenetre
133  // xx globalParam_ = createGlobalParamWidget();
134  // xx mainGridLayout->addWidget(globalParam_,3,1);
135
136  globalParam_ = new GWt_globalParameters(dtmanage_);
137  sectorParam_ = new GWt_sectorParameters(dtmanage_);
138  WWidget* dboard = createDashBoard();
139  dboard->setMinimumSize(300,100);
140  mainGridLayout->addWidget(dboard,3,1);
141  //-----------
142
143  graphicsDialog_ = new GWt_dialog("graphical analysis",createDrawingWidget(),false);
144  graphicsDialog_->setMinimumSize(400,400);
145  graphicsDialog_->setClosable(true);
146  graphicsDialog_->hide();
147 
148  executeWidget_ = new GWt_softwarePanel(dtmanage_,this);
149  executeWidget_->setMinimumSize(400,100);
150  mainGridLayout->addWidget(executeWidget_,4,1);
151 
152  console_ = new GWt_console();
153  console_->setMinimumSize(300,100);
154  mainGridLayout->addWidget(console_,3,2,2,1);
155
156  // Let row 2 and column 1 take the excess space.
157  mainGridLayout->setRowStretch(4,1);
158  mainGridLayout->setColumnStretch(2,1);
159 
160  widroot->setLayout(mainGridLayout);
161  // set auto scrollbar if needed
162  widroot->setOverflow(WContainerWidget::OverflowAuto);
163}
164
165WWidget* PspaApplication::createDashBoard()
166{
167  WContainerWidget *wt= new WContainerWidget();
168  wt->setStyleClass("text");
169  WText *st= new WText("Go ahead...",wt); 
170
171  WContainerWidget *buttons = new WContainerWidget();
172  buttons->setStyleClass("buttons");
173  WPushButton *button;
174  button = new WPushButton("globalParameters",buttons);
175  button->clicked().connect(this, &PspaApplication::messageBox1);
176  button = new WPushButton("sectors", buttons);
177  button->clicked().connect(this, &PspaApplication::messageBox2);
178 
179  styleSheet().addRule(".buttons","padding: 5px;");
180  styleSheet().addRule(".text", "padding: 4px 8px");
181  styleSheet().addRule("body", "margin: 0px;");
182
183  WContainerWidget *cnt = new WContainerWidget();
184  cnt->addWidget(buttons);
185  cnt->addWidget(wt);
186
187  WContainerWidget *result = new WContainerWidget();
188  WPanel *panel = new WPanel(result);
189  panel->setTitle("dashboard");
190  panel->setCentralWidget(cnt);
191
192  globalParam_->setText(st);
193  sectorParam_->setText(st);
194  return result;
195}
196
197void PspaApplication::messageBox1()
198{
199  globalParam_->initilializeDialog();
200  return;
201}
202
203void PspaApplication::messageBox2()
204{
205  sectorParam_->execDialog();
206  return;
207}
208
209WWidget* PspaApplication::createPalette()
210{
211  WContainerWidget* palette = new WContainerWidget();
212  vector <GWt_abstractElement*> elems = abstractElementFactory_->getAllElements();
213  for (unsigned a = 0; a < elems.size(); a++) {
214    elems[a]->createDragImage(palette);
215    new WBreak(palette);
216  }
217  return palette; 
218}
219
220void PspaApplication::createDragImage(const char *url,const char *smallurl,const char *mimeType,WContainerWidget *p,WString())
221{
222  GWt_draggableImage *result = new GWt_draggableImage(url,p);
223 
224  /*
225   * Set the image to be draggable, showing the other image (dragImage)
226   * to be used as the widget that is visually dragged.
227   */
228  result->setDraggable(mimeType,new WImage(smallurl,p),true);
229}
230
231Wt::WWidget* PspaApplication::createBeamLine()
232{
233  extensionFile_= 0;
234  if (!beamLine_) {
235    beamLine_ = new GWt_LigneFaisceau(this);
236  } else {
237    beamLine_->clear();
238  }
239  beamLine_->setMinimumSize(300,100);
240  setTitle(applicationDefaultTitle_);
241
242  return beamLine_;
243}
244
245void PspaApplication::dialogOnGraphics()
246{
247  graphicsDialog_->show();
248}
249
250WContainerWidget* PspaApplication::createDrawingWidget()
251{
252  WGridLayout *glayout= new WGridLayout();
253  WContainerWidget* dessin = new WContainerWidget();
254  dessin->setLayout(glayout);
255
256  group_ = new Wt::WButtonGroup(this);
257 
258  /////////////////////////////////////////////////////////////////////
259       
260  choixEnveloppeDessin_ = new WComboBox();
261  choixEnveloppeDessin_->addItem("x");
262  // choixEnveloppeDessin_->addItem("y");
263  Wt::WRadioButton *button1= new WRadioButton(" enveloppe");
264  group_->addButton(button1,1);
265  glayout->addWidget(button1,0,1);
266  glayout->addWidget(choixEnveloppeDessin_,0,2);
267  /////////////////////////////////////////////////////////////////////
268
269  choixElementDessin_ = new WComboBox();
270  choixElementDessin_->setMinimumSize(50,10);
271  choixTypeDessinFaisceau_ = new WComboBox();
272  choixTypeDessinFaisceau_->addItem("courant_snyder");
273  choixTypeDessinFaisceau_->addItem("macroparticles");
274  Wt::WRadioButton *button2= new WRadioButton(" phase space after element");
275
276  choixAbsPhase_ = new WComboBox();
277  choixAbsPhase_->addItem("x");
278  choixAbsPhase_->addItem("y");
279  choixAbsPhase_->addItem("dz");
280  choixAbsPhase_->addItem("xp");
281  choixAbsPhase_->addItem("yp");
282  choixAbsPhase_->addItem("dE/E");
283  choixAbsPhase_->setCurrentIndex(0);
284 
285  choixOrdPhase_ = new WComboBox();
286  choixOrdPhase_->addItem("x");
287  choixOrdPhase_->addItem("y");
288  choixOrdPhase_->addItem("dz");
289  choixOrdPhase_->addItem("xp");
290  choixOrdPhase_->addItem("yp");
291  choixOrdPhase_->addItem("dE/E");
292  choixOrdPhase_->setCurrentIndex(3);
293 
294  group_->addButton(button2,2);
295  glayout->addWidget(button2,1,1);
296  glayout->addWidget(choixElementDessin_,1,2);
297  glayout->addWidget(choixTypeDessinFaisceau_,1,3);
298  //  glayout->addWidget(new WText("abs. :"),2,2);
299  glayout->addWidget(choixAbsPhase_,2,2);
300  //  glayout->addWidget(new WText("ord. :"),2,4);
301  glayout->addWidget(choixOrdPhase_,2,3);
302  /////////////////////////////////////////////////////////////////////
303
304  choixVariableHisto_= new WComboBox();
305  choixVariableHisto_->addItem("x");
306  choixVariableHisto_->addItem("y");
307  choixVariableHisto_->addItem("z");
308  choixVariableHisto_->addItem("xp");
309  choixVariableHisto_->addItem("yp");
310  choixVariableHisto_->addItem("dE/E");
311  choixVariableHisto_->setCurrentIndex(5);
312
313  choixHistoDessin_ = new WComboBox();
314  Wt::WRadioButton *button3= new WRadioButton(" histogram after element ");
315  group_->addButton(button3,3);
316
317  glayout->addWidget(button3,3,1);
318  glayout->addWidget(choixHistoDessin_,3,2);
319  glayout->addWidget(choixVariableHisto_,3,3);
320  /////////////////////////////////////////////////////////////////////
321 
322  WPushButton *close= new WPushButton("close"); 
323  glayout->addWidget(close,4,1);
324  close->clicked().connect(this,&PspaApplication::closeGraphicDialog);
325 
326  WPushButton *draw= new WPushButton("draw"); 
327  glayout->addWidget(draw,4,2,2,1);
328  draw->clicked().connect(this,&PspaApplication::dessiner);
329  /////////////////////////////////////////////////////////////////////
330
331  toto_ = new WContainerWidget();
332  dessin->addWidget(toto_);
333  return dessin;
334}
335
336void PspaApplication::closeGraphicDialog()
337{
338  cout << " close graphicsDialog_" << endl;
339  for(unsigned i = 0; i < eDialog_.size(); i++) eDialog_[i]->done(WDialog::Rejected);
340  eDialog_.clear();
341  graphicsDialog_->done(WDialog::Rejected);
342}
343
344void PspaApplication::dessiner()
345{
346  int k= group_->checkedId();
347  cout << "dessiner k= " << k << endl;
348
349  switch(k) {
350  case  1: 
351    dessinerEnveloppe();
352    break;
353  case 2:
354    dessinerPhaseSpace();
355    break;
356  case 3:
357    dessinerHistogramme();
358    break;
359  default:
360    GWt_dialog graphicalWarning("graphical analysis","select a button",GWt_dialog::Warning,false,true);
361    graphicalWarning.exec();
362    break;
363  }
364}
365
366
367void PspaApplication::sauver()
368{
369  cout << "***********************************" << endl;
370  cout << " on sauve " << endl<<endl;
371 
372  console_->addConsoleMessage("sauvegarde \n"); 
373  dialogSave_ = new WDialog("save");
374  new WText("name of case : ",dialogSave_->contents());
375  saveNameEdit_ = new WLineEdit(nameOfCase_.c_str(), dialogSave_->contents());
376  WPushButton *annule = new WPushButton("cancel",dialogSave_->contents());
377  WPushButton *submit = new WPushButton("OK",dialogSave_->contents());
378  annule->clicked().connect(dialogSave_, &Wt::WDialog::reject);
379  submit->clicked().connect(dialogSave_, &Wt::WDialog::accept);
380  dialogSave_->finished().connect(this, &PspaApplication::dialogSaveDone);
381  dialogSave_->show();
382}
383
384void PspaApplication::dialogSaveDone(WDialog::DialogCode code)
385{
386  if ( code != Wt::WDialog::Accepted ) {
387    return;
388  }
389  nameOfCase_ = saveNameEdit_->text().toUTF8();
390  delete dialogSave_;
391  dialogSave_ = NULL;
392
393  //globalParam_->updateGlobals();
394
395  //  dtmanage_->saveConfiguration(nameOfCase_);
396  dtmanage_->saveConfiguration(sessionId(),nameOfCase_);
397
398  // en test: j'ajoute la sauvegarde sur .aml
399  dtmanage_->writeToAMLFile(nameOfCase_);
400}
401
402void PspaApplication::restaurer()
403{
404  cout << "***********************************" << endl;
405  cout << " on restaure " << endl<<endl;
406
407  GWt_serverFileSelector * fs = new GWt_serverFileSelector("Select a configuration file",workingDir_);
408  string fileName = fs->exec();
409  nameOfCase_ = fileName;
410  if (fileName == "") {
411    return;
412  }
413   
414  removePathFromConfigName(nameOfCase_);
415  cout << " nom sans path " << nameOfCase_ << endl;
416  removeExtensionFromConfigName(nameOfCase_);
417  cout << " nom sans extension " << nameOfCase_ << endl;
418  if (nameOfCase_ == "") return;
419  cout << " PspaApplication::restaure le fichier   : " << fileName << endl;
420  dialogSave_ = NULL;
421 
422  bool test = dtmanage_->restoreElements(fileName);
423  if ( !test ) {
424    GWt_dialog restoreWarning(" element restoring", "failure in restoring elements from file : " + fileName , GWt_dialog::Error, false,true);
425    restoreWarning.exec();
426  } else {
427    // Change the window title
428    unsigned long found = fileName.find_last_of("/");
429    setTitle(applicationDefaultTitle_+" : "+fileName.substr(found+1));
430  }
431 
432  globalParam_->renew(); 
433  //sectorParam_->renew();
434  console_->addConsoleMessage(string("restauration terminee \n"));
435}
436
437void PspaApplication::openFileSelector()
438{
439  WContainerWidget *result = new WContainerWidget();
440  WVBoxLayout* myLayout = new WVBoxLayout(); 
441  uploadFileSelectorWidget_ = new WFileUpload();
442  uploadFileSelectorWidget_->setFileTextSize(40);
443  myLayout->addWidget(new WText("Select the configuration file for pspa : "));
444  myLayout->addWidget(uploadFileSelectorWidget_);
445  result->setLayout (myLayout);
446   
447  // Upload automatically when the user entered a file.
448  uploadFileSelectorWidget_->changed().connect(uploadFileSelectorWidget_, &WFileUpload::upload);
449 
450  // React to a succesfull upload.
451  uploadFileSelectorWidget_->uploaded().connect(this, &PspaApplication::chargerConfig);
452 
453  // React to a fileupload problem.
454  uploadFileSelectorWidget_->fileTooLarge().connect(this, &PspaApplication::fileTooLarge);
455 
456  GWt_dialog* fileSelectorDialog = new GWt_dialog("Load a file",result,false);
457  fileSelectorDialog->exec();
458}
459
460void PspaApplication::chargerConfig()
461{
462  GWt_dialog*  message= new GWt_dialog("File successfully upload","The file has been correctly upload to" + uploadFileSelectorWidget_->spoolFileName(),GWt_dialog::Warning,false,true);
463   
464  string nomDuFichier = (uploadFileSelectorWidget_->clientFileName()).toUTF8();
465  cout << " fichier client : " << nomDuFichier << endl;
466  bool test = removeExtensionFromConfigName(nomDuFichier);
467  cout << " fichier client sans extension : " << nomDuFichier << endl;
468 
469  if ( test ) {
470    nameOfCase_ = nomDuFichier;
471    console_->addConsoleMessage(string("restauration..."));
472     
473    if ( !dtmanage_->restoreElements(uploadFileSelectorWidget_->spoolFileName()) ) {
474      GWt_dialog restoreWarning(" element restoring", "failure in restoring elements !", GWt_dialog::Error, false,true);
475      restoreWarning.exec();
476    }
477     
478    globalParam_->renew(); 
479    //sectorParam_->renew();
480    console_->addConsoleMessage(string("rechargement de la 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  messageBox->show();
497}
498
499void PspaApplication::faireDessin()
500{
501  choixElementDessin_->clear();
502  choixHistoDessin_->clear();
503
504  int nombre = dtmanage_->getBeamLineSize(); 
505  for(int i = 1; i <= nombre; i++) {
506    abstractElement* elPtr = dtmanage_->getElementPointerFromNumero(i);
507    //    if(elPtr->getNomdElement().getElementType() == snapshot) continue;
508
509    choixElementDessin_->addItem(elPtr->getLabel());
510    choixHistoDessin_->addItem(elPtr->getLabel());
511  } 
512}
513
514void PspaApplication::dessinerPhaseSpace()
515{   
516   
517  int typeFaisceau = choixTypeDessinFaisceau_->currentIndex();
518  //  int index = choixElementDessin_->currentIndex();
519  string nameAbs = choixAbsPhase_->currentText().toUTF8();
520  string nameOrd = choixOrdPhase_->currentText().toUTF8();
521  if ( nameAbs == nameOrd ) {
522        GWt_dialog phaseSpaceError(" graphical analysis", "the beam coordinates must be different !", GWt_dialog::Error, false,true);
523        phaseSpaceError.exec();
524        return;
525  }
526  string elementLabel = choixElementDessin_->currentText().toUTF8();
527  particleBeam* beam = dtmanage_->getDiagnosticBeam(elementLabel);
528  //  particleBeam* beam = dtmanage_->getDiagnosticBeam(index);
529   
530  if ( beam == NULL ) {
531    GWt_dialog warningBeamState(" graphical analysis", "the beam does not exist at the end of this element !", GWt_dialog::Error, false,true);
532    warningBeamState.exec();
533    return;
534  }
535   
536    toto_->clear();
537    faireDessinPhaseSpace(toto_, beam, elementLabel, nameAbs, nameOrd, typeFaisceau);
538
539  // if ( typeFaisceau == 0 )
540  //   {
541  //     if ( !beam->momentRepresentationOk() ) beam->buildMomentRepresentation();
542  //     faireDessinTransport(toto_, beam, nameAbs, nameOrd );
543  //   }
544  // else if ( typeFaisceau == 1 )
545  //   {
546  //     if ( beam->particleRepresentationOk() ) faireDessinParmela(toto_, beam, nameAbs, nameOrd );
547  //     else {
548  //    GWt_dialog warningBeamState(" graphical analysis", "the beam state does not allow providing a drawing with macroparticles !", GWt_dialog::Error, false,true);
549  //    warningBeamState.exec();
550  //     }
551  //   }
552  // else {
553  //   GWt_dialog warningTypeDrawing(" graphical analysis", "type of  drawing not programmed !", GWt_dialog::Error, false,true);
554  //   warningTypeDrawing.exec();
555  // }
556}
557
558void PspaApplication::dessinerEnveloppe()
559{
560  toto_->clear();
561 
562  int typeEnveloppe = choixEnveloppeDessin_->currentIndex();
563  if ( typeEnveloppe == 0 )
564    {
565      faireDessinEnveloppe(toto_, "x");
566    }
567  else {
568    GWt_dialog warningTypeEnveloppe(" graphical analysis", "type of enveloppe drawing not programmed !", GWt_dialog::Error, false,true);
569    warningTypeEnveloppe.exec();
570  }
571}
572
573void PspaApplication::faireDessinEnveloppe(WContainerWidget* toto, string type)
574{
575 
576  std::time_t result = std::time(NULL);
577 
578  GWt_dialog* envDialog = new GWt_dialog(WString("enveloppe&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(")+std::asctime(std::localtime(&result))+")",toto,false);
579  eDialog_.push_back(envDialog);
580  envDialog->setMinimumSize(400,400);
581  envDialog->setClosable(true);
582  envDialog->show();
583     
584  //  unsigned nbel = dtmanage_->getBeamLineSize();
585  vector<double> xcor;
586  vector<double> ycor;
587  string titre;
588  string legendx;
589  string legendy;
590  dtmanage_->donneesRmsEnveloppe(type,xcor,ycor,titre,legendx,legendy);
591
592  Wt::Chart::SeriesType seriesType = LineSeries;
593  Wt::Chart::FillRangeType fillRange = NoFill;
594  Wt::Chart::AxisValue location = ZeroValue;
595  bool isGridLinesEnabled = false; 
596  chartPlot2vec(envDialog->contents(),xcor,ycor,seriesType,fillRange,location,isGridLinesEnabled,titre,legendx,legendy,500,300);
597}
598
599void PspaApplication::faireDessinPhaseSpace(WContainerWidget* toto, particleBeam* beam, string elementLabel, string namex, string namey, int typeFaisceau)
600{
601
602  vector<double> xcor;
603  vector<double> ycor;
604  vector<string> legende;
605  string titre;
606  Wt::Chart::SeriesType seriesType;
607 
608  if ( typeFaisceau == 0 ) {
609    seriesType = LineSeries;
610    titre = "phase space rms";
611    beam->donneesDessinEllipse(xcor,ycor,legende,namex,namey);
612  } else if (  typeFaisceau == 1 ) {
613    seriesType = PointSeries;
614    titre = " phase space ";
615    beam->particlesPhaseSpaceData(xcor,ycor,legende,namex,namey);
616    // for ( int kk=0; kk < xcor.size(); kk++)
617    //   {
618    //  cout << " dess. phas. x= " << xcor.at(kk) << " x'= " << ycor.at(kk) << endl;
619    //   }
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 < (int)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  //  cout << " PspaApplication::chartPlot2vec FLAG 0 " << endl;
882  WCartesianChart *chart = new WCartesianChart(toto);
883  if (!makeIcon) chart->setTitle(title);
884  chart->initLayout(); 
885  chart->setModel(model);        // set the model
886  chart->setXSeriesColumn(0);    // set the column that holds the X data
887 
888  chart->setType(ScatterPlot);   // set type to ScatterPlot
889  chart->setLegendEnabled(false); // enable the legend 
890
891  // Provide space for the X and Y axis and title.
892  chart->setPlotAreaPadding(80, Left); 
893  chart->setPlotAreaPadding(40, Bottom);
894  chart->setPlotAreaPadding(60, Top);
895 
896  // Typically, for mathematical functions, you want the axes to cross
897  // at the 0 mark (value= ZeroValue)
898  chart->axis(XAxis).setLocation(value);
899  chart->axis(YAxis).setLocation(value);
900 
901  //  cout << " PspaApplication::chartPlot2vec FLAG 1 " << endl;
902
903  WDataSeries s(1,seriesType);
904  s.setPen(WColor("red"));
905  s.setFillRange(fillRange);
906  chart->addSeries(s);
907 
908  WAxis& axis = chart->axis(XAxis);
909  axis.setLabelFormat("%.3f");
910  axis.setTitle(legendx);
911   
912  chart->axis(Y1Axis).setTitle(legendy);
913
914  if(isGridLinesEnables) {
915    axis.setGridLinesEnabled(true);
916    chart->axis(Y1Axis).setGridLinesEnabled(true);
917  }
918  //   cout << " PspaApplication::chartPlot2vec FLAG 2 " << endl;
919
920  chart->resize(width,height); // WPaintedWidget must be given explicit size
921  chart->setMargin(10, Top | Bottom); // ?
922  chart->setMargin(WLength::Auto, Left | Right); //?
923
924  if (makeIcon) {
925    chart->setLegendEnabled(false); // enable the legend
926    chart->setPlotAreaPadding(0);
927    chart->setAxisPadding(0);
928    WFont xAxisFont = chart->axis(XAxis).labelFont();
929    xAxisFont.setSize(8);
930    WFont yAxisFont = chart->axis(YAxis).labelFont();
931    yAxisFont.setSize(8);
932    chart->axis(XAxis).setLabelFont(xAxisFont);
933    chart->axis(YAxis).setLabelFont(yAxisFont);
934  }
935  //  cout << " PspaApplication::chartPlot2vec FLAG 3 " << endl;
936
937#ifdef HAS_IMAGEMAGIC
938
939  //  cout << " PspaApplication::chartPlot2vec FLAG MAGIC " << endl;
940
941  // no background
942  chart->setBackground(WBrush(Wt::NoBrush));
943
944  // axis black
945  chart->axis(XAxis).setPen(WColor("black"));
946  chart->axis(YAxis).setPen(WColor("black"));
947   
948  std::string name;
949  /*
950 Wt::WRasterImage pngImage("png", 1000, 1000);
951  Wt::WPainter p(&pngImage);
952  chart->paint(p);
953  name = workingDir_ + "/chart-"+sessionId ()+".png";
954  std::ofstream f(name.c_str(), std::ios::out |std::ios::trunc | std::ios::binary);
955  pngImage.write(f);
956  */
957
958  Wt::WPdfImage pdfImage("1000", "600");
959    {
960   Wt::WPainter p1(&pdfImage);
961    chart->paint(p1);
962      }
963
964  name = workingDir_ + "/chart-"+sessionId ()+".pdf";
965  std::ofstream f1(name.c_str(), std::ios::out | std::ios::binary);
966  pdfImage.write(f1);
967
968  new WText("<a href='workingArea/chart-"+sessionId ()+".pdf' target='_blank'>Sauvegarder l'image</a>",toto);
969
970#endif
971  //  cout << " PspaApplication::chartPlot2vec FLAG FIN" << endl;
972
973}
974
975void PspaApplication::dessinerHistogramme()
976{
977  toto_->clear();
978 
979  // std::time_t result = std::time(NULL);
980  // GWt_dialog* histoDialog = new GWt_dialog(WString("histogramme&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(")+std::asctime(std::localtime(&result))+")",toto_,false);
981  // eDialog_.push_back(histoDialog);
982  // histoDialog->setMinimumSize(400,400);
983  // histoDialog->setClosable(true);
984
985  string elementLabel = choixHistoDessin_->currentText().toUTF8();   
986  particleBeam* beam = dtmanage_->getDiagnosticBeam(elementLabel);
987  if (beam == NULL) {
988    GWt_dialog warningBeamState("histogram","the beam does not exist at the end of this element !", GWt_dialog::Error, false,true);
989    warningBeamState.exec();
990    return;
991  }
992 
993  if (!beam->particleRepresentationOk()) {
994    GWt_dialog warningBeamState("histogram", "the beam state does not allow providing a drawing with macroparticles !", GWt_dialog::Error, false,true);
995    warningBeamState.exec();
996    return;
997  }
998
999  unsigned iabs= choixVariableHisto_->currentIndex(); 
1000  vector<double> xcor;
1001  vector<int> hist;
1002  //  double out[3]= {0.0};
1003  vector<string> legende;
1004  beam->histogramme(iabs,xcor,hist,legende);
1005
1006  cout<<"xcor.size()= "<<xcor.size()<<", hist.size()= "<<hist.size()<<endl;
1007  //////////////////////////////////////////////////////////////////////////////////
1008 
1009  int n = hist.size();
1010  vector<double> x;
1011  x.resize(2*n+2);
1012  vector<double> y;
1013  y.resize(2*n+2);
1014
1015  int j= 0;
1016  x.at(j)= xcor[0];
1017  y.at(j)= 0.0;
1018  j++;
1019  x.at(j)= xcor[0];
1020  y.at(j)= hist[0];
1021  j++;
1022 
1023  for (int i = 1; i < n; ++i) {
1024    x.at(j)= xcor[i];
1025    y.at(j)= hist[i-1];
1026    j++;
1027    x.at(j)= xcor[i];
1028    y.at(j)= hist[i];
1029    j++;
1030  }
1031
1032  x.at(j)= xcor[n];
1033  y.at(j)= hist[n-1];
1034  j++;
1035  x.at(j)= xcor[n];
1036  y.at(j)= 0.0;
1037  //////////////////////////////////////////////////////////////////////////////////
1038  std::time_t result = std::time(NULL);
1039  GWt_dialog* histoDialog = new GWt_dialog(WString("histogramme&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(")+std::asctime(std::localtime(&result))+")",toto_,false);
1040  eDialog_.push_back(histoDialog);
1041  histoDialog->setMinimumSize(400,400);
1042  histoDialog->setClosable(true);
1043
1044  histoDialog->show();
1045
1046  WContainerWidget *w= histoDialog->contents();
1047
1048  // // legendes
1049  // string unites[2];
1050  // if(iabs == 0 || iabs == 1 || iabs == 2) {
1051  //   unites[0]= unites[1]= " mm";
1052  // }
1053  // if(iabs == 3 || iabs == 4) {
1054  //   unites[0]= unites[1]= " mrad";
1055  // }
1056  // if(iabs == 5) {
1057  //   unites[0]= " MeV"; unites[1]= " KeV";
1058  // }
1059
1060  new WText(" case : " + nameOfCase_, w);
1061  new WBreak(w);
1062  new WText(" after element :  " + elementLabel, w);
1063
1064  for (int k=0 ; k < (int)legende.size(); k++) {
1065    new WBreak(w);
1066    new WText(legende.at(k), w);
1067  }
1068
1069
1070  // new WText(" entries : "+ mixedTools::intToString((int)out[0]),w);
1071  // new WBreak(w);
1072  // new WText(" mean : "+ mixedTools::doubleToString(out[1])+unites[0],w);
1073  // new WBreak(w);
1074  // new WText(" sigma rms : "+ mixedTools::doubleToString(out[2])+unites[1],w);
1075  //////////////////////////////////////////////////////////////////////////////////
1076
1077  string titre;
1078  if(iabs == 0) titre="x-coordinate";
1079  if(iabs == 1) titre="y-coordinate";
1080  if(iabs == 2) titre="z-coordinate";
1081  if(iabs == 3) titre="xp-divergence";
1082  if(iabs == 4) titre="yp-devergence";
1083  if(iabs == 5) titre="kinetic energy";
1084
1085  string xname;
1086  if(iabs == 0) xname=" x (mm)";
1087  if(iabs == 1) xname=" y (mm)";
1088  if(iabs == 2) xname=" z (mm)";
1089  if(iabs == 3) xname=" xp (mrad)";
1090  if(iabs == 4) xname=" yp (mrad)";
1091  if(iabs == 5) xname=" dEcin/Ecin (%)";
1092
1093  string yname;
1094  Wt::Chart::SeriesType seriesType = LineSeries;
1095  Wt::Chart::FillRangeType fillRange = ZeroValueFill;
1096  Wt::Chart::AxisValue location = MinimumValue;
1097  bool isGridLinesEnabled = true; 
1098  chartPlot2vec(w,x,y,seriesType,fillRange,location,isGridLinesEnabled,titre,xname,yname,500,300);
1099}
1100
1101
Note: See TracBrowser for help on using the repository browser.