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

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

test....

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