------------------------------------------------------------------------------
--                                                                          --
--                An Ada2005 binding to the Qt C++ framework                --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
-- Copyright © 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: 3670 $ $Date: 2009-12-21 09:36:28 +0300 (Mon, 21 Dec 2009) $
------------------------------------------------------------------------------
with Ada.Unchecked_Deallocation;

with Qt4.Cursors;
with Qt4.V_Box_Layouts.Constructors;
with Qt4.Objects;
with Qt4.Progress_Bars.Constructors;
with Qt4.Push_Buttons.Constructors;
with Qt4.Strings;
with Qt4.Widgets.Constructors;

with Main_Windows.Moc;
pragma Unreferenced (Main_Windows.Moc);

package body Main_Windows is

   ---------------
   -- Completed --
   ---------------

   procedure Completed (Self : not null access Main_Window'Class) is

      procedure Free is
        new Ada.Unchecked_Deallocation (Jobs.Job, Jobs.Job_Access);

   begin
      Self.Unset_Cursor;
      Self.Progress_Bar.Reset;
      Self.Start_Button.Set_Enabled (True);
      Self.Stop_Button.Set_Enabled (False);
      Free (Self.Job);
   end Completed;

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

   function Create return not null Main_Window_Access is
   begin
      return Self : constant not null Main_Window_Access
        := new Main_Window
      do
         Qt4.Main_Windows.Directors.Constructors.Initialize (Self);

         declare
            Central  : constant Qt4.Widgets.Q_Widget_Access
              := Qt4.Widgets.Constructors.Create (Self);
            Layout   : constant Qt4.V_Box_Layouts.Q_V_Box_Layout_Access
              := Qt4.V_Box_Layouts.Constructors.Create (Central);

         begin
            Self.Set_Central_Widget (Central);

            --  Create visual components.

            Self.Progress_Bar :=
              Qt4.Progress_Bars.Constructors.Create (Central);
            Layout.Add_Widget (Self.Progress_Bar);

            Self.Start_Button :=
              Qt4.Push_Buttons.Constructors.Create
               (Qt4.Strings.From_Utf_16 ("Start"), Central);
            Layout.Add_Widget (Self.Start_Button);

            Self.Stop_Button :=
              Qt4.Push_Buttons.Constructors.Create
               (Qt4.Strings.From_Utf_16 ("Stop"), Central);
            Self.Stop_Button.Set_Enabled (False);
            Layout.Add_Widget (Self.Stop_Button);

            --  Establish signal/slot connections.

            Qt4.Objects.Connect
             (Self.Start_Button, Qt4.Signal ("clicked(bool)"),
              Self, Qt4.Slot ("start()"));
            Qt4.Objects.Connect
             (Self.Stop_Button, Qt4.Signal ("clicked(bool)"),
              Self, Qt4.Slot ("stop()"));
         end;
      end return;
   end Create;

   -----------
   -- Start --
   -----------

   procedure Start (Self : not null access Main_Window'Class) is
      E : Qt4.Objects.Q_Object_Pointer;

   begin
      Self.Job := new Jobs.Job;
      Self.Job.Initialize (E);

      --  Establish signal/slot connections.

      Qt4.Objects.Connect
       (E.Get, Qt4.Signal ("progressValueChanged(int)"),
        Self.Progress_Bar, Qt4.Slot ("setValue(int)"));
      Qt4.Objects.Connect
       (E.Get, Qt4.Signal ("completed()"),
        Self, Qt4.Slot ("completed()"));

      --  Set control's state.

      Self.Start_Button.Set_Enabled (False);
      Self.Stop_Button.Set_Enabled (True);
      Self.Set_Cursor (Qt4.Cursors.Create (Qt4.Busy_Cursor));
   end Start;

   ----------
   -- Stop --
   ----------

   procedure Stop (Self : not null access Main_Window'Class) is
   begin
      Self.Start_Button.Set_Enabled (False);
      Self.Stop_Button.Set_Enabled (False);
      Self.Job.Stop;
   end Stop;

end Main_Windows;
