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