source: PSPA/Interface_Web/trunk/pspaWT/src/GWt_pspaApplication.cc @ 218

Last change on this file since 218 was 218, checked in by garnier, 12 years ago

ajout de charts : sans test avec pdf

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