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

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

Affichage du nom du cas test dans le titre de la fenetre + see History

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