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