------------------------------------------------------------------------------
--                                                                          --
--                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 Ada.Text_IO;

with Qt4.Actions;
with Qt4.Colors;
with Qt4.Core_Applications;
with Qt4.File_Dialogs;
with Qt4.Item_Selection_Models.Constructors;
with Qt4.Menus.Constructors;
with Qt4.Menu_Bars;
pragma Warnings (Off, Qt4.Menu_Bars);
--  XXX Gnat Pro 6.1.0w (20070424-41)
with Qt4.Model_Indices;
with Qt4.Objects;
with Qt4.Splitters.Constructors;
with Qt4.Standard_Item_Models.Constructors;
with Qt4.Status_Bars;
pragma Warnings (Off, Qt4.Status_Bars);
--  XXX Gnat Pro 6.1.0w (20070424-41)
with Qt4.String_Lists;
with Qt4.Strings;
with Qt4.Table_Views.Constructors;
with Qt4.Variants.GUI_Helpers;

with Pie_Views;

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

package body Main_Windows is

   use type Qt4.Q_Integer;
   use type Qt4.Strings.Q_String;

   package body Constructors is

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

      function Create return not null Main_Window_Access is
         Self : constant Main_Window_Access := new Main_Windows.Main_Window;

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

         declare
            File_Menu   : constant not null Qt4.Menus.Q_Menu_Access
              := Qt4.Menus.Constructors.Create
                  (Qt4.Strings.From_Utf_16 ("&File"), Self);
            Open_Action : constant not null Qt4.Actions.Q_Action_Access
              := Qt4.Actions.Q_Action_Access
                  (File_Menu.Add_Action
                    (Qt4.Strings.From_Utf_16 ("&Open...")));
            Save_Action : constant not null Qt4.Actions.Q_Action_Access
              := Qt4.Actions.Q_Action_Access
                  (File_Menu.Add_Action
                    (Qt4.Strings.From_Utf_16 ("&Save As...")));
            Quit_Action : constant not null Qt4.Actions.Q_Action_Access
              := Qt4.Actions.Q_Action_Access
                  (File_Menu.Add_Action (Qt4.Strings.From_Utf_16 ("E&xit")));
            Menu_Bar    : constant not null Qt4.Actions.Q_Action_Access
              := Qt4.Actions.Q_Action_Access
                  (Self.Menu_Bar.Add_Menu (File_Menu));
            pragma Unreferenced (Menu_Bar);

         begin
--          Open_Action.Set_Shortcut (Key_Sequences.Constructors.Create
--                                     (Strings.To_Q_String ("Ctrl+O")));
--          Save_Action.Set_Shortcut (Key_Sequences.Constructors.Create
--                                     (Strings.To_Q_String ("Ctrl+S")));
--          Quit_Action.Set_Shortcut (Key_Sequences.Constructors.Create
--                                     (Strings.To_Q_String ("Ctrl+Q")));

            Self.Setup_Model;
            Self.Setup_Views;

            Qt4.Objects.Connect
             (Open_Action,
              Qt4.Signal ("triggered()"),
              Self,
              Qt4.Slot ("openFile()"));
            Qt4.Objects.Connect
             (Save_Action,
              Qt4.Signal ("triggered()"),
              Self,
              Qt4.Slot ("saveFile()"));
            Qt4.Objects.Connect
             (Quit_Action,
              Qt4.Signal ("triggered()"),
              Qt4.Core_Applications.Instance,
              Qt4.Slot ("quit()"));

--          Result.Open_File (Strings.To_Q_String (":/Charts/qtdata.cht"));
            Self.Set_Window_Title (Qt4.Strings.From_Utf_16 ("Chart"));
            Self.Resize (640, 480);
         end;

         return Self;
      end Create;

   end Constructors;

   ---------------
   -- Open_File --
   ---------------

   procedure Open_File (Self : not null access Main_Window'Class) is
      File_Name : constant Qt4.Strings.Q_String
        := Qt4.File_Dialogs.Get_Open_File_Name
            (Self,
             Qt4.Strings.From_Utf_16 ("Choose a data file"),
             Qt4.Strings.From_Utf_16 (""),
             Qt4.Strings.From_Utf_16 ("*.cht"));

   begin

      if not File_Name.Is_Empty then
         declare
            File : Ada.Text_IO.File_Type;
            Line : Qt4.Strings.Q_String;
            Row  : Qt4.Q_Integer := 0;

         begin
            Ada.Text_IO.Open
             (File, Ada.Text_IO.In_File, File_Name.To_Local_8_Bit);

            Self.Model.Remove_Rows
             (0,
              Self.Model.Row_Count (Qt4.Model_Indices.Create),
              Qt4.Model_Indices.Create);

            while not Ada.Text_IO.End_Of_File (File) loop
               Line :=
                 Qt4.Strings.From_Local_8_Bit (Ada.Text_IO.Get_Line (File));

               if not Line.Is_Empty then
                  Self.Model.Insert_Rows (Row, 1, Qt4.Model_Indices.Create);

                  declare
                     Pieces : constant Qt4.String_Lists.Q_String_List
                       := Line.Split
                           (Qt4.Strings.From_Utf_16 (","),
                            Qt4.Strings.Skip_Empty_Parts);

                  begin
                     Self.Model.Set_Data
                      (Self.Model.Index (Row, 0, Qt4.Model_Indices.Create),
                       Qt4.Variants.Create (Pieces.Value (0)));
                     Self.Model.Set_Data
                      (Self.Model.Index (Row, 1, Qt4.Model_Indices.Create),
                       Qt4.Variants.Create (Pieces.Value (1)));
                     Self.Model.Set_Data
                      (Self.Model.Index (Row, 0, Qt4.Model_Indices.Create),
                       Qt4.Variants.GUI_Helpers.To_Q_Variant
                        (Qt4.Colors.Create (Pieces.Value (2))),
                       Qt4.Decoration_Role);

                     Row := Row + 1;
                  end;
               end if;
            end loop;

            Ada.Text_IO.Close (File);
            Self.Status_Bar.Show_Message
             (Qt4.Strings.From_Utf_16 ("Loaded ") & File_Name, 2.0);
         end;
      end if;
   end Open_File;

   ---------------
   -- Save_File --
   ---------------

   procedure Save_File (Self : not null access Main_Window'Class) is
      File_Name : constant Qt4.Strings.Q_String
        := Qt4.File_Dialogs.Get_Save_File_Name
            (Self,
             Qt4.Strings.From_Utf_16 ("Save file as"),
             Qt4.Strings.From_Utf_16 (""),
             Qt4.Strings.From_Utf_16 ("*.cht"));

      Tmp_Index : Qt4.Model_Indices.Q_Model_Index;

   begin
      if not File_Name.Is_Empty then
         declare
            File : Ada.Text_IO.File_Type;

         begin
            Ada.Text_IO.Create
             (File, Ada.Text_IO.Out_File, File_Name.To_Local_8_Bit);

            for Row in 0 .. Self.Model.Row_Count (Tmp_Index) - 1 loop
               declare
                  Pieces : Qt4.String_Lists.Q_String_List;

               begin
                  Pieces.Append
                   (Self.Model.Data
                     (Self.Model.Index (Row, 0, Tmp_Index),
                      Qt4.Display_Role).To_String);
                  Pieces.Append
                   (Self.Model.Data
                     (Self.Model.Index (Row, 1, Tmp_Index),
                      Qt4.Display_Role).To_String);
                  Pieces.Append
                   (Self.Model.Data
                     (Self.Model.Index (Row, 0, Tmp_Index),
                      Qt4.Decoration_Role).To_String);

                  Ada.Text_IO.Put_Line
                   (File,
                    Pieces.Join
                     (Qt4.Strings.From_Utf_16 (",")).To_Local_8_Bit);
               end;
            end loop;

            Ada.Text_IO.Close (File);
            Self.Status_Bar.Show_Message
             (Qt4.Strings.From_Utf_16 ("Saved ") & File_Name, 2.0);
         end;
      end if;
   end Save_File;

   -----------------
   -- Setup_Model --
   -----------------

   procedure Setup_Model (Self : not null access Main_Window'Class) is
   begin
      Self.Model :=
        Qt4.Abstract_Item_Models.Q_Abstract_Item_Model_Access
         (Qt4.Standard_Item_Models.Constructors.Create (8, 2, Self));
      Self.Model.Set_Header_Data
       (0,
        Qt4.Horizontal,
        Qt4.Variants.Create (Qt4.Strings.From_Utf_16 ("Label")));
      Self.Model.Set_Header_Data
       (1,
        Qt4.Horizontal,
        Qt4.Variants.Create (Qt4.Strings.From_Utf_16 ("Quantity")));
   end Setup_Model;

   -----------------
   -- Setup_Views --
   -----------------

   procedure Setup_Views (Self : not null access Main_Window'Class)
   is
      Splitter        : constant not null Qt4.Splitters.Q_Splitter_Access
        := Qt4.Splitters.Constructors.Create;
      Selection_Model : constant not null
        Qt4.Item_Selection_Models.Q_Item_Selection_Model_Access
          := Qt4.Item_Selection_Models.Constructors.Create (Self.Model);
      Table           : constant not null Qt4.Table_Views.Q_Table_View_Access
        := Qt4.Table_Views.Constructors.Create;

   begin
      Self.Pie_Chart :=
        Qt4.Abstract_Item_Views.Q_Abstract_Item_View_Access
         (Pie_Views.Constructors.Create);
      Splitter.Add_Widget (Table);
      Splitter.Add_Widget (Self.Pie_Chart);
      Splitter.Set_Stretch_Factor (0, 0);
      Splitter.Set_Stretch_Factor (1, 1);

      Table.Set_Model (Self.Model);
      Self.Pie_Chart.Set_Model (Self.Model);

      Table.Set_Selection_Model (Selection_Model);
      Self.Pie_Chart.Set_Selection_Model (Selection_Model);

      Self.Set_Central_Widget (Splitter);
   end Setup_Views;

end Main_Windows;
