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 "../../include/Grafico2D/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();
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,color colore,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  penna.SetColour(colore.red,colore.green,colore.blue);
00152  penna.SetWidth(width);
00153  sheet->SetPen(penna);
00154  step=(options.xmax-options.xmin)/2000;
00155  for(values[0]=options.xmin;values[0]<=(options.xmax-step);values[0]+=step)
00156  {
00157    values2[0]=values[0]+step;
00158    val=valore.CalcolaValore(pointstructure,values2,&error);
00159    val2=valore.CalcolaValore(pointstructure,values,&error1);
00160   if((val>= options.ymin)&&(val<= options.ymax)&&(error==0)&&(error1==0))
00161    sheet->DrawLine(calculate_x(values[0]),calculate_y(val2),calculate_x(values2[0]),calculate_y(val));
00162 
00163  }
00164 
00165 
00166 }
00167 
00168 
00169 /* -------------------------------------------------------------
00170    function: draw_grid
00171    input:    void
00172    output:   void
00173    description: draw the orizontal and vertical grids to the panel
00174 ----------------------------------------------------------------- */
00175 void PnlGraph::draw_grid(void)
00176 {wxPen penna;
00177  double step,i;
00178 
00179 
00180   wxFont Myfont(8,wxROMAN ,wxNORMAL,wxLIGHT);
00181   sheet->SetFont(Myfont);
00182 
00183  penna.SetColour(0,255,0);
00184 
00185 
00186  sheet->SetPen(penna);
00187 
00188  if(options.xgrid)
00189  {step=((double)options.xmax-(double)options.xmin)/(double)options.nstepsx;
00190 
00191 
00192   for(i=options.xmin+step;i<=options.xmax;i+=step)
00193   {
00194      sheet->DrawLine((int)calculate_x(i),(int)calculate_y(options.ymin),(int)calculate_x(i),(int)calculate_y(options.ymax));
00195 
00196   }
00197  }
00198  if(options.ygrid)
00199  {step=(options.ymax-options.ymin)/options.nstepsy;
00200 
00201   for(i=options.ymin+step;i<options.ymax;i+=step)
00202    {
00203       sheet->DrawLine((int)calculate_x(options.xmin),(int)calculate_y(i),(int)calculate_x(options.xmax),(int)calculate_y(i));
00204    }
00205  }
00206 
00207 
00208 }
00209 
00210 
00211 //Process the left-down event
00212 
00213 void PnlGraph::OnLeftDown(wxMouseEvent& event)
00214 {
00215  SetFocus();
00216  wxMouseEvent cevent(EVT_MOUSE_GRAPH_LCLICK);
00217  // Give it some contents
00218     cevent.m_x=event.GetPosition().x;
00219     cevent.m_y=event.GetPosition().y;
00220  // Send it
00221  if (GetParent())
00222      GetParent()->AddPendingEvent(cevent);
00223  event.Skip();
00224 }
00225 //Process the right-down event
00226 void PnlGraph::OnRightDown(wxMouseEvent& event)
00227 {
00228          PopupMenu(PopLista);
00229          wxMouseEvent cevent(EVT_MOUSE_GRAPH_LCLICK);
00230          // Give it some contents
00231            cevent.m_x=event.GetPosition().x;
00232               cevent.m_y=event.GetPosition().y;
00233                // Send it
00234         if (GetParent())
00235             GetParent()->AddPendingEvent(cevent);
00236              event.Skip();
00237 }
00238 
00239 //Process the left-up event
00240 void PnlGraph::OnLeftUp(wxMouseEvent& event)
00241 {
00242 
00243 
00244      event.Skip();
00245 }
00246 
00247 
00248 
00249 
00250 
00251 //Process the mouse motion event
00252 void PnlGraph::OnMotion(wxMouseEvent& event)
00253 {
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     {
00262      GetParent()->AddPendingEvent(cevent);
00263     }
00264     event.Skip();
00265 
00266 }
00267 
00268 
00269 
00270 
00271 
00272 //Draw the step with respoctive quota un the axis
00273 void PnlGraph::draw_steps(void)
00274 {
00275    int i;
00276    double x,y,deltax,deltay;
00277    wxString stringa;
00278    wxPen penna;
00279    penna.SetColour(0,0,0);
00280    penna.SetStyle(0);
00281 
00282    sheet->SetPen(penna);
00283    x=options.xmin;
00284    y=options.ymin;
00285 
00286 if((options.ymin<=0)&&(options.ymax>=0)&& (options.showstepsx==1))
00287  {
00288   if(options.nstepsx % 2==0)
00289    x=x+(options.xmax-options.xmin)/options.nstepsx;
00290 
00291    deltay=-fabs(options.ymax-options.ymin)/100;
00292 
00293 
00294   for (i=0;i<options.nstepsx;i++)
00295   { if ((double)((int)x)==x)
00296         stringa.Printf(_T("%.0f"),x);
00297     else
00298       if ((double)((int)i)==(double)(((int)(i*10))/10))
00299          stringa.Printf(_T("%.1f"),x);
00300       else
00301          stringa.Printf(_T("%.5f"),x);
00302 
00303      sheet->DrawText(stringa, (int)calculate_x(x), (int)calculate_y(0));
00304      sheet->DrawLine((int)calculate_x(x),(int)calculate_y(-deltay),(int)calculate_x(x),(int)calculate_y(deltay));
00305     x=x+(options.xmax-options.xmin)/options.nstepsx;
00306   }
00307  }
00308 
00309 //Drawing y axis
00310 if((options.xmin<=0)&&(options.xmax>=0)&& (options.showstepsy==1))
00311  {
00312     deltax=fabs(options.xmax-options.xmin)/100;
00313   for (i=0;i<options.nstepsy;i++)
00314    {
00315       if ((double)((int)y)==y)
00316        stringa.Printf(_T("%.0f"),y);
00317       else
00318        if ((double)((int)y)==(double)(((int)(y*10))/10))
00319           stringa.Printf(_T("%.1f"),y);
00320        else
00321           stringa.Printf(_T("%.5f"),y);
00322 
00323           sheet->DrawText(stringa, (int)calculate_x(0), (int)calculate_y(y));
00324 
00325 
00326      sheet->DrawLine((int)calculate_x(-deltax),(int)calculate_y(y),(int)calculate_x(deltax),(int)calculate_y(y));
00327      y=y+(options.ymax-options.ymin)/options.nstepsy;
00328    }
00329   }
00330 }
00331 
00332 
00333 
00334 /*
00335  * MnuproprietaClick
00336  */
00337 void PnlGraph::SettingsClick(wxCommandEvent& event)
00338 {
00339         // insert your code here
00340         DlgOpzioni* frmopz=new DlgOpzioni(this,&(this->options));
00341         frmopz->ShowModal();
00342         frmopz->Destroy();
00343         event.Skip();
00344 }
00345 
00346 wxMenu* PnlGraph::GetPopUp()
00347 { return PopLista;
00348 }
00349 
00350 

 

SourceForge Logo