------------------------------------------------------------------------------
--                                                                          --
--                An Ada2005 binding to the Qt C++ framework                --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
-- Copyright © 2008, 2009 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: 3266 $ $Date: 2009-04-18 13:27:41 +0400 (Sat, 18 Apr 2009) $
------------------------------------------------------------------------------
with Qt4.Colors;
with Qt4.Painters;
with Qt4.Palettes.Constructors;
with Qt4.Strings;
with Qt4.Paint_Events;

with Cannon_Fields.MOC;
pragma Warnings (Off, Cannon_Fields.MOC);
--  Child package MOC must be included in the partition.

package body Cannon_Fields is

   -----------
   -- Angle --
   -----------

   function Angle (Self : not null access constant Cannon_Field'Class)
     return Qt4.Q_Integer
   is
   begin
      return Self.Current_Angle;
   end Angle;

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

   package body Constructors is

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

      function Create (Parent : access Qt4.Widgets.Q_Widget'Class := null)
        return not null Cannon_Field_Access
      is
         Self    : constant Cannon_Field_Access := new Cannon_Field;
         Color   : constant Qt4.Colors.Q_Color
           := Qt4.Colors.Create (250, 250, 200);
         Palette : constant Qt4.Palettes.Q_Palette
           := Qt4.Palettes.Constructors.Create (Color);

      begin
         Qt4.Widgets.Directors.Constructors.Initialize (Self, Parent);

         Self.Current_Angle := 45;
         Self.Set_Palette (Palette);
         Self.Set_Auto_Fill_Background (True);

         return Self;
      end Create;

   end Constructors;

   ------------------------
   -- Emit_Angle_Changed --
   ------------------------

   procedure Emit_Angle_Changed
    (Self       : not null access Cannon_Field'Class;
     New_Angele : Qt4.Q_Integer)
       is separate;

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

   overriding procedure Paint_Event
    (Self  : not null access Cannon_Field;
     Event : not null access Qt4.Paint_Events.Q_Paint_Event'Class)
   is
     pragma Unreferenced (Event);
     --  Параметр не используется.

     Painter      : Qt4.Painters.Q_Painter := Qt4.Painters.Create (Self);
     Angle_String : constant Qt4.Strings.Q_String
       := Qt4.Strings.From_Utf_16
           ("Angle = " & Integer'Wide_Image (Integer (Self.Current_Angle)));

   begin
      Painter.Draw_Text (200, 200, Angle_String);
   end Paint_Event;

   ---------------
   -- Set_Angle --
   ---------------

   procedure Set_Angle
    (Self  : not null access Cannon_Field'Class;
     Angle : Qt4.Q_Integer)
   is
      use type Qt4.Q_Integer;

      Aux : Qt4.Q_Integer := Angle;

   begin
      if Aux < 5 then
         Aux := 5;

      elsif Aux > 70 then
         Aux := 70;
      end if;

      if Self.Current_Angle = Aux then
        return;
      end if;

      Self.Current_Angle := Aux;
      Self.Update;
      Self.Emit_Angle_Changed (Self.Current_Angle);
   end Set_Angle;

end Cannon_Fields;
