------------------------------------------------------------------------------
--                                                                          --
--                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.Combo_Boxes.Constructors;
with Qt4.Grid_Layouts.Constructors;
with Qt4.Group_Boxes.Constructors;
with Qt4.Labels.Constructors;
with Qt4.Line_Edits.Constructors;
with Qt4.Objects;
with Qt4.Strings;

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

package body My_Widgets is

   --------------------
   -- Access_Changed --
   --------------------

   procedure Access_Changed
    (Self  : not null access My_Widget'Class;
     Value : Qt4.Q_Integer)
   is
   begin
      case Value is
         when 0 =>
            Self.Access_Line_Edit.Set_Read_Only (False);

         when 1 =>
            Self.Access_Line_Edit.Set_Read_Only (True);

         when others =>
           null;
      end case;
   end Access_Changed;

   -----------------------
   -- Alignment_Changed --
   -----------------------

   procedure Alignment_Changed
    (Self  : not null access My_Widget'Class;
     Value : Qt4.Q_Integer)
   is
      use type Qt4.Alignment;

   begin
      case Value is
         when 0 =>
            Self.Alignment_Line_Edit.Set_Alignment (+Qt4.Align_Left);

         when 1 =>
            Self.Alignment_Line_Edit.Set_Alignment (+Qt4.Align_Center);

         when 2 =>
            Self.Alignment_Line_Edit.Set_Alignment (+Qt4.Align_Right);

         when others =>
           null;
      end case;
   end Alignment_Changed;

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

   function Create return not null My_Widget_Access is
      Self       : constant My_Widget_Access := new My_Widget;

      --  Echo

      Echo_Group : constant not null access Qt4.Group_Boxes.Q_Group_Box'Class
        := Qt4.Group_Boxes.Constructors.Create
            (Qt4.Strings.From_Utf_16 ("Echo"));
      Echo_Label : constant not  null access Qt4.Labels.Q_Label'Class
          := Qt4.Labels.Constructors.Create
              (Qt4.Strings.From_Utf_16 ("Mode :"));
      Echo_Combo_Box :
        constant not  null access Qt4.Combo_Boxes.Q_Combo_Box'Class
          := Qt4.Combo_Boxes.Constructors.Create;

      --  Validator

      Validator_Group    :
        constant not null access Qt4.Group_Boxes.Q_Group_Box'Class
          := Qt4.Group_Boxes.Constructors.Create
              (Qt4.Strings.From_Utf_16 ("Validator"));
      Validator_Label    : constant not  null access Qt4.Labels.Q_Label'Class
          := Qt4.Labels.Constructors.Create
              (Qt4.Strings.From_Utf_16 ("Type :"));
      Validator_Combo_Box :
        constant not  null access Qt4.Combo_Boxes.Q_Combo_Box'Class
          := Qt4.Combo_Boxes.Constructors.Create;

      --  Alignment

      Alignment_Group    :
        constant not null access Qt4.Group_Boxes.Q_Group_Box'Class
          := Qt4.Group_Boxes.Constructors.Create
              (Qt4.Strings.From_Utf_16 ("Alignment"));
      Alignment_Label    : constant not  null access Qt4.Labels.Q_Label'Class
          := Qt4.Labels.Constructors.Create
              (Qt4.Strings.From_Utf_16 ("Type :"));
      Alignment_Combo_Box :
        constant not  null access Qt4.Combo_Boxes.Q_Combo_Box'Class
          := Qt4.Combo_Boxes.Constructors.Create;

      --  Input Mask

      Input_Mask_Group    :
        constant not null access Qt4.Group_Boxes.Q_Group_Box'Class
          := Qt4.Group_Boxes.Constructors.Create
              (Qt4.Strings.From_Utf_16 ("Input Mask"));
      Input_Mask_Label    : constant not  null access Qt4.Labels.Q_Label'Class
          := Qt4.Labels.Constructors.Create
              (Qt4.Strings.From_Utf_16 ("Type :"));
      Input_Mask_Combo_Box :
        constant not  null access Qt4.Combo_Boxes.Q_Combo_Box'Class
          := Qt4.Combo_Boxes.Constructors.Create;

      --  Access

      Access_Group    :
        constant not null access Qt4.Group_Boxes.Q_Group_Box'Class
          := Qt4.Group_Boxes.Constructors.Create
              (Qt4.Strings.From_Utf_16 ("Access"));
      Access_Label    : constant not  null access Qt4.Labels.Q_Label'Class
          := Qt4.Labels.Constructors.Create
              (Qt4.Strings.From_Utf_16 ("Read-only :"));
      Access_Combo_Box :
        constant not  null access Qt4.Combo_Boxes.Q_Combo_Box'Class
          := Qt4.Combo_Boxes.Constructors.Create;

      --  Layouts

      Echo_Layout          : constant not null access
        Qt4.Grid_Layouts.Q_Grid_Layout'Class
          := Qt4.Grid_Layouts.Constructors.Create;

      Validator_Layout          : constant not null access
        Qt4.Grid_Layouts.Q_Grid_Layout'Class
          := Qt4.Grid_Layouts.Constructors.Create;

      Alignment_Layout          : constant not null access
        Qt4.Grid_Layouts.Q_Grid_Layout'Class
          := Qt4.Grid_Layouts.Constructors.Create;

      Input_Mask_Layout          : constant not null access
        Qt4.Grid_Layouts.Q_Grid_Layout'Class
          := Qt4.Grid_Layouts.Constructors.Create;

      Access_Layout          : constant not null access
        Qt4.Grid_Layouts.Q_Grid_Layout'Class
          := Qt4.Grid_Layouts.Constructors.Create;

      Layout          : constant not null access
        Qt4.Grid_Layouts.Q_Grid_Layout'Class
          := Qt4.Grid_Layouts.Constructors.Create;

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

      Self.Access_Line_Edit := Qt4.Line_Edits.Constructors.Create;
      Self.Alignment_Line_Edit := Qt4.Line_Edits.Constructors.Create;
      Self.Echo_Line_Edit := Qt4.Line_Edits.Constructors.Create;
      Self.Input_Mask_Line_Edit := Qt4.Line_Edits.Constructors.Create;
      Self.Validator_Line_Edit := Qt4.Line_Edits.Constructors.Create;

      Qt4.Objects.Connect
       (Access_Combo_Box,
        Qt4.Signal ("activated(int)"),
        Self,
        Qt4.Slot ("accessChanged(int)"));
      Qt4.Objects.Connect
       (Alignment_Combo_Box,
        Qt4.Signal ("activated(int)"),
        Self,
        Qt4.Slot ("alignmentChanged(int)"));

      Qt4.Objects.Connect
       (Echo_Combo_Box,
        Qt4.Signal ("activated(int)"),
        Self,
        Qt4.Slot ("echoChanged(int)"));

      Qt4.Objects.Connect
       (Validator_Combo_Box,
        Qt4.Signal ("activated(int)"),
        Self,
        Qt4.Slot ("validatorChanged(int)"));

      Qt4.Objects.Connect
       (Input_Mask_Combo_Box,
        Qt4.Signal ("activated(int)"),
        Self,
        Qt4.Slot ("inputMaskChanged(int)"));

      Echo_Combo_Box.Add_Item (Qt4.Strings.From_Utf_16 ("Normal"));
      Echo_Combo_Box.Add_Item (Qt4.Strings.From_Utf_16 ("Password"));
      Echo_Combo_Box.Add_Item (Qt4.Strings.From_Utf_16 ("PasswordEchoOnEdit"));
      Echo_Combo_Box.Add_Item (Qt4.Strings.From_Utf_16 ("No Echo"));

      Validator_Combo_Box.Add_Item (Qt4.Strings.From_Utf_16 ("No validator"));
      Validator_Combo_Box.Add_Item
       (Qt4.Strings.From_Utf_16 ("Integer validator"));
      Validator_Combo_Box.Add_Item
       (Qt4.Strings.From_Utf_16 ("Double validator"));

      Alignment_Combo_Box.Add_Item (Qt4.Strings.From_Utf_16 ("Left"));
      Alignment_Combo_Box.Add_Item (Qt4.Strings.From_Utf_16 ("Centered"));
      Alignment_Combo_Box.Add_Item (Qt4.Strings.From_Utf_16 ("Right"));

      Input_Mask_Combo_Box.Add_Item (Qt4.Strings.From_Utf_16 ("No mask"));
      Input_Mask_Combo_Box.Add_Item (Qt4.Strings.From_Utf_16 ("Phone number"));
      Input_Mask_Combo_Box.Add_Item (Qt4.Strings.From_Utf_16 ("ISO date"));
      Input_Mask_Combo_Box.Add_Item (Qt4.Strings.From_Utf_16 ("License key"));

      Access_Combo_Box.Add_Item (Qt4.Strings.From_Utf_16 ("False"));
      Access_Combo_Box.Add_Item (Qt4.Strings.From_Utf_16 ("True"));

      Echo_Layout.Add_Widget (Echo_Label, 0, 0);
      Echo_Layout.Add_Widget (Echo_Combo_Box, 0, 1);
      Echo_Layout.Add_Widget (Self.Echo_Line_Edit, 1, 0, 1, 2);
      Echo_Group.Set_Layout (Echo_Layout);

      Validator_Layout.Add_Widget (Validator_Label, 0, 0);
      Validator_Layout.Add_Widget (Validator_Combo_Box, 0, 1);
      Validator_Layout.Add_Widget (Self.Validator_Line_Edit, 1, 0, 1, 2);
      Validator_Group.Set_Layout (Validator_Layout);

      Alignment_Layout.Add_Widget (Alignment_Label, 0, 0);
      Alignment_Layout.Add_Widget (Alignment_Combo_Box, 0, 1);
      Alignment_Layout.Add_Widget (Self.Alignment_Line_Edit, 1, 0, 1, 2);
      Alignment_Group.Set_Layout (Alignment_Layout);

      Input_Mask_Layout.Add_Widget (Input_Mask_Label, 0, 0);
      Input_Mask_Layout.Add_Widget (Input_Mask_Combo_Box, 0, 1);
      Input_Mask_Layout.Add_Widget (Self.Input_Mask_Line_Edit, 1, 0, 1, 2);
      Input_Mask_Group.Set_Layout (Input_Mask_Layout);

      Access_Layout.Add_Widget (Access_Label, 0, 0);
      Access_Layout.Add_Widget (Access_Combo_Box, 0, 1);
      Access_Layout.Add_Widget (Self.Access_Line_Edit, 1, 0, 1, 2);
      Access_Group.Set_Layout (Access_Layout);

      Layout.Add_Widget (Echo_Group, 0, 0);
      Layout.Add_Widget (Validator_Group, 1, 0);
      Layout.Add_Widget (Alignment_Group, 2, 0);
      Layout.Add_Widget (Input_Mask_Group, 0, 1);
      Layout.Add_Widget (Access_Group, 1, 1);
      Self.Set_Layout (Layout);

      return Self;
   end Create;

   ------------------
   -- Echo_Changed --
   ------------------

   procedure Echo_Changed
    (Self  : not null access My_Widget'Class;
     Value : Qt4.Q_Integer)
   is
   begin
      case Value is
         when 0 =>
            Self.Echo_Line_Edit.Set_Echo_Mode (Qt4.Line_Edits.Normal);

         when 1 =>
            Self.Echo_Line_Edit.Set_Echo_Mode (Qt4.Line_Edits.Password);

         when 2 =>
            Self.Echo_Line_Edit.Set_Echo_Mode
             (Qt4.Line_Edits.Password_Echo_On_Edit);

         when 3 =>
                 Self.Echo_Line_Edit.Set_Echo_Mode (Qt4.Line_Edits.No_Echo);

         when others =>
           null;
      end case;
   end Echo_Changed;

   ------------------------
   -- Input_Mask_Changed --
   ------------------------

   procedure Input_Mask_Changed
    (Self  : not null access My_Widget'Class;
     Value : Qt4.Q_Integer)
   is
   begin
      case Value is
         when 0 =>
            Self.Input_Mask_Line_Edit.Set_Input_Mask
             (Qt4.Strings.From_Utf_16 (""));

         when 1 =>
            Self.Input_Mask_Line_Edit.Set_Input_Mask
             (Qt4.Strings.From_Utf_16 ("+99 99 99 99 99;_"));

         when 2 =>
            Self.Input_Mask_Line_Edit.Set_Input_Mask
             (Qt4.Strings.From_Utf_16 ("0000-00-00"));
            Self.Input_Mask_Line_Edit.Set_Text
             (Qt4.Strings.From_Utf_16 ("00000000"));
            Self.Input_Mask_Line_Edit.Set_Cursor_Position (0);

         when 3 =>
            Self.Input_Mask_Line_Edit.Set_Input_Mask
             (Qt4.Strings.From_Utf_16 (">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#"));

         when others =>
            null;
      end case;
   end Input_Mask_Changed;

   -----------------------
   -- Validator_Changed --
   -----------------------

   procedure Validator_Changed
    (Self  : not null access My_Widget'Class;
     Value : Qt4.Q_Integer)
   is
   begin
      case Value is
         when 0 =>
            Self.Validator_Line_Edit.Set_Validator (null);

--          when 1 =>
--             Self.Validator_Line_Edit.Set_Validator
--              (Qt4.Int_Validators.Constructors.Create
--                (Self.Validator_Line_Edit));
--
--          when 2 =>
--             Self.Validator_Line_Edit.Set_Validator
--              (Qt4.Double_Validators.Constructors.Create
--                (-999.0, 999.0, Self.Validator_Line_Edit));

         when others =>
            null;
      end case;

      Self.Validator_Line_Edit.Clear;
   end Validator_Changed;

end My_Widgets;
