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

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

Bug #28 fixed

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    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        console_->addConsoleMessage(string("...terminee"));
423        message->show();
424    }
425}
426
427
428void PspaApplication::fileTooLarge()
429{
430    std::stringstream stream;
431    stream << maximumRequestSize ();
432    std::string maxRequestSize(stream.str());
433   
434    std::string message = "This file is too large, please select a one\n";
435    message += "Maximum file size is "+ maxRequestSize;
436    message += " bytes\n";
437   
438    GWt_dialog*  messageBox= new GWt_dialog("Error during upload file" ,message ,GWt_dialog::Error,false,true);
439   
440    messageBox->show();
441}
442
443
444void PspaApplication::faireDessin()
445{
446    choixElementDessin_->clear();
447    int nombre = dtmanage_->getBeamLineSize();
448    for ( int numero =1; numero <= nombre; numero++)
449    {
450        choixElementDessin_->addItem(dtmanage_->getLabelFromElementNumero(numero));
451    }
452   
453}
454
455void PspaApplication::dessiner()
456{
457   
458    toto_->clear();
459   
460    int typeFaisceau = choixTypeDessinFaisceau_->currentIndex();
461   
462    int index = choixElementDessin_->currentIndex();
463   
464   
465    particleBeam* beam = dtmanage_->getDiagnosticBeam(index);
466   
467    if ( beam == NULL ) {
468        GWt_dialog warningBeamState(" graphical analysis", "the beam does not exist at the end of this element !", GWt_dialog::Error, false,true);
469        warningBeamState.exec();
470        return;
471    }
472   
473   
474    if ( typeFaisceau == 0 )
475    {
476        if ( !beam->momentRepresentationOk() ) beam->buildMomentRepresentation();
477        faireDessinTransport(toto_, beam);
478    }
479    else if ( typeFaisceau == 1 )
480    {
481        if ( beam->particleRepresentationOk() ) faireDessinParmela(toto_, beam);
482        else {
483            GWt_dialog warningBeamState(" graphical analysis", "the beam state does not allow providing a drawing with macroparticles !", GWt_dialog::Error, false,true);
484            warningBeamState.exec();
485        }
486    }
487    else {
488        GWt_dialog warningTypeDrawing(" graphical analysis", "type of  drawing not programmed !", GWt_dialog::Error, false,true);
489        warningTypeDrawing.exec();
490    }
491    //////////////////////////////////////////
492}
493
494void PspaApplication::dessinerEnveloppe()
495{
496   
497    toto_->clear();
498   
499    int typeEnveloppe = choixEnveloppeDessin_->currentIndex();
500    if ( typeEnveloppe == 0 )
501    {
502        faireDessinEnveloppe(toto_, "x");
503    }
504    else {
505        GWt_dialog warningTypeEnveloppe(" graphical analysis", "type of enveloppe drawing not programmed !", GWt_dialog::Error, false,true);
506        warningTypeEnveloppe.exec();
507    }
508    //////////////////////////////////////////
509}
510
511void PspaApplication::faireDessinEnveloppe(WContainerWidget* toto, string type)
512{
513   
514    GWt_dialog* envDialog = new GWt_dialog("enveloppe",toto_,false);
515    envDialog->setMinimumSize(400,400);
516    envDialog->setClosable(true);
517    envDialog->show();
518   
519   
520   
521    //       new WText(nameOfCase_ + " : enveloppe", toto);
522    unsigned nbel = dtmanage_->getBeamLineSize();
523   
524   
525    vector<double> xcor;
526    vector<double> ycor;
527   
528   
529    dtmanage_->donneesRmsEnveloppe(type,1, nbel, xcor,ycor);
530   
531    scatterPlot1D(envDialog->contents(),xcor,ycor);
532   
533}
534
535void PspaApplication::faireDessinParmela(WContainerWidget* toto, particleBeam* beam)
536{
537   
538    GWt_dialog* pointsDialog = new GWt_dialog("space phase",toto,false);
539    pointsDialog->setMinimumSize(400,400);
540    pointsDialog->setClosable(true);
541    // pointsDialog->show();
542   
543   
544   
545    vector<bareParticle>& partic = beam->getParticleVector();
546    new WText(nameOfCase_ + " : espace de phase x,x' "+ " nb partic. "+ mixedTools::intToString(partic.size()), pointsDialog->contents());
547    WStandardItemModel *model = new WStandardItemModel(partic.size(), 3, pointsDialog->contents());
548    //    model->setHeaderData(0, WString("X"));
549    //   model->setHeaderData(1, WString("Y = sin(X)"));
550   
551   
552    for (unsigned i = 0; i < partic.size(); ++i) {
553        double x= partic.at(i).getPosition().getComponent(0);
554        double begamz = partic.at(i).getBetaGamma().getComponent(2);
555        double xp = partic.at(i).getBetaGamma().getComponent(0)/begamz;
556        //    cout << "x = " << x << " xp= " << xp << endl;
557        model->setData(i, 0, x);
558        model->setData(i, 1,1.e3*xp);
559        model->setData(i, 2,2.e3*xp);
560    }
561   
562    WCartesianChart *chart = new WCartesianChart(pointsDialog->contents());
563    chart->setModel(model);        // set the model
564    chart->setXSeriesColumn(0);    // set the column that holds the X data
565    chart->setLegendEnabled(true); // enable the legend
566   
567    chart->setType(ScatterPlot);   // set type to ScatterPlot
568   
569    // Typically, for mathematical functions, you want the axes to cross
570    // at the 0 mark:
571    chart->axis(XAxis).setLocation(ZeroValue);
572    chart->axis(YAxis).setLocation(ZeroValue);
573   
574    // Provide space for the X and Y axis and title.
575    chart->setPlotAreaPadding(80, Left);
576    chart->setPlotAreaPadding(40, Top | Bottom);
577    // Add the curves
578    WDataSeries s(1, PointSeries, Y1Axis);
579    //  s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
580    //    s.setMarker(SquareMarker);
581    //  s.setMarkerSize(600.);
582    //  cout << "le marker est : " << s.marker() << endl;
583    chart->addSeries(s);
584   
585    chart->resize(400, 400); // WPaintedWidget must be given explicit size
586   
587   
588#ifdef HAS_IMAGEMAGIC
589    Wt::WRasterImage pngImage("png", 600, 600);
590    Wt::WPainter p(&pngImage);
591    chart->paint(p);
592    std::string name;
593    name = workingDir_ + "/chart-"+sessionId ()+".png";
594    std::ofstream f(name.c_str(), std::ios::out |std::ios::trunc | std::ios::binary);
595    pngImage.write(f);
596    new WText("<a href='workingArea/chart-"+sessionId ()+".png' target='_blank'>Afficher l'image</a>",toto);
597#endif
598   
599    pointsDialog->show();
600   
601    //  chart->setMargin(10, Top | Bottom);            // add margin vertically
602    //  chart->setMargin(WLength::Auto, Left | Right); // center horizontally
603}
604
605
606// void PspaApplication::addElemToGlobals()
607// {
608//   static_cast<GWt_globalParameters*>(globalParam_)->addElem();
609// }
610
611void PspaApplication::faireDessinTransport(WContainerWidget* toto, particleBeam* beam)
612{
613    //  WContainerWidget* toto = static_cast<WContainerWidget*>(leDessin_);
614    //  toto->clear();
615   
616    //  new WText(nameOfCase_ + " : espace de phase x,x' "+ " nb partic. "+ mixedTools::intToString(partic.size()), toto);
617   
618    GWt_dialog* ellipseDialog = new GWt_dialog("ellipse",toto,false);
619    ellipseDialog->setMinimumSize(400,400);
620    ellipseDialog->setClosable(true);
621    ellipseDialog->show();
622   
623   
624    new WText(nameOfCase_ + " : emittance x,x' " + mixedTools::doubleToString(beam->getUnnormalizedEmittanceX()) + " pi.mm.mrad" , ellipseDialog->contents());
625    new WBreak(ellipseDialog->contents());
626    new WText(" xmax = " + mixedTools::doubleToString(beam->getSigmaTransportij(1,1)) + " cm ", ellipseDialog->contents());
627    new WBreak(ellipseDialog->contents());
628    new WText(" x' max = " + mixedTools::doubleToString(beam->getSigmaTransportij(2,2)) + " mrad ", ellipseDialog->contents());
629    new WBreak(ellipseDialog->contents());
630    new WText(" corr. 12 = " + mixedTools::doubleToString(beam->getSigmaTransportij(2,1))  + " sqrt(cm.rad) ", ellipseDialog->contents());
631   
632    vector<double> xcor;
633    vector<double> ycor;
634    //  dtmanage_->getCurrentBeam()->donneesDessinEllipseXxp(xcor,ycor);
635    beam->donneesDessinEllipseXxp(xcor,ycor);
636    scatterPlot1D(ellipseDialog->contents(),xcor,ycor);
637   
638}
639
640// void PspaApplication::scatterPlot1D(WContainerWidget* toto, vector<double>& xcor, vector<double>& ycor)
641// {
642//   int nbpts = xcor.size();
643//   cout << " PspaApplication::scatterPlot1D nbpts = " << nbpts << endl;
644//   WStandardItemModel *model = new WStandardItemModel(nbpts, 2, toto);
645//   for (int i = 0; i < nbpts; ++i) {
646//     model->setData(i, 0, xcor.at(i));
647//     model->setData(i, 1, ycor.at(i));
648//     //    cout << " PspaApplication::scatterPlot1D el= " << i+1 << " x= " << xcor.at(i) << " y= " << ycor.at(i) << endl;
649//   }
650
651//   WCartesianChart *chart = new WCartesianChart(toto);
652
653//   chart->initLayout();
654
655//   chart->setModel(model);        // set the model
656//   chart->setXSeriesColumn(0);    // set the column that holds the X data
657//   chart->setLegendEnabled(true); // enable the legend
658
659//   chart->setType(ScatterPlot);   // set type to ScatterPlot
660
661//   // Typically, for mathematical functions, you want the axes to cross
662//   // at the 0 mark:
663//    chart->axis(XAxis).setLocation(ZeroValue);
664//    chart->axis(YAxis).setLocation(ZeroValue);
665
666//   // Provide space for the X and Y axis and title.
667// //   chart->setPlotAreaPadding(80, Left);
668// //   chart->setPlotAreaPadding(40, Top | Bottom);
669//   // Add the curves
670//   WDataSeries s(1, CurveSeries, Y1Axis);
671//   chart->addSeries(s);
672
673//   // chart->setBackground (WBrush(WColor("blue")));
674//   chart->resize(300, 300); // WPaintedWidget must be given explicit size
675
676// #ifdef HAS_IMAGEMAGIC
677//  Wt::WRasterImage pngImage("png", 600, 600);
678//  Wt::WPainter p(&pngImage);
679
680//  chart->paint(p);
681//  std::string name;
682//  name = workingDir_ + "/chart-"+sessionId ()+".png";
683//  std::ofstream f(name.c_str(), std::ios::out |std::ios::trunc | std::ios::binary);
684//  pngImage.write(f);
685
686//  new WText("<a href='workingArea/chart-"+sessionId ()+".png' target='_blank'>Afficher l'image</a>",toto);
687
688//  /*
689//  Wt::WPdfImage pdfImage("30cm", "30cm");
690//  Wt::WPainter p1(&pdfImage);
691//  chart->paint(p1);
692//  std::ofstream f1("chart.pdf", std::ios::out | std::ios::binary);
693//  pdfImage.write(f1);
694//  */
695// #endif
696// }
697
698
699void PspaApplication::scatterPlot1D(WContainerWidget* toto, vector<double>& xcor, vector<double>& ycor)
700{
701    int nbpts = xcor.size();
702    cout << " PspaApplication::scatterPlot1D nbpts = " << nbpts << endl;
703    WStandardItemModel *model = new WStandardItemModel(nbpts, 2, toto);
704    for (int i = 0; i < nbpts; ++i) {
705        model->setData(i, 0, xcor.at(i));
706        model->setData(i, 1, ycor.at(i));
707        //    cout << " PspaApplication::scatterPlot1D el= " << i+1 << " x= " << xcor.at(i) << " y= " << ycor.at(i) << endl;
708    }
709   
710    WCartesianChart *chart = new WCartesianChart(toto);
711    chart->setTitle("titre");
712   
713    //  chart->initLayout();
714   
715    chart->setModel(model);        // set the model
716    chart->setXSeriesColumn(0);    // set the column that holds the X data
717    chart->setLegendEnabled(true); // enable the legend
718   
719    chart->setType(ScatterPlot);   // set type to ScatterPlot
720   
721    // Typically, for mathematical functions, you want the axes to cross
722    // at the 0 mark:
723    chart->axis(XAxis).setLocation(ZeroValue);
724    chart->axis(YAxis).setLocation(ZeroValue);
725   
726    // Provide space for the X and Y axis and title.
727    //   chart->setPlotAreaPadding(80, Left);
728    //   chart->setPlotAreaPadding(40, Top | Bottom);
729    // Add the curves
730    WDataSeries s(1, LineSeries, Y1Axis);
731    chart->addSeries(s);
732   
733    // chart->setBackground (WBrush(WColor("blue")));
734    chart->resize(500, 300); // WPaintedWidget must be given explicit size
735   
736#ifdef HAS_IMAGEMAGIC
737    Wt::WRasterImage pngImage("png", 600, 600);
738    Wt::WPainter p(&pngImage);
739   
740    chart->paint(p);
741    std::string name;
742    name = workingDir_ + "/chart-"+sessionId ()+".png";
743    std::ofstream f(name.c_str(), std::ios::out |std::ios::trunc | std::ios::binary);
744    pngImage.write(f);
745   
746    new WText("<a href='workingArea/chart-"+sessionId ()+".png' target='_blank'>Afficher l'image</a>",toto);
747   
748    /*
749     Wt::WPdfImage pdfImage("30cm", "30cm");
750     Wt::WPainter p1(&pdfImage);
751     chart->paint(p1);
752     std::ofstream f1("chart.pdf", std::ios::out | std::ios::binary);
753     pdfImage.write(f1);
754     */
755#endif
756}
757
758
759
760WText* PspaApplication::createTitle(const WString& title)
761{
762    WText *result = new WText(title);
763    result->setInline(false);
764    result->setStyleClass("title");
765    result->setMinimumSize(30,30);
766   
767    return result;
768}
769
770// void PspaApplication::addConsoleMessage(WString msg) {
771//   WText *w = new WText(console_);
772//   w->setTextFormat(PlainText);
773//   w->setText(msg);
774//   w->setInline(false);
775
776//   /*
777//    * Little javascript trick to make sure we scroll along with new content
778//    */
779//   WApplication *app = WApplication::instance();
780//   app->doJavaScript(console_->jsRef() + ".scrollTop += "
781//                     + console_->jsRef() + ".scrollHeight;");
782
783// }
784
785
786bool PspaApplication::removeExtensionFromConfigName(string& config)
787{
788    //  string configName;
789    string extension(".save");
790    bool test = true;
791    string::size_type nn = config.rfind('.');
792    if ( nn == string::npos )
793    {
794        // pas de point
795        test = false;
796    }
797    string fin = config.substr(nn);
798    if ( fin != extension )
799    {
800        // l'extension n'est pas la bonne
801        test = false;
802    }
803    if ( test )
804    {
805        string::size_type count = config.length() - extension.length();
806        config = config.substr(0, count);
807    }
808    else
809    {
810        GWt_dialog checkConfigNameDialog(" checking config file name", " the file must have the extension " + extension, GWt_dialog::Error,true,true);
811        checkConfigNameDialog.exec();
812    }
813    return test;
814}
815
816/////////////////////////
817//   c'est un embryon d'histogramme
818
819void PspaApplication::dessinerHistogramme()
820{
821    // pour mon test : histo en energie
822    toto_->clear();
823   
824    GWt_dialog* pourVoir= new GWt_dialog("histogramme",toto_,false);
825    pourVoir->setMinimumSize(400,400);
826    pourVoir->setClosable(true);
827    pourVoir->show();
828   
829   
830    particleBeam* beam = dtmanage_->getDiagnosticBeam(1);
831    vector<double> xcor;
832    vector<int> hist;
833    int cnts;
834    beam->histogramme(xcor,hist,cnts);
835   
836    cout<<"xcor.size()= "<<xcor.size()<<", hist.size()= "<<hist.size()<<endl;
837   
838    //////////////////////////////////////////////////////////////////////////////////
839   
840    WContainerWidget *w = toto_;
841    int nbpts= xcor.size()+cnts+1;
842    WStandardItemModel *model = new WStandardItemModel(nbpts,2,w);
843    model->setHeaderData(1, WString("energie"));
844   
845    int j= 0;
846    if(hist[0] > 0) {
847        model->setData(j,0,xcor[0]);
848        model->setData(j,1,hist[0]);
849        j++;
850    }
851    model->setData(j,0,xcor[0]);
852    model->setData(j,1,hist[0]);
853    j++;
854    for (int i = 1; i < nbpts; ++i) {
855        if(hist[i] == 0) {
856            if(hist[i-1] > 0) {
857                model->setData(j,0,xcor[i]);
858                model->setData(j,1,hist[i-1]);
859                j++;
860            }
861            model->setData(j,0,xcor[i]);
862            model->setData(j,1,hist[i]);
863            j++;
864        }
865        if(hist[i] > 0) {
866            model->setData(j,0,xcor[i]);
867            model->setData(j,1,hist[i-1]);
868            j++;
869            model->setData(j,0,xcor[i]);
870            model->setData(j,1,hist[i]);
871            j++;
872        }
873    }
874   
875    WCartesianChart *chart = new WCartesianChart(w);
876    chart->setTitle("Histogramme");
877    chart->setModel(model);        // set the model
878    chart->setXSeriesColumn(0);    // set the column that holds the categories
879    chart->setLegendEnabled(true); // enable the legend
880   
881    // Provide space for the X and Y axis and title.
882    chart->setPlotAreaPadding(200, Right);
883    chart->setPlotAreaPadding(80, Left);
884    chart->setPlotAreaPadding(40, Top | Bottom);
885   
886    chart->setType(ScatterPlot);
887   
888    WAxis& axis = chart->axis(XAxis);
889    axis.setLabelFormat("%.3f");
890    axis.setGridLinesEnabled(true);
891    chart->axis(Y1Axis).setGridLinesEnabled(true);
892   
893    WDataSeries s(1, LineSeries);
894    chart->addSeries(s);
895   
896   
897    chart->resize(800, 400);
898   
899    chart->setMargin(10, Top | Bottom);
900    chart->setMargin(WLength::Auto, Left | Right);
901   
902    LegendLocation location = LegendOutside;
903    Side side = Right;
904    AlignmentFlag alignment = AlignMiddle;
905    chart->setLegendLocation(location, side, alignment);
906    chart->setLegendStyle(chart->legendFont(), WPen(black), WBrush(WColor(0xFF, 0xFA, 0xE5)));
907}
Note: See TracBrowser for help on using the repository browser.