------------------------------------------------------------------------------
--                                                                          --
--                An Ada2005 binding to the Qt C++ framework                --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
-- Copyright © 2008-2011 Vadim Godunko <vgodunko@gmail.com>                 --
--                                                                          --
-- QtAda is free software;  you can  redistribute it and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 2,  or (at your option) any later ver- --
-- sion. QtAda is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
-- for  more details.  You should have  received  a copy of the GNU General --
-- Public License distributed with QtAda;  see file COPYING.  If not, write --
-- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
-- Boston, MA 02110-1301, USA.                                              --
--                                                                          --
------------------------------------------------------------------------------
--  $Revision: 3821 $ $Date: 2011-01-24 17:14:50 +0300 (Mon, 24 Jan 2011) $
------------------------------------------------------------------------------
with Qt4.Brushes;
with Qt4.Colors;
with Qt4.Painters;
with Qt4.Pens;
with Qt4.Points;
with Qt4.Times;

with Analog_Clocks.Moc;

package body Analog_Clocks is

   use type Qt4.Q_Integer;

   Hour_Hand : constant Qt4.Points.Q_Point_Array :=
    (Qt4.Points.Create (7, 8),
     Qt4.Points.Create (-7, 8),
     Qt4.Points.Create (0, -40));

   Hour_Color   : constant Qt4.Colors.Q_Color
     := Qt4.Colors.Create (127, 0, 127);

--   Minute_Hand : constant Points.Q_Point_Array :=
--    (Qt4.Points.Create (7, 8),
--     Qt4.Points.Create (-7, 8),
--     Qt4.Points.Create (0, -70));

   Minute_Color : constant Qt4.Colors.Q_Color
     := Qt4.Colors.Create (0, 127, 127, 191);

   ------------------
   -- Constructors --
   ------------------

   package body Constructors is

      ------------
      -- Create --
      ------------

      function Create
       (Parent : access Qt4.Widgets.Q_Widget'Class := null)
          return not null Analog_Clock_Access
      is
--         use Qt4;
--         use Analog_Clocks.Impl;

      begin
         return Self : constant not null Analog_Clock_Access
           := new Analog_Clock
         do
            Qt4.Widgets.Directors.Constructors.Initialize (Self, Parent);

--            declare
--               Timer : constant not null access Timers.Q_Timer'Class
--                 := Timers.Constructors.Create (Result);
--
--            begin
--               Objects.Connect (Timer,
--                                Signal ("timeout()"),
--                                Result,
--                                Slot ("update()"));
--               Timer.Start (1.0);
--
--               --  XXX Раскомментировать, когда появиться реализация
--               --  Set_Window_Title.
--
--               --  Analog_Clock_Impl'Class (Result.all)'Access.Set_Window_Title
--               --   ("Analog Clock");
--               Analog_Clock_Impl'Class (Result.all)'Access.Resize (200, 200);
--            end;
            Self.Resize (200, 200);
         end return;
      end Create;

   end Constructors;

   -----------------
   -- Paint_Event --
   -----------------

   overriding procedure Paint_Event
    (Self  : not null access Analog_Clock;
     Event : not null access Qt4.Paint_Events.Q_Paint_Event'Class)
   is
      pragma Unreferenced (Event);

      use type Qt4.Q_Real;

      Side    : Qt4.Q_Integer;
      Time    : constant Qt4.Times.Q_Time := Qt4.Times.Current_Time;
      Painter : Qt4.Painters.Q_Painter := Qt4.Painters.Create (Self);

   begin
      Side :=
        Qt4.Q_Integer'Min
         (Qt4.Widgets.Width (Self), Qt4.Widgets.Height (Self));

      Painter.Set_Render_Hint (Qt4.Painters.Antialiasing);
      Painter.Translate
       (Qt4.Q_Real (Qt4.Widgets.Width (Self)) / 2.0,
        Qt4.Q_Real (Qt4.Widgets.Height (Self)) / 2.0);
      Painter.Scale (Qt4.Q_Real (Side) / 200.0, Qt4.Q_Real (Side) / 200.0);

      Painter.Set_Pen (Qt4.No_Pen);
      Painter.Set_Brush (Qt4.Brushes.Create (Hour_Color));

      Painter.Save;
      Painter.Rotate
       (30.0 * (Qt4.Q_Real (Time.Hour) + Qt4.Q_Real (Time.Minute) / 60.0));
--      Painter.Draw_Convex_Polygon (Hour_Hand);
      Painter.Restore;

      Painter.Set_Pen (Qt4.Pens.Create (Hour_Color));

      for I in 0 .. 11 loop
         Painter.Draw_Line (88, 0, 96, 0);
         Painter.Rotate (30.0);
      end loop;

      Painter.Set_Pen (Qt4.No_Pen);
      Painter.Set_Brush (Qt4.Brushes.Create (Minute_Color));

      Painter.Save;
      Painter.Rotate
       (6.0 * (Qt4.Q_Real (Time.Minute) + Qt4.Q_Real (Time.Second) / 60.0));
--      Painter.Draw_Convex_Polygon (Minute_Hand);
      Painter.Restore;

      Painter.Set_Pen (Qt4.Pens.Create (Minute_Color));

      for J in 0 .. 59 loop
         if J mod 5 /= 0  then
            Painter.Draw_Line (92, 0, 96, 0);
         end if;

         Painter.Rotate (6.0);
      end loop;
   end Paint_Event;

end Analog_Clocks;
