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

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

Début de stockage de façon plus générique des parametres (ne change rien pour le moment)

File size: 29.9 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    /////////////////////////////////////////////////////////////////////
282    dessin->addWidget(new WText(" histogramme :  "));
283    WPushButton* okHisto = new WPushButton("histogramme");
284    dessin->addWidget(okHisto);
285
286    okHisto->clicked().connect(this,&PspaApplication::dessinerHistogramme);
287    /////////////////////////////////////////////////////////////////////
288   
289    toto_ = new WContainerWidget();
290    dessin->addWidget(toto_);
291    return dessin;
292}
293
294
295
296
297void PspaApplication::sauver()
298{
299    cout << " on sauve " << endl;
300    console_->addConsoleMessage("sauvegarde");
301   
302    dialogSave_ = new WDialog("save");
303    new WText("name of case : ",dialogSave_->contents());
304    saveNameEdit_ = new WLineEdit(nameOfCase_.c_str(), dialogSave_->contents());
305    WPushButton *annule = new WPushButton("cancel",dialogSave_->contents());
306    WPushButton *submit = new WPushButton("OK",dialogSave_->contents());
307    annule->clicked().connect(dialogSave_, &Wt::WDialog::reject);
308    submit->clicked().connect(dialogSave_, &Wt::WDialog::accept);
309    dialogSave_->finished().connect(this, &PspaApplication::dialogSaveDone);
310    dialogSave_->show();
311}
312
313void PspaApplication::dialogSaveDone(WDialog::DialogCode code)
314{
315   
316    if ( code != Wt::WDialog::Accepted ) { console_->addConsoleMessage(" pas de sauvegarde"); return;}
317    else { console_->addConsoleMessage("sauvegarde sur repertoire : " + workingDir_);}
318    nameOfCase_ = saveNameEdit_->text().toUTF8();
319    cout << " PspaApplication::dialogSaveDone() nameOfCase_= " << nameOfCase_ << endl;
320    delete dialogSave_;
321    dialogSave_ = NULL;
322    GWt_globalParameters* bibi = static_cast<GWt_globalParameters*>(globalParam_);
323    bibi->updateGlobals();
324    //  dtmanage_->saveConfiguration(nameOfCase_);
325    dtmanage_->saveConfiguration( nameOfCase_);
326}
327
328
329
330void PspaApplication::restaurer()
331{
332    console_->addConsoleMessage(string("on va recharger..."));
333    dialogOpen_ = new WDialog("open");
334    new WText("name of case : ",dialogOpen_->contents());
335    openNameEdit_ = new WLineEdit(nameOfCase_.c_str(), dialogOpen_->contents());
336    WPushButton *annule = new WPushButton("cancel",dialogOpen_->contents());
337    WPushButton *submit = new WPushButton("OK",dialogOpen_->contents());
338    annule->clicked().connect(dialogOpen_, &Wt::WDialog::reject);
339    submit->clicked().connect(dialogOpen_, &Wt::WDialog::accept);
340    dialogOpen_->finished().connect(this, &PspaApplication::dialogOpenDone);
341    dialogOpen_->show();
342}
343
344void PspaApplication::dialogOpenDone(WDialog::DialogCode code)
345{
346   
347    if ( code != Wt::WDialog::Accepted ) { console_->addConsoleMessage(" pas de restauration"); return;}
348    else { console_->addConsoleMessage("restauration depuis le repertoire " + workingDir_ );}
349   
350    nameOfCase_ = openNameEdit_->text().toUTF8();
351    cout << " PspaApplication::dialogOpenDone() nameOfCase_= " << nameOfCase_ << endl;
352    delete dialogOpen_;
353    dialogSave_ = NULL;
354   
355    bool test = dtmanage_->restoreElements(nameOfCase_);
356    if ( !test ) {
357        GWt_dialog restoreWarning(" element restoring", "failure in restoring elements !", GWt_dialog::Error, false,true);
358        restoreWarning.exec();
359    }
360   
361    GWt_globalParameters* bibi = static_cast<GWt_globalParameters*>(globalParam_);
362    bibi->renew();
363       
364    console_->addConsoleMessage(string("...terminee"));
365}
366
367
368void PspaApplication::openFileSelector()
369{
370   
371    WContainerWidget *result = new WContainerWidget();
372    WVBoxLayout* myLayout = new WVBoxLayout();
373   
374    uploadFileSelectorWidget_ = new WFileUpload();
375   
376    uploadFileSelectorWidget_->setFileTextSize(40);
377   
378    myLayout->addWidget(new WText("Select the configuration file for pspa : "));
379    myLayout->addWidget(uploadFileSelectorWidget_);
380   
381    result->setLayout (myLayout);
382   
383    // Upload automatically when the user entered a file.
384    uploadFileSelectorWidget_->changed().connect(uploadFileSelectorWidget_, &WFileUpload::upload);
385   
386    // React to a succesfull upload.
387    uploadFileSelectorWidget_->uploaded().connect(this, &PspaApplication::chargerConfig);
388   
389    // React to a fileupload problem.
390    uploadFileSelectorWidget_->fileTooLarge().connect(this, &PspaApplication::fileTooLarge);
391   
392   
393    GWt_dialog* fileSelectorDialog = new GWt_dialog("Load a file",result,false);
394   
395    fileSelectorDialog->exec();
396}
397
398
399void PspaApplication::chargerConfig()
400{
401    GWt_dialog*  message= new GWt_dialog("File successfully upload","The file has been correctly upload to" + uploadFileSelectorWidget_->spoolFileName(),GWt_dialog::Warning,false,true);
402   
403    string nomDuFichier = (uploadFileSelectorWidget_->clientFileName()).toUTF8();
404    cout << " fichier client : " << nomDuFichier << endl;
405    bool test = removeExtensionFromConfigName(nomDuFichier);
406    cout << " fichier client sans extension : " << nomDuFichier << endl;
407   
408    if ( test )
409    {
410        nameOfCase_ = nomDuFichier;
411        console_->addConsoleMessage(string("restauration..."));
412       
413        if ( !dtmanage_->restoreElements(uploadFileSelectorWidget_->spoolFileName()) ) {
414            GWt_dialog restoreWarning(" element restoring", "failure in restoring elements !", GWt_dialog::Error, false,true);
415            restoreWarning.exec();
416        }
417       
418        GWt_globalParameters* bibi = static_cast<GWt_globalParameters*>(globalParam_);
419        bibi->renew();
420               
421        console_->addConsoleMessage(string("...terminee"));
422        message->show();
423    }
424}
425
426
427void PspaApplication::fileTooLarge()
428{
429    std::stringstream stream;
430    stream << maximumRequestSize ();
431    std::string maxRequestSize(stream.str());
432   
433    std::string message = "This file is too large, please select a one\n";
434    message += "Maximum file size is "+ maxRequestSize;
435    message += " bytes\n";
436   
437    GWt_dialog*  messageBox= new GWt_dialog("Error during upload file" ,message ,GWt_dialog::Error,false,true);
438   
439    messageBox->show();
440}
441
442
443void PspaApplication::faireDessin()
444{
445    choixElementDessin_->clear();
446    int nombre = dtmanage_->getBeamLineSize();
447    cout << " PspaApplication::faireDessin() nb elements : " << nombre << endl;
448
449    for ( int numero =1; numero <= nombre; numero++)
450    {
451        choixElementDessin_->addItem(dtmanage_->getLabelFromElementNumero(numero));
452    }
453   
454}
455
456void PspaApplication::dessiner()
457{
458   
459    toto_->clear();
460   
461    int typeFaisceau = choixTypeDessinFaisceau_->currentIndex();
462   
463    int index = choixElementDessin_->currentIndex();
464   
465   
466    particleBeam* beam = dtmanage_->getDiagnosticBeam(index);
467   
468    if ( beam == NULL ) {
469        GWt_dialog warningBeamState(" graphical analysis", "the beam does not exist at the end of this element !", GWt_dialog::Error, false,true);
470        warningBeamState.exec();
471        return;
472    }
473   
474   
475    if ( typeFaisceau == 0 )
476    {
477        if ( !beam->momentRepresentationOk() ) beam->buildMomentRepresentation();
478        faireDessinTransport(toto_, beam);
479    }
480    else if ( typeFaisceau == 1 )
481    {
482        if ( beam->particleRepresentationOk() ) faireDessinParmela(toto_, beam);
483        else {
484            GWt_dialog warningBeamState(" graphical analysis", "the beam state does not allow providing a drawing with macroparticles !", GWt_dialog::Error, false,true);
485            warningBeamState.exec();
486        }
487    }
488    else {
489        GWt_dialog warningTypeDrawing(" graphical analysis", "type of  drawing not programmed !", GWt_dialog::Error, false,true);
490        warningTypeDrawing.exec();
491    }
492    //////////////////////////////////////////
493}
494
495void PspaApplication::dessinerEnveloppe()
496{
497   
498    toto_->clear();
499   
500    int typeEnveloppe = choixEnveloppeDessin_->currentIndex();
501    if ( typeEnveloppe == 0 )
502    {
503        faireDessinEnveloppe(toto_, "x");
504    }
505    else {
506        GWt_dialog warningTypeEnveloppe(" graphical analysis", "type of enveloppe drawing not programmed !", GWt_dialog::Error, false,true);
507        warningTypeEnveloppe.exec();
508    }
509    //////////////////////////////////////////
510}
511
512void PspaApplication::faireDessinEnveloppe(WContainerWidget* toto, string type)
513{
514   
515    GWt_dialog* envDialog = new GWt_dialog("enveloppe",toto_,false);
516    envDialog->setMinimumSize(400,400);
517    envDialog->setClosable(true);
518    envDialog->show();
519   
520   
521   
522    //       new WText(nameOfCase_ + " : enveloppe", toto);
523    unsigned nbel = dtmanage_->getBeamLineSize();
524   
525   
526    vector<double> xcor;
527    vector<double> ycor;
528   
529   
530    dtmanage_->donneesRmsEnveloppe(type,1, nbel, xcor,ycor);
531   
532    scatterPlot1D(envDialog->contents(),xcor,ycor);
533   
534}
535
536void PspaApplication::faireDessinParmela(WContainerWidget* toto, particleBeam* beam)
537{
538   
539    GWt_dialog* pointsDialog = new GWt_dialog("space phase",toto,false);
540    pointsDialog->setMinimumSize(400,400);
541    pointsDialog->setClosable(true);
542    // pointsDialog->show();
543   
544   
545   
546    vector<bareParticle>& partic = beam->getParticleVector();
547    new WText(nameOfCase_ + " : espace de phase x,x' "+ " nb partic. "+ mixedTools::intToString(partic.size()), pointsDialog->contents());
548    WStandardItemModel *model = new WStandardItemModel(partic.size(), 3, pointsDialog->contents());
549    //    model->setHeaderData(0, WString("X"));
550    //   model->setHeaderData(1, WString("Y = sin(X)"));
551   
552   
553    for (unsigned i = 0; i < partic.size(); ++i) {
554        double x= partic.at(i).getPosition().getComponent(0);
555        double begamz = partic.at(i).getBetaGamma().getComponent(2);
556        double xp;
557        if ( begamz != 0.0) {
558          xp = partic.at(i).getBetaGamma().getComponent(0)/begamz;
559        } else {
560          xp = 0.0;
561        }
562        //    cout << "x = " << x << " xp= " << xp << endl;
563        model->setData(i, 0, x);
564        model->setData(i, 1,1.e3*xp);
565        model->setData(i, 2,2.e3*xp);
566    }
567   
568    WCartesianChart *chart = new WCartesianChart(pointsDialog->contents());
569    chart->setModel(model);        // set the model
570    chart->setXSeriesColumn(0);    // set the column that holds the X data
571    chart->setLegendEnabled(true); // enable the legend
572   
573    chart->setType(ScatterPlot);   // set type to ScatterPlot
574   
575    // Typically, for mathematical functions, you want the axes to cross
576    // at the 0 mark:
577    chart->axis(XAxis).setLocation(ZeroValue);
578    chart->axis(YAxis).setLocation(ZeroValue);
579   
580    // Provide space for the X and Y axis and title.
581    chart->setPlotAreaPadding(80, Left);
582    chart->setPlotAreaPadding(40, Top | Bottom);
583    // Add the curves
584    WDataSeries s(1, PointSeries, Y1Axis);
585    //  s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
586    //    s.setMarker(SquareMarker);
587    //  s.setMarkerSize(600.);
588    //  cout << "le marker est : " << s.marker() << endl;
589    chart->addSeries(s);
590   
591    chart->resize(400, 400); // WPaintedWidget must be given explicit size
592   
593   
594#ifdef HAS_IMAGEMAGIC
595    Wt::WRasterImage pngImage("png", 600, 600);
596    Wt::WPainter p(&pngImage);
597    chart->paint(p);
598    std::string name;
599    name = workingDir_ + "/chart-"+sessionId ()+".png";
600    std::ofstream f(name.c_str(), std::ios::out |std::ios::trunc | std::ios::binary);
601    pngImage.write(f);
602    new WText("<a href='workingArea/chart-"+sessionId ()+".png' target='_blank'>Afficher l'image</a>",toto);
603#endif
604   
605    pointsDialog->show();
606   
607    //  chart->setMargin(10, Top | Bottom);            // add margin vertically
608    //  chart->setMargin(WLength::Auto, Left | Right); // center horizontally
609}
610
611
612// void PspaApplication::addElemToGlobals()
613// {
614//   static_cast<GWt_globalParameters*>(globalParam_)->addElem();
615// }
616
617void PspaApplication::faireDessinTransport(WContainerWidget* toto, particleBeam* beam)
618{
619    //  WContainerWidget* toto = static_cast<WContainerWidget*>(leDessin_);
620    //  toto->clear();
621   
622    //  new WText(nameOfCase_ + " : espace de phase x,x' "+ " nb partic. "+ mixedTools::intToString(partic.size()), toto);
623   
624    GWt_dialog* ellipseDialog = new GWt_dialog("ellipse",toto,false);
625    ellipseDialog->setMinimumSize(400,400);
626    ellipseDialog->setClosable(true);
627    ellipseDialog->show();
628   
629   
630    new WText(nameOfCase_ + " : emittance x,x' " + mixedTools::doubleToString(beam->getUnnormalizedEmittanceX()) + " pi.mm.mrad" , ellipseDialog->contents());
631    new WBreak(ellipseDialog->contents());
632    new WText(" xmax = " + mixedTools::doubleToString(beam->getSigmaTransportij(1,1)) + " cm ", ellipseDialog->contents());
633    new WBreak(ellipseDialog->contents());
634    new WText(" x' max = " + mixedTools::doubleToString(beam->getSigmaTransportij(2,2)) + " mrad ", ellipseDialog->contents());
635    new WBreak(ellipseDialog->contents());
636    new WText(" corr. 12 = " + mixedTools::doubleToString(beam->getSigmaTransportij(2,1))  + " sqrt(cm.rad) ", ellipseDialog->contents());
637   
638    vector<double> xcor;
639    vector<double> ycor;
640    //  dtmanage_->getCurrentBeam()->donneesDessinEllipseXxp(xcor,ycor);
641    beam->donneesDessinEllipseXxp(xcor,ycor);
642    scatterPlot1D(ellipseDialog->contents(),xcor,ycor);
643   
644}
645
646// void PspaApplication::scatterPlot1D(WContainerWidget* toto, vector<double>& xcor, vector<double>& ycor)
647// {
648//   int nbpts = xcor.size();
649//   cout << " PspaApplication::scatterPlot1D nbpts = " << nbpts << endl;
650//   WStandardItemModel *model = new WStandardItemModel(nbpts, 2, toto);
651//   for (int i = 0; i < nbpts; ++i) {
652//     model->setData(i, 0, xcor.at(i));
653//     model->setData(i, 1, ycor.at(i));
654//     //    cout << " PspaApplication::scatterPlot1D el= " << i+1 << " x= " << xcor.at(i) << " y= " << ycor.at(i) << endl;
655//   }
656
657//   WCartesianChart *chart = new WCartesianChart(toto);
658
659//   chart->initLayout();
660
661//   chart->setModel(model);        // set the model
662//   chart->setXSeriesColumn(0);    // set the column that holds the X data
663//   chart->setLegendEnabled(true); // enable the legend
664
665//   chart->setType(ScatterPlot);   // set type to ScatterPlot
666
667//   // Typically, for mathematical functions, you want the axes to cross
668//   // at the 0 mark:
669//    chart->axis(XAxis).setLocation(ZeroValue);
670//    chart->axis(YAxis).setLocation(ZeroValue);
671
672//   // Provide space for the X and Y axis and title.
673// //   chart->setPlotAreaPadding(80, Left);
674// //   chart->setPlotAreaPadding(40, Top | Bottom);
675//   // Add the curves
676//   WDataSeries s(1, CurveSeries, Y1Axis);
677//   chart->addSeries(s);
678
679//   // chart->setBackground (WBrush(WColor("blue")));
680//   chart->resize(300, 300); // WPaintedWidget must be given explicit size
681
682// #ifdef HAS_IMAGEMAGIC
683//  Wt::WRasterImage pngImage("png", 600, 600);
684//  Wt::WPainter p(&pngImage);
685
686//  chart->paint(p);
687//  std::string name;
688//  name = workingDir_ + "/chart-"+sessionId ()+".png";
689//  std::ofstream f(name.c_str(), std::ios::out |std::ios::trunc | std::ios::binary);
690//  pngImage.write(f);
691
692//  new WText("<a href='workingArea/chart-"+sessionId ()+".png' target='_blank'>Afficher l'image</a>",toto);
693
694//  /*
695//  Wt::WPdfImage pdfImage("30cm", "30cm");
696//  Wt::WPainter p1(&pdfImage);
697//  chart->paint(p1);
698//  std::ofstream f1("chart.pdf", std::ios::out | std::ios::binary);
699//  pdfImage.write(f1);
700//  */
701// #endif
702// }
703
704
705void PspaApplication::scatterPlot1D(WContainerWidget* toto, vector<double>& xcor, vector<double>& ycor)
706{
707    int nbpts = xcor.size();
708    cout << " PspaApplication::scatterPlot1D nbpts = " << nbpts << endl;
709    WStandardItemModel *model = new WStandardItemModel(nbpts, 2, toto);
710    for (int i = 0; i < nbpts; ++i) {
711        model->setData(i, 0, xcor.at(i));
712        model->setData(i, 1, ycor.at(i));
713        //    cout << " PspaApplication::scatterPlot1D el= " << i+1 << " x= " << xcor.at(i) << " y= " << ycor.at(i) << endl;
714    }
715   
716    WCartesianChart *chart = new WCartesianChart(toto);
717    chart->setTitle("titre");
718   
719    //  chart->initLayout();
720   
721    chart->setModel(model);        // set the model
722    chart->setXSeriesColumn(0);    // set the column that holds the X data
723    chart->setLegendEnabled(true); // enable the legend
724   
725    chart->setType(ScatterPlot);   // set type to ScatterPlot
726   
727    // Typically, for mathematical functions, you want the axes to cross
728    // at the 0 mark:
729    chart->axis(XAxis).setLocation(ZeroValue);
730    chart->axis(YAxis).setLocation(ZeroValue);
731   
732    // Provide space for the X and Y axis and title.
733    //   chart->setPlotAreaPadding(80, Left);
734    //   chart->setPlotAreaPadding(40, Top | Bottom);
735    // Add the curves
736    WDataSeries s(1, LineSeries, Y1Axis);
737    chart->addSeries(s);
738   
739    // chart->setBackground (WBrush(WColor("blue")));
740    chart->resize(500, 300); // WPaintedWidget must be given explicit size
741   
742#ifdef HAS_IMAGEMAGIC
743    Wt::WRasterImage pngImage("png", 600, 600);
744    Wt::WPainter p(&pngImage);
745   
746    chart->paint(p);
747    std::string name;
748    name = workingDir_ + "/chart-"+sessionId ()+".png";
749    std::ofstream f(name.c_str(), std::ios::out |std::ios::trunc | std::ios::binary);
750    pngImage.write(f);
751   
752    new WText("<a href='workingArea/chart-"+sessionId ()+".png' target='_blank'>Afficher l'image</a>",toto);
753   
754    /*
755     Wt::WPdfImage pdfImage("30cm", "30cm");
756     Wt::WPainter p1(&pdfImage);
757     chart->paint(p1);
758     std::ofstream f1("chart.pdf", std::ios::out | std::ios::binary);
759     pdfImage.write(f1);
760     */
761#endif
762}
763
764
765
766WText* PspaApplication::createTitle(const WString& title)
767{
768    WText *result = new WText(title);
769    result->setInline(false);
770    result->setStyleClass("title");
771    result->setMinimumSize(30,30);
772   
773    return result;
774}
775
776// void PspaApplication::addConsoleMessage(WString msg) {
777//   WText *w = new WText(console_);
778//   w->setTextFormat(PlainText);
779//   w->setText(msg);
780//   w->setInline(false);
781
782//   /*
783//    * Little javascript trick to make sure we scroll along with new content
784//    */
785//   WApplication *app = WApplication::instance();
786//   app->doJavaScript(console_->jsRef() + ".scrollTop += "
787//                     + console_->jsRef() + ".scrollHeight;");
788
789// }
790
791
792bool PspaApplication::removeExtensionFromConfigName(string& config)
793{
794    //  string configName;
795    string extension(".save");
796    bool test = true;
797    string::size_type nn = config.rfind('.');
798    if ( nn == string::npos )
799    {
800        // pas de point
801        test = false;
802    }
803    string fin = config.substr(nn);
804    if ( fin != extension )
805    {
806        // l'extension n'est pas la bonne
807        test = false;
808    }
809    if ( test )
810    {
811        string::size_type count = config.length() - extension.length();
812        config = config.substr(0, count);
813    }
814    else
815    {
816        GWt_dialog checkConfigNameDialog(" checking config file name", " the file must have the extension " + extension, GWt_dialog::Error,true,true);
817        checkConfigNameDialog.exec();
818    }
819    return test;
820}
821
822/////////////////////////
823//   c'est un embryon d'histogramme
824
825void PspaApplication::dessinerHistogramme()
826{
827    // pour mon test : histo en energie
828    toto_->clear();
829   
830    GWt_dialog* pourVoir= new GWt_dialog("histogramme",toto_,false);
831    pourVoir->setMinimumSize(400,400);
832    pourVoir->setClosable(true);
833    pourVoir->show();
834   
835   
836    particleBeam* beam = dtmanage_->getDiagnosticBeam(1);
837    vector<double> xcor;
838    vector<int> hist;
839    int cnts;
840    beam->histogramme(xcor,hist,cnts);
841   
842    cout<<"xcor.size()= "<<xcor.size()<<", hist.size()= "<<hist.size()<<endl;
843   
844    //////////////////////////////////////////////////////////////////////////////////
845   
846    WContainerWidget *w = toto_;
847    int nbpts= xcor.size()+cnts+1;
848    WStandardItemModel *model = new WStandardItemModel(nbpts,2,w);
849    model->setHeaderData(1, WString("energie"));
850   
851    int j= 0;
852    if(hist[0] > 0) {
853        model->setData(j,0,xcor[0]);
854        model->setData(j,1,hist[0]);
855        j++;
856    }
857    model->setData(j,0,xcor[0]);
858    model->setData(j,1,hist[0]);
859    j++;
860    for (int i = 1; i < nbpts; ++i) {
861        if(hist[i] == 0) {
862            if(hist[i-1] > 0) {
863                model->setData(j,0,xcor[i]);
864                model->setData(j,1,hist[i-1]);
865                j++;
866            }
867            model->setData(j,0,xcor[i]);
868            model->setData(j,1,hist[i]);
869            j++;
870        }
871        if(hist[i] > 0) {
872            model->setData(j,0,xcor[i]);
873            model->setData(j,1,hist[i-1]);
874            j++;
875            model->setData(j,0,xcor[i]);
876            model->setData(j,1,hist[i]);
877            j++;
878        }
879    }
880   
881    WCartesianChart *chart = new WCartesianChart(w);
882    chart->setTitle("Histogramme");
883    chart->setModel(model);        // set the model
884    chart->setXSeriesColumn(0);    // set the column that holds the categories
885    chart->setLegendEnabled(true); // enable the legend
886   
887    // Provide space for the X and Y axis and title.
888    chart->setPlotAreaPadding(200, Right);
889    chart->setPlotAreaPadding(80, Left);
890    chart->setPlotAreaPadding(40, Top | Bottom);
891   
892    chart->setType(ScatterPlot);
893   
894    WAxis& axis = chart->axis(XAxis);
895    axis.setLabelFormat("%.3f");
896    axis.setGridLinesEnabled(true);
897    chart->axis(Y1Axis).setGridLinesEnabled(true);
898   
899    WDataSeries s(1, LineSeries);
900    chart->addSeries(s);
901   
902   
903    chart->resize(800, 400);
904   
905    chart->setMargin(10, Top | Bottom);
906    chart->setMargin(WLength::Auto, Left | Right);
907   
908    LegendLocation location = LegendOutside;
909    Side side = Right;
910    AlignmentFlag alignment = AlignMiddle;
911    chart->setLegendLocation(location, side, alignment);
912    chart->setLegendStyle(chart->legendFont(), WPen(black), WBrush(WColor(0xFF, 0xFA, 0xE5)));
913}
Note: See TracBrowser for help on using the repository browser.