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

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

systeme periodique (mailles) + multipoles + madx

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