⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wscontrols.pas

📁 Workflow Studio是一款专为商业进程管理(BPM)设计的Delphi VCL框架。通过Workflow Studio你可以轻易地将工作流与BPM功能添加到你的应用程序里。这样能使你或你的最
💻 PAS
📖 第 1 页 / 共 3 页
字号:
constructor TTaskFieldPanel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FFields := TTaskFields.Create(nil, TTaskField);
  FEditors := TTaskFieldEditors.Create(TTaskFieldEditor);
  FHorzMargin := 5;
  FVertMargin := 5;
  FHorzInterSpace := 5;
  FVertInterSpace := 5;
end;

destructor TTaskFieldPanel.Destroy;
begin
  FEditors.Free;
  FFields.Free;
  inherited;
end;

procedure TTaskFieldPanel.SetFields(const Value: TTaskFields);
begin
  FFields.Assign(Value);
  UpdateVarControls;
end;

procedure TTaskFieldPanel.UpdateVarControls;
var
  c: integer;
begin
  inc(FUpdatingVars);
  try
    DestroyControls;
    for c := 0 to FFields.Count - 1 do
    begin
      CreateControl(FFields[c]);
    end;
    ArrangeControls;
  finally
    dec(FUpdatingVars);
    FModified := false;
  end;
end;

procedure TTaskFieldPanel.ArrangeControls;
begin
  ArrangeLabelsVertical;
end;

procedure TTaskFieldPanel.DestroyControls;
begin
  FEditors.Clear;
end;

procedure TTaskFieldPanel.CreateControl(AField: TTaskField);
var
  NewEditor: TTaskFieldEditor;
begin
  NewEditor := FEditors.Add;
  With NewEditor do
  begin
    FField := AField;

    FLabel := TLabel.Create(Self);
    FEditor := TTaskFieldEdit.Create(Self);
    FEditor.FControl := FEditor.CreateControl(Self);
    FLabel.Parent := Self;
    FEditor.FControl.Parent := Self;
    FLabel.FocusControl := FEditor.Control;
    FLabel.Caption := AField.Caption;
    if AField.Required then
      FLabel.Caption := FLabel.Caption + ' (*)';
    FEditor.SetReadOnly(FField.ReadOnly or Self.ReadOnly);
    FEditor.SetValue(Null);
  end;
end;

procedure TTaskFieldPanel.ArrangeLabelsVertical;
Var
  MaxLabelWidth, MaxControlWidth: integer;
  c, x, y: integer;
  CurLab: TLabel;
  CurCtrl: TWinControl;

  function GetLabelTop(Lab: TLabel; OffY: integer): integer;
  begin
    if Assigned(Lab.FocusControl) and (Lab.FocusControl.Height > Lab.Height) then
      result := Offy + (Lab.FocusControl.Height div 2) - (Lab.Height div 2)
    else
      result := OffY;
  end;

  function GetControlTop(Lab: TLabel;OffY: integer): integer;
  begin
    if Assigned(Lab.FocusControl) and (Lab.FocusControl.Height < Lab.Height) then
      result := Offy + (Lab.Height div 2) - (Lab.FocusControl.Height div 2)
    else
      result := OffY;
  end;

  function GetLineHeight(Lab: TLabel): integer;
  begin
    result := Lab.Height;
    if Assigned(Lab.FocusControl) and (Lab.FocusControl.Height > result) then
      result := Lab.FocusControl.Height;
  end;

begin
  MaxLabelWidth := 0;
  MaxControlWidth := 0;

  {Get max width of labels and controls, and set tab order}
  for c := 0 to FEditors.Count - 1 do With FEditors[c] do
  begin
     if Assigned(FEditor.FControl) then FEditor.FControl.TabOrder := c;
     if FLabel.Canvas.TextWidth(FLabel.Caption) > MaxLabelWidth then MaxLabelWidth := FLabel.Canvas.TextWidth(FLabel.Caption);
     if FLabel.Width > MaxControlWidth then MaxControlWidth := FLabel.Width;
  end;

  x := MaxLabelWidth + FHorzMargin + FHorzInterSpace;
  y := FVertMargin;
  for c := 0 to FEditors.Count - 1 do
  begin
     CurLab := FEditors[c].FLabel;
     CurLab.Top := GetLabelTop(CurLab, y);
     Case CurLab.Alignment of
       taLeftJustify:  CurLab.Left := FHorzMargin;
       taRightJustify: CurLab.Left := x - FHorzInterSpace - CurLab.Width;
       taCenter: CurLab.Left := FHorzMargin + ((x - FHorzMargin - FHorzInterspace) div 2) - (CurLab.Width div 2);
     end;
     CurCtrl := FEditors[c].FEditor.FControl;
     CurCtrl.Top := GetControlTop(CurLab, y);
     CurCtrl.Left := x;
     CurCtrl.Width := CurLab.Width;
     if CurCtrl.Left + Width> Self.Width - FHorzMargin then
       CurCtrl.Width := Self.ClientWidth - FHorzMargin - CurCtrl.Left;
     y := y + GetLineHeight(CurLab) + FVertInterSpace;
  end;
end;

procedure TTaskFieldPanel.Resize;
begin
  inherited;
  if not (csDestroying in ComponentState) then
    ArrangeControls;
end;

procedure TTaskFieldPanel.LoadDiagramVariables(
  ADiagram: TWorkflowDiagram);
var
  c: integer;
  WorkflowVar: TWorkflowVariable;
begin
  for c := 0 to FEditors.Count - 1 do
    if FEditors[c].FField <> nil then
    begin
      WorkflowVar := ADiagram.Variables.FindByName(FEditors[c].FField.WorkflowVarName);
      if WorkflowVar <> nil then
        FEditors[c].FEditor.SetValue(WorkflowVar.Value);
    end;
end;

procedure TTaskFieldPanel.SaveDiagramVariables(
  ADiagram: TWorkflowDiagram);
var
  c: integer;
  WorkflowVar: TWorkflowVariable;
begin
  for c := 0 to FEditors.Count - 1 do
    if FEditors[c].FEditor.FModified and (FEditors[c].FField <> nil) then
    begin
      WorkflowVar := ADiagram.Variables.FindByName(FEditors[c].FField.WorkflowVarName);
      if WorkflowVar <> nil then
        WorkflowVar.Value := FEditors[c].FEditor.GetValue;
    end;
end;

function TTaskFieldPanel.ValidateValues: boolean;
var
  c: integer;
begin
  result := true;
  for c := 0 to FEditors.Count - 1 do
    if FEditors[c].FField <> nil then
    begin
      {check required}
      if FEditors[c].FField.Required and FEditors[c].FEditor.IsEmpty then
      begin
        FErrorInfo.Editor := FEditors[c];
        FErrorInfo.Msg := Format(_str(SWarningFieldValueRequired), [FEditors[c].FField.Caption]);
        result := false;
        exit;
      end;
    end;
end;

procedure TTaskFieldPanel.FocusLastError;
begin
  if (ErrorInfo.Editor <> nil) and (ErrorInfo.Editor.FEditor <> nil) and
    (ErrorInfo.Editor.FEditor.FControl <> nil) and ErrorInfo.Editor.FEditor.FControl.CanFocus then
    ErrorInfo.Editor.FEditor.FControl.SetFocus;
end;

procedure TTaskFieldPanel.SetReadOnly(const Value: boolean);
var
  c: integer;
begin
  if FReadOnly <> Value then
  begin
    FReadOnly := Value;
    {Update readonly}
    for c := 0 to FEditors.Count - 1 do
      FEditors[c].FEditor.SetReadOnly(FReadOnly or FEditors[c].FField.ReadOnly);
  end;
end;

{ TTaskFieldEditors }

function TTaskFieldEditors.Add: TTaskFieldEditor;
begin
  result := TTaskFieldEditor(inherited Add);
end;

function TTaskFieldEditors.GetItem(Index: integer): TTaskFieldEditor;
begin
  result := TTaskFieldEditor(inherited Items[Index]);
end;

{ TTaskFieldCustomControl }

procedure TTaskFieldCustomControl.Change;
begin
  if (FPanel <> nil) and (FPanel.FUpdatingVars = 0) then
  begin
    FModified := true;
  end;
end;

constructor TTaskFieldCustomControl.Create(AOwner: TTaskFieldPanel);
begin
  FPanel := AOwner;
end;

destructor TTaskFieldCustomControl.Destroy;
begin
  if FControl <> nil then
  begin
    FControl.Free;
    FControl := nil;
  end;
  inherited;
end;

procedure TTaskFieldCustomControl.EditorChange(Sender: TObject);
begin
  Change;
end;

procedure TTaskFieldCustomControl.SetReadOnly(AValue: boolean);
begin
  if FControl <> nil then
    FControl.Enabled := AValue;
end;

{ TTaskFieldEdit }

function TTaskFieldEdit.CreateControl(AOwner: TComponent): TWinControl;
var
  AEdit: TEdit;
begin
  AEdit := TEdit.Create(AOwner);
  AEdit.OnChange := EditorChange;
  result := AEdit;
end;

function TTaskFieldEdit.EditControl: TEdit;
begin
  result := TEdit(Control);
end;

function TTaskFieldEdit.GetValue: Variant;
begin
  result := EditControl.Text;
end;

function TTaskFieldEdit.IsEmpty: boolean;
begin
  result := Trim(EditControl.Text) = '';
end;

procedure TTaskFieldEdit.SetReadOnly(AValue: boolean);
begin
  EditControl.ReadOnly := AValue;
  EditControl.Enabled := not AValue;
end;

procedure TTaskFieldEdit.SetValue(AValue: Variant);
begin
  EditControl.Text := VarToStr(AValue);
end;

{ TTaskFieldEditor }

destructor TTaskFieldEditor.Destroy;
begin
  if FLabel <> nil then
  begin
    FLabel.Free;
    FLabel := nil;
  end;

  if FEditor <> nil then
  begin
    FEditor.Free;
    FEditor := nil;
  end;

  inherited;
end;

{ TTaskLogListView }

constructor TTaskLogListView.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FLogItems := TTaskLogItems.Create(nil, TTaskLogItem);
  ViewStyle := vsReport;
  HideSelection := false;
  RowSelect := true;
  UpdateColumns;
end;

destructor TTaskLogListView.Destroy;
begin
  FLogItems.Clear;
  FLogItems.Free;
  inherited;
end;

procedure TTaskLogListView.Loaded;
begin
  inherited;
  UpdateColumns;
end;

procedure TTaskLogListView.LoadFromDatabase;
{var
  AKey: string;}
begin
  {AKey := 'xxx';
  if SelectedTask <> nil then
    AKey := SelectedTask.Key;}
  FLogItems.Clear;
  WorkflowStudio.WorkflowDB.TaskLogLoadList(FTaskInsKey, FLogItems);
  UpdateItems;
  //SelectItemByKey(AKey);
end;

procedure TTaskLogListView.UpdateColumns;
begin
  Columns.Clear;
  With Columns.Add do
  begin
    Caption := _str(SLogColOperation);
    Width := 100;
  end;
  With Columns.Add do
  begin
    Caption := _str(SLogColDate);
    Width := 90;
  end;
  With Columns.Add do
  begin
    Caption := _str(SLogColTime);
    Width := 70;
  end;
  With Columns.Add do
  begin        
    Caption := _str(SLogColUserId);
    Width := 100;
  end;
  With Columns.Add do
  begin
    Caption := _str(SLogColInfo);
    Width := 200;
  end;
end;

procedure TTaskLogListView.UpdateItems;
var
  c: integer;
begin
  Items.BeginUpdate;
  try
    {Save the old selected log item}
    //ATask := SelectedTask;

    {clear and recreate the items}
    Items.Clear;
    for c := 0 to FLogItems.Count - 1 do
      UpdateLogItem(Items.Add, FLogItems[c]);

    {recover the previously selected log item, if it's the same}
    //SelectItemByTask(ATask);
  finally
    Items.EndUpdate;
  end;
end;

function TTaskLogListView.LogOperationText(AOperation: TTaskLogOperation): string;
begin
  Case AOperation of
    tlCreate:
      result := _str(STaskLogCreate);
    tlStatusChange:
      result := _str(STaskLogStatusChange);
    tlUpdate:
      result := _str(STaskLogUpdate);
  else
    result := '';
  end;
end;

procedure TTaskLogListView.UpdateLogItem(AItem: TListItem;
  ALogItem: TTaskLogItem);
var
  AUser: TWorkflowUser;
  AInfo: string;
begin
  AItem.Caption := LogOperationText(ALogItem.Operation);
  AItem.SubItems.Add(DateToStr(ALogItem.EventDate));
  AItem.SubItems.Add(TimeToStr(ALogItem.EventDate));

  AUser := WorkflowStudio.UserManager.Users.FindById(ALogItem.UserID);
  if AUser <> nil then
    AItem.SubItems.Add(AUser.UserName)
  else
    AItem.SubItems.Add(ALogItem.UserId);

  Case ALogItem.Operation of
    tlStatusChange:
      AInfo := Format(_str(SStatusChangeInfo), [ALogItem.Info, ALogItem.Info2]);
  else
    AInfo := '';
  end;
  AItem.SubItems.Add(AInfo);


  {$WARNINGS OFF}
  AItem.Data := ALogItem;
  {$WARNINGS ON}
end;

end.

⌨️ 快捷键说明

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