Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   OOP (https://www.clubdelphi.com/foros/forumdisplay.php?f=5)
-   -   Problemas al guardar componentes con TStream!! (https://www.clubdelphi.com/foros/showthread.php?t=25835)

OzzyzzO 05-10-2005 15:58:47

Problemas al guardar componentes con TStream!!
 
Hola!

Estoy desarrollando una aplicación en la que creo componentes en forma dinámica, y al cerrar el form los guardo en un archivo de texto para volver a recuperarlos en el momento en que vuelva a ejecutar la aplicación. Esto lo logro con una función que saqué de la ayuda de delphi con una pequeña modificación:

Código Delphi [-]
// Esta función convierte el componente a un string (¿obvio no?)
function ComponentToString(Component: TComponent): string;
var
  BinStream:TMemoryStream;
  StrStream: TStringStream;
  s: string;
begin
  BinStream := TMemoryStream.Create;
  try
    StrStream := TStringStream.Create(s);
    try
      BinStream.WriteComponent(Component);
      BinStream.Seek(0, soFromBeginning);
      ObjectBinaryToText(BinStream, StrStream);
      StrStream.Seek(0, soFromBeginning);
      Result:= StrStream.DataString;
    finally
      StrStream.Free;

    end;
  finally
    BinStream.Free
  end;
end;

// Esta función convierte un string  a componente nuevamente
function StringToComponent(Value: string; Component: TComponent): TComponent;//acá meti la mano
var
  StrStream:TStringStream;
  BinStream: TMemoryStream;
begin
  StrStream := TStringStream.Create(Value);
  try
    BinStream := TMemoryStream.Create;
    try
      ObjectTextToBinary(StrStream, BinStream);
      BinStream.Seek(0, soFromBeginning);
      Result := BinStream.ReadComponent(Component);//acá meti la mano

    finally
      BinStream.Free;
    end;
  finally
    StrStream.Free;
  end;
end;

Ahora bien, funciona todo correctamente mientras guarde un componente que no tiene subcomponentes, por ejemplo, un TButton. El problema surge cuando intento recuperar un componente con subcomponentes, es decir un componente de mi creación, derivado de TCustomPanel, que tiene un TImage, un TStaticText y un TPopupMenu; el error que surge es: "class TImage not found".

Estas funciones las encontré en el ejemplo de la ayuda del método WriteComponent del componente TStream.

Se que en otro hilo del foro se describió otra forma de realizar esto, pero mi pequeño cerebrito fué incapaz de procesar esa información ya habiendo empezado con este método :( .

Espero haber sido lo suficientemente claro.

Agradezco a todo aquel que aporte alguna pista.

delphi.com.ar 05-10-2005 16:44:05

Cita:

Empezado por OzzyzzO
...El problema surge cuando intento recuperar un componente con subcomponentes, es decir un componente de mi creación, derivado de TCustomPanel, que tiene un TImage, un TStaticText y un TPopupMenu; el error que surge es: "class TImage not found"....

Cuando dices "...tiene un TImage..." te refieres que tiene embebido un TImage, o que tiene una propiedad del tipo TImage donde enlazas otro componente?...
Si esta embebido, queda en tu componente implementar los métodos y propiedades para leer y guardar los datos de sus objetos, pero si no te funciona WriteComponent / ReadComponent, tampoco tendría que funcionar cuando carga / lee el dfm el propio IDE de Delphi o tu aplicación compilada.

Saludos!

OzzyzzO 05-10-2005 18:11:15

Ante todo gracias por responder.

Los componentes que están dentro del componente (el TImage, el TStaticText, etc) los creo en el medo AfterConstruction de mi componente:

Código Delphi [-]
procedure TDigitalDisplay.AfterConstruction;
var
  MenuItem: TMenuItem;
begin
  StaticText1 := TStaticText.Create(self);
  StaticText2 := TStaticText.Create(self);
  StaticText3 := TStaticText.Create(self);
  Image1 := TImage.Create(self);
  PopupMenu1 := TPopupMenu.Create(self);
  MenuItem := TMenuItem.Create(PopupMenu1);
  with MenuItem do
  begin
    Caption := 'Configurar';
    OnClick := PopupConfigurar;
  end;

//...
//...
//...

  inherited AfterConstruction;
end;

En cuanto al dfm, si, el IDE lo lee sin problemas. Un dato más: a las funciones para guardar y leer las llamo desde los eventos OnClose y OnCreate del form respectivamente, aunque también probé de llamarlas mediante un botón y tampoco funcionó. Además, el metodo write funciona, pero el Read es el que da el error. :confused:

Saludos.

rounin 06-10-2005 11:12:31

Procedures Stream.WriteComponent and ReadComponent are
purposed for deal mainly with Root components (Form, DataModule, ...).

Somewhen I was experimenting with components
and wrote procedures for read/write
non-root components from stream:

Código Delphi [-]
 
 
{*******************************************************************************
* *
* Reonid sources *
* Categoty: other *
* (c) reonid@yahoo.com *
* *
*******************************************************************************}
unit Comp2Stm;
interface
uses
Classes, Controls;
 
function LoadComponentFromStream(ARoot, AOwner: TComponent; AParent: TWinControl;
SrcTxtStream: TStream; AComp: TComponent = nil): TComponent;
 
procedure SaveComponentToStream(AComp, ARoot: TComponent; DestTxtStream: TStream);
 
implementation
 
type
TDummy = class
class procedure ReaderSetName(Reader: TReader; Component: TComponent; var Name: string);
end;
 
class procedure TDummy.ReaderSetName(Reader: TReader; Component: TComponent; var Name: string);
begin
if (Reader.Owner <> nil) and
(Reader.Owner.FindComponent(Name) <> nil) then Name := '';
// The simplest way to resolve the name conflicts
end;
 
function LoadComponentFromStream(ARoot, AOwner: TComponent; AParent: TWinControl;
SrcTxtStream: TStream; AComp: TComponent = nil): TComponent;
var
BinStream: TStream;
Reader: TReader;
Comp: TComponent;
begin
BinStream := TMemoryStream.Create;
try
Reader := TReader.Create(BinStream, 4096);
 
//SrcTxtStream.Position := 0;
ObjectTextToBinary(SrcTxtStream, BinStream);
try
Reader.Position := 0;
Reader.Owner := AOwner;
Reader.Parent := AParent;
Reader.Root := ARoot;
// Owner, Parent and Root for new Component
// Reader will search in RTTI-tables of the Root component 
// classes and event handlers etc.
 
Reader.OnSetName := TDummy.ReaderSetName;
// Äëÿ ðàçðåøåíèÿ êîíôëèêòîâ èì¸í
// For the name conflicts resolution
 
//Reader.OnFindMethod := ;
//Reader.OnFindComponentClass := ;
 
Reader.BeginReferences;
try
Reader.ReadSignature;
Comp := Reader.ReadComponent(AComp);
Result := Comp;
Reader.FixupReferences;
// Initializes references on other components,
// that saved in DFM as names.
finally
Reader.EndReferences;
end;
finally
Reader.Free;
end;
finally
BinStream.Free;
end;
end;
 
procedure SaveComponentToStream(AComp, ARoot: TComponent; DestTxtStream: TStream);
var BinStream: TStream;
Writer: TWriter;
begin
BinStream := TMemoryStream.Create();
try
Writer := TWriter.Create(BinStream, 4096);
try
Writer.Root := ARoot;
Writer.WriteSignature;
Writer.WriteComponent(AComp);
Writer.WriteListEnd;
finally
Writer.Free;
end;
BinStream.Position := 0;
ObjectBinaryToText(BinStream, DestTxtStream);
finally
BinStream.Free;
end;
end;
 
end.

PS: When Reader reads a component from stream,
initially it reads the Class Name of the component.
Then (if the component has not been created before reading)
Reader tries to find the Class Reference by Class Name.

First, it searches the class in the RTTI-table of Root component.
If there is any component of the same class in the Root,
then success.

Second, the Reader searches the class in the
registered classes. If the class have been registered by
RegisterClass (RegisterClasses), then success.

Third, the Reader let user to resolve the Class Name
to the Class Reference by himself
(it calls OnFindComponentClass event handler).

If all this attempts have failed, the Reader raise EClassNotFound
"class <Class Name> not found".
Use RegisterClasses to solve it.

OzzyzzO 06-10-2005 14:05:24

Thanks! Rounin. I think that your solution will help me. Very good explanation. Thanks again!

Saludos

vtdeleon 06-10-2005 17:32:21

Saludos

Rounin, Sabes escribir en español?, Lo digo porque en este foro la gran mayoría hablamos(o escribimos) en español. Entonces los forista que consulten este hilo y tenga pocos conocimiento del ingles estarán despistado.

Ante todo, Es muy grata y muy buena tu ayuda (hasta a mi me has ayudado:D). Espero seguir viendote por aqui;)

rounin 06-10-2005 20:44:43

Saludos,

Estoy estudiando español,
(yo periodicamente voy en comisiones de servicio a España)
pero mi nivel de español no es bastante alto,
y lo escribir en español todavía es dificil para mi.
Lo siento. :(

vtdeleon 06-10-2005 20:50:09

Saludos
Cita:

Empezado por rounin
pero mi nivel de español no es bastante alto,

Cómo va ser. Se entiende perfectamente:D
Cita:

Empezado por rounin
y lo escribir en español todavía es dificil para mi.

Aquí te serviría de práctica;)

rounin 07-10-2005 13:38:04

Saludos,

pienso que en tu caso necesita incluir flag csSubcomponent
en tus subcomponentes:

Código:

  TDigitalDisplay = class(TCustomPanel)
  private
        FImage1: TImage;
        FStaticText1: TStaticText;
        FPopupMenu1: TPopupMenu;
        {...}
  published
        property StaticText1: TStaticText read FStaticText1; {r/o}
        property Image1: TImage read FImage1;
        property PopupMenu1: TPopupMenu read FPopupMenu1;

        {...}
  end;
 
 
constructor TDigitalDisplay.Create(AOwner: TComponent);
 
  function CreateSubComponent(AClass: TComponentClass): TComponent;
  begin
        Result := AClass.Create(Self);
        Result.SetSubComponent(True); {!!!!!!!!}
        Result.FreeNotification(Self);
        if Result is TControl then TControl(Result).Parent := Self;
  end;
 
var
  MenuItem: TMenuItem;
begin
  inherited;
  {...}
  FStaticText1 := CreateSubComponent(TStaticText) as TStaticText;
  FStaticText2 := CreateSubComponent(TStaticText) as TStaticText;
  FStaticText3 := CreateSubComponent(TStaticText) as TStaticText;
  FImage1          := CreateSubComponent(TImage) as TImage;
  FPopupMenu1  := CreateSubComponent(TPopupMenu) as TPopupMenu;
 
  MenuItem := TMenuItem.Create(PopupMenu1);
  FPopupMenu1.Items.Add(MenuItem);

  with MenuItem do
  begin
        Caption := 'Configurar';
        OnClick := PopupConfigurar;
  end;

end;
 
procedure TDigitalDisplay.Notification(AComponent: TComponent; Operation: TOperation);
begin
  inherited;
  if (AComponent = FStaticText1) and (Operation = opRemove) then FStaticText1 := nil;
  if (AComponent = FStaticText2) and (Operation = opRemove) then FStaticText2 := nil;
  if (AComponent = FStaticText3) and (Operation = opRemove) then FStaticText3 := nil;
  if (AComponent = FPopupMenu1) and (Operation = opRemove) then FPopupMenu1 := nil;
  if (AComponent = FImage1)        and (Operation = opRemove) then FImage1        := nil;
end;

PS Porque creas subcomponentes en AfterConstruction?
Porque no en Create?

OzzyzzO 20-10-2005 13:58:17

Cita:

Empezado por rounin
PS Porque creas subcomponentes en AfterConstruction?
Porque no en Create?

Perdón por no contestar :D , estuve con mucho trabajo.

Los subcomponentes los creo en el metodo AfterConstruction porque en el constructor Create el componente aun no está creado y no puedo asignarle la variable Self a la propiedad Parent del subcomponente, ni tampoco pasarlo como Owner en la creación del subcomponente. O almenos eso entendí.

Saludos y éxitos con el español.

rounin 20-10-2005 16:29:02

Puedes hacer eso en Create después inherited.
En este lugar el componente esta creado totalmente.
(sólo las propiedades no ya esta cargado de DFM y
la ventana no ya esta creado).

OzzyzzO 20-10-2005 18:02:51

Cita:

Empezado por rounin
Puedes hacer eso en Create después inherited.

Es cierto Rounin ... ¿Como no me dí cuenta? ... bueno supongo que el apuro y la falta de experiencia tuvieron algo que ver :D .

Gracias de nuevo.

Y con respecto a:
Cita:

Empezado por vtdeleon
... Entonces los forista que consulten este hilo y tenga pocos conocimiento del ingles estarán despistado.

¿Eso lo dijiste por mi respueta? :D je...je...

Saludos


La franja horaria es GMT +2. Ahora son las 12:25:08.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi