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

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

Suppression de la classe "elementsCollection" et bug fixed #20

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