| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843 |
- {Unidad con definiciones básicas para las clases MiConfigINI y MiConfigXML.
- Aquí se define la clase TMiConfigBasic, que incluye los métodos para crear las
- asociaciones entre las variables (propiedades) y los controles.
- También incluye las rutinas para mover datos entre los controles y las variables.
- En teoría, se podría usar esta clase, si no fuera necesario guardar datos a disco,
- solamente entre controles y variables:
- +------------+ +------------+
- | | <----PropertiesToWindow---- | |
- | Controles | | Variables |
- | | ----WindowToProperties----> | |
- +------------+ +------------+
- Pero como es común salvar los datos a disco, se le debe agregar las funcionalidades
- de accesos a disco, como se hace en las unidades MiConfigINI y MiConfigXML.
- Las asociaciones entre variables y controles, se hacen con los métodos:
- Asoc_Int, Asoc_Dbl, Asoc_Str, ... que están sobrecargados para manejar diversos controles
- o crear la asociación sin control.
- Solo se han creado asociaciones entre tipos comunes y controles comunes.
- Para agregar un nuevo tipo de asociación, en esta unidad se debe:
- 1. Crear el identificador de la nueva asociación en el tipo TTipPar.
- 2. Crear el nuevo método de asociación (Asoc_XXX) o sobrecargar uno existente.
- 3. Actualizar el método TMiConfigBasic.PropertyWindow(), con el nuevo tipo.
- Luego también debe implementarse el acceso a disco, para esta nueva asociación, en las
- unidades MiConfigINI y MiConfigXML.
- Por Tito Hinostroza 29/07/2016
- }
- unit MiConfigBasic;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils, StdCtrls, Spin, Graphics, EditBtn, Dialogs,
- ExtCtrls, Grids, ColorBox, fgl;
- type
- //Tipos de asociaciones
- TTipPar = (
- tp_Int //Entero sin asociación
- ,tp_Int_TEdit //entero asociado a TEdit
- ,tp_Int_TSpinEdit //entero asociado a TSpinEdit
- ,tp_Int_TRadioGroup //entero asociado a TRadioGroup
- ,tp_Dbl //Double sin asociación
- ,tp_Dbl_TEdit //Double asociado a TEdit
- ,tp_Dbl_TFloatSpinEdit //Double asociado a TFloatSpinEdit
- ,tp_Str //String sin asociación
- ,tp_Str_TEdit //string asociado a TEdit
- ,tp_Str_TEditButton //string asociado a TEditButton (ancestro de TFileNameEdit, TDirectoryEdit, ...)
- ,tp_Str_TCmbBox //string asociado a TComboBox
- ,tp_Bol //Boleano sin asociación
- ,tp_Bol_TCheckBox //booleano asociado a CheckBox
- ,tp_Bol_TRadBut //Booleano asociado a TRadioButton
- ,tp_Enum //Enumerado sin asociación
- ,tp_Enum_TRadBut //Enumerado asociado a TRadioButton
- ,tp_Enum_TRadGroup //Enumerado asociado a TRadioGroup
- ,tp_TCol_TColBut //TColor asociado a TColorButton
- ,tp_TCol_TColBox //TColor asociado a TColorBox
- ,tp_StrList //TStringList sin asociación
- ,tp_StrList_TListBox //StringList asociado a TListBox
- ,tp_StrList_TStringGrid //StringList asociado a TStringGrid
- );
- //Objeto de asociación variable-control
- { TParElem }
- TParElem = class
- private //Getters and setters
- pCtl: TComponent; //referencia al control
- radButs: array of TRadioButton; //referencia a controles TRadioButton (se usan en conjunto)
- minEnt, maxEnt: integer; //valores máximos y mínimos para variables enteras
- minDbl, maxDbl: Double; //valores máximos y mínimos para variables Double
- function GetAsBoolean: Boolean;
- function GetAsInteger: integer;
- procedure SetAsBoolean(AValue: Boolean);
- procedure SetAsInteger(AValue: integer);
- function GetAsDouble: double;
- procedure SetAsDouble(AValue: double);
- function GetAsString: string;
- procedure SetAsString(AValue: string);
- function GetAsInt32: Int32;
- procedure SetAsInt32(AValue: Int32);
- function GetAsTColor: TColor;
- procedure SetAsTColor(AValue: TColor);
- public
- pVar : pointer; //referencia a la variable
- lVar : integer; //tamaño de variable. (Cuando no sea conocido)
- tipPar : TTipPar; //tipo de par agregado
- etiqVar: string; //etiqueta usada para grabar la variable en archivo INI o XML
- categ : integer; //Categoría. Usada para leer selectivamente con
- //Campos para configurar la grilla, cuando se use
- HasHeader : boolean; //Si incluye encabezado
- HasFixedCol: boolean; //Si tiene una columna fija
- ColCount : byte; //Cantidad de columnas para la grilla
- OnPropertyToWindow: procedure of object;
- OnWindowToProperty: procedure of object;
- OnFileToProperty: procedure of object; //Después de guardar el elemento a disco
- OnPropertyToFile: procedure of object; //Antes de guardar el elemento a disco
- public //Valores por defecto
- defInt: integer; //Valor entero por defecto al leer de archivo
- defDbl: Double; //Valor double por defecto al leer de archivo
- defStr: string; //Valor string por defecto al leer de archivo
- defBol: boolean; //Valor booleano por defecto al leer de archivo
- defCol: TColor; //Valor TColor por defecto al leer de archivo
- public //Propiedades para facilitar el acceso a pVar^, usando diversos tipos
- property AsInteger: integer read GetAsInteger write SetAsInteger;
- property AsDouble: double read GetAsDouble write SetAsDouble;
- property AsString: string read GetAsString write SetAsString;
- property AsBoolean: Boolean read GetAsBoolean write SetAsBoolean;
- property AsInt32: Int32 read GetAsInt32 write SetAsInt32;
- property AsTColor: TColor read GetAsTColor write SetAsTColor;
- end;
- TParElem_list = specialize TFPGObjectList<TParElem>;
- { TMiConfigBasic }
- TMiConfigBasic = class
- private
- procedure SetFocusEd(ed: TEdit);
- protected
- valInt: integer; //valor entero de salida
- valDbl: Double; //valor double de salida
- listParElem : TParElem_list;
- procedure PropertyWindow(r: TParElem; PropToWindow: boolean);
- public //Rutinas de movimientos entre: Controles <-> Propiedades <-> Archivo
- OnPropertiesChanges: procedure of object; //Cuando se actualizan las propiedades
- function PropertiesToWindow: boolean; virtual;
- function WindowToProperties: boolean; virtual;
- protected //Rutinas de validación
- function EditValidateInt(edit: TEdit; min: integer=MaxInt; max: integer=-MaxInt): boolean;
- function EditValidateDbl(edit: TEdit; min: Double=0; max: Double=1e6): boolean;
- public //Métodos para asociar pares: variable-control
- function Asoc_Int(etiq: string; ptrInt: pointer; defVal: integer): TParElem;
- function Asoc_Int(etiq: string; ptrInt: pointer; edit: TEdit;
- defVal: integer; minVal, maxVal: integer): TParElem;
- function Asoc_Int(etiq: string; ptrInt: pointer; spEdit: TSpinEdit;
- defVal: integer): TParElem;
- function Asoc_Int(etiq: string; ptrInt: pointer; radGroup: TRadioGroup;
- defVal: integer): TParElem;
- //---------------------------------------------------------------------
- function Asoc_Dbl(etiq: string; ptrDbl: PDouble; defVal: double): TParElem;
- function Asoc_Dbl(etiq: string; ptrDbl: PDouble; edit: TEdit;
- defVal: double; minVal, maxVal: double): TParElem;
- function Asoc_Dbl(etiq: string; ptrDbl: PDouble; spEdit: TFloatSpinEdit;
- defVal: double): TParElem;
- //---------------------------------------------------------------------
- function Asoc_Str(etiq: string; ptrStr: pointer; defVal: string): TParElem;
- function Asoc_Str(etiq: string; ptrStr: pointer; edit: TCustomEdit;
- defVal: string): TParElem;
- function Asoc_Str(etiq: string; ptrStr: pointer; edit: TCustomEditButton;
- defVal: string): TParElem;
- function Asoc_Str(etiq: string; ptrStr: pointer; cmbBox: TComboBox;
- defVal: string): TParElem;
- //---------------------------------------------------------------------
- function Asoc_Bol(etiq: string; ptrBol: pointer; defVal: boolean): TParElem;
- function Asoc_Bol(etiq: string; ptrBol: pointer; chk: TCheckBox;
- defVal: boolean): TParElem;
- function Asoc_Bol(etiq: string; ptrBol: pointer;
- radButs: array of TRadioButton; defVal: boolean): TParElem;
- //---------------------------------------------------------------------
- function Asoc_Enum(etiq: string; ptrEnum: pointer; EnumSize: integer; defVal: integer): TParElem;
- function Asoc_Enum(etiq: string; ptrEnum: pointer; EnumSize: integer;
- radButs: array of TRadioButton; defVal: integer): TParElem;
- function Asoc_Enum(etiq: string; ptrEnum: pointer; EnumSize: integer;
- radGroup: TRadioGroup; defVal: integer): TParElem;
- //---------------------------------------------------------------------
- function Asoc_TCol(etiq: string; ptrTCol: pointer; colBut: TColorButton;
- defVal: TColor): TParElem;
- function Asoc_TCol(etiq: string; ptrTCol: pointer; colBut: TColorBox;
- defVal: TColor): TParElem;
- //---------------------------------------------------------------------
- function Asoc_StrList(etiq: string; ptrStrList: pointer): TParElem;
- function Asoc_StrList_TListBox(etiq: string; ptrStrList: pointer; lstBox: TlistBox): TParElem;
- public
- MsjErr: string; //mensaje de error
- ctlErr: TParElem; //elemento con error
- constructor Create;
- destructor Destroy; override;
- end;
- implementation
- { TParElem }
- function TParElem.GetAsInteger: integer;
- begin
- Result := Integer(Pvar^);
- end;
- procedure TParElem.SetAsInteger(AValue: integer);
- begin
- //if FAsInteger=AValue then Exit;
- Integer(Pvar^) := AValue;
- end;
- function TParElem.GetAsDouble: double;
- begin
- Result := Double(Pvar^);
- end;
- procedure TParElem.SetAsDouble(AValue: double);
- begin
- Double(Pvar^) := AValue;
- end;
- function TParElem.GetAsString: string;
- begin
- Result := string(Pvar^);
- end;
- procedure TParElem.SetAsString(AValue: string);
- begin
- string(Pvar^) := AValue;
- end;
- function TParElem.GetAsTColor: TColor;
- begin
- Result := TColor(Pvar^);
- end;
- procedure TParElem.SetAsTColor(AValue: TColor);
- begin
- TColor(Pvar^) := AValue;
- end;
- function TParElem.GetAsBoolean: Boolean;
- begin
- Result := boolean(Pvar^);
- end;
- procedure TParElem.SetAsBoolean(AValue: Boolean);
- begin
- boolean(Pvar^) := AValue;
- end;
- function TParElem.GetAsInt32: Int32;
- begin
- Result := Int32(Pvar^);
- end;
- procedure TParElem.SetAsInt32(AValue: Int32);
- begin
- Int32(pVar^) := AValue;
- end;
- { TMiConfigBasic }
- procedure TMiConfigBasic.PropertyWindow(r: TParElem; PropToWindow: boolean);
- {Implementa el movimiento de datos entre las propiedades y los controles de la ventana
- Permite leer o escribir una propiedad, desde o hacia un comtrol}
- var
- n, j: Integer;
- list: TStringList;
- gr: TStringGrid;
- spEd: TSpinEdit;
- spFloatEd: TFloatSpinEdit;
- rdGr: TRadioGroup;
- begin
- if r.pVar = nil then exit; //se inició con NIL
- case r.tipPar of
- tp_Int:; //no tiene control asociado
- tp_Int_TEdit:
- if PropToWindow then begin //entero en TEdit
- //carga entero
- TEdit(r.pCtl).Text:=IntToStr(r.AsInteger);
- end else begin
- if not EditValidateInt(TEdit(r.pCtl),r.minEnt, r.MaxEnt) then
- exit; //hubo error. con mensaje en "msjErr"
- r.AsInteger := valInt; //guarda
- end;
- tp_Int_TSpinEdit:
- if PropToWindow then begin //entero en TSpinEdit
- //carga entero
- TSpinEdit(r.pCtl).Value:= r.AsInteger;
- end else begin
- spEd := TSpinEdit(r.pCtl);
- r.AsInteger := spEd.Value;
- end;
- tp_Int_TRadioGroup:
- if PropToWindow then begin //entero en TSpinEdit
- //carga entero
- TRadioGroup(r.pCtl).ItemIndex := r.AsInteger;
- end else begin
- rdGr := TRadioGroup(r.pCtl);
- r.AsInteger := rdGr.ItemIndex;
- end;
- //---------------------------------------------------------------------
- tp_Dbl:;
- tp_Dbl_TEdit:
- if PropToWindow then begin
- //carga double
- TEdit(r.pCtl).Text:=FloatToStr(r.AsDouble);
- end else begin
- if not EditValidateDbl(TEdit(r.pCtl),r.minDbl, r.MaxDbl) then
- exit; //hubo error. con mensaje en "msjErr"
- r.AsDouble := valDbl; //guarda
- end;
- tp_Dbl_TFloatSpinEdit:
- if PropToWindow then begin
- //carga double
- TFloatSpinEdit(r.pCtl).Value := r.AsDouble;
- end else begin
- spFloatEd := TFloatSpinEdit(r.pCtl);
- //las validaciones de rango las hace el mismo control
- r.AsDouble := spFloatEd.Value;
- end;
- //---------------------------------------------------------------------
- tp_Str:; //no tiene control asociado
- tp_Str_TEdit:
- if PropToWindow then begin //cadena en TEdit
- //carga cadena
- TEdit(r.pCtl).Text := r.AsString;
- end else begin
- r.AsString := TEdit(r.pCtl).Text;
- end;
- tp_Str_TEditButton:
- if PropToWindow then begin
- //carga cadena
- TEditButton(r.pCtl).Text := r.AsString;
- end else begin
- r.AsString := TEditButton(r.pCtl).Text;
- end;
- tp_Str_TCmbBox:
- if PropToWindow then begin //cadena en TComboBox
- //carga cadena
- TComboBox(r.pCtl).Text := r.AsString;
- end else begin
- r.AsString := TComboBox(r.pCtl).Text;
- end;
- //---------------------------------------------------------------------
- tp_Bol:; //no tiene control asociado
- tp_Bol_TCheckBox:
- if PropToWindow then begin //boolean a TCheckBox
- TCheckBox(r.pCtl).Checked := r.AsBoolean;
- end else begin
- r.AsBoolean := TCheckBox(r.pCtl).Checked;
- end;
- tp_Bol_TRadBut:
- if PropToWindow then begin //Enumerado a TRadioButtons
- if 1<=High(r.radButs) then begin
- if r.AsBoolean then r.radButs[1].checked := true //activa primero
- else r.radButs[0].checked := true //activa segundo
- end;
- end else begin
- //busca el que está marcado
- if high(r.radButs)>=1 then begin
- if r.radButs[1].checked then r.AsBoolean := true
- else r.AsBoolean := false;
- end;
- end;
- //---------------------------------------------------------------------
- tp_Enum:; //no tiene control asociado
- tp_Enum_TRadBut:
- if PropToWindow then begin //Enumerado a TRadioButtons
- if r.lVar = 4 then begin //enumerado de 4 bytes
- n := r.AsInt32; //convierte a entero
- if n<=High(r.radButs) then
- r.radButs[n].checked := true; //lo activa
- end else begin //tamño no implementado
- msjErr := 'Enumerated type no handled.';
- exit;
- end;
- end else begin
- //busca el que está marcado
- for j:=0 to high(r.radButs) do begin
- if r.radButs[j].checked then begin
- //debe fijar el valor del enumerado
- if r.lVar = 4 then begin //se puede manejar como entero
- r.AsInt32 := j; //guarda
- break;
- end else begin //tamaño no implementado
- msjErr := 'Enumerated type no handled.';
- exit;
- end;
- end;
- end;
- end;
- tp_Enum_TRadGroup:
- if PropToWindow then begin
- if r.lVar = 4 then begin //enumerado de 4 bytes
- n := r.AsInt32; //convierte a entero
- if n<TRadioGroup(r.pCtl).Items.Count then
- TRadioGroup(r.pCtl).ItemIndex:=n; //activa
- end else begin //tamño no implementado
- msjErr := 'Enumerated type no handled.';
- exit;
- end;
- end else begin
- //debe fijar el valor del enumerado
- if r.lVar = 4 then begin //se puede manejar como entero
- r.AsInt32 := TRadioGroup(r.pCtl).ItemIndex; //lee
- end else begin //tamaño no implementado
- msjErr := 'Enumerated type no handled.';
- exit;
- end;
- end;
- //---------------------------------------------------------------------
- tp_TCol_TColBut:
- if PropToWindow then begin //Tcolor a TColorButton
- TColorButton(r.pCtl).ButtonColor := r.AsTColor;
- end else begin
- r.AsTColor := TColorButton(r.pCtl).ButtonColor;
- end;
- tp_TCol_TColBox:
- if PropToWindow then begin //Tcolor a TColorButton
- TColorBox(r.pCtl).Selected := r.AsTColor;
- end else begin
- r.AsTColor := TColorBox(r.pCtl).Selected;
- end;
- //---------------------------------------------------------------------
- tp_StrList:; //no tiene control asociado
- tp_StrList_TListBox:
- if PropToWindow then begin //lista en TlistBox
- //carga lista
- list := TStringList(r.Pvar^);
- TListBox(r.pCtl).Clear;
- for j:=0 to list.Count-1 do
- TListBox(r.pCtl).AddItem(list[j],nil);
- end else begin
- list := TStringList(r.Pvar^);
- list.Clear;
- for j:= 0 to TListBox(r.pCtl).Count-1 do
- list.Add(TListBox(r.pCtl).Items[j]);
- end;
- tp_StrList_TStringGrid:
- if PropToWindow then begin //lista en TStringGrid
- //carga lista
- list := TStringList(r.Pvar^);
- gr := TStringGrid(r.pCtl);
- gr.Clear;
- gr.BeginUpdate;
- if r.HasFixedCol then gr.FixedCols:=1 else gr.FixedCols:=0;
- gr.ColCount:=r.ColCount; //fija número de columnas
- if r.HasHeader then begin
- //Hay encabezado
- gr.RowCount:=list.Count+1; //deja espacio para encabezado
- for j:=0 to list.Count-1 do begin
- gr.Cells[0,j+1] := list[j];
- end;
- end else begin
- //No hay encabezado
- gr.RowCount:=list.Count;
- for j:=0 to list.Count-1 do begin
- gr.Cells[0,j] := list[j];
- end;
- end;
- gr.EndUpdate();
- end else begin
- //????????
- end;
- else //no se ha implementado bien
- msjErr := 'Design error.';
- exit;
- end;
- end;
- function TMiConfigBasic.PropertiesToWindow: boolean;
- {Muestra en los controles, las variables asociadas
- Si encuentra error devuelve FALSE, y el mensaje de error en "MsjErr", y el elemento
- con error en "ctlErr".}
- var
- r: TParElem;
- begin
- msjErr := '';
- for r in listParElem do begin
- PropertyWindow(r, true);
- if msjErr<>'' then begin
- ctlErr := r; //guarda la referencia al elemento, en caso de que haya error
- end;
- if r.OnPropertyToWindow<>nil then r.OnPropertyToWindow;
- end;
- Result := (msjErr='');
- end;
- function TMiConfigBasic.WindowToProperties: boolean;
- {Lee en las variables asociadas, los valores de los controles
- Si encuentra error devuelve FALSE, y el mensaje de error en "MsjErr", y el elemento
- con error en "ctlErr".}
- var
- r: TParElem;
- begin
- msjErr := '';
- for r in listParElem do begin
- PropertyWindow(r, false);
- if msjErr<>'' then begin
- ctlErr := r; //guarda la referencia al elemento, en caso de que haya error
- end;
- if r.OnWindowToProperty<>nil then r.OnWindowToProperty;
- end;
- //Terminó con éxito. Actualiza los cambios
- if OnPropertiesChanges<>nil then OnPropertiesChanges;
- Result := (msjErr=''); //si hubo error, se habrá actualizado "ctlErr"
- end;
- procedure TMiConfigBasic.SetFocusEd(ed: TEdit);
- {Pone el enfoque en un TEdit, si es posible.}
- begin
- try
- if ed.visible and ed.enabled and ed.CanFocus then begin //Valida condiciones.
- ed.SetFocus; //Pone enfoque.
- end;
- //Ya si todo falla, solo confiamos en el "try"
- finally
- end;
- end;
- //Rutinas de validación
- function TMiConfigBasic.EditValidateInt(edit: TEdit; min: integer; max: integer): boolean;
- {Valida el contenido de un TEdit, para ver si se puede convertir a un valor entero.
- Si no se puede convertir, devuelve FALSE, devuelve el mensaje de error en "MsjErr", y
- pone el TEdit con enfoque.
- Si se puede convertir, devuelve TRUE, y el valor convertido en "valInt".}
- var
- tmp : string;
- c : char;
- signo: string;
- larMaxInt: Integer;
- n: Int64;
- begin
- Result := false;
- //validaciones previas
- larMaxInt := length(IntToStr(MaxInt));
- tmp := trim(edit.Text);
- if tmp = '' then begin
- MsjErr:= 'Field must contain a value.';
- SetFocusEd(edit);
- exit;
- end;
- if tmp[1] = '-' then begin //es negativo
- signo := '-'; //guarda signo
- tmp := copy(tmp, 2, length(tmp)); //quita signo
- end;
- for c in tmp do begin
- if not (c in ['0'..'9']) then begin
- MsjErr:= 'Only numeric values are allowed.';
- SetFocusEd(edit);
- exit;
- end;
- end;
- if length(tmp) > larMaxInt then begin
- MsjErr:= 'Numeric value is too large.';
- SetFocusEd(edit);
- exit;
- end;
- //lo leemos en Int64 por seguridad y validamos
- n := StrToInt64(signo + tmp);
- if n>max then begin
- MsjErr:= Format('The maximun allowed value is: %d', [max]);
- SetFocusEd(edit);
- exit;
- end;
- if n<min then begin
- MsjErr:= Format('The minimun allowed value is: %d', [min]);
- SetFocusEd(edit);
- exit;
- end;
- //pasó las validaciones
- valInt:=n; //actualiza valor
- Result := true; //tuvo éxito
- end;
- function TMiConfigBasic.EditValidateDbl(edit: TEdit; min: Double; max: Double): boolean;
- {Valida el contenido de un TEdit, para ver si se puede convertir a un valor Double.
- Si no se puede convertir, devuelve FALSE, devuelve el mensaje de error en "MsjErr", y
- pone el TEdit con enfoque.
- Si se puede convertir, devuelve TRUE, y el valor convertido en "valDbl".}
- var
- d: double;
- begin
- Result := false;
- //intenta convertir
- if not TryStrToFloat(edit.Text, d) then begin
- MsjErr:= 'Wrong float number.';
- SetFocusEd(edit);
- exit;
- end;
- //validamos
- if d>max then begin
- MsjErr:= Format('The maximun allowed value is: %f', [max]);
- SetFocusEd(edit);
- exit;
- end;
- if d<min then begin
- MsjErr:= Format('The minimun allowed value is: %f', [min]);
- SetFocusEd(edit);
- exit;
- end;
- //pasó las validaciones
- valDbl:=d; //actualiza valor
- Result := true; //tuvo éxito
- end;
- //Métodos de asociación
- function TMiConfigBasic.Asoc_Int(etiq: string; ptrInt: pointer; defVal: integer
- ): TParElem;
- //Agrega una variable Entera para guardarla en el archivo.
- var
- r: TParElem;
- begin
- r := TParElem.Create;
- r.pVar := ptrInt; //toma referencia
- r.tipPar := tp_Int; //tipo de par
- r.etiqVar:= etiq;
- r.defInt := defVal;
- listParElem.Add(r);
- Result := r;
- end;
- function TMiConfigBasic.Asoc_Int(etiq: string; ptrInt: pointer; edit: TEdit;
- defVal: integer; minVal, maxVal: integer): TParElem;
- //Agrega un par variable entera - Control TEdit
- begin
- Result := Asoc_Int(etiq, ptrInt, defVal);
- Result.pCtl := edit; //toma referencia
- Result.tipPar := tp_Int_TEdit; //tipo de par
- Result.minEnt := minVal; //protección de rango
- Result.maxEnt := maxVal; //protección de rango
- end;
- function TMiConfigBasic.Asoc_Int(etiq: string; ptrInt: pointer;
- spEdit: TSpinEdit; defVal: integer): TParElem;
- //Agrega un par variable entera - Control TSpinEdit
- begin
- Result := Asoc_Int(etiq, ptrInt, defVal);
- Result.pCtl := spEdit; //toma referencia
- Result.tipPar := tp_Int_TSpinEdit; //tipo de par
- end;
- function TMiConfigBasic.Asoc_Int(etiq: string; ptrInt: pointer;
- radGroup: TRadioGroup; defVal: integer): TParElem;
- //Agrega un par variable entera - Control TRadioGroup
- begin
- Result := Asoc_Int(etiq, ptrInt, defVal);
- Result.pCtl := radGroup; //toma referencia
- Result.tipPar := tp_Int_TRadioGroup; //tipo de par
- end;
- //---------------------------------------------------------------------
- function TMiConfigBasic.Asoc_Dbl(etiq: string; ptrDbl: PDouble; defVal: double
- ): TParElem;
- var
- r: TParElem;
- begin
- r := TParElem.Create;
- r.pVar := ptrDbl; //toma referencia
- r.tipPar := tp_Dbl; //tipo de par
- r.etiqVar:= etiq;
- r.defDbl := defVal;
- listParElem.Add(r);
- Result := r;
- end;
- function TMiConfigBasic.Asoc_Dbl(etiq: string; ptrDbl: PDouble; edit: TEdit;
- defVal: double; minVal, maxVal: double): TParElem;
- //Agrega un par variable double - Control TEdit
- begin
- Result := Asoc_Dbl(etiq, ptrDbl, defVal);
- Result.pCtl := edit; //toma referencia
- Result.tipPar := tp_Dbl_TEdit; //tipo de par
- Result.minDbl := minVal; //protección de rango
- Result.maxDbl := maxVal; //protección de rango
- end;
- function TMiConfigBasic.Asoc_Dbl(etiq: string; ptrDbl: PDouble;
- spEdit: TFloatSpinEdit; defVal: double): TParElem;
- begin
- Result := Asoc_Dbl(etiq, ptrDbl, defVal);
- Result.pCtl := spEdit; //toma referencia
- Result.tipPar := tp_Dbl_TFloatSpinEdit; //tipo de par
- end;
- //---------------------------------------------------------------------
- function TMiConfigBasic.Asoc_Str(etiq: string; ptrStr: pointer; defVal: string
- ): TParElem;
- //Agrega una variable String para guardarla en el archivo.
- var
- r: TParElem;
- begin
- r := TParElem.Create;
- r.pVar := ptrStr; //toma referencia
- r.tipPar := tp_Str; //tipo de par
- r.etiqVar:= etiq;
- r.defStr := defVal;
- listParElem.Add(r);
- Result := r;
- end;
- function TMiConfigBasic.Asoc_Str(etiq: string; ptrStr: pointer;
- edit: TCustomEdit; defVal: string): TParElem;
- //Agrega un par variable string - Control TEdit
- begin
- Result := Asoc_Str(etiq, ptrStr, defVal);
- Result.pCtl := edit; //toma referencia
- Result.tipPar := tp_Str_TEdit; //tipo de par
- end;
- function TMiConfigBasic.Asoc_Str(etiq: string; ptrStr: pointer;
- edit: TCustomEditButton; defVal: string): TParElem;
- //Agrega un par variable string - Control TEditButton
- begin
- Result := Asoc_Str(etiq, ptrStr, defVal);
- Result.pCtl := edit; //toma referencia
- Result.tipPar := tp_Str_TEditButton; //tipo de par
- end;
- function TMiConfigBasic.Asoc_Str(etiq: string; ptrStr: pointer;
- cmbBox: TComboBox; defVal: string): TParElem;
- //Agrega un par variable string - Control TEdit
- begin
- Result := Asoc_Str(etiq, ptrStr, defVal);
- Result.pCtl := cmbBox; //toma referencia
- Result.tipPar := tp_Str_TCmbBox; //tipo de par
- end;
- //---------------------------------------------------------------------
- function TMiConfigBasic.Asoc_Bol(etiq: string; ptrBol: pointer; defVal: boolean
- ): TParElem;
- var
- r: TParElem;
- begin
- r := TParElem.Create;
- r.pVar := ptrBol; //toma referencia
- r.tipPar := tp_Bol; //tipo de par
- r.etiqVar:= etiq;
- r.defBol := defVal;
- listParElem.Add(r);
- Result := r;
- end;
- function TMiConfigBasic.Asoc_Bol(etiq: string; ptrBol: pointer; chk: TCheckBox;
- defVal: boolean): TParElem;
- //Agrega un para variable booleana - Control TCheckBox
- begin
- Result := Asoc_Bol(etiq, ptrBol, defVal);
- Result.pCtl := chk; //toma referencia
- Result.tipPar := tp_Bol_TCheckBox; //tipo de par
- end;
- function TMiConfigBasic.Asoc_Bol(etiq: string; ptrBol: pointer;
- radButs: array of TRadioButton; defVal: boolean): TParElem;
- //Agrega un par variable Enumerated - Controles TRadioButton
- //Solo se permiten enumerados de hasta 32 bits de tamaño
- var
- i: Integer;
- begin
- Result := Asoc_Bol(etiq, ptrBol, defVal);
- // Result.pCtl := ; //toma referencia
- Result.tipPar := tp_Bol_TRadBut; //tipo de par
- //guarda lista de controles
- setlength(Result.radButs, high(radButs)+1); //hace espacio
- for i:=0 to high(radButs) do
- Result.radButs[i]:= radButs[i];
- end;
- //---------------------------------------------------------------------
- function TMiConfigBasic.Asoc_Enum(etiq: string; ptrEnum: pointer;
- EnumSize: integer; defVal: integer): TParElem;
- var
- r: TParElem;
- begin
- r := TParElem.Create;
- r.pVar := ptrEnum; //toma referencia
- r.lVar := EnumSize; //necesita el tamaño para modificarlo luego
- r.tipPar := tp_Enum; //tipo de par
- r.etiqVar:= etiq;
- r.defInt := defVal; //se maneja como entero
- listParElem.Add(r);
- Result := r;
- end;
- function TMiConfigBasic.Asoc_Enum(etiq: string; ptrEnum: pointer;
- EnumSize: integer; radButs: array of TRadioButton; defVal: integer): TParElem;
- //Agrega un par variable Enumerated - Controles TRadioButton
- //Solo se permiten enumerados de hasta 32 bits de tamaño
- var
- i: Integer;
- begin
- Result := Asoc_Enum(etiq, ptrEnum, EnumSize, defVal);
- // Result.pCtl := ; //toma referencia
- Result.tipPar := tp_Enum_TRadBut; //tipo de par
- //guarda lista de controles
- setlength(Result.radButs, high(radButs)+1); //hace espacio
- for i:=0 to high(radButs) do
- Result.radButs[i]:= radButs[i];
- end;
- function TMiConfigBasic.Asoc_Enum(etiq: string; ptrEnum: pointer; EnumSize: integer;
- radGroup: TRadioGroup; defVal: integer): TParElem;
- //Agrega un par variable Enumerated - Control TRadioGroup
- //Solo se permiten enumerados de hasta 32 bits de tamaño
- begin
- Result := Asoc_Enum(etiq, ptrEnum, EnumSize, defVal);
- Result.pCtl := radGroup; //toma referencia a control
- Result.tipPar := tp_Enum_TRadGroup; //tipo de par
- end;
- //---------------------------------------------------------------------
- function TMiConfigBasic.Asoc_TCol(etiq: string; ptrTCol: pointer;
- colBut: TColorButton; defVal: TColor): TParElem;
- //Agrega un par variable TColor - Control TColorButton
- var
- r: TParElem;
- begin
- r := TParElem.Create;
- r.pVar := ptrTCol; //toma referencia
- r.pCtl := colBut; //toma referencia a control
- r.tipPar := tp_TCol_TColBut; //tipo de par
- r.etiqVar:= etiq;
- r.defCol := defVal;
- listParElem.Add(r);
- Result := r;
- end;
- function TMiConfigBasic.Asoc_TCol(etiq: string; ptrTCol: pointer; colBut: TColorBox;
- defVal: TColor): TParElem;
- //Agrega un par variable TColor - Control TColorButton
- var
- r: TParElem;
- begin
- r := TParElem.Create;
- r.pVar := ptrTCol; //toma referencia
- r.pCtl := colBut; //toma referencia a control
- r.tipPar := tp_TCol_TColBox; //tipo de par
- r.etiqVar:= etiq;
- r.defCol := defVal;
- listParElem.Add(r);
- Result := r;
- end;
- //---------------------------------------------------------------------
- function TMiConfigBasic.Asoc_StrList(etiq: string; ptrStrList: pointer
- ): TParElem;
- //Agrega una variable TStringList para guardarla en el archivo. El StrinList, debe estar
- //ya creado, sino dará error.
- var
- r: TParElem;
- begin
- r := TParElem.Create;
- r.pVar := ptrStrList; //toma referencia
- // r.pCtl := colBut; //toma referencia
- r.tipPar := tp_StrList; //tipo de par
- r.etiqVar:= etiq;
- // r.defCol := defVal;
- listParElem.Add(r);
- Result := r;
- end;
- function TMiConfigBasic.Asoc_StrList_TListBox(etiq: string;
- ptrStrList: pointer; lstBox: TlistBox): TParElem;
- var
- r: TParElem;
- begin
- r := TParElem.Create;
- r.pVar := ptrStrList; //toma referencia
- r.pCtl := lstBox; //toma referencia
- r.tipPar := tp_StrList_TlistBox; //tipo de par
- r.etiqVar:= etiq;
- // r.defCol := defVal;
- listParElem.Add(r);
- Result := r;
- end;
- constructor TMiConfigBasic.Create;
- begin
- listParElem := TParElem_list.Create(true);
- end;
- destructor TMiConfigBasic.Destroy;
- begin
- listParElem.Destroy;
- inherited Destroy;
- end;
- end.
|