------------------------------------------------------------------------------
--                                                                          --
--                An Ada2005 binding to the Qt C++ framework                --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
-- Copyright © 2009-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.                                              --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable  to  be --
-- covered  by the  GNU  General  Public  License.  This exception does not --
-- however invalidate  any other reasons why  the executable file  might be --
-- covered by the  GNU Public License.                                      --
--                                                                          --
------------------------------------------------------------------------------
--  $Revision: 3769 $ $Date: 2010-10-24 03:16:28 +0400 (Sun, 24 Oct 2010) $
------------------------------------------------------------------------------
with Interfaces.C;

with Qt_Ada.Internals.Objects;
with Qt_Ada.Internals.Object_Registry;

package body Qt_Ada.Internals.Graphics_Item_Registry is

   function Get_Associate
    (Cpp_Object : not null access constant
       Qt_Ada.Internals.Graphics_Items.QGraphicsItem)
         return access
           Qt_Ada.Internals.Graphics_Items.Q_Graphics_Item_Base'Class;
   pragma Import
    (C, Get_Associate, "__Qt4Ada_get_QGraphicsItem_LimitedEntity");
   pragma Warnings (Off, Get_Associate);

   function type_virtual
    (Self : not null access constant
       Qt_Ada.Internals.Graphics_Items.QGraphicsItem)
         return Interfaces.C.int;
   pragma Import (C, type_virtual, "__Qt4Ada_QGraphicsItem_type_virtual");

   Registry : array (Qt4.Graphics_Items.Graphics_Item_Type)
     of Graphics_Item_Proxy_Constructor
       := (others => null);

   -------------------------------------
   -- Register_QGraphicsItem_Subclass --
   -------------------------------------

   procedure Register_QGraphicsItem_Subclass
    (Class_Type  : Qt4.Graphics_Items.Graphics_Item_Type;
     Constructor : Graphics_Item_Proxy_Constructor)
   is
   begin
      if Registry (Class_Type) /= null then
         raise Program_Error
           with "Constructor for"
                  & Qt4.Graphics_Items.Graphics_Item_Type'Image (Class_Type)
                  & " already registered";
      end if;

      Registry (Class_Type) := Constructor;
   end Register_QGraphicsItem_Subclass;

   ------------------------
   -- To_Q_Graphics_Item --
   ------------------------

   function toGraphicsObject_0_wrapper
    (Self : not null access Qt_Ada.Internals.Graphics_Items.QGraphicsItem)
       return access Qt_Ada.Internals.Objects.QObject;
   pragma Import (C, toGraphicsObject_0_wrapper,
                     "__Qt4Ada_QGraphicsItem_toGraphicsObject_0_wrapper");

   function To_Q_Graphics_Item
    (Item : not null access Qt_Ada.Internals.Graphics_Items.QGraphicsItem)
       return not null access
         Qt_Ada.Internals.Graphics_Items.Q_Graphics_Item_Base'Class
   is
   begin
      if Get_Associate (Item) = null then
         --  Если объект C++ ещё не был ассоциирован с объектом Ada, то
         --  создаём объект Ada соответствующего типа и устанавливаем
         --  асоциацию.

         declare
            Class_Type : constant Qt4.Graphics_Items.Graphics_Item_Type
              := Qt4.Graphics_Items.Graphics_Item_Type (type_virtual (Item));
              --  Получаем тип объекта С++.

         begin
            if Registry (Class_Type) /= null then
               --  Explicitly defined constructor.

               return
                 Registry (Class_Type).all
                  (Qt_Ada.Internals.Graphics_Items.QGraphicsItem_Access
                    (Item));

            elsif toGraphicsObject_0_wrapper (Item) /= null then
               --  Mixed subclass from QObject and QGraphicsItem. Proxy object
               --  will be created using QObject's proxy construction
               --  machinery.

               return
                 Qt_Ada.Internals.Graphics_Items.Q_Graphics_Item_Base'Class
                  (Qt_Ada.Internals.Object_Registry.To_Q_Object
                    (toGraphicsObject_0_wrapper (Item)).all)'Access;

            else
               raise Program_Error
                 with "Graphics item type '"
                        & Qt4.Graphics_Items.Graphics_Item_Type'Image
                           (Class_Type)
                        & "' is not registered";
            end if;
         end;
      end if;

      return
        Qt4.Graphics_Items.Q_Graphics_Item'Class
         (Get_Associate (Item).all)'Access;
   end To_Q_Graphics_Item;

end Qt_Ada.Internals.Graphics_Item_Registry;
