]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/pPlotter/pPlotterWindow.cxx
76ffc6a7b13b891006e46d79afb5273d8901358a
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / pPlotter / pPlotterWindow.cxx
1 //----------------------------------------------------------------------------
2 // Class definition include
3 //----------------------------------------------------------------------------
4 #include "pPlotterWindow.h"
5 #include "MaximumsDialog.h"
6 #include <iostream>
7 #include <fstream>
8 #include <string>
9
10 // ----------------------------------------------------------------------------
11 // WX headers inclusion.
12 // For compilers that support precompilation, includes <wx/wx.h>.
13 // ----------------------------------------------------------------------------
14
15 #ifndef WX_PRECOMP
16 #include <wx/wx.h>
17 #endif
18
19 #include "wx/colordlg.h"
20 //----------------------------------------------------------------------------
21 //DECLARING THE NEW EVENT
22 //wxEVT_TSBAR= event of the two side bar
23 //----------------------------------------------------------------------------
24 BEGIN_DECLARE_EVENT_TYPES()
25 DECLARE_EVENT_TYPE(wxEVT_PPLOTTER, -1)
26 DECLARE_EVENT_TYPE(wxEVT_PPLOTTER_CHANGED_FUNCTION, -1)
27 DECLARE_EVENT_TYPE(wxEVT_PPLOTTER_POINT_MOVE, -1)
28 DECLARE_EVENT_TYPE(wxEVT_PPLOTTER_POINT_ADD, -1)
29 DECLARE_EVENT_TYPE(wxEVT_PPLOTTER_POINT_DELETE, -1)
30 DECLARE_EVENT_TYPE(wxEVT_PPLOTTER_GUIDELINES, -1)
31 END_DECLARE_EVENT_TYPES()
32
33 //definitions
34 DEFINE_EVENT_TYPE(wxEVT_PPLOTTER)
35 DEFINE_EVENT_TYPE(wxEVT_PPLOTTER_CHANGED_FUNCTION)
36 DEFINE_EVENT_TYPE(wxEVT_PPLOTTER_POINT_MOVE)
37 DEFINE_EVENT_TYPE(wxEVT_PPLOTTER_POINT_ADD)
38 DEFINE_EVENT_TYPE(wxEVT_PPLOTTER_POINT_DELETE)
39 DEFINE_EVENT_TYPE(wxEVT_PPLOTTER_GUIDELINES)
40
41
42 //----------------------------------------------------------------------------
43 // Class implementation
44 //----------------------------------------------------------------------------
45
46 IMPLEMENT_CLASS(pPlotterWindow, mpWindow)
47
48 //----------------------------------------------------------------------------
49 // Event Table
50 //----------------------------------------------------------------------------
51
52 BEGIN_EVENT_TABLE(pPlotterWindow,mpWindow)
53 EVT_MOTION (pPlotterWindow::onMouseMove)
54 EVT_LEFT_DCLICK(pPlotterWindow::onChangeActual)
55 EVT_LEFT_DOWN(pPlotterWindow::onAddPoint)
56 EVT_MENU(pwID_DELPOINT, pPlotterWindow::onDeletePoint)
57 EVT_MENU(pwID_SHOWPOINTS, pPlotterWindow::onShowPoints)
58 EVT_MENU(pwID_STARTDRAWING, pPlotterWindow::onStartDrawing)
59 //EVT_MENU(pwID_DELFUNCTION,pPlotterWindow::onDeleteFunction)
60 EVT_MENU(pwID_STOPDRAWING, pPlotterWindow::onStopDrawing)
61 EVT_MENU(pwID_ADDNEWPOINT, pPlotterWindow::onAddNewPoint)
62 EVT_MENU(pwID_NOSHOWPOINTS, pPlotterWindow::onNoShowPoints)
63 EVT_MENU(pwID_MYZOOMOUT,pPlotterWindow::onMyZoomOut)
64 EVT_MENU(pwID_MYZOOMIN,pPlotterWindow::onMyZoomIn)
65 EVT_MENU(pwID_SPLINE,pPlotterWindow::onSplinePoints)
66 EVT_MENU(pwID_LINE,pPlotterWindow::onLine)
67 EVT_MENU(pwID_SAVE,pPlotterWindow::onSave)
68 EVT_MENU(pwID_LOAD,pPlotterWindow::onLoad)
69 EVT_MENU(pwID_CHANGECOLOR,pPlotterWindow::onChangeColor)
70 EVT_MENU( mpID_LINE_GUIDES, pPlotterWindow::OnGuideLines)
71 EVT_MENU( pwID_TRASLATEACTUALFUNCTION, pPlotterWindow::onMoveFunction)
72 EVT_LEFT_UP(pPlotterWindow::onLeftUp)
73 END_EVENT_TABLE()
74
75 //----------------------------------------------------------------------------
76 // Constructors
77 //----------------------------------------------------------------------------
78
79 pPlotterWindow::pPlotterWindow( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int flag )
80 : mpWindow( parent, id, pos, size, flag)
81 {
82         drawing=false;
83         movingPointIndex=-1;
84         actual=NULL;
85         scaleWay=DEFAULT_SCALE;
86         zoomFactor=1;
87         //drawGuideLines=true;
88         type=DEFAULT_PLOTTER;
89         drawGuides=false;
90         movingFunction=false;
91         initialMovingCLick=-1;
92         movingPoints=false;
93         
94         /*
95         PopMenu options
96         */
97         m_popmenu.Remove( mpID_ZOOM_IN);
98         m_popmenu.Remove( mpID_ZOOM_OUT);
99         m_popmenu.Remove( mpID_CENTER);
100         m_popmenu.Remove( mpID_FIT);
101         m_popmenu.Remove( mpID_LOCKASPECT);
102
103         m_popmenu.Append(pwID_STARTDRAWING ,   _("Start Drawing"),    _("Starts to draw a new function"));//it changes the actual function
104         m_popmenu.Append(pwID_STOPDRAWING ,   _("Stop drawing"),    _("Stops of drawing the new function"));
105         m_popmenu.Append(pwID_LOAD,   _("Load"),    _("Load the actual function from a plane file"));
106         m_popmenu.Append(pwID_SHOWPOINTS ,   _("Show Points"),    _("shows the discrete points of the function"));
107         m_popmenu.Append(pwID_NOSHOWPOINTS,  _("No show Points"),    _("Dont show the points of the function"));
108         m_popmenu.Append(pwID_ADDNEWPOINT,   _("Add point"),    _("Add New point to the actual function"));
109         m_popmenu.Append(pwID_DELPOINT ,   _("Delete point"),    _("Deletes a point from the actual function."));
110         m_popmenu.Append(pwID_MYZOOMIN,   _("Zoom in"),    _("Zoom in the points of the function"));
111         m_popmenu.Append(pwID_MYZOOMOUT,   _("Zoom out"),    _("Zoom out the points of the function"));
112         m_popmenu.Append(pwID_SPLINE,   _("Smooth"),    _("Splines the points ofthe actual curve"));
113         m_popmenu.Append(pwID_LINE,   _("Line"),    _("Piecewise the points ofthe actual curve"));      
114         m_popmenu.Append(pwID_SAVE,   _("Save"),    _("Save the actual function in plane file"));       
115         m_popmenu.Append(pwID_CHANGECOLOR,   _("Change Color"),    _("Change the color of the function"));              
116         m_popmenu.Append(pwID_TRASLATEACTUALFUNCTION, _("Start Moving Function"),  _("Move the actual Function"));
117         
118         m_popmenu.Enable(pwID_NOSHOWPOINTS,false);
119         m_popmenu.Enable(pwID_ADDNEWPOINT,false);
120         m_popmenu.Enable(pwID_SHOWPOINTS,false);
121         m_popmenu.Enable(pwID_DELPOINT,false);
122         m_popmenu.Enable(pwID_STOPDRAWING,false);
123         m_popmenu.Enable(pwID_MYZOOMIN,false);
124         m_popmenu.Enable(pwID_MYZOOMOUT,false);
125         m_popmenu.Enable(pwID_SPLINE,false);
126         m_popmenu.Enable(pwID_LINE,false);
127         m_popmenu.Enable(pwID_SAVE,false);
128         m_popmenu.Enable(pwID_LOAD,true);
129         m_popmenu.Enable(pwID_CHANGECOLOR,false);
130         m_popmenu.Enable(pwID_STARTDRAWING,true);
131         m_popmenu.Enable(pwID_LOAD,true);
132         //m_popmenu.Enable(pwID_STARTDRAWING,false);
133         m_popmenu.Enable(mpID_LINE_GUIDES,false);
134         m_popmenu.Enable(pwID_TRASLATEACTUALFUNCTION,false);
135         
136          
137 }
138
139
140 pPlotterWindow::~pPlotterWindow()
141 {
142 }
143 //----------------------------------------------------------------------------
144 // Methods
145 //----------------------------------------------------------------------------
146
147 /**
148 * Sets the actual function
149 */
150 void  pPlotterWindow::setActualFunction(pGraphicalFunction* newActual)
151 {
152         if(actual!=NULL)
153                 actual->setActual(false);
154         newActual->setActual(true);
155         actual=newActual;
156 }
157
158 /**
159 * Gets the actual function
160 */
161 pGraphicalFunction* pPlotterWindow:: getActualFunction()
162 {
163         return actual;
164 }
165
166 /**
167 * Transform the point clicked  from window
168 * to the real value,given by the scale
169 * Pre: m_scaleX and m_scaleY are already actualized
170 * with setActualScales() method
171 */
172 wxPoint pPlotterWindow:: getRealPoint(wxPoint pixelPoint)
173 {
174         int x,y;        
175         x=pixelPoint.x-70;
176         y=pixelPoint.y-40;                      
177         //real point on the function
178         wxPoint realPoint;
179         int xS=(x-offsetPixelX)/m_scaleX + offsetX + minScrX;
180         int yS=(y-offsetPixelY)/m_scaleY + offsetY + minScrY;
181         
182         if(xS<=maxScrX && yS<=maxScrY )
183         {
184                 //setActualScales();
185                 realPoint.x= xS;
186                 realPoint.y= yS;                
187         }
188         else
189         {
190                 realPoint.x= -1;
191                 realPoint.y= -1;                
192         }
193         return realPoint;
194 }
195 /*
196  Set the scales according to the size of the window
197  and the maximum given by the user
198 */
199
200 void pPlotterWindow::setActualScales()
201 {
202         // int maxX,maxY,minX,minY; // JPRx
203         int maxX,maxY;//,minX,minY; // JPRx  //EED
204         /*
205         if the function is drawed interactively the maxX and the 
206         maxY in actual are sets by the user
207         */
208         /*
209         maxX=actual->getMaxX();
210         maxY=actual->getMaxY();
211
212         int dx=maxX;
213         int dy=maxY;
214         
215         */
216         int dx=actual->getMaxXShowed()-actual->getMinXShowed();
217         int dy=actual->getMaxYShowed()-actual->getMinYShowed();
218         
219         if(dx!=0 && dy!=0)
220         {
221                 m_scaleX= (((float)m_scrX-100)/dx)*zoomFactor;
222                 m_scaleY= (((float)m_scrY-50)/dy)*zoomFactor;
223         }
224         //setting the maximums and minimums showed in the window
225         /*
226         setMaxScrX(maxX);
227         setMaxScrY(maxY);
228         */
229         setMaxScrX(actual->getMaxXShowed());
230         setMinScrX(actual->getMinXShowed());
231         setMaxScrY(actual->getMaxYShowed());
232         setMinScrY(actual->getMinYShowed());
233         actual->setScreens(m_scrX,m_scrY);
234         actual->setScales();
235 }
236 //-------------------------
237 // Methods for interaction
238 //-------------------------
239
240         //------------------------------------
241         //      Manipulating clicks on the plotter
242         //-------------------------------------
243         
244
245         /*
246                 Manipulating clicks on the plotter
247         */
248         void pPlotterWindow::onLeftUp(wxMouseEvent& event)
249         {
250                 movingPoints=false;
251                 movingPointIndex=-1;
252         }
253
254
255
256         //-------------------------------
257         //Traslating the actual function
258         //-------------------------------
259         /*
260          Move the actual function  when the user
261          is moving the mouse
262         */
263         void pPlotterWindow::onMoveFunction(wxCommandEvent& aEvent)
264         {
265                 if(movingFunction)
266                 {
267                         movingFunction=false;
268                         m_popmenu.SetLabel(pwID_TRASLATEACTUALFUNCTION, _("Start Moving Function"));
269                 }
270                 else
271                 {
272                         movingFunction=true;
273                         m_popmenu.SetLabel(pwID_TRASLATEACTUALFUNCTION, _("Stop Moving Function"));
274                 }
275         }
276
277 //-----------------------
278 // Color Function
279 //-----------------------
280
281
282         /**
283         * Change the color of the function
284         */
285 void pPlotterWindow::onChangeColor(wxCommandEvent& aEvent)
286         {
287                         
288                 wxColour colour;
289                 wxColourData data;
290                 wxColourDialog dialog(this, &data);
291
292                 if ( dialog.ShowModal() == wxID_OK )
293                 {
294                         colour = dialog.GetColourData().GetColour();
295                 }
296         
297                 wxPen mypen(colour);
298                 mypen.SetWidth(2);
299                 actual->SetPen( mypen);
300                 UpdateAll();
301         }
302
303
304 //----------------------
305 //Persistence
306 //----------------------
307         
308
309         /**
310         * Load the function from a .txt file
311         */
312         void pPlotterWindow::onLoad(wxCommandEvent& aEvent)
313         {
314                 
315                 actual=new pGraphicalFunction(wxT("f(x) = function1(x)"),mpALIGN_NE);
316                 actual->setActual(true);
317                 actual->setFromWindow(false);
318                 actual->SetDrawing(drawing);
319                 actual->setFactorZoom(zoomFactor);
320                 
321                 wxPen mypen(*wxRED, 5, wxSOLID);
322                 mypen.SetWidth(2);
323                 actual->SetPen( mypen);
324                                 
325                 wxString nameF=wxFileSelector(_T("Save Function"));
326                 actual->load(nameF);
327                 AddLayer(actual);
328                 
329                 functions.Append(actual);
330                 addFunctionToMove(actual);
331                 
332                 m_popmenu.Enable(pwID_STARTDRAWING,true);
333                 
334                 UpdateAll();
335
336                 //------------------------------------------
337                 //Sending the paarticular event of changed actual function
338                 //-------------------------------------------
339                 wxCommandEvent cevent( wxEVT_PPLOTTER_CHANGED_FUNCTION, GetId() );
340                 cevent.SetEventObject(this);
341         GetEventHandler()->ProcessEvent( cevent );
342         }
343
344
345         /**
346         * Save the points of the function in a .txt file
347         */
348         void pPlotterWindow::onSave(wxCommandEvent& aEvent)
349         {
350                 wxString nameF=wxFileSelector(_T("Save Function"));
351                 actual->save(nameF);
352         }
353
354 //-----------------------
355 // Spline
356 //----------------------
357 /**
358 * Spline the points of the actual curve
359 */
360
361 void pPlotterWindow::onSplinePoints(wxCommandEvent& aEvent)
362 {
363         actual->setType(2);
364         actual->initializeSplines();
365         actual->initializeSplineVectors();
366         UpdateAll();
367         m_popmenu.Enable(pwID_SPLINE,false);
368         m_popmenu.Enable(pwID_LINE,true);
369 }
370
371 /**
372 * Change the type of drawing of the actual function
373 * to a piecewise function
374 */
375 void pPlotterWindow::onLine(wxCommandEvent& aEvent)
376 {
377         actual->clearSplineVectors();
378         actual->setType(1);
379         UpdateAll();
380         m_popmenu.Enable(pwID_SPLINE,true);
381         m_popmenu.Enable(pwID_LINE,false);
382
383 }
384
385 //--------------------------
386 // Zooming
387 //---------------------------
388
389 /**
390 * Zoom out the functions
391 */
392 void pPlotterWindow::onMyZoomOut(wxCommandEvent& aEvent)
393 {
394         wxNode* node=functions.GetFirst();
395         zoomFactor=zoomFactor*(float)(1/(float)ZOOM_FACTOR);
396         while(node)
397         {
398                 pGraphicalFunction* f= (pGraphicalFunction*)node->GetData();
399                 
400                 f->setScreenX(m_scrX);
401                 f->setScreenY(m_scrY);
402
403                 
404                 if(zoomFactor>1.01)
405                 {       
406                         f->setOffsets(m_clickedX-70,m_scrY-m_clickedY-40);
407                         f->setFactorZoom(zoomFactor);
408                 }
409                 
410                 else
411                 {
412                         f->setOffsetPixelX(0);
413                         f->setOffsetPixelY(0);
414                         f->setOffsetX(0);
415                         f->setOffsetY(0);
416                         f->setFactorZoom(zoomFactor);
417                 }
418                 
419
420                 if(f->getActual())
421                 {
422                         //TODO: send a event of resize
423                         //setMinScrX(f->getMinXShowed());
424                         //setMinScrY(f->getMinYShowed());
425                         setOffsetPixelX(f->getOffsetPixelsX());
426                         setOffsetPixelY(f->getOffsetPixelsY());
427                         setOffsetX(f->getOffsetX());
428                         setOffsetY(f->getOffsetY());
429                 }
430                 node=node->GetNext();
431         }
432         UpdateAll();
433 }
434 /**
435 * Zoom in the functions
436 */
437 void pPlotterWindow::onMyZoomIn(wxCommandEvent& aEvent)
438 {
439                 
440         zoomFactor=zoomFactor*(float)ZOOM_FACTOR;
441         wxNode* node=functions.GetFirst();
442         while(node)
443         {
444                 pGraphicalFunction* f= (pGraphicalFunction*)node->GetData();
445                 if(zoomFactor > 1.01)
446                 {       
447                         f->setScreenX(m_scrX);
448                         f->setScreenY(m_scrY);
449                         f->setOffsets(m_clickedX-70,m_scrY-m_clickedY-40);
450                         f->setFactorZoom(zoomFactor);
451                 }
452                 else 
453                 {
454                         f->setOffsetPixelX(0);
455                         f->setOffsetPixelY(0);
456                         f->setOffsetX(0);
457                         f->setOffsetY(0);
458                         f->setFactorZoom(zoomFactor);
459                 }
460                 if(f->getActual())
461                 {
462                         //TODO: send an event of resize
463                         //setMinScrX(f->getMinXShowed());
464                         //setMinScrY(f->getMinYShowed());
465                         setOffsetPixelX(f->getOffsetPixelsX());
466                         setOffsetPixelY(f->getOffsetPixelsY());
467                         setOffsetX(f->getOffsetX());
468                         setOffsetY(f->getOffsetY());
469
470                 }
471                 
472                 node=node->GetNext();
473         }
474
475         UpdateAll();
476 }
477
478
479 //-------------------------
480 // Drawing
481 //-------------------------
482
483
484 /**
485 * Let to the user to start drawing
486 */
487 void pPlotterWindow::onStartDrawing(wxCommandEvent& aEvent)
488 {
489
490         if(actual!=NULL)
491         {
492                 actual->setActual(false);
493                 if(actual->getShowPoints())
494                         actual->SetShowPoints(false);
495         }       
496         m_popmenu.Enable(pwID_NOSHOWPOINTS,false);
497         m_popmenu.Enable(pwID_ADDNEWPOINT,false);
498         m_popmenu.Enable(pwID_SHOWPOINTS,false);
499         m_popmenu.Enable(pwID_DELPOINT,false);
500         m_popmenu.Enable(pwID_MYZOOMIN,false);
501         m_popmenu.Enable(pwID_MYZOOMOUT,false);
502         m_popmenu.Enable(pwID_STARTDRAWING,false);
503         m_popmenu.Enable(pwID_SPLINE,false);
504         m_popmenu.Enable(pwID_LINE,false);
505         m_popmenu.Enable(pwID_STOPDRAWING,true);
506         m_popmenu.Enable(pwID_SAVE,false);
507         m_popmenu.Enable(pwID_LOAD,false);
508         m_popmenu.Enable(pwID_CHANGECOLOR,false);
509         m_popmenu.Enable(pwID_TRASLATEACTUALFUNCTION,false);
510
511         if(!drawing)
512         {
513                 drawing=true;
514                 
515                 
516                 actual=new pGraphicalFunction(wxT("f(x) = function1(x)"),mpALIGN_NE);
517                 actual->setActual(true);
518                 actual->setFromWindow(true);
519                 actual->setScaleWay(USER_SCALE);
520                 actual->SetDrawing(drawing);
521                 actual->setFactorZoom(zoomFactor);
522                 
523                 //we change the offsets
524                 setOffsetPixelX(actual->getOffsetPixelsX());
525                 setOffsetPixelY(actual->getOffsetPixelsY());
526                 setOffsetX(actual->getOffsetX());
527                 setOffsetY(actual->getOffsetY());
528         
529                 AddLayer(actual);
530                 wxPen mypen(*wxRED, 5, wxSOLID);
531                 mypen.SetWidth(2);
532                 actual->SetPen( mypen);
533                 
534                 /*
535                 int result = wxMessageBox("Do you Want a Smooth Curve?", "Spline Points", wxYES_NO | wxCANCEL);
536                 if(result==wxYES)
537                 {
538                         actual->setType(2);
539                 }
540                 */
541                 
542                 /**************************************************************************/
543                 // Setting Maximums
544                 /**************************************************************************/
545         
546                 //opening  the dialog of maximums
547                 float maxX=0,maxY=0;
548                 MaximumsDialog *dlg=new MaximumsDialog(this,_T("Set Maximums"));
549                 
550                 if(dlg->ShowModal()== wxID_OK )
551                 {
552                         maxX=dlg->getXValue();
553                         maxY=dlg->getYValue();
554                         if(maxX>0 && maxY>0 && actual->getFromWindow())
555                         {
556                                 setMaxScrX(maxX);
557                                 setMaxScrY(maxY);
558                                 
559                                 actual->setMaxX(maxScrX);                               
560                                 actual->setMinX(minScrX);
561                                 actual->setMaxY(maxY);
562                                 actual->setMinY(0);
563                                 
564                                 actual->setMinShowedX(0);
565                                 actual->setMinShowedY(0);
566                                 actual->setMaxShowedX(maxX);
567                                 actual->setMaxShowedY(maxY);
568                                 
569                                 setActualScales();
570                         }
571                         else if(maxX<0 || maxY<0)
572                         {
573                                  wxMessageBox(_T("Both numbers have to be positive, please enter valid numbers"), _T("Error"),
574                             wxOK);
575                                  
576                                  DelLayer(actual);
577                                  actual=NULL;
578                                                 
579                                 m_popmenu.Enable(pwID_NOSHOWPOINTS,false);
580                                 m_popmenu.Enable(pwID_ADDNEWPOINT,false);
581                                 m_popmenu.Enable(pwID_SHOWPOINTS,false);
582                                 m_popmenu.Enable(pwID_DELPOINT,false);
583                                 m_popmenu.Enable(pwID_MYZOOMIN,false);
584                                 m_popmenu.Enable(pwID_MYZOOMOUT,false);
585                                 m_popmenu.Enable(pwID_STARTDRAWING,true);
586                                 m_popmenu.Enable(pwID_SPLINE,false);
587                                 m_popmenu.Enable(pwID_LINE,false);
588                                 m_popmenu.Enable(pwID_STOPDRAWING,false);
589                                 m_popmenu.Enable(pwID_SAVE,false);
590                                 m_popmenu.Enable(pwID_LOAD,true);
591                                 m_popmenu.Enable(pwID_CHANGECOLOR,false);
592                                 m_popmenu.Enable(pwID_TRASLATEACTUALFUNCTION,false);
593                                 drawing=false;
594                                 //actual->SetDrawing(false);
595
596                         }
597
598                         else if(maxX==0 && maxY==0)
599                         {
600                                 actual->setMaxX(100);
601                                 actual->setMinX(0);
602                                 actual->setMaxY(100);
603                                 actual->setMinY(0);
604                                         
605                                 actual->setMinShowedX(0);
606                                 actual->setMinShowedY(0);
607                                 actual->setMaxShowedX(100);
608                                 actual->setMaxShowedY(100);
609
610                                 maxScrX=100;
611                                 maxScrY=100;
612                                 minScrX=0;
613                                 minScrY=0;
614
615                                 setActualScales();
616                         }
617                         
618                 }
619                 
620         else if(dlg->ShowModal()== wxID_CANCEL)
621                 {
622                         DelLayer(actual);
623                         actual=NULL;
624                                         
625                         m_popmenu.Enable(pwID_NOSHOWPOINTS,false);
626                         m_popmenu.Enable(pwID_ADDNEWPOINT,false);
627                         m_popmenu.Enable(pwID_SHOWPOINTS,false);
628                         m_popmenu.Enable(pwID_DELPOINT,false);
629                         m_popmenu.Enable(pwID_MYZOOMIN,false);
630                         m_popmenu.Enable(pwID_MYZOOMOUT,false);
631                         m_popmenu.Enable(pwID_STARTDRAWING,true);
632                         m_popmenu.Enable(pwID_SPLINE,false);
633                         m_popmenu.Enable(pwID_LINE,false);
634                         m_popmenu.Enable(pwID_STOPDRAWING,false);
635                         m_popmenu.Enable(pwID_SAVE,false);
636                         m_popmenu.Enable(pwID_LOAD,true);
637                         m_popmenu.Enable(pwID_CHANGECOLOR,false);
638                         m_popmenu.Enable(pwID_TRASLATEACTUALFUNCTION,false);
639                         drawing=false;
640                         //actual->SetDrawing(false);
641
642                 }
643                 dlg->Destroy();
644                 if(drawing)
645                 {
646                         //------------------------------------------
647                         //Sending the paarticular event of changed actual function
648                         //-------------------------------------------
649                         wxCommandEvent cevent( wxEVT_PPLOTTER_CHANGED_FUNCTION, GetId() );
650                         cevent.SetEventObject(this);
651                         GetEventHandler()->ProcessEvent( cevent );
652                 }
653         }
654         
655 }
656 /**
657 Stop the drawing of the actual function
658 */
659 void pPlotterWindow::onStopDrawing(wxCommandEvent& aEvent)
660 {
661         drawing=false;
662         actual->SetDrawing(drawing);
663         functions.Append(actual);
664         addFunctionToMove(actual);
665         m_popmenu.Enable(pwID_ADDNEWPOINT,true);
666         m_popmenu.Enable(pwID_DELPOINT,true);
667         m_popmenu.Enable(pwID_SHOWPOINTS,true);
668         m_popmenu.Enable(pwID_MYZOOMIN,true);
669         m_popmenu.Enable(pwID_MYZOOMOUT,true);
670         m_popmenu.Enable(pwID_STARTDRAWING,true);
671         m_popmenu.Enable(pwID_SPLINE,true);
672         m_popmenu.Enable(pwID_STOPDRAWING,false);
673         m_popmenu.Enable(pwID_LINE,false);
674         m_popmenu.Enable(pwID_CHANGECOLOR,true);
675         m_popmenu.Enable(pwID_TRASLATEACTUALFUNCTION,true);
676
677         if(actual->getSizePoints()==0||actual->getSizePoints()==1)
678         {         
679         
680           m_popmenu.Enable(pwID_ADDNEWPOINT,false);
681           //functions.DeleteObject(actual);
682           DelLayer(actual);
683           actual=NULL;   
684          
685         }
686         
687         UpdateAll();
688 }
689 /**
690 * The user wants to see the points(given) of the function
691 */
692 void pPlotterWindow::onShowPoints(wxCommandEvent& aEvent)
693 {
694         actual->SetShowPoints(true);
695         m_popmenu.Enable(pwID_NOSHOWPOINTS,true);
696         m_popmenu.Enable(pwID_SHOWPOINTS,false);        
697         UpdateAll();
698 }
699 /**
700 * The user doesnt want see the points of the function
701 */
702 void pPlotterWindow::onNoShowPoints(wxCommandEvent& aEvent)
703 {       
704         actual->SetShowPoints(false);
705         m_popmenu.Enable(pwID_NOSHOWPOINTS,false);
706         m_popmenu.Enable(pwID_SHOWPOINTS,true);
707         UpdateAll();    
708 }
709 /**
710 * if the user is going to add a point to the function
711 */
712 void pPlotterWindow::onAddPoint(wxMouseEvent& aEvent)
713 {
714         
715         wxPoint point=aEvent.GetPosition();
716         
717         point.y=m_scrY-point.y;
718         wxPoint realPoint=getRealPoint(point);
719
720         bool isAValidPoint=realPoint.x>=0 && realPoint.y>=0;
721
722         if(isAValidPoint && drawing)
723         {
724                 if(actual->AddPoint(realPoint.x,realPoint.y))
725                 {
726                         text = wxString::Format(_T("added  point ( %d, %d)\n"), realPoint.x, realPoint.y);
727                         
728                         //m_log->AppendText(text);
729
730                         //if the function is smooth
731                         if(actual->getType()==2)
732                         {
733                                 actual->clearSplineVectors();
734                                 actual->addSplinesPoints();
735                                 actual->initializeSplineVectors();                              
736                         }
737                         
738                         UpdateAll();
739                 }
740         }
741         else if(isAValidPoint && !drawing && aEvent.m_shiftDown && actual!=NULL)
742         {
743                 if(actual->AddNewPoint(realPoint.x,realPoint.y))
744                 {
745                         text = wxString::Format(_T("added  point ( %d, %d)\n"), realPoint.x, realPoint.y);
746                         //if the function is smooth
747                         if(actual->getType()==2)
748                         {
749                                 actual->clearSplineVectors();
750                                 actual->addSplinesPoints();
751                                 actual->initializeSplineVectors();                              
752                         }
753                         
754                         UpdateAll();
755                 }
756         }
757         //made a click for moving the points
758         else if(!movingPoints)
759         {
760                 if(actual)      
761                         movingPointIndex=actual->validPointOnFunction(realPoint);
762                 if(movingPointIndex>=0)
763                         movingPoints=true;
764         }
765                                         
766         //------------------------------------------
767         //Sending the  event
768         //-------------------------------------------
769         wxCommandEvent cevent( wxEVT_PPLOTTER_POINT_ADD, GetId() );
770         cevent.SetEventObject(this);
771
772         //------------------------------------------
773         //Sending the  general event
774         //-------------------------------------------
775         wxCommandEvent pevent( wxEVT_PPLOTTER, GetId() );
776         pevent.SetEventObject(this);
777         //send the events
778         GetEventHandler()->ProcessEvent( cevent );
779         GetEventHandler()->ProcessEvent( pevent );
780 }
781
782 /**
783 * Adds a point  to the function after the definition of the initial points of the function
784 * know this point is  a point of the function
785 *
786 */
787 void pPlotterWindow::onAddNewPoint(wxCommandEvent& aEvent)
788 {
789         wxPoint point;
790         point.x=m_clickedX;
791         point.y=m_scrY-m_clickedY;
792
793         wxPoint realPoint=getRealPoint(point);
794         bool isAValidPoint=realPoint.x>=0 && realPoint.y>=0;
795
796         
797         if(isAValidPoint && actual->AddNewPoint(realPoint.x,realPoint.y))
798         {
799                 text = wxString::Format(_T("added  point ( %d, %d)\n"), realPoint.x, realPoint.y);
800                 //if the function is smooth
801                 if(actual->getType()==2)
802                 {
803                         actual->clearSplineVectors();
804                         actual->addSplinesPoints();
805                         actual->initializeSplineVectors();
806                         
807                 }
808                 UpdateAll();
809                 //------------------------------------------
810                 //Sending the  event
811                 //-------------------------------------------
812                 wxCommandEvent cevent( wxEVT_PPLOTTER_POINT_ADD, GetId() );
813                 cevent.SetEventObject(this);
814
815                 //------------------------------------------
816                 //Sending the  general event
817                 //-------------------------------------------
818                 wxCommandEvent pevent( wxEVT_PPLOTTER, GetId() );
819                 pevent.SetEventObject(this);
820                 //send the events
821                 GetEventHandler()->ProcessEvent( cevent );
822                 GetEventHandler()->ProcessEvent( pevent );
823
824         }
825 }
826
827 /**
828 * if the user is going to delete a point of the function
829 */
830 void pPlotterWindow::onDeletePoint(wxCommandEvent& aEvent)
831 {
832         wxPoint point;
833         point.x=m_clickedX;
834         point.y=m_scrY-m_clickedY;
835
836         //actual->setScreenX(m_scrX);
837         //actual->setScreenY(m_scrY);
838
839         wxPoint realPoint=getRealPoint(point);
840         
841         bool isAValidPoint=realPoint.x>=0 && realPoint.y>=0;
842
843         //this method verify a sensible region in every point of actual
844         int pointIndex=actual->validPointOnFunction(realPoint);
845         if(isAValidPoint && actual->deletePointAt(pointIndex))
846         {
847                 setActualScales();
848                 if(actual->getSizePoints()==0)
849                 {
850                         //functions.DeleteObject(actual);
851                         //DelLayer(actual);
852                         //actual=NULL;
853                 }
854                 //if the function is smooth
855                 if(actual->getType()==2)
856                 {
857                         actual->clearSplineVectors();
858                         actual->addSplinesPoints();
859                         actual->initializeSplineVectors();
860                         
861                 }
862                 text = wxString::Format(_T("deleted  point ( %d, %d)\n"), realPoint.x, realPoint.y);
863                 UpdateAll();
864                 //------------------------------------------
865                 //Sending the  event
866                 //-------------------------------------------
867                 wxCommandEvent cevent( wxEVT_PPLOTTER_POINT_DELETE, GetId() );
868                 cevent.SetEventObject(this);
869                 //------------------------------------------
870                 //Sending the  general event
871                 //-------------------------------------------
872                 wxCommandEvent pevent( wxEVT_PPLOTTER, GetId() );
873                 pevent.SetEventObject(this);
874                 //send the events
875                 GetEventHandler()->ProcessEvent( cevent );
876                 GetEventHandler()->ProcessEvent( pevent );
877         }  
878 }
879
880
881 /**
882 * If the mouse is moving in the plot area
883 * is because the user is moving a point of the current curve
884 * the user has to call before show points, to see the points of the 
885 * function
886 */
887
888 void pPlotterWindow::onMouseMove(wxMouseEvent& event)
889 {
890         wxPoint point=event.GetPosition();
891         bool change=false;
892         point.y=m_scrY-point.y;
893         
894         wxPoint realPoint=getRealPoint(point);
895         bool isAValidPoint=realPoint.x>=0 && realPoint.y>=0;
896
897         if(isAValidPoint && !drawing && actual!=NULL && !movingFunction && movingPoints)
898         {
899                 //never is going to get in
900                 if(movingPointIndex==-1)
901                 {       
902                         //this method verify a sensible region in every point of _actualFunction
903                         movingPointIndex=actual->validPointOnFunction(realPoint);
904                                         
905                 }
906                 else if(event.LeftIsDown())
907                 {                               
908                         
909                         /*if((movingPointIndex-1)>=0 && (movingPointIndex+1)<actual->getSizePoints() && actual->getEditable())
910                         {
911                                 wxNode* next= actual->GetPointAt(movingPointIndex+1);
912                                 wxNode* before=actual->GetPointAt(movingPointIndex-1);
913                                 if(next)
914                                 {       
915                                         pFunctionPoint* np=(pFunctionPoint*)(next->GetData());
916                                         if(np)
917                                                 {
918                                                         int n=np->getRealX();
919                                                         if(realPoint.x>n)
920                                                         {
921                                                                 wxPoint p;
922                                                                 p.x=np->getRealX()-1;
923                                                                 p.y=realPoint.y;
924                                                                 change=actual->changePoint(p,movingPointIndex);
925                                                                 if(change)
926                                                                 {
927                                                                         setActualScales();
928                                                                                                                         
929                                                                         //if the function is smooth
930                                                                         if(actual->getType()==2)
931                                                                         {
932                                                                                 actual->clearSplineVectors();
933                                                                                 actual->addSplinesPoints();
934                                                                                 actual->initializeSplineVectors();
935                                                                                 
936                                                                         }
937                                                                         
938                                                                         text = wxString::Format(_T("Moved  point ( %d, %d)\n"), p.x, p.y);
939                                                                         UpdateAll();
940                                                                         //------------------------------------------
941                                                                         //Sending the particular event
942                                                                         //-------------------------------------------
943                                                                         wxCommandEvent cevent( wxEVT_PPLOTTER_POINT_MOVE, GetId() );
944                                                                         cevent.SetEventObject(this);
945
946                                                                         //------------------------------------------
947                                                                         //Sending the  general event
948                                                                         //-------------------------------------------
949                                                                         wxCommandEvent pevent( wxEVT_PPLOTTER, GetId() );
950                                                                         pevent.SetEventObject(this);
951
952                                                                         //send the events
953                                                                         GetEventHandler()->ProcessEvent( cevent );
954                                                                         GetEventHandler()->ProcessEvent( pevent );
955                                                                 }
956                                                         }
957                                                 }
958                                 }
959                                 if(before)
960                                 {
961                                         pFunctionPoint* bp=(pFunctionPoint*)(before->GetData());        
962                                         if(bp)
963                                          { 
964                                                  int b=bp->getRealX();
965                                                  if(realPoint.x<b)
966                                                 {
967                                                         wxPoint p;
968                                                         p.x=bp->getRealX()+1;
969                                                         p.y=realPoint.y;
970                                                         change=actual->changePoint(p,movingPointIndex);
971                                                         if(change)
972                                                         {
973                                                                 setActualScales();
974                                                                 //if the function is smooth
975                                                                 if(actual->getType()==2)
976                                                                 {
977                                                                         actual->clearSplineVectors();
978                                                                         actual->addSplinesPoints();
979                                                                         actual->initializeSplineVectors();
980                                                                         
981                                                                 }
982                                                                 text = wxString::Format(_T("Moved  point ( %d, %d)\n"), p.x, p.y);
983                                                                 UpdateAll();
984                                                                 //------------------------------------------
985                                                                 //Sending the particular event
986                                                                 //-------------------------------------------
987                                                                 wxCommandEvent cevent( wxEVT_PPLOTTER_POINT_MOVE, GetId() );
988                                                                 cevent.SetEventObject(this);
989
990                                                                 //------------------------------------------
991                                                                 //Sending the  general event
992                                                                 //-------------------------------------------
993                                                                 wxCommandEvent pevent( wxEVT_PPLOTTER, GetId() );
994                                                                 pevent.SetEventObject(this);
995
996                                                                 //send the events
997                                                                 GetEventHandler()->ProcessEvent( cevent );
998                                                                 GetEventHandler()->ProcessEvent( pevent );
999                                                         }
1000
1001                                                 }
1002                                           }
1003                                 }       
1004                         }       */
1005                         // SIL ******************************************************************* // SIL
1006                         if((movingPointIndex-1)>=0 && (movingPointIndex+1)<actual->getSizePoints() && actual->getEditable())
1007                         {
1008                                 change=actual->changePoint(realPoint,movingPointIndex);
1009                         }
1010                         if(change)
1011                         {
1012                                 setActualScales();
1013                                 //if the function is smooth
1014                                 if(actual->getType()==2)
1015                                 {
1016                                         actual->clearSplineVectors();
1017                                         actual->addSplinesPoints();
1018                                         actual->initializeSplineVectors();                                      
1019                                 }
1020                                 text = wxString::Format(_T("Moved  point ( %d, %d)\n"), realPoint.x, realPoint.y);
1021                                 UpdateAll();
1022                                 //------------------------------------------
1023                                 //Sending the particular event
1024                                 //-------------------------------------------
1025                                 wxCommandEvent cevent( wxEVT_PPLOTTER_POINT_MOVE, GetId() );
1026                                 cevent.SetEventObject(this);
1027
1028                                 //------------------------------------------
1029                                 //Sending the  general event
1030                                 //-------------------------------------------
1031                                 wxCommandEvent pevent( wxEVT_PPLOTTER, GetId() );
1032                                 pevent.SetEventObject(this);
1033
1034                                 //send the events
1035                                 GetEventHandler()->ProcessEvent( cevent );
1036                                 GetEventHandler()->ProcessEvent( pevent );
1037                         }
1038                         // SIL ******************************************************************* // SIL
1039
1040                         //change the point that is in the index, returns true if could be changed.
1041                         if(!change && actual->changePoint(realPoint,movingPointIndex)&& actual->getEditable())
1042                         {
1043                                 setActualScales();
1044                                 //if the function is smooth
1045                                 if(actual->getType()==2)
1046                                 {
1047                                         actual->clearSplineVectors();
1048                                         actual->addSplinesPoints();
1049                                         actual->initializeSplineVectors();
1050                                         
1051                                 }
1052                                 text = wxString::Format(_T("Moved  point ( %d, %d)\n"), realPoint.x,realPoint.y);
1053                                 
1054                                 UpdateAll();
1055
1056                                 //------------------------------------------
1057                                 //Sending the particular event
1058                                 //-------------------------------------------
1059                                 wxCommandEvent cevent( wxEVT_PPLOTTER_POINT_MOVE, GetId() );
1060                                 cevent.SetEventObject(this);
1061
1062                                 //------------------------------------------
1063                                 //Sending the  general event
1064                                 //-------------------------------------------
1065                                 wxCommandEvent pevent( wxEVT_PPLOTTER, GetId() );
1066                                 pevent.SetEventObject(this);
1067
1068                                 //send the events
1069                                 GetEventHandler()->ProcessEvent( cevent );
1070                                 GetEventHandler()->ProcessEvent( pevent );
1071                         }
1072                 }
1073                 else
1074                 {
1075                         movingPointIndex=-1;
1076                         movingPoints=false;
1077                 }
1078         }
1079         //if the user is drawing
1080         else if(isAValidPoint && drawing)
1081         {
1082
1083                 actual->SetMousePoint(realPoint);               
1084                 UpdateAll();
1085         }
1086         //is moving the function
1087         else if(isAValidPoint && movingFunction && actual!=NULL)
1088         {
1089                 actual->setScreenX(m_scrX);
1090                 actual->setScreenY(m_scrY);
1091                 //int j=0; // JPRx
1092                 if((initialMovingCLick==-1) && event.LeftIsDown()  )
1093                  {
1094                         if(actual->isInFunction(realPoint.x,realPoint.y))
1095                         {
1096                                 initialMovingCLick=event.GetPosition().x;
1097                                 int numPoints=actual->getSizePoints();
1098                                 int i=0;
1099                                 backUpActualVector.clear();
1100                                 while(i<numPoints)
1101                                 {
1102                                         //if(i>0 && i< (numPoints-1))
1103                                         //{
1104                                                 wxNode* pointNode=actual->GetPointAt(i);
1105                                                 pFunctionPoint* point= (pFunctionPoint*)pointNode->GetData();
1106                                                 backUpActualVector.push_back(point->getRealX());
1107                                                 
1108                                         //}
1109                                         i++;
1110                                 }
1111                         }
1112                  }      
1113                 else if(event.LeftIsDown())
1114                 {
1115                         wxPoint aux(initialMovingCLick,0);
1116
1117                         wxPoint p1r=getRealPoint(event.GetPosition());
1118                         wxPoint p2r=getRealPoint(aux);
1119
1120                         double delta = p1r.x-p2r.x;
1121                         int numPoints=actual->getSizePoints();
1122                         int i=0;
1123                         wxPoint p;
1124                         if(type==HISTOGRAM)
1125                         {
1126                                 while(i<numPoints)
1127                                 {
1128                                         if(i>0 && i<(numPoints-1))
1129                                         {
1130                                                 wxNode* pointNode=actual->GetPointAt(i);
1131                                                 pFunctionPoint* point= (pFunctionPoint*)pointNode->GetData();
1132                                                 int xNew=backUpActualVector[i]+delta;
1133                                                 //it has to preserve the state of functin                               
1134                                                 if(backUpActualVector[0]<xNew && backUpActualVector[numPoints-1]>xNew)
1135                                                                 point->setRealX(xNew);
1136                                         }
1137                                         i++;
1138                                 }
1139                         }
1140                         else
1141                         {
1142                                 i=0;
1143                                 while(i<numPoints)
1144                                 {
1145                                 
1146                                         wxNode* pointNode=actual->GetPointAt(i);
1147                                         pFunctionPoint* point= (pFunctionPoint*)pointNode->GetData();
1148                                         int xNew=backUpActualVector[i]+delta;
1149                                         point->setRealX(xNew);
1150                                         i++;
1151                                 }
1152                         }
1153                         //If is a spline
1154                         if(actual->getType()==2)
1155                         {
1156                                 actual->clearSplineVectors();
1157                                 actual->addSplinesPoints();
1158                                 actual->initializeSplineVectors();
1159
1160                         }
1161                         UpdateAll();
1162
1163                 }
1164                 else 
1165                 {       
1166                         initialMovingCLick=-1;
1167                 }
1168                 
1169         }
1170 }
1171 /**
1172 * if the user give us a double click is because he wants to change
1173 * the actual function to the function that has that point
1174 */
1175 void pPlotterWindow::onChangeActual(wxMouseEvent& event)
1176 {
1177         wxString text = _T("\n points \n");
1178         
1179         
1180         if(!drawing)
1181         {
1182                 //we have to call showpoints before changing the function
1183                 wxPoint point=event.GetPosition();
1184                 /*****************************/
1185                 point.y=m_scrY-point.y;
1186                 /*****************************/
1187        
1188         
1189                 
1190                 //real point on the function
1191                 wxPoint realPoint;//=getRealPoint(point);
1192                 bool isAValidPoint=realPoint.x>=0 && realPoint.y>=0;
1193                 if(isAValidPoint)
1194                 {
1195                         wxNode * node= functions.GetFirst();
1196                         while (node)
1197                         {
1198                                 pGraphicalFunction* fnode=(pGraphicalFunction*)node->GetData();
1199                                 
1200                                 
1201                                 int maxX=fnode->getMaxXShowed();
1202                                 int maxY=fnode->getMaxYShowed();
1203                                 int minX=fnode->getMinXShowed();
1204                                 int minY=fnode->getMinYShowed();
1205                                 
1206                                 
1207                                 /*
1208                                 float sx=(((float)m_scrX-100)/(maxScrX-minScrX))*zoomFactor;
1209                                 float sy=(((float)m_scrY-50)/(maxScrY-minScrY))*zoomFactor;
1210                                 */
1211                                 
1212                                 
1213                                 float sx=(((float)m_scrX-100)/(maxX-minX))*zoomFactor;
1214                                 float sy=(((float)m_scrY-50)/(maxY-minY))*zoomFactor;
1215                                 
1216
1217                                 realPoint.x=((point.x-70-fnode->getOffsetPixelsX())/sx)+ fnode->getOffsetX()+ minX;
1218                                 realPoint.y=((point.y-40-fnode->getOffsetPixelsY())/sy)+ fnode->getOffsetY()+ minY;
1219                                 /*
1220                                 LOG
1221                                 */
1222                                 if(fnode->getActual())
1223                                         text = wxString::Format(_T("Selected point ( %d, %d)\n"), realPoint.x, realPoint.y);
1224                                 //m_log->AppendText(text);
1225                                 
1226                                 bool hasIt=fnode->isInFunction(realPoint.x,realPoint.y);
1227                                         /*
1228                                         if(fnode->getEditable())        
1229                                                 hasIt=fnode->isInFunction(realPoint.x,realPoint.y);
1230                                         else
1231                                                 hasIt=false;
1232                                         */
1233                                 if(hasIt)
1234                                 {
1235                                         if(actual!=fnode)
1236                                         {
1237                                                 setActualFunction(fnode);
1238                                                 //we change the offsets
1239                                                 setOffsetPixelX(fnode->getOffsetPixelsX());
1240                                                 setOffsetPixelY(fnode->getOffsetPixelsY());
1241                                                 setOffsetX(fnode->getOffsetX());
1242                                                 setOffsetY(fnode->getOffsetY());
1243
1244
1245                                                 setActualScales();
1246                                                 if(!actual->getFromWindow() && actual->getEditable()||actual->getFromWindow() && actual->getEditable())
1247                                                 {
1248                                                         m_popmenu.Enable(pwID_ADDNEWPOINT,true);
1249                                                         m_popmenu.Enable(pwID_DELPOINT,true);
1250                                                         if(actual->getShowPoints())
1251                                                         {
1252                                                                 m_popmenu.Enable(pwID_SHOWPOINTS,false);
1253                                                                 m_popmenu.Enable(pwID_NOSHOWPOINTS,true);
1254                                                         }
1255                                                         else
1256                                                         {
1257                                                                 m_popmenu.Enable(pwID_SHOWPOINTS,true);
1258                                                                 m_popmenu.Enable(pwID_NOSHOWPOINTS,false);
1259                                                         }
1260                                                         m_popmenu.Enable(pwID_MYZOOMIN,true);
1261                                                         m_popmenu.Enable(pwID_MYZOOMOUT,true);
1262                                                         if(actual->getType()==2)//Smoothed function
1263                                                         {
1264                                                                 m_popmenu.Enable(pwID_SPLINE,false);
1265                                                                 m_popmenu.Enable(pwID_LINE,true);
1266                                                         }
1267                                                         else
1268                                                         {
1269                                                                 m_popmenu.Enable(pwID_SPLINE,true);
1270                                                                 m_popmenu.Enable(pwID_LINE,false);
1271
1272                                                         }
1273                                                         if(type==2)
1274                                                         {
1275                                                                 m_popmenu.Enable(pwID_LOAD,false);
1276                                                                 m_popmenu.Enable(pwID_STARTDRAWING,false);
1277                                                         }       
1278                                                         else
1279                                                         {
1280                                                             m_popmenu.Enable(pwID_LOAD,true);
1281                                                                 m_popmenu.Enable(pwID_STARTDRAWING,true);
1282                                                         }
1283
1284                                                         m_popmenu.Enable(pwID_SAVE,true);
1285                                                         m_popmenu.Enable(pwID_LOAD,false);
1286                                                         m_popmenu.Enable(pwID_STOPDRAWING,false);
1287                                                         m_popmenu.Enable(pwID_CHANGECOLOR,true);
1288                                                         m_popmenu.Enable(mpID_LINE_GUIDES,true);
1289                                                         m_popmenu.Enable(pwID_TRASLATEACTUALFUNCTION,true);
1290                                                 }
1291                                                 //for the functions that came from data and arent editables
1292                                                 else if(!actual->getFromWindow() && !actual->getEditable()||actual->getFromWindow() && !actual->getEditable())
1293                                                 {
1294                                                         if(actual->getShowPoints())
1295                                                         {
1296                                                                 m_popmenu.Enable(pwID_SHOWPOINTS,false);
1297                                                                 m_popmenu.Enable(pwID_NOSHOWPOINTS,true);
1298                                                         }
1299                                                         else
1300                                                         {
1301                                                                 m_popmenu.Enable(pwID_SHOWPOINTS,true);
1302                                                                 m_popmenu.Enable(pwID_NOSHOWPOINTS,false);
1303                                                         }
1304                                                         if(actual->getType()==2)//Smoothed function
1305                                                         {
1306                                                                 m_popmenu.Enable(pwID_SPLINE,false);
1307                                                                 m_popmenu.Enable(pwID_LINE,true);
1308                                                         }
1309                                                         else
1310                                                         {
1311                                                                 m_popmenu.Enable(pwID_SPLINE,true);
1312                                                                 m_popmenu.Enable(pwID_LINE,false);
1313
1314                                                         }
1315                                                         if(type==2)
1316                                                         {
1317                                                                 m_popmenu.Enable(pwID_LOAD,false);
1318                                                                 m_popmenu.Enable(pwID_STARTDRAWING,false);
1319                                                         }       
1320                                                         m_popmenu.Enable(pwID_SAVE,false);
1321                                                         m_popmenu.Enable(pwID_LOAD,false);
1322                                                         m_popmenu.Enable(pwID_STOPDRAWING,false);
1323                                                         m_popmenu.Enable(pwID_MYZOOMIN,true);
1324                                                         m_popmenu.Enable(pwID_MYZOOMOUT,true);
1325                                                         m_popmenu.Enable(pwID_CHANGECOLOR,true);
1326                                                         m_popmenu.Enable(mpID_LINE_GUIDES,true);
1327                                                         m_popmenu.Enable(pwID_SAVE,true);
1328                                                         m_popmenu.Enable(pwID_TRASLATEACTUALFUNCTION,true);
1329
1330                                                 }
1331                                                 //if the function is smooth
1332                                                 if(actual->getType()==2)
1333                                                 {
1334                                                         actual->clearSplineVectors();
1335                                                         actual->addSplinesPoints();
1336                                                         actual->initializeSplineVectors();
1337                                                         
1338                                                 }
1339                 
1340                                                 UpdateAll();
1341                                                 //------------------------------------------
1342                                                 //Sending the particular event
1343                                                 //-------------------------------------------
1344                                                 wxCommandEvent cevent( wxEVT_PPLOTTER_CHANGED_FUNCTION, GetId() );
1345                                                 cevent.SetEventObject(this);
1346
1347                                                 //------------------------------------------
1348                                                 //Sending the  general event
1349                                                 //-------------------------------------------
1350                                                 wxCommandEvent pevent( wxEVT_PPLOTTER, GetId() );
1351                                                 pevent.SetEventObject(this);
1352                                                 //send the events
1353                                                 GetEventHandler()->ProcessEvent( cevent );
1354                                                 GetEventHandler()->ProcessEvent( pevent );
1355                                         }
1356                                 }
1357                                 node=node->GetNext();
1358                         }
1359                   }
1360                 }
1361                 //the user is finishing the drawing of the function
1362                 else
1363                 {
1364                         drawing=false;
1365                         functions.Append(actual);
1366                 }
1367 }
1368
1369 /**
1370 * The user wants to change the type of function: line, smooth
1371 * TODO is an extension
1372 */
1373 void pPlotterWindow::changeFuntionType(int aTheType, bool aForAll)
1374 {
1375
1376 }
1377 //------------------------------------------
1378 //If the data doesnt come from interaction
1379 //-------------------------------------------
1380 /**
1381 *  Add a function to the window, given its points
1382 */
1383 int pPlotterWindow:: addFunction(pGraphicalFunction * function)
1384 {
1385         if(function->getType()==2)
1386         {
1387                 function->initializeSplines();
1388                 function->initializeSplineVectors();
1389                 UpdateAll();
1390         }
1391         
1392 //      bool added1= functions.Append(function)!=NULL; // JPRx
1393         functions.Append(function);  //EED      
1394         
1395         int id=functions.IndexOf(function);
1396         
1397         // bool added2=  AddLayer(function);// JPRx
1398         AddLayer(function); //EED
1399          
1400         UpdateAll();
1401         return id;//added1 && added2;
1402 }
1403 /**
1404 *  Delete a point of a function
1405 *  @param point: is the real value that the functon takes, and is gointg to be deleted  
1406 */
1407
1408 bool pPlotterWindow::DeletePointFunction(pGraphicalFunction* function,wxPoint point)
1409 {
1410
1411         wxNode* node= functions.GetFirst();
1412         while(node)
1413         {
1414                 pGraphicalFunction* f= (pGraphicalFunction*)node->GetData();
1415                 if(f==function)
1416                 {
1417                         return function->DeletePoint(point.x,point.y);
1418                 }
1419                 node=node->GetNext();
1420         }
1421         return false;
1422 }
1423 /**
1424 * Delete a function of the plotter
1425 */
1426 bool pPlotterWindow::deleteFunction(pGraphicalFunction * f)
1427 {
1428         functions.DeleteObject(f);
1429         functionsToMove.DeleteObject(f);
1430         return true;
1431 }
1432 /**
1433 * Creates and returns a graphical funcion according to the indicated vectors.
1434 */
1435 pGraphicalFunction * pPlotterWindow::getFunctionForVectors( std::vector<double> *vectorX, std::vector<double> *vectorY )
1436 {
1437         double coordX = -1 ;    
1438         double coordY = -1;
1439
1440         pGraphicalFunction * ff = new pGraphicalFunction( _T(" "), 1);
1441
1442         int i=0;
1443         int sizeX = vectorX->size();
1444         int sizeY = vectorY->size();
1445         if( sizeX == sizeY )
1446         {
1447                 while ( i < sizeX )
1448                 {
1449                         coordX = (*vectorX)[i];                 
1450                         coordY = (*vectorY)[i];
1451                         ff -> AddPoint( coordX, coordY);                
1452                         i++;
1453                 }
1454         }
1455
1456    InitFunctionForVectors( ff );
1457    return ff;
1458 }
1459
1460 pGraphicalFunction * pPlotterWindow::getFunctionForVectors( double * vectorX, int sizeX, double * vectorY, int sizeY )
1461 {
1462         double coordX = -1 ;    
1463         double coordY = -1;
1464
1465         pGraphicalFunction * ff = new pGraphicalFunction( _T(" "), 1);
1466
1467         /*wxString text = "\n Including points from two vectors \n";
1468         m_log->AppendText(text);*/
1469
1470         int i=0;
1471         if(sizeX == sizeY)
1472         {
1473                 while ( i < sizeX )
1474                 {
1475                         coordX = *vectorX;                      
1476                         coordY = *vectorY;
1477                         ff -> AddPoint( coordX, coordY);                
1478         /*              text = wxString::Format("Included point ( %d, %d)\n", coordX, coordY);
1479                         m_log->AppendText(text);*/
1480
1481                         vectorX++;
1482                         vectorY++;              
1483                         i++;
1484                 }
1485         }
1486
1487    InitFunctionForVectors( ff );
1488    return ff;
1489 }
1490
1491
1492
1493 void pPlotterWindow::InitFunctionForVectors( pGraphicalFunction *ff )
1494 {
1495         int maxX,maxY,minX,minY;
1496         /*
1497         if the function is  not drawed interactively the maxX and the 
1498         maxY are the from the points of the function
1499         */
1500         maxX=ff->getMaxX();
1501         maxY=ff->getMaxY();
1502         minX= ff->getMinX();
1503         minY= ff->getMinY();
1504
1505                 
1506         ff->setMinShowedX(minX);
1507         ff->setMinShowedY(minY);
1508         ff->setMaxShowedX(maxX);
1509         ff->setMaxShowedY(maxY);
1510         
1511         GetClientSize(&m_scrX,&m_scrY);
1512         //set
1513
1514         if((maxX-minX)!=0 && (maxY-minY)!=0)
1515         {
1516                 m_scaleX= ((float)m_scrX-100)/(maxX-minX);
1517                 m_scaleY= ((float)m_scrY-50)/(maxY-minY);
1518         }
1519         maxScrX=maxX;
1520         maxScrY=maxY;
1521         minScrX=minX;
1522         minScrY=minY;
1523         
1524         
1525         if(type==HISTOGRAM)
1526         {       
1527                 actual=ff;
1528                 setActualScales();
1529                 m_popmenu.Enable(pwID_NOSHOWPOINTS,true);
1530                 m_popmenu.Enable(pwID_ADDNEWPOINT,true);
1531                 m_popmenu.Enable(pwID_SHOWPOINTS,false);
1532                 m_popmenu.Enable(pwID_DELPOINT,true);
1533                 //m_popmenu.Enable(pwID_SPLINE,false);
1534                 //m_popmenu.Enable(pwID_LINE,true);
1535                 m_popmenu.Enable(pwID_SAVE,true);
1536                 m_popmenu.Enable(pwID_LOAD,false);
1537                 m_popmenu.Enable(pwID_CHANGECOLOR,true);
1538                 //m_popmenu.Enable(pwID_STARTDRAWING,false);
1539                 m_popmenu.Enable(mpID_LINE_GUIDES,true);
1540                 m_popmenu.Enable(pwID_TRASLATEACTUALFUNCTION,true);
1541                 //------------------------------------------
1542                 //Sending the particular event of changed actual function
1543                 //-------------------------------------------
1544                 wxCommandEvent cevent( wxEVT_PPLOTTER_CHANGED_FUNCTION, GetId() );
1545                 cevent.SetEventObject(this);
1546                 GetEventHandler()->ProcessEvent( cevent );
1547         }
1548         
1549         /*
1550         maxScrX=100;
1551         minScrX=50;
1552         offsetX=50;
1553         ff->setOffsetX(50);
1554         */
1555         //actual->setScreens(scrX,scrY);
1556         //actual->setScales();
1557         //setActualScales();
1558 }
1559 /*
1560 bool pPlotterWindow::AddFunction(wxPoint realPoints[],int numberOfPoints,wxString name,int flags)
1561 {
1562 pGraphicalFunction* function= new pGraphicalFunction(name,flags);
1563
1564 for(int i= 0; i<numberOfPoints;i++)
1565 {
1566 int x=realPoints[i].x;
1567 int y=realPoints[i].y;
1568 bool added=function->AddPoint(x,y);
1569 if(!added )
1570 return added;
1571 }
1572 functions.Append(function);
1573 bool addL=mpWindow::AddLayer(function);
1574 if(addL)
1575 {
1576 mpWindow::UpdateAll();
1577 }
1578 return addL;
1579 }
1580 */
1581
1582 /*
1583 * Actualizes the view range of the plotter and the actual function
1584 * @param newMinX Is the min-value to be shown in the x-scale
1585 * @param newMaxX Is the max-value to be shown in the x-scale
1586 */
1587 /*
1588 DEPRECATED
1589 */
1590 void pPlotterWindow :: actualizeViewRange(int newMinX, int newMaxX)
1591 {
1592         int minPixX,maxPixX/*, min, max*/;  // JPRx
1593         double sx;
1594         /*
1595                 getiing pixels
1596         */
1597         int dx=newMaxX-newMinX;
1598         
1599         if(dx!=0)
1600         {
1601                 sx= (((float)m_scrX-100)/dx)*zoomFactor;
1602         }
1603         
1604          minPixX=newMinX*sx;
1605          maxPixX=newMaxX*sx;
1606
1607                 //vx.at(i)-offsetX)*scaleX+offsetpx
1608         if(minScrX!=newMinX)
1609         {
1610                 setOffsetX(newMinX);
1611                 setMinScrX(newMinX);
1612                 actual->setOffsetX(newMinX);
1613                 actual->setMinShowedX(newMinX);                         
1614                 /*
1615                 wxNode * node= functions.GetFirst();
1616                 while (node)
1617                         {
1618                                 pGraphicalFunction* fnode=(pGraphicalFunction*)node->GetData();
1619                                 if(fnode)
1620                                 {
1621                                         if(!fnode->getActual())
1622                                         {
1623                                                 fnode->setScreens(m_scrX,m_scrY);
1624                                                 fnode->setScales();
1625                                                 //max=minPixX/fnode->getScaleX();
1626                                                 min=minPixX/fnode->getScaleX();
1627                                                 fnode->setOffsetX(min);
1628                                                 fnode->setMinShowedX(min);
1629                                                 
1630                                         }
1631                                 }
1632                                 node=node->GetNext();
1633                         }
1634                 */
1635             
1636         }
1637         if(maxScrX!=newMaxX)
1638         {
1639                 setMaxScrX(newMaxX);
1640             actual->setMaxShowedX(newMaxX);
1641                 /*
1642                 wxNode * node= functions.GetFirst();
1643                 while (node)
1644                         {
1645                                 
1646                                 pGraphicalFunction* fnode=(pGraphicalFunction*)node->GetData();
1647                                 if(fnode)
1648                                 {
1649                                         if(!fnode->getActual())
1650                                         {
1651                                                 fnode->setScreens(m_scrX,m_scrY);
1652                                                 fnode->setScales();
1653                                                 max=maxPixX/fnode->getScaleX();
1654                                                 //min=maxPixX/fnode->getScaleX();
1655                                                 //fnode->setOffsetX(max);
1656                                                 //fnode->setMinShowedX(min);
1657                                                 fnode->setMaxShowedX(max);
1658                                         }
1659                                 }
1660
1661                                 node=node->GetNext();
1662                         }
1663                 */
1664         }
1665         /*
1666         if(actual->getMinXShowed()!=actual->getMinX() || actual->getMinYShowed()!=actual->getMinY()||
1667                 actual->getMaxXShowed()!=actual->getMaxX() || actual->getMaxYShowed()!=actual->getMaxY())
1668         {
1669                 m_popmenu.Enable(pwID_MYZOOMIN,false);
1670                 m_popmenu.Enable(pwID_MYZOOMOUT,false);
1671         }
1672         else if(actual->getMinXShowed()==actual->getMinX() && actual->getMaxXShowed()==actual->getMaxX())
1673         {
1674                 
1675                 m_popmenu.Enable(pwID_MYZOOMIN,true);
1676                 m_popmenu.Enable(pwID_MYZOOMOUT,true);
1677         }
1678         */
1679                  
1680         UpdateAll();
1681 }
1682 /*
1683 * Guide lines menu handler method that reacts to the mpID_LINE_GUIDES command event
1684 * event The corresponding event to handle
1685 */
1686 void pPlotterWindow :: OnGuideLines (wxCommandEvent   &event)
1687 {
1688         wxString nextAction_text;
1689         if( drawGuides )
1690         {
1691                 setLineGuidesCondition(false);
1692 //              nextAction_text << "Turn on guide lines";
1693                 nextAction_text = _T("Turn on guide lines");
1694                 UpdateAll();
1695         }
1696         else
1697         {
1698                 setLineGuidesCondition(true);
1699 //              nextAction_text << "Turn off guide lines";
1700                 nextAction_text = _T("Turn off guide lines");
1701                 UpdateAll();
1702         }
1703         //------------------------------------------
1704         //Sending the paarticular event of changed actual function
1705         //-------------------------------------------
1706         wxCommandEvent cevent( wxEVT_PPLOTTER_GUIDELINES, GetId() );
1707         cevent.SetEventObject(this);
1708     GetEventHandler()->ProcessEvent( cevent );
1709         
1710         //SETTING LABEL
1711         m_popmenu.SetLabel ( mpID_LINE_GUIDES, nextAction_text ); 
1712 }
1713 /*
1714         Get a function in the plotter given the index
1715 */
1716 pGraphicalFunction* pPlotterWindow::getFunction(int index)
1717 {
1718         wxNode* node=functions.Item(index);
1719         if(node==NULL){
1720                 return NULL;
1721         }
1722         return (pGraphicalFunction*)node->GetData();
1723 }
1724 /*
1725           move the functions that the user wants to move
1726           and that were set in functionsToMove
1727           @param porcentageMinX:the percentage that the minShowed
1728           of the functions have to be move
1729                 0<=porcentageMinX<=1
1730           @param porcentageMaxX:the percentage that the maxShowed
1731           of the functions have to be move
1732                 0<=porcentageMaxX<=1
1733
1734         */
1735
1736 void pPlotterWindow::moveFunctions( float porcentageMinX,float porcentageMaxX)
1737         {
1738                 //Adding the actual function
1739         
1740                 //functionsToMove.Append(actual);
1741
1742                 //
1743                 //float minShowed,maxShowed,fMaxXShowed,fMinXShowed;
1744                 float x,y;
1745                  
1746                 
1747                 int minShowed,maxShowed,fMaxXShowed,fMinXShowed;
1748                 wxNode* fnode=functionsToMove.GetFirst();
1749                 while(fnode)
1750                 {
1751                         pGraphicalFunction* fg=(pGraphicalFunction*)fnode->GetData();
1752                         x=porcentageMaxX*(fg->getMaxX()-fg->getMinX());
1753                         y=porcentageMinX*(fg->getMaxX()-fg->getMinX());
1754                         
1755                         maxShowed=fg->getMinX()+ x;
1756                         minShowed=fg->getMinX()+ y;
1757
1758                         fMinXShowed=fg->getMinXShowed();
1759                         fMaxXShowed=fg->getMaxXShowed();
1760                         
1761                         if(fMinXShowed!=minShowed)
1762                         {
1763                                 if(fg->getActual())
1764                                 {
1765                                         setOffsetX(minShowed);
1766                                         setMinScrX(minShowed);
1767                                 }
1768                                 fg->setOffsetX(minShowed);
1769                                 fg->setMinShowedX(minShowed);                             
1770                         }
1771                         if(fMaxXShowed!=maxShowed)
1772                         {
1773                                 if(fg->getActual())
1774                                 {
1775                                         setMaxScrX(maxShowed);
1776                                 }
1777                                 fg->setMaxShowedX(maxShowed);   
1778                         }
1779                         if(fg->getActual()&& (fg->getMinXShowed()!=fg->getMinX() || fg->getMinYShowed()!=fg->getMinY()||
1780                                                                   fg->getMaxXShowed()!=fg->getMaxX() || fg->getMaxYShowed()!=fg->getMaxY()))
1781                         {
1782                                 if(type!=HISTOGRAM)
1783                                 {
1784                                         m_popmenu.Enable(pwID_MYZOOMIN,false);
1785                                         m_popmenu.Enable(pwID_MYZOOMOUT,false);
1786                                 }
1787                         }
1788                         else if(fg->getActual()&& fg->getMinXShowed()==fg->getMinX() && fg->getMaxXShowed()==fg->getMaxX())
1789                         {
1790                                 if(type!=HISTOGRAM)
1791                                 {
1792                                         m_popmenu.Enable(pwID_MYZOOMIN,true);
1793                                         m_popmenu.Enable(pwID_MYZOOMOUT,true);
1794                                 }
1795                         }
1796
1797
1798                                         fnode=fnode->GetNext();
1799                 }
1800 }
1801
1802 /*
1803  adds a function such that when the min or max of the bar
1804  changes it is going to that function
1805 */
1806 int pPlotterWindow::addFunctionToMove(pGraphicalFunction *function)
1807 {
1808         //bool added1=  functionsToMove.Append(function)!=NULL; // JPRx
1809         functionsToMove.Append(function); // EED
1810         
1811         int id=functionsToMove.IndexOf(function);
1812         UpdateAll();
1813         return id;
1814 }
1815 //----------------------------
1816 //Handling Options Menu
1817 //----------------------------
1818 void pPlotterWindow::setPopUpMenu(bool startD,bool stopD,bool smooth,bool line, bool zoomIn,
1819                                           bool zoomOut,bool showPoints,bool noShowPoints,bool changeColor, bool addP,
1820                                           bool delPoint,bool load,bool save)
1821 {
1822         /*
1823         pwID_ADDPOINT = 2010,    //!< add a point to the function
1824         pwID_DELPOINT,       //!< delete a point of the function
1825         pwID_SHOWPOINTS,  //!<shows the points that the function has
1826         pwID_STARTDRAWING, //!<let to the user to start drawing the curve
1827         pwID_DELFUNCTION, //!<delete a function that is in the plotter
1828         pwID_STOPDRAWING,//!<Stops the drawing of the function
1829         pwID_ADDNEWPOINT,//!<add a new point to the function.
1830         pwID_NOSHOWPOINTS, //!<No show the points of the function.
1831         pwID_MYZOOMIN,//!<Zoom in the function
1832         pwID_MYZOOMOUT,//!<Zoom out the points of the function.
1833         pwID_SPLINE,//!<Spline the actual curve.
1834         pwID_LINE,//!<Piecewise the actual curve.
1835         pwID_SAVE,//!<Save the function in a .txt file.
1836         pwID_LOAD, //!<Load the function from a .txt file.
1837         pwID_CHANGECOLOR, //!<Change the color of the function
1838         pwID_TRASLATEACTUALFUNCTION //!<move the actual function
1839 */
1840         if(startD)
1841                 m_popmenu.Remove( pwID_STARTDRAWING);
1842         if(stopD)
1843                 m_popmenu.Remove( pwID_STOPDRAWING);
1844         if(load)
1845                 m_popmenu.Remove( pwID_LOAD);
1846         if(save)
1847                 m_popmenu.Remove( pwID_SAVE);
1848         if(showPoints)
1849                 m_popmenu.Remove( pwID_SHOWPOINTS);
1850         if(noShowPoints)
1851                 m_popmenu.Remove( pwID_NOSHOWPOINTS);
1852         if(smooth)
1853                 m_popmenu.Remove( pwID_SPLINE);
1854         if(line)
1855                 m_popmenu.Remove( pwID_LINE);
1856         if(zoomIn)
1857                 m_popmenu.Remove( pwID_MYZOOMIN);
1858         if(zoomOut)
1859                 m_popmenu.Remove( pwID_MYZOOMOUT);
1860         if(changeColor)
1861                 m_popmenu.Remove( pwID_CHANGECOLOR);
1862         if(addP)
1863                 m_popmenu.Remove( pwID_ADDPOINT);
1864         if(delPoint)
1865                 m_popmenu.Remove( pwID_DELPOINT);
1866 }
1867
1868