Advertisement
AntonioVillanueva

Reloj Analogico wxWidgets

Jul 12th, 2018
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.50 KB | None | 0 0
  1. //Reloj analogico en  wxWidgets Antonio Villanueva segura
  2. //g++ -Wall -static-libstdc++ -std=c++11 -Wunused-but-set-variable `wx-config --cxxflags` -o reloj *.cpp `wx-config --libs`
  3. //Declara la clase aplicacion
  4.  
  5. #include <iostream>
  6. #include "wx/wx.h"
  7. #include <wx/popupwin.h>
  8. #include <wx/menu.h> //menu en el frame
  9. #include <wx/event.h>
  10. #include <wx/pen.h> //lapiz
  11. #include <wx/dcmemory.h>
  12. #include <wx/sizer.h>
  13. #include <wx/button.h>
  14. #include <wx/dc.h>
  15. #include <wx/dcbuffer.h>
  16. #include <wx/dcclient.h>
  17. #include <wx/font.h>
  18. #include <wx/timer.h>
  19.  
  20. #include <wx/spinbutt.h> //spin button
  21. //using namespace std;
  22. #define PI 3.14159265
  23. #define RADIO 90
  24. //Declaraciones
  25.  //Cada aplicacion wxWidget define una clase derivada de wxApp
  26. class MiApp:public wxApp
  27. {
  28.     public:
  29.     //Llamado al inicio startup, es como el main en c
  30.     virtual bool OnInit();//main wxWidgets , mas abajo se implementa
  31.    
  32. };
  33.  
  34. //Declaracion de la clase frame principal
  35.  
  36. //----------------------------------------------------------------------
  37.  
  38. class MiFrame:public wxFrame
  39. {
  40.     public:
  41.     //Constructor de la clase
  42.     MiFrame();
  43.    
  44.     private:
  45.     wxTimer m_timer;//Variable temporizador
  46.     void OnTimer(wxTimerEvent& event);//Timer    
  47.     void ActualizaTiempo (wxSpinEvent& event);//Actualiza setting ti hh:mm:ss
  48.     void OnSet(wxCommandEvent& event);//Menu setting horario    
  49.     void OnAbout(wxCommandEvent& event);// Menu Acerca de
  50.     void OnQuit(wxCommandEvent& event);// Menu Salir    
  51.     void AccionBoton(wxCommandEvent& event);////Accion para el evento de boton    
  52.     void DibujaReloj(wxDC& dc);
  53.     void Numeros (wxDC& dc,int x,int y,int radio);//Dibuja numeros del Reloj
  54.     void Aguja(wxDC& dc,int x,int y,int radio,int pos,bool hs);//Dibuja Agujas .
  55.     float radian(float angulo);//Conversion de angulo a Radianes
  56.     wxPanel* Panel1;
  57.     int horas,minutos,segundos;//Control del tiemo
  58.     int CentroX,CentroY;//Centro de la ventana
  59.     wxSpinButton* heure;//Setting horas
  60.     wxSpinButton* mins;//Setting minutos
  61.     wxSpinButton* sec; //Setting segundos  
  62.     /*Macro para informar a wxWidgets de la gestion de eventos
  63.     *Declara la tabla de eventos en esta clase ,mas abajo
  64.     * la implemento entre BEGIN_ y END_EVENT_TABLE
  65.     */
  66.     DECLARE_EVENT_TABLE()
  67. };
  68.  
  69. //----------------------------------------------------------------------
  70.  
  71. /*Implementacion , MiApp
  72. *Cuando se crea el objeto MiApp se asigna a la variable global wxTheApp
  73. * pero mejor que hacer un CAST emplear el MACRO  DECLARE_APP despues de
  74. * la declaracion de la clase , wxGetApp retorna una ref. al objeto MiApp
  75. */
  76. DECLARE_APP(MiApp)
  77.  
  78. //Macro que Proporciona a wxWidgets los medios de crear el objeto MiApp
  79. IMPLEMENT_APP(MiApp)
  80.  
  81. //----------------------------------------------------------------------
  82.  
  83. //Implementacion OnInit,Inicializa la aplicacion
  84.  
  85. bool MiApp::OnInit()
  86. {
  87.     //Crea la ventana principal , una instancia de nuestra clase MiFrame
  88.     //El titulo lo pasamos al constructor envuelto en el macro wxT
  89.     //macro de conversion de strings y char al tipo apropiado
  90.     MiFrame *frame=new MiFrame();
  91.    
  92.     //Mostrar la ventana
  93.     frame->Show(true);
  94.    
  95.     //Arranca el bucle de eventos
  96.     return true ;//Si false limpia sus estructuras y sale
  97. }
  98.  
  99. //----------------------------------------------------------------------
  100. wxWindowIDRef ID_TEMPORIZADOR=wxWindow::NewControlId();
  101. wxWindowIDRef ID_SET=wxWindow::NewControlId();
  102. //Tabla de eventos para MiFrame DECLARE_EVENT_TABLE() en MiFrame
  103. //Deteccion de los clicks de raton con wxID_ABOUT y wxID_EXIT
  104. //Que se dirigen a MiFrame::OnAbout y OnQuit
  105. BEGIN_EVENT_TABLE ( MiFrame, wxFrame)
  106.     EVT_TIMER(ID_TEMPORIZADOR, MiFrame::OnTimer)//Temporizador 1s
  107.     EVT_MENU(ID_SET,    MiFrame::OnSet)
  108.     EVT_MENU(wxID_ABOUT,    MiFrame::OnAbout)
  109.     EVT_MENU(wxID_EXIT,     MiFrame::OnQuit)   
  110.     EVT_SPIN(wxID_ANY, MiFrame::ActualizaTiempo)////Actualiza setting ti hh:mm:ss  
  111. END_EVENT_TABLE()
  112.  
  113. //----------------------------------------------------------------------
  114. //Constructor de MiFrame
  115. //No resizable wxDEFAULT_FRAME_STYLE & ~(wxRESIZE_BORDER | wxMAXIMIZE_BOX)
  116. MiFrame::MiFrame():wxFrame(NULL,wxID_ANY,"Reloj",wxDefaultPosition,wxSize(200,200)
  117.     ,wxDEFAULT_FRAME_STYLE & ~(wxRESIZE_BORDER | wxMAXIMIZE_BOX)),
  118.     m_timer(this,ID_TEMPORIZADOR),horas(12),minutos(1),segundos(1)
  119. {
  120.     //Timer
  121.      m_timer.Start(1000); // 1 second interval 1000
  122.      
  123.     //Barra de Menu en el parte superior del Frame
  124.     wxMenuBar* BarraDeMenu=new wxMenuBar;
  125.    
  126.     //Ficheros del Menu
  127.     wxMenu* FicherosMenu=new wxMenu;
  128.    
  129.     FicherosMenu->Append(ID_SET,wxT("&Set"),wxT("Ajusta hora"));
  130.     FicherosMenu->AppendSeparator();
  131.     FicherosMenu->Append(wxID_ABOUT,wxT("&Acerca de "),wxT("info about"));
  132.     FicherosMenu->AppendSeparator();
  133.     FicherosMenu->Append(wxID_EXIT,wxT("&SALIR "),wxT("salir y cerrar"));  
  134.    
  135.     //Anade los Ficheros del Menu a la barra de Menu
  136.     BarraDeMenu-> Append(FicherosMenu,"Opciones");
  137.    
  138.     //Anade esta barra al frame actual
  139.     SetMenuBar(BarraDeMenu);
  140.          
  141.     //Crea un wxPanel
  142.     //WxPanel1 = new wxPanel(this, ID_WXPANEL1, wxPoint(5,5), wxSize(299,288));
  143.     Panel1=new wxPanel(this,wxID_ANY, wxDefaultPosition,wxSize (50,50) );
  144.    
  145.     //Sizer  
  146.     wxBoxSizer* horizontal = new wxBoxSizer(wxHORIZONTAL);//sizer Horizontal
  147.    
  148.     //Anade Sizer al wxPanel
  149.     Panel1->SetSizer(horizontal);
  150.    
  151.     //Centro de la ventana
  152.     CentroX=GetClientSize().x/2;
  153.     CentroY=GetClientSize().y/2;   
  154. }
  155. //----------------------------------------------------------------------
  156. void MiFrame::DibujaReloj(wxDC& dc){
  157.      
  158.     dc.SetPen(wxPen(*wxBLACK,5));//Lapiz color 
  159.     dc.SetBrush(wxBrush(*wxWHITE));//Fondo
  160.    
  161.     //Dibuja circulo externo
  162.     dc.DrawCircle(CentroX,CentroY,RADIO);
  163.  
  164.     //Dibuja numeros
  165.     Numeros(dc,CentroX-5,CentroY-5,RADIO-10);
  166.    
  167.     //Hora digital en el centro del reloj
  168.     wxString tiempoDigital=wxString::Format ("%i",horas)+":";
  169.     tiempoDigital+= wxString::Format ("%i",minutos)+":";
  170.     tiempoDigital+= wxString::Format ("%i",segundos);
  171.    
  172.     dc.DrawText(tiempoDigital,wxPoint(CentroX-20,CentroY+10)); 
  173. }
  174. //----------------------------------------------------------------------
  175. void MiFrame::OnTimer(wxTimerEvent& event) //Cada segundo se ejecuta esto
  176. {      
  177.     wxClientDC dc(Panel1);//Pizarra de dibujo
  178.  
  179.     dc.Clear();//Limpia la pizarra
  180.    
  181.     DibujaReloj(dc);//Dibuja el reloj base,circulo,numeros  
  182.    
  183.     //Aguja HORA
  184.     dc.SetPen( wxPen (*wxBLUE,4));
  185.     Aguja(dc,CentroX,CentroY,RADIO-20,horas,true);//wxClientDC,X1,Y1,Radio,hora
  186.    
  187.     //Aguja MINUTOS
  188.     dc.SetPen( wxPen (*wxCYAN,3));
  189.     Aguja(dc,CentroX,CentroY,RADIO-15,minutos,false);//wxClientDC,X1,Y1,Radio,minutos
  190.    
  191.     //Aguja SEGUNDO
  192.     dc.SetPen( wxPen (*wxLIGHT_GREY,2));
  193.     Aguja(dc,CentroX,CentroY,RADIO-10,segundos,false);//wxClientDC,X1,Y1,Radio,segundos
  194.    
  195.     //Control deL tiempo
  196.     if (segundos++>60){//60 segundos un minuto
  197.         segundos=1;
  198.         minutos++;
  199.     }
  200.     if (minutos>60){minutos=1;horas++;}//60 minutos una hora
  201.     if (horas>12){minutos=0;horas=0;}//12 horas
  202.  
  203. }
  204. //----------------------------------------------------------------------
  205. void MiFrame::Numeros (wxDC& dc,int x,int y,int radio){//Dibujo los numeros del reloj
  206.     int numero=3;
  207.    
  208.     wxFont fuente (10,wxFONTFAMILY_MODERN,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD);
  209.     dc.SetFont(fuente);
  210.    
  211.     dc.SetBackgroundMode(wxTRANSPARENT);
  212.     dc.SetTextForeground(*wxRED);
  213.     dc.SetTextBackground(*wxWHITE);
  214.    
  215.     //Dibuja los numeros del reloj cada 30 grados
  216.     for (int angulo=0;angulo <360;angulo+=30){
  217.         dc.DrawText ( wxString::Format(wxT("%i"),numero), x+ radio*cos(radian(angulo)), y+radio*sin(radian(angulo)));
  218.         numero++;//Numero hora
  219.         if (numero>12){numero=1;} //A partir de las 12 comienza en 1
  220.     }
  221. }
  222. //----------------------------------------------------------------------
  223. void MiFrame::Aguja(wxDC& dc,int x,int y,int radio,int pos,bool hs=true){//Dibuja Aguja Horas o Seg.
  224.     int grados (hs?30:6);//Angulo HORAS o SEGUNDOS
  225.     dc.DrawLine(x,y,  x+ radio*cos(radian(pos*grados -90)), y+radio*sin(radian(pos*grados-90)));
  226. }
  227. //----------------------------------------------------------------------
  228. float MiFrame::radian(float angulo){return angulo *PI/180.0;}//Angulo a radianes
  229. //----------------------------------------------------------------------
  230. void MiFrame::OnAbout(wxCommandEvent& event){// Menu Acerca de
  231.      wxMessageBox (wxT("Antonio Villanueva Segura"));
  232. }
  233. //----------------------------------------------------------------------
  234. void MiFrame::OnQuit(wxCommandEvent& event){Close();}// Menu Salir
  235. //----------------------------------------------------------------------
  236. void MiFrame::OnSet(wxCommandEvent& event){//Barra menu setting horario
  237.      wxFrame *panel = new wxFrame(this,wxID_ANY,wxT("Settings"),wxDefaultPosition,wxSize(150,35),
  238.      wxDEFAULT_FRAME_STYLE & ~(wxRESIZE_BORDER | wxMAXIMIZE_BOX));//fijo    
  239.      
  240.      wxPanel* ventana=new wxPanel(panel,wxID_ANY,wxDefaultPosition,wxSize(150,35) );
  241.      
  242.      //Sizers vertical contiene el horizontal 1 y 2
  243.      wxBoxSizer* vertical = new wxBoxSizer(wxVERTICAL);//sizer Vertical
  244.      wxBoxSizer* hor1 = new wxBoxSizer(wxHORIZONTAL);//sizer Horizontal  
  245.      wxBoxSizer* hor2 = new wxBoxSizer(wxHORIZONTAL);//sizer Horizontal  
  246.      vertical->Add(hor2,wxEXPAND);//Anadimos al wxBoxSizer los horisontales
  247.      vertical->Add(hor1,wxEXPAND);
  248.    
  249.     //wxSpinButton para ajustar Horas Minutos Segundos
  250.      heure=new wxSpinButton(panel,wxID_ANY,wxDefaultPosition,wxSize(50,30),wxSP_VERTICAL);
  251.      mins=new wxSpinButton(panel,wxID_ANY,wxDefaultPosition,wxSize(50,30),wxSP_VERTICAL);
  252.      sec=new wxSpinButton(panel,wxID_ANY,wxDefaultPosition,wxSize(50,30),wxSP_VERTICAL);
  253.      
  254.      // Defino el rango de los wxSpinButton ..SetRange (int min, int max)    
  255.      heure->SetRange(1,12);
  256.      mins->SetRange(1,60);
  257.      sec->SetRange(1,60);    
  258.      
  259.      //Recupero las variables HH:SS:MM y las copio en el wxSpinButto
  260.      heure->SetValue(horas);//Copio horas en el wxSpinButton heure
  261.      mins->SetValue(minutos);//Copio minutos en el wxSpinButton mins
  262.      sec->SetValue(segundos);//Copio segundos en el wxSpinButton sec         
  263.      
  264.     //Anado los wxSpinButton al wxBoxSizer Horizontal2 que va en el vertical
  265.     hor2->Add(heure);
  266.     hor2->Add(mins);
  267.     hor2->Add(sec);                        
  268.    
  269.     //wxPanel utiliza el wxBoxSizer vertical
  270.     ventana->SetSizer(vertical);//wxPanel utiliza el wxSizer horizontal          
  271.      
  272.     panel->Show(true);
  273. }
  274. //----------------------------------------------------------------------
  275. void MiFrame::ActualizaTiempo (wxSpinEvent& event){//Actualiza setting ti hh:mm:ss
  276.     horas=heure->GetValue();
  277.     minutos=mins->GetValue();
  278.     segundos=sec->GetValue();      
  279. }
  280. //----------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement