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

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

Modifications en vue de changer l'interface utilisateur

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