------------------------------------------------------------------------------
--                                                                          --
--                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.Dialog_Button_Boxes.Constructors;
with Qt4.H_Box_Layouts.Constructors;
with Qt4.Message_Boxes;
with Qt4.Objects;
with Qt4.Push_Buttons.Constructors;
with Qt4.Sql_Table_Models.Constructors;
with Qt4.Table_Views.Constructors;
with Qt4.Variants;

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

package body Table_Editors is

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

   function Create
    (DB         : Qt4.Sql_Databases.Q_Sql_Database;
     Table_Name : Qt4.Strings.Q_String;
     Parent     : access Qt4.Widgets.Q_Widget'Class := null)
       return not null Table_Editor_Access
   is
      Self : constant Table_Editor_Access := new Table_Editors.Table_Editor;

      Main_Layout : constant not null access Qt4.H_Box_Layouts.Q_H_Box_Layout
        := Qt4.H_Box_Layouts.Constructors.Create;
      View        : constant not null access Qt4.Table_Views.Q_Table_View'Class
        := Qt4.Table_Views.Constructors.Create;

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

      Self.Model := Qt4.Sql_Table_Models.Constructors.Create (Self, DB);
      Self.Model.Set_Table (Table_Name);
      Self.Model.Set_Edit_Strategy (Qt4.Sql_Table_Models.On_Manual_Submit);
      Self.Model.Select_Query;

      Self.Model.Set_Header_Data
       (0,
        Qt4.Horizontal,
        Qt4.Variants.Create (Qt4.Strings.From_Utf_16 ("ID")));
      Self.Model.Set_Header_Data
       (1,
        Qt4.Horizontal,
        Qt4.Variants.Create (Qt4.Strings.From_Utf_16 ("First name")));
      Self.Model.Set_Header_Data
       (2,
        Qt4.Horizontal,
        Qt4.Variants.Create (Qt4.Strings.From_Utf_16 ("Last name")));

      View.Set_Model (Self.Model);

      Self.Submit_Button :=
        Qt4.Push_Buttons.Constructors.Create
         (Qt4.Strings.From_Utf_16 ("Submit"));
      Self.Submit_Button.Set_Default (True);
      Self.Revert_Button :=
        Qt4.Push_Buttons.Constructors.Create
         (Qt4.Strings.From_Utf_16 ("&Revert"));
      Self.Quit_Button :=
        Qt4.Push_Buttons.Constructors.Create
         (Qt4.Strings.From_Utf_16 ("Quit"));

      Self.Button_Box :=
        Qt4.Dialog_Button_Boxes.Constructors.Create (Qt4.Vertical);
      Self.Button_Box.Add_Button
       (Self.Submit_Button, Qt4.Dialog_Button_Boxes.Action_Role);
      Self.Button_Box.Add_Button
       (Self.Revert_Button, Qt4.Dialog_Button_Boxes.Action_Role);
      Self.Button_Box.Add_Button
       (Self.Quit_Button, Qt4.Dialog_Button_Boxes.Reject_Role);

      Qt4.Objects.Connect
       (Self.Submit_Button,
        Qt4.Signal ("clicked()"),
        Self,
        Qt4.Slot ("submit()"));

      Qt4.Objects.Connect
       (Self.Revert_Button,
        Qt4.Signal ("clicked()"),
        Self.Model,
        Qt4.Slot ("revertAll()"));

      Qt4.Objects.Connect
       (Self.Quit_Button,
        Qt4.Signal ("clicked()"),
        Self,
        Qt4.Slot ("close()"));

      Main_Layout.Add_Widget (View);
      Main_Layout.Add_Widget (Self.Button_Box);
      Self.Set_Layout (Main_Layout);

      Self.Set_Window_Title (Qt4.Strings.From_Utf_16 ("Cached Table"));

      return Self;
   end Create;

   ------------
   -- Submit --
   ------------

   procedure Submit (Self : not null access Table_Editor'Class) is

      use type Qt4.Strings.Q_String;

      procedure Check (Value : Boolean);

      -----------
      -- Check --
      -----------

      procedure Check (Value : Boolean) is
      begin
         if not Value then
            raise Program_Error;
         end if;
      end Check;

      Database : aliased Qt4.Sql_Databases.Q_Sql_Database
        := Self.Model.Database;

   begin
      Check (Database.Transaction);

      if Self.Model.Submit_All then
         Check (Database.Commit);

      else
         Check (Database.Rollback);
         Qt4.Message_Boxes.Warning
          (Self,
           Qt4.Strings.From_Utf_16 ("Cached Table"),
           Qt4.Strings.From_Utf_16 ("The database reported an error: ")
                                      & Self.Model.Last_Error.Text);
      end if;
   end Submit;

end Table_Editors;
