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

📄 fattachmenteditor.pas

📁 Workflow Studio是一款专为商业进程管理(BPM)设计的Delphi VCL框架。通过Workflow Studio你可以轻易地将工作流与BPM功能添加到你的应用程序里。这样能使你或你的最
💻 PAS
字号:
unit fAttachmentEditor;

{$I wsdefs.inc}

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  {$IFDEF DELPHI6_LVL}
  Variants,
  {$ENDIF}
  ComCtrls, wsClasses, StdCtrls, wsControls, fAttachmentFrame,
  Menus;

type
  TfmAttachmentEditor = class(TForm)
    PageControl1: TPageControl;
    tsAttachments: TTabSheet;
    lvAttachs: TListView;
    btAdd: TButton;
    btDelete: TButton;
    btCancel: TButton;
    btOk: TButton;
    frItems: TfrAttachmentFrame;
    procedure btOkClick(Sender: TObject);
    procedure btCancelClick(Sender: TObject);
    procedure btAddClick(Sender: TObject);
    procedure lvAttachsSelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    procedure btDeleteClick(Sender: TObject);
    procedure lvAttachsEdited(Sender: TObject; Item: TListItem;
      var S: String);
  private
    { Private declarations }
    FLastAttachs: TAttachmentItems;
    FAttachs: TWorkflowAttachments;
    FUpdatingControls: integer;
    procedure ClearEditors;
    procedure BeginUpdateControls;
    procedure EndUpdateControls;
    procedure SetControlsEnabled(AEnabled: boolean);
    procedure AttachsToInterface;
    procedure DeleteCurrentAttach;
    procedure LoadAttachInEditors(AAttach: TWorkflowAttachment);
    procedure UpdateListItem(AItem: TListItem;
      AAttach: TWorkflowAttachment);
    procedure SaveAttachs;
    procedure Localize;
    function AttachmentFromItem(AItem: TListItem): TWorkflowAttachment;
  protected
    procedure Loaded; override;
  public
    { Public declarations }
    function EditAttachments(AAttachs: TWorkflowAttachments): boolean;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

implementation
uses wsMain, wsRes;

{$R *.DFM}

{ TForm1 }

function TfmAttachmentEditor.EditAttachments(AAttachs: TWorkflowAttachments): boolean;
begin
  FAttachs.Assign(AAttachs);
  AttachsToInterface;
  result := ShowModal = mrOk;
  if result then
  begin
    SaveAttachs;
    AAttachs.Assign(FAttachs);
  end;
end;

procedure TfmAttachmentEditor.btOkClick(Sender: TObject);
begin
  ModalResult := mrOk;
end;

procedure TfmAttachmentEditor.btCancelClick(Sender: TObject);
begin
  ModalResult := mrCancel;
end;

procedure TfmAttachmentEditor.AttachsToInterface;
var
  c: integer;
begin
  lvAttachs.Items.Clear;
  for c := 0 to FAttachs.Count - 1 do
    UpdateListItem(lvAttachs.Items.Add, FAttachs[c]); 
  ClearEditors;
  if lvAttachs.Items.Count > 0 then
    lvAttachs.Selected := lvAttachs.Items[0];
end;

procedure TfmAttachmentEditor.UpdateListItem(AItem: TListItem; AAttach: TWorkflowAttachment);
begin
  AItem.Caption := AAttach.Name;
  {$WARNINGS OFF}
  AItem.Data := AAttach;
  {$WARNINGS ON}
end;

procedure TfmAttachmentEditor.btAddClick(Sender: TObject);
var
  NewAttach: TWorkflowAttachment;
  NewName: string;
  i: integer;
  NewItem: TListItem;
begin
  {Add attachment in collection}
  NewAttach := FAttachs.Add;

  {Find a new unique attach name}
  i := 0;
  repeat
    inc(i);
    NewName := Format('%s%d', [_str(SAttachment), i]);
  until (FAttachs.FindByName(NewName) = nil);
  NewAttach.Name := NewName;

  {Create item in list view}
  NewItem := lvAttachs.Items.Add;
  UpdateListItem(NewItem, NewAttach);

  {select and start renaming the item. EditCaption already selects the item}
  NewItem.EditCaption;
end;

procedure TfmAttachmentEditor.lvAttachsSelectItem(Sender: TObject; Item: TListItem;
  Selected: Boolean);
begin
  if (FAttachs <> nil) and not (csDestroying in ComponentState) then
  begin
    SaveAttachs;
    if (lvAttachs.SelCount = 1) then
      LoadAttachInEditors(AttachmentFromItem(lvAttachs.Selected))
    else
      ClearEditors;
  end;
end;

function TfmAttachmentEditor.AttachmentFromItem(AItem: TListItem): TWorkflowAttachment;
begin
  {$WARNINGS OFF}
  result := TWorkflowAttachment(AItem.Data);
  {$WARNINGS ON}
end;

procedure TfmAttachmentEditor.SaveAttachs;
begin
  if FLastAttachs <> nil then
  begin
    FLastAttachs.Assign(frItems.lvItems.Attachments);
    FLastAttachs := nil;
  end
end;

procedure TfmAttachmentEditor.LoadAttachInEditors(AAttach: TWorkflowAttachment);
begin
  if AAttach <> nil then
  begin
    BeginUpdateControls;
    try
      SaveAttachs;

      {set new attachment items}
      frItems.lvItems.Attachments := AAttach.Items;
      FLastAttachs := AAttach.Items;
      SetControlsEnabled(true);
    finally
      EndUpdateControls;
    end;
  end;
end;

procedure TfmAttachmentEditor.ClearEditors;
begin
  BeginUpdateControls;
  try
    frItems.lvItems.Attachments.Clear;
    SetControlsEnabled(false);
  finally
    EndUpdateControls;
  end;
end;

procedure TfmAttachmentEditor.btDeleteClick(Sender: TObject);
begin
  if MessageDlg(_str(SConfirmDeleteItems),
     mtConfirmation,[mbYes,mbNo],0) = mrYes then
  begin
     while lvAttachs.SelCount > 0 do
        DeleteCurrentAttach;
     if lvAttachs.Items.Count > 0 then
        lvAttachs.Selected := lvAttachs.Items[lvAttachs.items.Count - 1]
     else
        ClearEditors;
     lvAttachs.SetFocus;
  end;
end;

procedure TfmAttachmentEditor.DeleteCurrentAttach;
begin
  if Assigned(lvAttachs.Selected) then
  begin
    FLastAttachs := nil;
    AttachmentFromItem(lvAttachs.Selected).Free;
    {$WARNINGS OFF}
    lvAttachs.Selected.Data := nil;
    {$WARNINGS ON}
    lvAttachs.Selected.Free; 
  end;
end;

constructor TfmAttachmentEditor.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FLastAttachs := nil;
  FAttachs := TWorkflowAttachments.Create(nil);
  frItems.Permissions := AllAttachmentPermissions;
  //lvItems.ViewStyle := vsList;
end;

destructor TfmAttachmentEditor.Destroy;
begin
  FAttachs.Free;
  FAttachs := nil;
  inherited;
end;

procedure TfmAttachmentEditor.BeginUpdateControls;
begin
  inc(FUpdatingControls);
end;

procedure TfmAttachmentEditor.EndUpdateControls;
begin
  dec(FUpdatingControls);
end;

procedure TfmAttachmentEditor.lvAttachsEdited(Sender: TObject; Item: TListItem;
  var S: String);
var
  AAttach: TWorkflowAttachment;
begin
  AAttach := AttachmentFromItem(Item);
  if AAttach <> nil then
  begin
    if Trim(S) = '' then
      S := AAttach.Name
    else
      AAttach.Name := S;
  end;
end;

procedure TfmAttachmentEditor.SetControlsEnabled(AEnabled: boolean);
begin
  frItems.lvItems.Enabled := AEnabled;
end;

procedure TfmAttachmentEditor.Localize;
begin
  Self.Caption := _str('fmAttachmentEditor.Self.Caption');
  tsAttachments.Caption := _str('fmAttachmentEditor.tsAttachments.Caption');
  btAdd.Caption := _str('fmAttachmentEditor.btAdd.Caption');
  btDelete.Caption := _str('fmAttachmentEditor.btDelete.Caption');
  btCancel.Caption := _str('fmAttachmentEditor.btCancel.Caption');
  btOk.Caption := _str('fmAttachmentEditor.btOk.Caption');
end;

procedure TfmAttachmentEditor.Loaded;
begin
  inherited;
  with lvAttachs.Columns.Add do
  begin
    AutoSize := true;
    Caption := _str(SAttachment);
  end;
  Localize;
end;

end.

⌨️ 快捷键说明

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