------------------------------------------------------------------------------
--                                                                          --
--                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: 3234 $ $Date: 2009-04-09 23:05:06 +0400 (Thu, 09 Apr 2009) $
------------------------------------------------------------------------------
with Qt4.V_Box_Layouts.Constructors;
with Qt4.Labels.Constructors;
with Qt4.LCD_Numbers.Constructors;
with Qt4.Objects;
with Qt4.Size_Policies.Constructors;
with Qt4.Sliders.Constructors;

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

package body LCD_Ranges is

   package body Constructors is

      procedure Initialize
       (Self   : not null access LCD_Range'Class;
        Parent : access Qt4.Widgets.Q_Widget'Class);

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

      function Create (Parent : access Qt4.Widgets.Q_Widget'Class := null)
        return not null LCD_Range_Access
      is
         Self : constant not null LCD_Range_Access := new LCD_Range;

      begin
         Initialize (Self, Parent);

         return Self;
      end Create;

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

      function Create
       (Text   : Qt4.Strings.Q_String;
        Parent : access Qt4.Widgets.Q_Widget'Class := null)
          return not null LCD_Range_Access
      is
         Self : constant not null LCD_Range_Access := new LCD_Range;

      begin
         Initialize (Self, Parent);
         Self.Set_Text (Text);

         return LCD_Range_Access (Self);
      end Create;

      ----------------
      -- Initialize --
      ----------------

      procedure Initialize
       (Self   : not null access LCD_Range'Class;
        Parent : access Qt4.Widgets.Q_Widget'Class)
      is
         use type Qt4.Alignment_Flag;

         LCD    : constant not null Qt4.LCD_Numbers.Q_LCD_Number_Access
           := Qt4.LCD_Numbers.Constructors.Create (2);
         Layout : constant not null Qt4.V_Box_Layouts.Q_V_Box_Layout_Access
           := Qt4.V_Box_Layouts.Constructors.Create;

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

         LCD.Set_Segment_Style (Qt4.LCD_Numbers.Filled);

         Self.Slider := Qt4.Sliders.Constructors.Create (Qt4.Horizontal);
         Self.Slider.Set_Range (0, 99);
         Self.Slider.Set_Value (0);

         Self.Label := Qt4.Labels.Constructors.Create;
         Self.Label.Set_Alignment (Qt4.Align_H_Center + Qt4.Align_Top);
         Self.Label.Set_Size_Policy
          (Qt4.Size_Policies.Constructors.Create
            (Qt4.Size_Policies.Preferred, Qt4.Size_Policies.Fixed));

         Qt4.Objects.Connect
          (Self.Slider,
           Qt4.Signal ("valueChanged(int)"),
           LCD,
           Qt4.Slot ("display(int)"));
         Qt4.Objects.Connect
          (Self.Slider,
           Qt4.Signal ("valueChanged(int)"),
           Self,
           Qt4.Signal ("valueChanged(int)"));

         Self.Label := Qt4.Labels.Constructors.Create;
         Self.Label.Set_Alignment (Qt4.Align_H_Center + Qt4.Align_Top);
         Self.Label.Set_Size_Policy
          (Qt4.Size_Policies.Constructors.Create
            (Qt4.Size_Policies.Preferred, Qt4.Size_Policies.Fixed));
         Layout.Add_Widget (LCD);
         Layout.Add_Widget (Self.Slider);
         Layout.Add_Widget (Self.Label);
         Self.Set_Layout (Layout);
         Self.Set_Focus_Proxy (Self.Slider);
      end Initialize;

   end Constructors;

   ------------------------
   -- Emit_Value_Changed --
   ------------------------

   procedure Emit_Value_Changed
    (Self      : not null access LCD_Range'Class;
     New_Value : Qt4.Q_Integer)
       is separate;

   ---------------
   -- Set_Range --
   ---------------

   procedure Set_Range
    (Self      : not null access LCD_Range'Class;
     Min_Value : Qt4.Q_Integer;
     Max_Value : Qt4.Q_Integer)
   is
   begin
      Self.Slider.Set_Range (Min_Value, Max_Value);
   end Set_Range;

   --------------
   -- Set_Text --
   --------------

   procedure Set_Text
    (Self : not null access LCD_Range'Class;
     Text : Qt4.Strings.Q_String)
   is
   begin
      Self.Label.Set_Text (Text);
   end Set_Text;

   ---------------
   -- Set_Value --
   ---------------

   procedure Set_Value
    (Self  : not null access LCD_Range'Class;
     Value : Qt4.Q_Integer)
   is
   begin
      Self.Slider.Set_Value (Value);
   end Set_Value;

end LCD_Ranges;
