unit P3E_TypyUkolu;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, P3KvaFormQE, AdvUtil, Vcl.Menus,
  System.ImageList, Vcl.ImgList, System.Actions, Vcl.ActnList, KvaActionSource,
  Vcl.Grids, AdvObj, BaseGrid, AdvGrid, Vcl.ExtCtrls, AdvGlowButton,
  AdvSmoothLabel, Vcl.StdCtrls, GradientLabel, AdvPanel, P3ObjPPC, P3Types,
  P3Global, P3SysTxt, P3VizTyp, P3ObjSFr;

type
  TE_TypyUkolu = class(TKvaFormQE)
    procedure OQedityCanEditCell(Sender: TObject; ARow, ACol: Integer;
      var CanEdit: Boolean);
    procedure OQedityCanSort(Sender: TObject; ACol: Integer;
      var DoSort: Boolean);
    procedure OQedityCellValidate(Sender: TObject; ACol, ARow: Integer;
      var Value: string; var Valid: Boolean);
    procedure OQedityGetAlignment(Sender: TObject; ARow, ACol: Integer;
      var HAlign: TAlignment; var VAlign: TVAlignment);
    procedure OQedityGetDisplText(Sender: TObject; ACol, ARow: Integer;
      var Value: string);
    procedure OQedityGetEditText(Sender: TObject; ACol, ARow: Integer;
      var Value: string);
  private
    { Private declarations }
  public
    { Public declarations }
    ETypyUkolu:TKvaTypUkolu;
    SetFormularWarn:TKvaSetFormular;
    procedure InitGrid; override;
    procedure ReFillGrid; override;
    procedure InitFirstGo; override;
    procedure InitForm(NewSectiom:TKvaTypeSection); override;
    function CorrectForm:integer; override;
    constructor CreateNew(AOwner:TComponent; Dummy: Integer=0); override;
  end;

var
  E_TypyUkolu: TE_TypyUkolu;

implementation

{$R *.dfm}

const
    rad_kod       = 0;
    rad_zkratka   = 1;
    rad_nazev     = 2;

    rad_pocet     = 3;

{ TE_TypyUkolu }

function TE_TypyUkolu.CorrectForm: integer;
var LTypUkoluList:TKvaTypUkoluList;
    LPocet:integer;
begin
   Result:=-1;
   LTypUkoluList:= TKvaTypUkoluList(ETypyUkolu.Owner);
   LPocet:=LTypUkoluList.PocetUkoluDleKodu(ETypyUkolu.typuko);
   if (LPocet>1) then
      begin
         ShowWarning(AktUzivatel,'Tento kód již existuje',0,5,SetFormularWarn);
      end
   else
      Result:=inherited CorrectForm;
end;

constructor TE_TypyUkolu.CreateNew(AOwner: TComponent; Dummy: Integer);
begin
  inherited;
   ReadComponentResFile(GUnitsDir+'\P3E_TypyUkolu.dfm',self);
end;

procedure TE_TypyUkolu.InitFirstGo;
begin
  FDefTop:=100;
   FDefLeft:=100;
   Constraints.MinHeight:= 300;
   Constraints.MinWidth:= 200;
   Constraints.MaxHeight:=GMaxHeight;
   Constraints.MaxWidth:=GMaxWidth;
  inherited;
   Caption:=AktUzivatel.CaptionPERM(Width,'5TypyUkolu',GUserMan);
   XEheadText.Caption.Text:=TxtLng('5TypyUkolu');
   OQedity.Options := OQedity.Options + [goEditing]-[goRowSelect];
   OQedity.ColCount:=2;
   OQedity.RowCount:=rad_pocet;
   OQedity.FixedCols:=0;
   OQedity.FixedRows:=0;
   OQedity.ColWidths[0]:=30;
   OQedity.ColWidths[1]:=120;
   FirstGo:=false;
end;

procedure TE_TypyUkolu.InitForm(NewSectiom: TKvaTypeSection);
begin
  inherited;
   ETypyUkolu:=TKvaTypUkolu(EditObj);
   if FirstGo then InitFirstGo;
   InitGrid;
   ReFillGrid;
end;

procedure TE_TypyUkolu.InitGrid;
begin
  inherited;
   OQedity.DefaultRowHeight:=18+((Lsf_Edity*4)*FDefPixel);
end;

procedure TE_TypyUkolu.OQedityCanEditCell(Sender: TObject; ARow, ACol: Integer;
  var CanEdit: Boolean);
begin
  inherited;
  if Acol=1 then
    begin
      case OQedity.RealColIndex(ARow) of
       0:   CanEdit:=IfInsert;
       1,2: CanEdit:=true;
       else CanEdit:=false;
      end;
    end
  else CanEdit:=false;
end;

procedure TE_TypyUkolu.OQedityCanSort(Sender: TObject; ACol: Integer;
  var DoSort: Boolean);
begin
  inherited;
   DoSort:=false;
end;

procedure TE_TypyUkolu.OQedityCellValidate(Sender: TObject; ACol, ARow: Integer;
  var Value: string; var Valid: Boolean);
begin
  inherited;
    if OQedity.RealColIndex(ACol)=1 then
    begin
      case OQedity.RealRowIndex(ARow) of
        rad_kod:      ETypyUkolu.typuko:=StrToInt(Trim(Value));
        rad_zkratka:  ETypyUkolu.zkrtyp:=Trim(Value);
        rad_nazev:    ETypyUkolu.naztyp:=Trim(Value);
      end;
    end;
end;

procedure TE_TypyUkolu.OQedityGetAlignment(Sender: TObject; ARow, ACol: Integer;
  var HAlign: TAlignment; var VAlign: TVAlignment);
begin
  inherited;
  case OQedity.RealColIndex(ACol) of
   0:   HAlign:=taRightJustify;
  else HAlign:=taLeftJustify;
  end;
end;

procedure TE_TypyUkolu.OQedityGetDisplText(Sender: TObject; ACol, ARow: Integer;
  var Value: string);
begin
  inherited;
  if OQedity.RealColIndex(ACol)=1 then
  begin
    case OQedity.RealRowIndex(ARow) of
      rad_kod:      Value:=ETypyUkolu.typuko.ToString;
      rad_zkratka:  Value:=ETypyUkolu.zkrtyp;
      rad_nazev:    Value:=ETypyUkolu.naztyp;
    end;
  end;
end;

procedure TE_TypyUkolu.OQedityGetEditText(Sender: TObject; ACol, ARow: Integer;
  var Value: string);
begin
  inherited;
  if (OQedity.RealColIndex(ACol)=1)and(ETypyUkolu<>nil) then
    begin
      case OQedity.RealRowIndex(ARow) of
        rad_kod:      Value:=ETypyUkolu.typuko.ToString;
        rad_zkratka:  Value:=ETypyUkolu.zkrtyp;
        rad_nazev:    Value:=ETypyUkolu.naztyp;
      end;
    end;
end;

procedure TE_TypyUkolu.ReFillGrid;
begin
  inherited;
  OQedity.Invalidate;
end;

end.
