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

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

réaménagement dans dessin histo

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