unit ColVisibleManager;

interface

uses
  Classes, sdGridDBA, ZhAPI, sdGridTreeDBA;

type
  TColVisible = class
  private
    FCustom: Boolean;
    // Defualt CaptionAcrossCols
    FDefaultCAC: Integer;
    FDBACol: TsdGridColumn;
  public
    constructor Create(ACol: TsdGridColumn); virtual;

    procedure RefreshVisible; virtual;

    property DBACol: TsdGridColumn read FDBACol;
    property DefaultCAC: Integer read FDefaultCAC;
    property Custom: Boolean read FCustom write FCustom;
  end;

  TBM_ColVisible = class(TColVisible)
  private
    FPriceChange: Boolean;
    FBGLCode: Boolean;
    FAlias: Boolean;
    FDesign: Boolean;
    FApprovalCode: Boolean;
    FIsGatherZJJL: Boolean;
    function GetVisible: Boolean;
  public
    constructor Create(ACol: TsdGridColumn); override;

    procedure RefreshVisible; override;

    property PriceChange: Boolean read FPriceChange write FPriceChange;
    property BGLCode: Boolean read FBGLCode write FBGLCode;
    property Design: Boolean read FDesign write FDesign;
    property Alias: Boolean read FAlias write FAlias;
    property ApprovalCode: Boolean read FApprovalCode write FApprovalCode;
    property IsGatherZJJL: Boolean read FIsGatherZJJL write FIsGatherZJJL;

    property Visible: Boolean read GetVisible;
  end;

  TColVisibleManager = class
  private
    FColVisibles: TList;
    function GetColVisible(AIndex: Integer): TColVisible;
  protected
    function CreateColVisible(ACol: TsdGridColumn): TColVisible; virtual;
  public
    constructor Create(ACols: TsdGridColumnList);
    destructor Destroy; override;

    procedure RefreshVisible; virtual;

    procedure ShowGridCol(AShow: Boolean; ABeginCol, AEndCol: Integer);

    property ColVisible[AIndex: Integer]: TColVisible read GetColVisible;
  end;

  TBM_ColVisibleManager = class(TColVisibleManager)
  private
    FGridFixedColCount: Integer;
    FOrgTreeCol: Integer;
    FGridTreeDBA: TsdGridTreeDBA;
  protected
    function CreateColVisible(ACol: TsdGridColumn): TColVisible; override;
  public
    constructor Create(AGridTreeDBA: TsdGridTreeDBA);

    procedure ShowPriceChange(AShow: Boolean);
    procedure ShowBGLCode(AShow: Boolean);
    procedure ShowDesign(AShow: Boolean);
    procedure ShowAlias(AShow: Boolean);
    procedure ShowApprovalCode(AShow: Boolean);
    procedure ShowIsGather(AShow: Boolean);

    procedure RefreshVisible; override;

    property GridTreeDBA: TsdGridTreeDBA read FGridTreeDBA;
  end;

implementation

uses SysUtils, ZJGrid;

{ TColVisible }

constructor TColVisible.Create(ACol: TsdGridColumn);
begin
  FDBACol := ACol;
  FDefaultCAC := StrToIntDef(FDBACol.Title.CaptionAcrossCols, 1);
  FCustom := FDBACol.Visible;
end;

procedure TColVisible.RefreshVisible;
begin
  FDBACol.Visible := FCustom;
end;

{ TBM_ColVisible }

constructor TBM_ColVisible.Create(ACol: TsdGridColumn);
begin
  inherited;
  FPriceChange := True;
  FBGLCode := True;
  FDesign := True;
  FAlias := True;
  FApprovalCode := True;
  FIsGatherZJJL := True;
end;

function TBM_ColVisible.GetVisible: Boolean;
begin
  Result := FCustom and FPriceChange and FBGLCode and FDesign and FAlias and FApprovalCode and FIsGatherZJJL;
end;

procedure TBM_ColVisible.RefreshVisible;
begin
  FDBACol.Visible := FCustom and FPriceChange and FBGLCode and FDesign and FAlias and FApprovalCode and FIsGatherZJJL;
end;

{ TColVisibleManager }

constructor TColVisibleManager.Create(ACols: TsdGridColumnList);
var
  iCol: Integer;
  ColVisible: TColVisible;
begin
  FColVisibles := TList.Create;
  for iCol := 0 to ACols.Count - 1 do
  begin
    ColVisible := CreateColVisible(ACols.Items[iCol]);
    FColVisibles.Add(ColVisible);
  end;
end;

function TColVisibleManager.CreateColVisible(
  ACol: TsdGridColumn): TColVisible;
begin
  Result := TColVisible.Create(ACol);
end;

destructor TColVisibleManager.Destroy;
begin
  ClearObjects(FColVisibles);
  FColVisibles.Free;
  inherited;
end;

function TColVisibleManager.GetColVisible(AIndex: Integer): TColVisible;
begin
  Result := TColVisible(FColVisibles[AIndex]);
end;

procedure TColVisibleManager.RefreshVisible;
var
  iCol: Integer;
begin
  for iCol := 0 to FColVisibles.Count - 1 do
    TColVisible(FColVisibles.Items[iCol]).RefreshVisible;
end;

procedure TColVisibleManager.ShowGridCol(AShow: Boolean; ABeginCol,
  AEndCol: Integer);
var
  iCol, iRelaCol: Integer;
  vCol, vRelaCol: TColVisible;
begin
  for iCol := ABeginCol to AEndCol do
  begin
    vCol := TColVisible(FColVisibles.Items[iCol]);
    if Pos('|', vCol.DBACol.Title.Caption) <> 1 then
    begin
      vCol.Custom := AShow;
      if vCol.DefaultCAC > 1 then
      begin
        for iRelaCol := 1 to vCol.DefaultCAC - 1 do
        begin
          vRelaCol := TColVisible(FColVisibles.Items[iCol+iRelaCol]);
          vRelaCol.Custom := AShow;
        end;
      end;
    end;
  end;
  RefreshVisible;
end;

{ TBM_ColVisibleManager }

constructor TBM_ColVisibleManager.Create(AGridTreeDBA: TsdGridTreeDBA);
begin
  inherited Create(AGridTreeDBA.Columns);
  FGridTreeDBA := AGridTreeDBA;
  FOrgTreeCol := AGridTreeDBA.TreeCellCol;
  if Assigned(AGridTreeDBA.Grid) then
    FGridFixedColCount := AGridTreeDBA.Grid.FixedColCount
  else
    FGridFixedColCount := 1;
end;

function TBM_ColVisibleManager.CreateColVisible(
  ACol: TsdGridColumn): TColVisible;
begin
  Result := TBM_ColVisible.Create(ACol);
end;

procedure TBM_ColVisibleManager.RefreshVisible;
var
  i, iCount: Integer;
  vGrid: TZJGrid;
begin
  (*
  iCount := 0;
  for i := 0 to FOrgTreeCol - FGridFixedColCount do
  begin
    if TBM_ColVisible(ColVisible[i]).Visible then
      Inc(iCount);
  end;
  FGridTreeDBA.TreeCellCol := FGridFixedColCount + iCount - 1;
  *)
  inherited;
  iCount := 0;
  for i := 0 to FOrgTreeCol - FGridFixedColCount do
  begin
    if TBM_ColVisible(ColVisible[i]).Visible then
      Inc(iCount);
  end;
  if FGridTreeDBA.TreeCellCol <> FGridFixedColCount + iCount - 1 then
  begin
    vGrid := FGridTreeDBA.Grid;
    FGridTreeDBA.Grid := nil;
    vGrid.BeginUpdate;
    vGrid.CellClass.Cols[FGridTreeDBA.TreeCellCol] := vGrid.CellClass.DefaultCellClass;
    vGrid.ColCount := vGrid.FixedColCount;                                             
    vGrid.RowCount := vGrid.FixedRowCount + FGridTreeDBA.ExtendRowCount;
    FGridTreeDBA.TreeCellCol := FGridFixedColCount + iCount - 1;
    FGridTreeDBA.Grid := vGrid;
    vGrid.EndUpdate;
  end;
end;

procedure TBM_ColVisibleManager.ShowAlias(AShow: Boolean);
var
  iCol: Integer;
  vCol: TBM_ColVisible;
begin
  for iCol := 0 to FColVisibles.Count - 1 do
  begin
    vCol := TBM_ColVisible(FColVisibles.Items[iCol]);
    if SameText('Alias', vCol.DBACol.FieldName) then
      vCol.FAlias := AShow;
  end;
  RefreshVisible;
end;

procedure TBM_ColVisibleManager.ShowApprovalCode(AShow: Boolean);
var
  iCol: Integer;
  vCol: TBM_ColVisible;
begin
  for iCol := 0 to FColVisibles.Count - 1 do
  begin
    vCol := TBM_ColVisible(FColVisibles.Items[iCol]);
    if SameText('ApprovalCode', vCol.DBACol.FieldName) then
      vCol.FApprovalCode := AShow;
  end;
  RefreshVisible;
end;

procedure TBM_ColVisibleManager.ShowBGLCode(AShow: Boolean);
var
  iCol: Integer;
  vCol: TBM_ColVisible;
begin
  for iCol := 0 to FColVisibles.Count - 1 do
  begin
    vCol := TBM_ColVisible(FColVisibles.Items[iCol]);
    if SameText('CurQcQuantity', vCol.DBACol.FieldName) or
        SameText('CurPcQuantity', vCol.DBACol.FieldName) then
    begin
      if AShow then
        vCol.DBACol.Title.CaptionAcrossCols := '3'
      else
        vCol.DBACol.Title.CaptionAcrossCols := '2';
    end
    else if Pos('BGLCode', vCol.DBACol.FieldName) > 1 then
      vCol.FBGLCode := AShow;
  end;
  RefreshVisible;
end;

procedure TBM_ColVisibleManager.ShowDesign(AShow: Boolean);
var
  iCol: Integer;
  vCol: TBM_ColVisible;
begin
  for iCol := 0 to FColVisibles.Count - 1 do
  begin
    vCol := TBM_ColVisible(FColVisibles.Items[iCol]);
    if Pos('Dgn', vCol.DBACol.FieldName) > 1 then
      vCol.FDesign := AShow;
  end;
  RefreshVisible;
end;

procedure TBM_ColVisibleManager.ShowIsGather(AShow: Boolean);
var
  iCol: Integer;
  vCol: TBM_ColVisible;
begin
  for iCol := 0 to FColVisibles.Count - 1 do
  begin
    vCol := TBM_ColVisible(FColVisibles.Items[iCol]);
    if SameText('IsGatherZJJL', vCol.DBACol.FieldName) then
      vCol.FIsGatherZJJL := AShow;
  end;
  RefreshVisible;
end;

procedure TBM_ColVisibleManager.ShowPriceChange(AShow: Boolean);
var
  iCol: Integer;
  vCol: TBM_ColVisible;
begin
  for iCol := 0 to FColVisibles.Count - 1 do
  begin
    vCol := TBM_ColVisible(FColVisibles.Items[iCol]);
    if SameText(vCol.DBACol.FieldName, 'NewPrice') or
        (Pos('CurPc', vCol.DBACol.FieldName) = 1) or
        (Pos('EndPc', vCol.DBACol.FieldName) = 1) or
        (Pos('AddPc', vCol.DBACol.FieldName) = 1) then
      vCol.PriceChange := AShow;
  end;
  RefreshVisible;
end;

end.
