cxgridlevel.pas

来自「胜天进销存源码,国产优秀的进销存」· PAS 代码 · 共 950 行 · 第 1/2 页

PAS
950
字号
function TcxGridLevel.GetVisibleIndex: Integer;
begin
  Result := FParent.FVisibleItems.IndexOf(Self);
end;

function TcxGridLevel.GetVisibleItem(Index: Integer): TcxGridLevel;
begin
  Result := TcxGridLevel(FVisibleItems[Index]);
end;

procedure TcxGridLevel.SetActive(Value: Boolean);
begin
  if Active <> Value then
    if Value then
      if IsTop then
        TcxCustomGrid(FControl).ActiveLevel := Self
      else  
    else
      TcxCustomGrid(FControl).ActiveLevel := nil;
end;

procedure TcxGridLevel.SetCaption(const Value: string);
begin
  if FCaption <> Value then
  begin
    FCaption := Value;
    Changed;
    NotifyControl(lcCaption);
  end;
end;

procedure TcxGridLevel.SetGridView(Value: TcxCustomGridView);
var
  I: Integer;
  APrevGridView: TcxCustomGridView;

  procedure CheckFocusedGridView;
  begin         
    if FGridView <> nil then
      if (APrevGridView = nil) and (TcxCustomGrid(FControl).FocusedView = nil) or
        (APrevGridView <> nil) and APrevGridView.Focused then
        FGridView.Focused := True
      else  
    else
      APrevGridView.Focused := False;
  end;

begin
  if FGridView <> Value then
  begin
    if (Value <> nil) and (Value.Level <> nil) then
      TcxGridLevel(Value.Level).GridView := nil;
    if FGridView <> nil then
    begin
      FGridView.BeginUpdate;
      try
        for I := 0 to Count - 1 do
          RemoveDataRelation(Items[I]);
      finally
        //FGridView.EndUpdate;
        FGridView.CancelUpdate;
      end;
      TcxCustomGridViewAccess(FGridView).SetLevel(nil);
    end;
    APrevGridView := FGridView;
    FGridView := Value;
    if FGridView <> nil then
      FGridView.Control := Control;  {5}
    if FParent <> nil then
      FParent.RefreshVisibleItemsList;

    if FGridView <> nil then FGridView.BeginUpdate;
    try
      if APrevGridView <> nil then
        APrevGridView.DataController.SetMasterMode(nil, True);
      if FGridView <> nil then
      begin
        TcxCustomGridViewAccess(FGridView).SetLevel(Self);
        SetupMasterRelation(FVisible, not IsTop);
        for I := 0 to Count - 1 do
          AddDataRelation(Items[I]);
      end;
      Changed(False);  {5}
    finally
      if FGridView <> nil then FGridView.EndUpdate;

      CheckFocusedGridView;
      if APrevGridView <> nil then
        APrevGridView.Control := APrevGridView.StorageControl;
      NotifyControl(lcGridView);
    end;
  end;
end;

procedure TcxGridLevel.SetImageIndex(Value: TImageIndex);
begin
  if Value < -1 then Value := -1;
  if FImageIndex <> Value then
  begin
    FImageIndex := Value;
    Changed;
  end;
end;

procedure TcxGridLevel.SetIndex(Value: Integer);
begin
  FParent.ChangeItemIndex(Self, Value);
end;

procedure TcxGridLevel.SetMaxDetailHeight(Value: Integer);
begin
  if Value < 0 then Value := 0;
  if FMaxDetailHeight <> Value then
  begin
    FMaxDetailHeight := Value;
    Changed;
  end;
end;

procedure TcxGridLevel.SetParent(Value: TcxGridLevel);
begin
  if FParent <> Value then
  begin
    TcxCustomGrid(Control).BeginUpdate;
    try
      if (Value <> nil) and ((Value = Self) or HasAsChild(Value)) then
        raise Exception.Create(scxGridRecursiveLevels);
      if FParent <> nil then
        FParent.RemoveItem(Self);
      if Value <> nil then
        Value.AddItem(Self);
    finally
      TcxCustomGrid(Control).EndUpdate;
    end;
  end;
end;

procedure TcxGridLevel.SetStyles(Value: TcxGridLevelStyles);
begin
  FStyles.Assign(Value);
end;

procedure TcxGridLevel.SetVisible(Value: Boolean);
begin
  if FVisible <> Value then
  begin
    {if Value and (FGridView <> nil) then
      SetupMasterRelation(Value, not IsTop);}
    FVisible := Value;
    if Value and (FGridView <> nil) then
      SetupMasterRelation(Value, not IsTop);
    if FParent <> nil then
      FParent.ItemVisibleChanged(Self);
    if FVisible then
      if TcxCustomGrid(FControl).ActiveLevel = nil then
        Active := True
      else
    else
      Active := False;
    Changed;
    TcxCustomGrid(Control).CheckFocusedView;
    if not Value and (FGridView <> nil) then
      SetupMasterRelation(Value, not IsTop);
    NotifyControl(lcVisibility);
  end;
end;

procedure TcxGridLevel.AddItem(AItem: TcxGridLevel);
begin
  TcxCustomGrid(Control).BeginUpdate;
  try
    FItems.Add(AItem);
    AItem.FControl := FControl;
    AItem.FParent := Self;
    RefreshVisibleItemsList;
    AddDataRelation(AItem);
    if IsRoot and (TcxCustomGrid(FControl).ActiveLevel = nil) then
      TcxCustomGrid(FControl).ActiveLevel := GetAvailableItem;
    Changed;
  finally
    TcxCustomGrid(Control).EndUpdate;
  end;
  NotifyControl(lcStructure);
end;

procedure TcxGridLevel.RemoveItem(AItem: TcxGridLevel);
begin
  TcxCustomGrid(Control).BeginUpdate;
  try
    FItems.Remove(AItem);
    FVisibleItems.Remove(AItem);
    AItem.FParent := nil;
    RemoveDataRelation(AItem);
    AItem.Active := False;
    Changed;
  finally
    TcxCustomGrid(Control).EndUpdate;
  end;
  NotifyControl(lcStructure);
end;

procedure TcxGridLevel.ChangeItemIndex(AItem: TcxGridLevel; Value: Integer);
begin
  if Value < 0 then Value := 0;
  if Value >= Count then Value := Count - 1;
  FItems.Move(AItem.Index, Value);
  RefreshVisibleItemsList;
  MoveDataRelation(AItem, Value);
  Changed;
  NotifyControl(lcStructure);
end;

function TcxGridLevel.GetItemIndex(AItem: TcxGridLevel): Integer;
begin
  Result := FItems.IndexOf(AItem);
end;

procedure TcxGridLevel.DestroyItems;
var
  I: Integer;
begin
  for I := Count - 1 downto 0 do
    Items[I].Free;
end;

procedure TcxGridLevel.RefreshVisibleItemsList;
var
  I: Integer;
begin
  FVisibleItems.Clear;
  for I := 0 to Count - 1 do
    if Items[I].Visible then
      FVisibleItems.Add(Items[I]);
end;

procedure TcxGridLevel.AddDataRelation(AItem: TcxGridLevel);
begin
  if FGridView <> nil then
  begin
    AItem.FDataRelation := FGridView.DataController.Relations.Add(AItem);
    with AItem do
      if GridView <> nil then
        SetupMasterRelation(Visible, GridView.DataController.IsPattern);
  end;
end;

procedure TcxGridLevel.MoveDataRelation(AItem: TcxGridLevel; AIndex: Integer);
begin
  if FGridView <> nil then
    FGridView.DataController.Relations.Move(AItem.DataRelation.Index, AIndex);
end;

procedure TcxGridLevel.RemoveDataRelation(AItem: TcxGridLevel);
begin
  FreeAndNil(AItem.FDataRelation);
end;

procedure TcxGridLevel.GetChildren(Proc: TGetChildProc; Root: TComponent);
var
  I: Integer;
begin
  inherited;
  for I := 0 to Count - 1 do
    if Items[I].Owner = Root then
      Proc(Items[I]);
end;

procedure TcxGridLevel.SetChildOrder(Child: TComponent; Order: Integer);
begin
  inherited;
  if Child is TcxGridLevel then
    TcxGridLevel(Child).Index := Order;
end;

procedure TcxGridLevel.SetName(const NewName: TComponentName);
begin
  inherited;
  NotifyControl(lcName);
end;

procedure TcxGridLevel.SetParentComponent(Value: TComponent);
begin
  inherited;
  if Value is TcxCustomGrid then
    TcxCustomGrid(Value).Levels.AddItem(Self)
  else
    if Value is TcxGridLevel then
      TcxGridLevel(Value).AddItem(Self);
end;

procedure TcxGridLevel.Changed(AHardChange: Boolean);
begin
  if FControl = nil then Exit;
  if AHardChange then
    TcxCustomGrid(FControl).SizeChanged
  else
    TcxCustomGrid(FControl).LayoutChanged
end;

function TcxGridLevel.GetItemClass: TcxGridLevelClass;
begin
  Result := TcxGridLevel;
end;

function TcxGridLevel.GetOptionsClass: TcxGridLevelOptionsClass;
begin
  Result := TcxGridLevelOptions; 
end;

function TcxGridLevel.GetStylesClass: TcxGridLevelStylesClass;
begin
  Result := TcxGridLevelStyles;
end;

procedure TcxGridLevel.ItemVisibleChanged(AItem: TcxGridLevel);
var
  APrevVisibleCount: Integer;
begin
  APrevVisibleCount := VisibleCount;
  RefreshVisibleItemsList;
  if FGridView <> nil then
  TcxCustomGridViewAccess(FGridView).DetailVisibleChanged(AItem,
    APrevVisibleCount, VisibleCount);
end;

procedure TcxGridLevel.NotifyControl(AChangeKind: TcxGridLevelChangeKind);
begin
  TcxCustomGridAccess(FControl).LevelChanged(Self, AChangeKind);
end;

procedure TcxGridLevel.SetupMasterRelation(AVisible, AIsPattern: Boolean);
begin
  if AVisible then
    FGridView.DataController.SetMasterMode(FDataRelation, AIsPattern)
  else
    FGridView.DataController.SetMasterMode(nil, True);
end;

function TcxGridLevel.GetParentComponent: TComponent;
begin
  if IsTop then
    Result := FControl
  else
    Result := FParent;
end;

function TcxGridLevel.HasParent: Boolean;
begin
  Result := True;
end;

function TcxGridLevel.Add: TcxGridLevel;
begin
  Result := GetItemClass.Create(FControl.Owner);
  AddItem(Result);
end;

function TcxGridLevel.GetAvailableItem: TcxGridLevel;
begin
  if (VisibleCount = 0) or Control.IsDestroying then
    Result := nil
  else
    Result := VisibleItems[0];
end;

function TcxGridLevel.HasAsChild(ALevel: TcxGridLevel): Boolean;
var
  I: Integer;
begin
  for I := 0 to Count - 1 do
  begin
    Result := (Items[I] = ALevel) or Items[I].HasAsChild(ALevel);
    if Result then Exit;
  end;
  Result := False;
end;

function TcxGridLevel.MakeVisible: TcxCustomGridView;
var
  AParentView: TcxCustomGridView;
begin
  if IsTop then
  begin
    Active := True;
    if Active then
      Result := GridView
    else
      Result := nil;
  end
  else
  begin
    AParentView := Parent.MakeVisible;
    if AParentView = nil then
      Result := nil
    else
      Result := AParentView.ViewData.MakeDetailVisible(Self);
  end;
end;

procedure TcxGridLevel.CheckHeight(var Value: Integer);
begin
  if Value < 0 then Value := 0;
  if (FMaxDetailHeight <> 0) and (Value > FMaxDetailHeight) then
    Value := FMaxDetailHeight;
end;

function TcxGridLevel.GetDisplayCaption: string;
begin
  Result := Caption;
  if cxGridTabAccelSupport then
    Result := StripHotkey(Result);
end;

function TcxGridLevel.CreateLinkObject(ARelation: TcxCustomDataRelation;
  ARecordIndex: Integer): TcxCustomGridView;
var
  AGridView: TcxCustomGridView;

  function GetMasterRecord: TcxCustomGridRecord;
  var
    AGridView: TcxCustomGridTableView;
  begin
    AGridView := (ARelation.DataController as IcxCustomGridDataController).GridView as TcxCustomGridTableView;
    Result := AGridView.ViewData.GetRecordByRecordIndex(ARecordIndex);
  end;

  function GetRelationsGridView: TcxCustomGridView;
  begin
    Result := FGridView;
    if Result = nil then
      Result := AGridView;
  end;

begin
  AGridView := FGridView;
  if Assigned(FOnGetGridView) then FOnGetGridView(Self, GetMasterRecord, AGridView);
  if (AGridView = nil) or not Visible then
    Result := nil
  else
  begin
    Result := TcxCustomGridViewClass(AGridView.ClassType).CreateCloned(FControl);
    Result.BeginUpdate;
    try
      TcxCustomGridViewAccess(Result).AssignPattern(AGridView);
      Result.DataController.Relations.BeginUpdate;
      try
        Result.DataController.Relations.Assign(GetRelationsGridView.DataController.Relations);
        Result.DataController.SetMasterRelation(ARelation, ARecordIndex);
      finally
        Result.DataController.Relations.EndUpdate;
      end;
      TcxCustomGridViewAccess(Result).SetLevel(Self);
    finally
      Result.CancelUpdate;
    end;
    TcxCustomGridViewAccess(AGridView).AddClone(Result);
  end;
end;

{ TcxGridLevelAccess }

class procedure TcxGridLevelAccess.CheckHeight(AInstance: TcxGridLevel;
  var Value: Integer);
begin
  AInstance.CheckHeight(Value);
end;

class function TcxGridLevelAccess.CreateLinkObject(AInstance: TcxGridLevel;
  ARelation: TcxCustomDataRelation; ARecordIndex: Integer): TcxCustomGridView;
begin
  Result := AInstance.CreateLinkObject(ARelation, ARecordIndex);
end;

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?