cxgriduihelper.pas

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

PAS
749
字号
{********************************************************************}
{                                                                    }
{       Developer Express Visual Component Library                   }
{       ExpressQuantumGrid Utils                                     }
{                                                                    }
{       Copyright (c) 1998-2008 Developer Express Inc.               }
{       ALL RIGHTS RESERVED                                          }
{                                                                    }
{   The entire contents of this file is protected by U.S. and        }
{   International Copyright Laws. Unauthorized reproduction,         }
{   reverse-engineering, and distribution of all or any portion of   }
{   the code contained in this file is strictly prohibited and may   }
{   result in severe civil and criminal penalties and will be        }
{   prosecuted to the maximum extent possible under the law.         }
{                                                                    }
{   RESTRICTIONS                                                     }
{                                                                    }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES            }
{   (DCU, OBJ, DLL, ETC.) ARE CONFIDENTIAL AND PROPRIETARY TRADE     }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS    }
{   LICENSED TO DISTRIBUTE THE EXPRESSQUANTUMGRID AND ALL            }
{   ACCOMPANYING VCL CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY. }
{                                                                    }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED       }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE         }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE        }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT   }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                       }
{                                                                    }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON        }
{   ADDITIONAL RESTRICTIONS.                                         }
{                                                                    }
{********************************************************************}

unit cxGridUIHelper;

{$I cxVer.inc}

interface

uses
  Classes, SysUtils, cxGrid, cxGridCustomView;

const
  GROP_FIRST  = 0;
  GROP_LAST   = 1;
  GROP_PREV   = 2;
  GROP_NEXT   = 3;
  GROP_INSERT = 4;
  GROP_DELETE = 5;
  GROP_COPYTOCLIPBOARD = 6;

  GROP_SHOWCOLUMNCUSTOMIZING = 100;
  GROP_SHOWGROUPINGPANEL = 101;
  GROP_SHOWINDICATOR = 102;
  GROP_INVERTSELECT = 103;
  GROP_SHOWHEADERS = 104;
  GROP_SHOWSUMMARYFOOTER = 105;
  GROP_SHOWBANDS = 106;
  GROP_SHOWGRID = 107;
  GROP_COLUMNAUTOWIDTH = 108;
  GROP_SHOWPREVIEW = 109;
  GROP_SHOWEDITBUTTONS = 110;
  GROP_LAYOUTDIRECTION = 111;

type
  TcxCustomGridOperationHelperParameters = class
  private
    FView: TcxCustomGridView;
    FIsPerform: Boolean;
  public
    constructor Create(AView: TcxCustomGridView; AIsPerform: Boolean = True); virtual;
    property View: TcxCustomGridView read FView;
    property IsPerform: Boolean read FIsPerform;
  end;

  TcxShowingGridOperationHelperParameters = class(TcxCustomGridOperationHelperParameters)
  private
    FShowing: Boolean;
  public
    property Showing: Boolean read FShowing write FShowing;
  end;

  TcxGridOperationHelperProc = procedure(const AParameter: TcxCustomGridOperationHelperParameters) of object;

  TcxCustomGridViewOperationHelper = class
  private
    FList: TList;
  protected
    procedure RegisterOperation(AOperationIndex: Integer; const AProc: TcxGridOperationHelperProc);
    procedure RegisterOperations; virtual;
    function GetShowProperty(const AParameter: TcxCustomGridOperationHelperParameters): Boolean;
    procedure SetShowProperty(const AParameter: TcxCustomGridOperationHelperParameters; AShow: Boolean);

    procedure DoFirst(const AParameter: TcxCustomGridOperationHelperParameters);
    procedure DoLast(const AParameter: TcxCustomGridOperationHelperParameters);
    procedure DoNext(const AParameter: TcxCustomGridOperationHelperParameters);
    procedure DoPrev(const AParameter: TcxCustomGridOperationHelperParameters);
    procedure DoInsert(const AParameter: TcxCustomGridOperationHelperParameters);
    procedure DoDelete(const AParameter: TcxCustomGridOperationHelperParameters);
  public
    constructor Create; virtual;
    destructor Destroy; override;

    class function GetViewClass: TcxCustomGridViewClass; virtual;
    function IsOperationAccessible(AOperationIndex: Integer): Boolean; virtual;
    function IsOperationEnabled(AView: TcxCustomGridView; AOperationIndex: Integer): Boolean; virtual;
    procedure PerformOperation(AOperationIndex: Integer; const AParameter: TcxCustomGridOperationHelperParameters); virtual;
  end;

  TcxCustomGridViewOperationHelperClass = class of TcxCustomGridViewOperationHelper;

  TcxCustomGridOperationHelper = class(TComponent)
  private
    FFocusedViewChangedNotification: TcxCustomGridNotification;
    FGrid: TcxGrid;
    FOnUpdateOperations: TNotifyEvent;
    FOnFocusedRecordChanged: TNotifyEvent;
    FOnCustomizationFormVisibleChanged: TNotifyEvent;

    function GetIsOperationAccessible(AOperationIndex: Integer): Boolean;
    function GetIsOperationEnabled(AOperationIndex: Integer): Boolean;
    function GetIsOperationShowing(AOperationIndex: Integer): Boolean;
    procedure SetGrid(const Value: TcxGrid);

    //TODO Sender should be TObject??
    procedure DoFocusedViewChanged;
  protected
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
    procedure DoUpdateOperations; dynamic;
    procedure DoFocusedRecordChanged; dynamic;
    procedure DoCustomizationFormVisibleChanged; dynamic;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    function FocusedView: TcxCustomGridView;
    function FocusedViewClass: TcxCustomGridViewClass;

    procedure PerformOperation(AOperationIndex: Integer; const AParameters: TcxCustomGridOperationHelperParameters);
    procedure PerformStarndardOperation(AOperationIndex: Integer);
    procedure PerformShowingOperation(AOperationIndex: Integer; AShow: Boolean);

    property IsOperationAccessible[AOperationIndex: Integer]: Boolean read GetIsOperationAccessible;
    property IsOperationEnabled[AOperationIndex: Integer]: Boolean read GetIsOperationEnabled;
    property IsOperationShowing[AOperationIndex: Integer]: Boolean read GetIsOperationShowing;
  public
    procedure DoFirst;
    procedure DoLast;
    procedure DoNext;
    procedure DoPrev;
    procedure DoInsert;
    procedure DoDelete;
    procedure CopyToClipboard;

    procedure DoShowColumnCustomizing(AShow: Boolean);
    procedure DoShowGroupingPanel(AShow: Boolean);
    procedure DoShowHeaders(AShow: Boolean);
    procedure DoShowBands(AShow: Boolean);
    procedure DoShowSummaryFooter(AShow: Boolean);
    procedure DoShowGrid(AShow: Boolean);
    procedure DoColumnAutoWidth(AShow: Boolean);
    procedure DoShowPreview(AShow: Boolean);
    procedure DoShowEditButtons(AShow: Boolean);
    procedure DoLayoutDirection(AShow: Boolean);
    procedure DoInvertSelect(AShow: Boolean);
    procedure DoShowIndicator(AShow: Boolean);

    function IsColumnsCustomizingShowing: Boolean;
    function IsGroupingPanelShowing: Boolean;
    function IsHeadersShowing: Boolean;
    function IsBandsShowing: Boolean;
    function IsSummaryFooterShowing: Boolean;
    function IsGridShowing: Boolean;
    function IsColumnAutoWidth: Boolean;
    function IsShowPreview: Boolean;
    function IsShowEditButtons: Boolean;
    function IsVertLayoutDirection: Boolean;
    function IsInvertSelect: Boolean;
    function IsShowIndicator: Boolean;

    property Grid: TcxGrid read FGrid write SetGrid;
    property OnUpdateOperations: TNotifyEvent read FOnUpdateOperations write FOnUpdateOperations;
    property onFocusedRecordChanged: TNotifyEvent read FOnFocusedRecordChanged write FOnFocusedRecordChanged;
    property OnCustomizationFormVisibleChanged: TNotifyEvent read FOnCustomizationFormVisibleChanged
                write FOnCustomizationFormVisibleChanged;
  end;

  TcxGridOperationHelper = class(TcxCustomGridOperationHelper)
  published
    property Grid;
    property OnUpdateOperations;
  end;

  procedure RegisterGridViewOperationHelper(AGridViewOperationHelperClass: TcxCustomGridViewOperationHelperClass);
  procedure UnregisterGridViewOperationHelper(AGridViewOperationHelperClass: TcxCustomGridViewOperationHelperClass);
  function GetGridViewOperationHelperByGridViewClass(const AGridViewClass: TcxCustomGridViewClass): TcxCustomGridViewOperationHelper;

implementation

uses
  cxCustomData, cxGridCustomTableView;

type
  TcxCustomGridViewOperationHelperRec = record
    Index: Integer;
    Proc: TcxGridOperationHelperProc;
  end;

  PcxCustomGridViewOperationHelperRec = ^TcxCustomGridViewOperationHelperRec;

  TcxGridNotifications = class(TcxCustomGridNotification)
  private
    FGridOperationHelper: TcxCustomGridOperationHelper;
  protected
    procedure Notify(AKind: TcxGridNotificationKind; AData: TObject; var AHandled: Boolean); override;
    function NotificationKinds: TcxGridNotificationKinds; override;
  end;

  procedure TcxGridNotifications.Notify(AKind: TcxGridNotificationKind; AData: TObject;
    var AHandled: Boolean);
  begin
    case AKind of
      gnkFocusedViewChanged:
        FGridOperationHelper.DoFocusedViewChanged;
      gnkFocusedRecordChanged, gnkRecordCountChanged:
        FGridOperationHelper.DoFocusedRecordChanged;
      gnkCustomization:
        FGridOperationHelper.DoCustomizationFormVisibleChanged;
    end;
  end;

  function TcxGridNotifications.NotificationKinds: TcxGridNotificationKinds;
  begin
    Result := [gnkFocusedViewChanged, gnkFocusedRecordChanged, gnkRecordCountChanged, gnkCustomization];
  end;

{ TcxCustomGridOperationHelperParameters }
constructor TcxCustomGridOperationHelperParameters.Create(
  AView: TcxCustomGridView; AIsPerform: Boolean = True);
begin
  inherited Create;
  FView := AView;
  FIsPerform := AIsPerform;
end;

{ TcxCustomGridViewOperationHelper }
constructor TcxCustomGridViewOperationHelper.Create;
begin
  inherited Create;
  FList := TList.Create;
  RegisterOperations;
end;

destructor TcxCustomGridViewOperationHelper.Destroy;
var
  I: Integer;
begin
  for I := 0 to FList.Count - 1 do
    Dispose(PcxCustomGridViewOperationHelperRec(FList[I]));
  FList.Free;
  inherited Destroy;
end;

class function TcxCustomGridViewOperationHelper.GetViewClass: TcxCustomGridViewClass;
begin
  Result := TcxCustomGridView;
end;

procedure TcxCustomGridViewOperationHelper.RegisterOperation(AOperationIndex: Integer; const AProc: TcxGridOperationHelperProc);
var
  ARec: PcxCustomGridViewOperationHelperRec;
begin
  New(ARec);
  ARec.Index := AOperationIndex;
  ARec.Proc := AProc;
  FList.Add(ARec);
end;

procedure TcxCustomGridViewOperationHelper.RegisterOperations;
begin
  RegisterOperation(GROP_FIRST, DoFirst);
  RegisterOperation(GROP_LAST, DoLast);
  RegisterOperation(GROP_NEXT, DoNext);
  RegisterOperation(GROP_PREV, DoPrev);
  RegisterOperation(GROP_INSERT, DoInsert);
  RegisterOperation(GROP_DELETE, DoDelete);
end;

function TcxCustomGridViewOperationHelper.IsOperationAccessible(AOperationIndex: Integer): Boolean;
var
  I: Integer;
begin
  Result := False;
  for I := 0 to FList.Count - 1 do
    if PcxCustomGridViewOperationHelperRec(FList[I]).Index = AOperationIndex then
    begin
      Result := True;
      break;
    end;
end;

function TcxCustomGridViewOperationHelper.IsOperationEnabled(AView: TcxCustomGridView; AOperationIndex: Integer): Boolean;

  function CanDelete: Boolean;
  begin
    Result := dceoDelete in AView.DataController.EditOperations;
    if Result and (AView is TcxCustomGridTableView) then
      Result := TcxCustomGridTableView(AView).OptionsData.Deleting;
  end;
  
  function CanInsert: Boolean;
  begin
    Result := dceoInsert in AView.DataController.EditOperations;;
    if Result and (AView is TcxCustomGridTableView) then
      Result := TcxCustomGridTableView(AView).OptionsData.Inserting;
  end;

begin
  Result := True;
  case AOperationIndex of
    GROP_FIRST, GROP_PREV:
      Result := AView.DataController.Active and not AView.DataController.IsBOF;
    GROP_LAST, GROP_NEXT:
      Result := AView.DataController.Active and not AView.DataController.IsEOF;
    GROP_INSERT:
      Result := AView.DataController.Active  and CanInsert;
    GROP_DELETE:
      Result := AView.DataController.Active and CanDelete;
  end;
end;

procedure TcxCustomGridViewOperationHelper.PerformOperation(AOperationIndex: Integer; const AParameter: TcxCustomGridOperationHelperParameters);
var
  I: Integer;
begin
  for I := 0 to FList.Count - 1 do
    if PcxCustomGridViewOperationHelperRec(FList[I]).Index = AOperationIndex then
    begin
      PcxCustomGridViewOperationHelperRec(FList[I]).Proc(AParameter);
      break;
    end;
end;

function TcxCustomGridViewOperationHelper.GetShowProperty(
  const AParameter: TcxCustomGridOperationHelperParameters): Boolean;
begin
  if AParameter is TcxShowingGridOperationHelperParameters then
    Result := (AParameter as TcxShowingGridOperationHelperParameters).Showing
  else Result := False;
end;

procedure TcxCustomGridViewOperationHelper.SetShowProperty(
  const AParameter: TcxCustomGridOperationHelperParameters; AShow: Boolean);
begin
  if AParameter is TcxShowingGridOperationHelperParameters then
    (AParameter as TcxShowingGridOperationHelperParameters).Showing := AShow;
end;

procedure TcxCustomGridViewOperationHelper.DoFirst(const AParameter: TcxCustomGridOperationHelperParameters);
begin
  AParameter.View.DataController.GotoFirst;
end;

procedure TcxCustomGridViewOperationHelper.DoLast(const AParameter: TcxCustomGridOperationHelperParameters);
begin
  AParameter.View.DataController.GotoLast;
end;

procedure TcxCustomGridViewOperationHelper.DoNext(const AParameter: TcxCustomGridOperationHelperParameters);
begin
  AParameter.View.DataController.GotoNext;
end;

procedure TcxCustomGridViewOperationHelper.DoPrev(const AParameter: TcxCustomGridOperationHelperParameters);

⌨️ 快捷键说明

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