------------------------------------------------------------------------------
--                                                                          --
--                An Ada2005 binding to the Qt C++ framework                --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
-- Copyright © 2008-2010 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: 3738 $ $Date: 2010-08-19 12:52:05 +0400 (Thu, 19 Aug 2010) $
------------------------------------------------------------------------------
with Ada.IO_Exceptions;
with Ada.Wide_Text_IO;

with Qt_Ada.Application;
with Qt4.Core_Applications;
with Qt4.Files.Constructors;
with Qt4.File_Dialogs;
with Qt4.IO_Devices;
with Qt4.String_Lists;
with Qt4.Strings;
with Qt4.Ui_Loaders.Constructors;
with Qt4.Widgets;

procedure Main is
begin
   Qt_Ada.Application.Initialize;

   declare
      use type Qt4.Q_Integer;
      use type Qt4.IO_Devices.Open_Mode;

      Loader    : constant Qt4.Ui_Loaders.Q_Ui_Loader_Access
        := Qt4.Ui_Loaders.Constructors.Create;
      File_Name : Qt4.Strings.Q_String;
      Args      : constant Qt4.String_Lists.Q_String_List
        := Qt4.Core_Applications.Arguments;

   begin
      if Args.Size = 1 then
         File_Name :=
           Qt4.File_Dialogs.Get_Open_File_Name
            (null,
             Qt4.Strings.From_Utf_16 ("Choose a file name"),
             Qt4.Strings.From_Utf_16 ("."),
             Qt4.Strings.From_Utf_16 ("User interfaces (*.ui)"));

      else
         File_Name := Qt4.Core_Applications.Arguments.Value (1);
      end if;

      if File_Name.Is_Empty then
         return;
      end if;

      declare
         File : constant access Qt4.Files.Q_File'Class
            := Qt4.Files.Constructors.Create (File_Name);

      begin
         File.Open (+Qt4.IO_Devices.Read_Only);

         declare
            Form : constant not null Qt4.Widgets.Q_Widget_Access
              := Qt4.Widgets.Q_Widget_Access (Loader.Load (File, null));

         begin
            File.Close;
            Form.Show;
            Qt_Ada.Application.Execute;
         end;

      exception
         when Ada.IO_Exceptions.Name_Error =>
            Ada.Wide_Text_IO.Put_Line
             (File_Name.To_Utf_16 & " : File not found");
      end;
   end;
end Main;
