unit tpPegPartSettingFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ZJGrid, ExtCtrls;

type
  TtpPegPartSettingForm = class(TForm)
    leCount: TLabeledEdit;
    zgTenders: TZJGrid;
    lblHint: TLabel;
    btnOk: TButton;
    btnCancel: TButton;
    procedure leCountKeyPress(Sender: TObject; var Key: Char);
    procedure leCountChange(Sender: TObject);
    procedure zgTendersCellCanEdit(Sender: TObject; const ACoord: TPoint;
      var Allow: Boolean);
    procedure zgTendersCellTextChanged(Sender: TObject; Col, Row: Integer);
    procedure btnOkClick(Sender: TObject);
  private
    FBeginPeg: string;
    FEndPeg: string;

    function CheckEmptyPeg: Boolean;

    procedure Init(ACount: Integer);
    procedure LoadHistoryData(APartPegs: TStringList);
  public
    constructor Create(APartPegs: TStringList);
    destructor Destroy; override;

    procedure AssignResult(APegs: TStringList);
  end;

function PegPartSetting(APartPegs: TStringList): Boolean;

implementation

uses
  mPegFilter, UtilMethods;

function PegPartSetting(APartPegs: TStringList): Boolean;
var
  SetForm: TtpPegPartSettingForm;
begin
  SetForm := TtpPegPartSettingForm.Create(APartPegs);
  try
    Result := SetForm.ShowModal = mrOk;
    if Result then
      SetForm.AssignResult(APartPegs);
  finally
    SetForm.Free;
  end;
end;

{$R *.dfm}

procedure TtpPegPartSettingForm.leCountKeyPress(Sender: TObject;
  var Key: Char);
begin
  if not (Key in ['0'..'9', #8, #13]) then
    Key := #0;
end;

procedure TtpPegPartSettingForm.leCountChange(Sender: TObject);
var
  iCount: Integer;
begin
  iCount := StrToIntDef(leCount.Text, 0);
  if iCount <= 30 then
  begin
    if iCount = 1 then
      lblHint.Caption := '仅划分为1个标段'
    else
      lblHint.Caption := Format('请输入%s~%s之间的前%d个标段的终点桩号：', [FBeginPeg, FEndPeg, iCount-1]);
    Init(iCount);
  end
  else
    WarningMessage('最多仅支持划分为30个标段。');
end;

procedure TtpPegPartSettingForm.zgTendersCellCanEdit(Sender: TObject;
  const ACoord: TPoint; var Allow: Boolean);
begin
  Allow := (ACoord.X = 2) and (ACoord.Y > 0) and (ACoord.Y < zgTenders.RowCount-1);
end;

constructor TtpPegPartSettingForm.Create(APartPegs: TStringList);
begin
  inherited Create(nil);
  LoadHistoryData(APartPegs);
end;

destructor TtpPegPartSettingForm.Destroy;
begin
  inherited;
end;

procedure TtpPegPartSettingForm.Init(ACount: Integer);
var
  iRow: Integer;
begin
  zgTenders.RowCount := ACount+1;
  zgTenders.Cells[1, 0].Text := '起点桩号';
  zgTenders.Cells[2, 0].Text := '终点桩号';
  zgTenders.Cells[1, 1].Text := FBeginPeg;
  for iRow := 1 to zgTenders.RowCount - 1 do
  begin
    zgTenders.Cells[1, iRow].Color := clSilver;
    zgTenders.Cells[1, iRow].Align := gaCenterLeft;
    zgTenders.Cells[2, iRow].Color := clWindow;
    zgTenders.Cells[2, iRow].Align := gaCenterLeft;
  end;
  zgTenders.Cells[2, zgTenders.RowCount-1].Text := FEndPeg;
  zgTenders.Cells[2, zgTenders.RowCount-1].Color := clSilver;
end;

procedure TtpPegPartSettingForm.LoadHistoryData(APartPegs: TStringList);
var
  iPeg: Integer;
begin
  PegFilter.Filter(APartPegs.Strings[0]);
  FBeginPeg := PegFilter.BeginPeg;
  if APartPegs.Count > 1 then
  begin
    PegFilter.Filter(APartPegs.Strings[APartPegs.Count-1]);
    FEndPeg := PegFilter.EndPeg;
  end
  else
    FEndPeg := PegFilter.EndPeg;

  leCount.Text := IntToStr(APartPegs.Count);

  if APartPegs.Count = 1 then Exit;
  for iPeg := 1 to APartPegs.Count - 1 do
  begin
    PegFilter.Filter(APartPegs.Strings[iPeg]);
    zgTenders.Cells[2, iPeg].Text := PegFilter.BeginPeg;
  end;
end;

procedure TtpPegPartSettingForm.zgTendersCellTextChanged(Sender: TObject;
  Col, Row: Integer);
begin
  if (Col = 2) and (Row > 0) and (Row < zgTenders.RowCount-1) then
    zgTenders.Cells[Col-1, Row+1].Text := zgTenders.Cells[Col, Row].Text;
end;

procedure TtpPegPartSettingForm.AssignResult(APegs: TStringList);
var
  iRow: Integer;
  sPeg: string;
begin
  APegs.Clear;
  for iRow := 1 to zgTenders.RowCount - 1 do
  begin
    sPeg := zgTenders.Cells[1, iRow].Text + '~' + zgTenders.Cells[2, iRow].Text;
    APegs.Add(sPeg);
  end;
end;

procedure TtpPegPartSettingForm.btnOkClick(Sender: TObject);
begin
  if CheckEmptyPeg then
    WarningMessage('请填写需要的桩号信息。')
  else
    ModalResult := mrOk;
end;

function TtpPegPartSettingForm.CheckEmptyPeg: Boolean;
var
  iRow: Integer;
begin
  Result := False;
  for iRow := 1 to zgTenders.RowCount - 1 do
  begin
    Result := (zgTenders.Cells[1, iRow].Text = '') or
              (zgTenders.Cells[2, iRow].Text = '');
    if Result then Break;
  end;
end;

end.
