MatOCAD Logo

Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

PnlGraph.cpp

Go to the documentation of this file.
00001  #include "PnlGraph.h"
00002 
00003 // ----------------------------------------------------------------------------
00004 // Events
00005 // ----------------------------------------------------------------------------
00006 
00007 DEFINE_EVENT_TYPE(EVT_MOUSE_POINTER_MOVED)
00008 DEFINE_EVENT_TYPE(EVT_MOUSE_GRAPH_LCLICK)
00009 DEFINE_EVENT_TYPE(EVT_MOUSE_GRAPH_RCLICK)
00010 
00011 IMPLEMENT_CLASS(PnlGraph,wxPanel)
00013 BEGIN_EVENT_TABLE(PnlGraph,wxPanel)
00015         EVT_PAINT(PnlGraph::OnPaint)  //Process a wxEVT_PAINT event.  
00016     EVT_LEFT_UP(PnlGraph::OnLeftUp) 
00017         EVT_RIGHT_DOWN(PnlGraph::OnRightDown) 
00018         EVT_MENU(1025,PnlGraph::SettingsClick)
00019     EVT_MOTION(PnlGraph::OnMotion) 
00020     EVT_LEFT_DOWN(PnlGraph::OnLeftDown)
00022 END_EVENT_TABLE()
00024 
00025 
00026 
00027 
00028 
00031 int PnlGraph::calculate_y(double ordinata)
00032         {   
00033           
00034             int width,height;  
00035             this->DoGetClientSize(& width,&height);
00036             return (int)((height) * fabs((fabs(options.ymax) - ordinata) / (options.ymax - options.ymin)));
00037         }
00038 
00039  
00040 //Converts the given ascissa to an x absolute position 
00041 //       Receive:  ordinata the ascissa
00042 int PnlGraph::calculate_x(double ascissa)
00043         {
00044             int width,height;  
00045             this->DoGetClientSize(& width,&height);
00046             return (int)((width) * fabs((options.xmin - ascissa) / (options.xmax - options.xmin)));
00047         }
00048 
00049 //Converts the given x absolute position to the respective ascissa
00050 //       Receive:  ascissa
00051 double PnlGraph::calculate_ascissa(double x)
00052         {
00053             int width,height;  
00054             this->DoGetClientSize(& width,&height);
00055             return options.xmin+ x / width * fabs(options.xmax - options.xmin);
00056         }
00057         
00058 //Converts the given y absolute position to the respective ordinata
00059 double PnlGraph::calculate_ordinata(double y)
00060         {
00061             int width,height;  
00062             this->DoGetClientSize(& width,&height);
00063             return options.ymax- y / height * fabs(options.ymax - options.ymin);
00064         }
00065         
00066         
00067 PnlGraph::PnlGraph(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxString& name) : wxPanel(parent,id,pos,size,style,name)
00068 { 
00069     PopLista =  new wxMenu(_("")  );
00070         PopLista->Append(1025,_("Proprieta..."),_("Cambia le proprietÓ dell'elemento corrente"), wxITEM_NORMAL);
00071     SetCursor(wxCURSOR_HAND); 
00072 } 
00073 
00074 
00075 void PnlGraph::OnPaint(wxPaintEvent& event)
00076 {  int i;
00077    
00078    this->SetBackgroundColour(options.bgcolor);
00079   
00080 
00081    this->Refresh();
00082     
00083    sheet= new wxPaintDC(this);
00084    sheet->BeginDrawing();
00085    draw_grid();
00086    draw_axis();
00087   
00088    draw_steps();
00089  /*--------------------------------------Function drawing loop-------------------*/
00090   
00091   for(i=1;i<=options.functions.nelements;i++)
00092   {if(options.functions.ExtractDrawFlag(i)==1)
00093     draw_function(options.functions.ExtractStructure(i),options.functions.ExtractColor(i),options.functions.ExtractWidth(i));                                             
00094   }
00095  /*-----------------------------------------------------------------------------------------*/
00096  
00097   sheet->EndDrawing();
00098  sheet->~wxPaintDC();
00099 
00100  event.Skip(0);
00101  
00102 }
00103 
00104 
00105 /* -------------------------------------------------------------
00106    funzione: draw_axis
00107    input:    void
00108    output:   void
00109    descrizione: draw the x,y axis to the panel
00110 ----------------------------------------------------------------- */
00111 
00112 void PnlGraph::draw_axis(void)
00113 {wxPen penna;
00114 
00115 
00116 penna.SetColour(0,0,0);
00117  penna.SetWidth(1);
00118 
00119  sheet->SetPen(penna);
00120  
00121 if((options.xaxis==1)&&(options.ymin<=0))
00122  sheet->DrawLine((int)calculate_x(options.xmin),(int)calculate_y(0),(int)calculate_x(options.xmax),(int)calculate_y(0));
00123 
00124 if((options.yaxis==1)&&(options.xmin<=0)) 
00125  sheet->DrawLine((int)calculate_x(0),(int)calculate_y(options.ymin),(int)calculate_x(0),(int)calculate_y(options.ymax));
00126 
00127 
00128      
00129 }
00130 
00131 
00132 
00133 /* -------------------------------------------------------------
00134    function: draw_function
00135    input:    function structure returned by the parser (rif)
00136    output:   void
00137    description: draw the received function to the panel
00138 ----------------------------------------------------------------- */
00139 
00140 void PnlGraph::draw_function(rif* pointstructure,wxColour color,int width)
00141 {double step;
00142  wxPen penna;
00143  double val;
00144  double val2;
00145  double values[2];
00146  double values2[2];
00147  int error;
00148 
00149  int error1;
00150  TParserValore valore;
00151   
00152  penna.SetColour(color);
00153  penna.SetWidth(width);
00154  sheet->SetPen(penna);
00155  step=(options.xmax-options.xmin)/2000;
00156  for(values[0]=options.xmin;values[0]<=(options.xmax-step);values[0]+=step)  
00157  { 
00158    values2[0]=values[0]+step;
00159    val=valore.CalcolaValore(pointstructure,values2,&error);
00160    val2=valore.CalcolaValore(pointstructure,values,&error1);
00161   if((val>= options.ymin)&&(val<= options.ymax)&&(error==0)&&(error1==0))
00162    sheet->DrawLine(calculate_x(values[0]),calculate_y(val2),calculate_x(values2[0]),calculate_y(val));
00163    
00164  }  
00165  
00166   
00167 }
00168 
00169 
00170 /* -------------------------------------------------------------
00171    function: draw_grid
00172    input:    void
00173    output:   void
00174    description: draw the orizontal and vertical grids to the panel
00175 ----------------------------------------------------------------- */
00176 void PnlGraph::draw_grid(void)
00177 {wxPen penna;
00178  double step,i; 
00179 
00180   
00181   wxFont Myfont(8,wxROMAN ,wxNORMAL,wxLIGHT);
00182   sheet->SetFont(Myfont);
00183  
00184  penna.SetColour(0,255,0);
00185  
00186  
00187  sheet->SetPen(penna);
00188  
00189  if(options.xgrid)
00190  {step=((double)options.xmax-(double)options.xmin)/(double)options.nstepsx;
00191 
00192  
00193   for(i=options.xmin+step;i<=options.xmax;i+=step) 
00194   {  
00195      sheet->DrawLine((int)calculate_x(i),(int)calculate_y(options.ymin),(int)calculate_x(i),(int)calculate_y(options.ymax));
00196    
00197   }
00198  } 
00199  if(options.ygrid)
00200  {step=(options.ymax-options.ymin)/options.nstepsy;
00201  
00202   for(i=options.ymin+step;i<options.ymax;i+=step) 
00203    {  
00204       sheet->DrawLine((int)calculate_x(options.xmin),(int)calculate_y(i),(int)calculate_x(options.xmax),(int)calculate_y(i));
00205    }
00206  }  
00207  
00208  
00209 }
00210 
00211 
00212 //Process the left-down event
00213 
00214 void PnlGraph::OnLeftDown(wxMouseEvent& event)
00215 {
00216  SetFocus();
00217  wxMouseEvent cevent(EVT_MOUSE_GRAPH_LCLICK);
00218  // Give it some contents
00219     cevent.m_x=event.GetPosition().x;
00220     cevent.m_y=event.GetPosition().y;
00221  // Send it
00222  if (GetParent())
00223      GetParent()->AddPendingEvent(cevent);
00224  event.Skip();
00225 }     
00226 //Process the right-down event
00227 void PnlGraph::OnRightDown(wxMouseEvent& event)
00228 {        
00229          PopupMenu(PopLista);
00230          wxMouseEvent cevent(EVT_MOUSE_GRAPH_LCLICK);
00231          // Give it some contents
00232            cevent.m_x=event.GetPosition().x;
00233               cevent.m_y=event.GetPosition().y;
00234                // Send it
00235         if (GetParent())
00236             GetParent()->AddPendingEvent(cevent);
00237              event.Skip();
00238 }
00239 
00240 //Process the left-up event
00241 void PnlGraph::OnLeftUp(wxMouseEvent& event)
00242 {
00243 
00244   
00245      event.Skip();
00246 }
00247 
00248 
00249 
00250 
00251 
00252 //Process the mouse motion event
00253 void PnlGraph::OnMotion(wxMouseEvent& event)
00254 {  
00255     wxMouseEvent cevent(EVT_MOUSE_POINTER_MOVED);
00256     // Give it some contents
00257     cevent.m_x=event.GetPosition().x;
00258     cevent.m_y=event.GetPosition().y;
00259     // Send it
00260     if (GetParent())
00261      GetParent()->AddPendingEvent(cevent);
00262 
00263 }
00264 
00265 
00266 
00267 
00268 
00269 //Draw the step with respoctive quota un the axis
00270 void PnlGraph::draw_steps(void)
00271 {  
00272    int i;
00273    double x,y,deltax,deltay;
00274     char stringa[200];  
00275    wxPen penna;
00276    penna.SetColour(0,0,0);
00277    penna.SetStyle(0);
00278  
00279    sheet->SetPen(penna);
00280    x=options.xmin;
00281    y=options.ymin;
00282    
00283 if((options.ymin<=0)&&(options.ymax>=0)&& (options.showstepsx==1))
00284  {
00285   if(options.nstepsx % 2==0)
00286    x=x+(options.xmax-options.xmin)/options.nstepsx;
00287   
00288    deltay=-fabs(options.ymax-options.ymin)/100;
00289    
00290 
00291   for (i=0;i<options.nstepsx;i++)
00292   { if ((double)((int)x)==x) 
00293        sprintf(stringa,"%.0f",x);
00294     else
00295       if ((double)((int)i)==(double)(((int)(i*10))/10))
00296          sprintf(stringa,"%.1f",x);
00297       else
00298          sprintf(stringa,"%.5f",x);
00299     
00300      sheet->DrawText(stringa, (int)calculate_x(x), (int)calculate_y(0));
00301      sheet->DrawLine((int)calculate_x(x),(int)calculate_y(-deltay),(int)calculate_x(x),(int)calculate_y(deltay)); 
00302     x=x+(options.xmax-options.xmin)/options.nstepsx;
00303   }
00304  }    
00305  
00306 //Drawing y axis
00307 if((options.xmin<=0)&&(options.xmax>=0)&& (options.showstepsy==1))
00308  {
00309     deltax=fabs(options.xmax-options.xmin)/100;
00310   for (i=0;i<options.nstepsy;i++)
00311    { 
00312       if ((double)((int)y)==y) 
00313        sprintf(stringa,"%.0f",y);
00314       else
00315        if ((double)((int)y)==(double)(((int)(y*10))/10))
00316           sprintf(stringa,"%.1f",y);
00317        else
00318           sprintf(stringa,"%.5f",y);
00319     
00320           sheet->DrawText(stringa, (int)calculate_x(0), (int)calculate_y(y));
00321      
00322     
00323      sheet->DrawLine((int)calculate_x(-deltax),(int)calculate_y(y),(int)calculate_x(deltax),(int)calculate_y(y)); 
00324      y=y+(options.ymax-options.ymin)/options.nstepsy;
00325    }
00326   }    
00327 }
00328 
00329 
00330 
00331 /*
00332  * MnuproprietaClick
00333  */
00334 void PnlGraph::SettingsClick(wxCommandEvent& event)
00335 {
00336         // insert your code here
00337         DlgOpzioni* frmopz=new DlgOpzioni(this,&(this->options));
00338         frmopz->ShowModal();
00339         frmopz->Destroy();
00340   }
00341   
00342 

 

SourceForge Logo