cxfilterdialog.pas

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

PAS
514
字号

{********************************************************************}
{                                                                    }
{       Developer Express Visual Component Library                   }
{       ExpressQuantumGrid                                           }
{                                                                    }
{       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 cxFilterDialog;

{$I cxVer.inc}

interface

uses
{$IFDEF DELPHI6}
  Variants,
{$ENDIF}
  Windows, Classes, Controls, Graphics, ExtCtrls, Forms, StdCtrls, SysUtils, cxButtonEdit,
  cxButtons, cxContainer, cxControls, cxDataStorage, cxDropDownEdit, cxEdit,
  cxFilter, cxFilterConsts, cxFilterControlUtils, cxLookAndFeels, cxMaskEdit,
  cxTextEdit, cxLookAndFeelPainters, cxRadioGroup, cxGraphics, Menus;

type
  TcxFilterDialogClass = class of TcxFilterDialog;

  TcxFilterDialog = class(TForm)
    lblTitle: TLabel;
    lblColumnCaption: TLabel;
    bvlLine: TBevel;
    lblSingle: TLabel;
    lblSeries: TLabel;
    btnOK: TcxButton;
    btnCancel: TcxButton;
    lblEdit1PlaceHolder: TLabel;
    lblEdit2PlaceHolder: TLabel;
    cbOperator1: TcxComboBox;
    cbOperator2: TcxComboBox;
    rbAnd: TcxRadioButton;
    rbOr: TcxRadioButton;
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure cbOperator1Click(Sender: TObject);
    procedure cbOperator2PropertiesChange(Sender: TObject);
  private
    FCriteria: TcxFilterCriteria;
    FDisplayValue1: TCaption;
    FDisplayValue2: TCaption;
    FEdit1: TcxCustomEdit;
    FEdit2: TcxCustomEdit;
    FEditProperties: TcxCustomEditProperties;
    FFilterEditHelper: TcxCustomFilterEditHelperClass;
    FItemLink: TObject;
    FValue1: Variant;
    FValue2: Variant;
    FValueTypeClass: TcxValueTypeClass;
  protected
    procedure AddFilterItem(AParent: TcxFilterCriteriaItemList; AComboBox: TcxComboBox;
      AValue: Variant; ADisplayValue: string);
    procedure CheckWildcardDescriptionVisibility;
    procedure CreateParams(var Params: TCreateParams); override;
    procedure GetFilterValues(AEdit: TcxCustomEdit; var AValue: Variant;
      var ADisplayValue: TCaption);
    function GetOperatorComboBox(AEdit: TcxCustomEdit): TcxComboBox; virtual;
    function GetSupportedOperators: TcxFilterControlOperators; virtual;
    procedure InitControls(const ACriteriaItemCaption: string); virtual;
    procedure InitControlValues; virtual;
    procedure InitEdits(AComboBox: TcxComboBox; AEdit: TcxCustomEdit;
      AItem: TcxFilterCriteriaItem);
    procedure InitLookAndFeel(ALookAndFeel: TcxLookAndFeel); virtual;
    procedure SetEditValidChars(AEdit: TcxCustomEdit);
    procedure ValidateValue(AEdit: TcxCustomEdit; var AValue: Variant);

    property FilterEditHelper: TcxCustomFilterEditHelperClass read FFilterEditHelper;
    property SupportedOperators: TcxFilterControlOperators read GetSupportedOperators;
  public
    constructor Create(ACriteria: TcxFilterCriteria; AItemLink: TObject;
      AEditProperties: TcxCustomEditProperties; const ACriteriaItemCaption: string;
      AValueTypeClass: TcxValueTypeClass; ALookAndFeel: TcxLookAndFeel; AFont: TFont); reintroduce; virtual;
    procedure ApplyChanges; virtual;
    class function GetFilterEditHelper(AEditProperties: TcxCustomEditProperties): TcxCustomFilterEditHelperClass;

    property Criteria: TcxFilterCriteria read FCriteria;
    property EditProperties: TcxCustomEditProperties read FEditProperties;
    property ItemLink: TObject read FItemLink;
    property ValueTypeClass: TcxValueTypeClass read FValueTypeClass;
  end;

var
  cxFilterDialogClass: TcxFilterDialogClass = TcxFilterDialog; 

function IsFilterControlDialogNeeded(ACriteria: TcxFilterCriteria): Boolean;
function ShowFilterDialog(ACriteria: TcxFilterCriteria;
  AItemLink: TObject; AEditProperties: TcxCustomEditProperties;
  const ACriteriaItemCaption: string; AValueTypeClass: TcxValueTypeClass;
  ALookAndFeel: TcxLookAndFeel = nil; AFont: TFont = nil): Boolean;

implementation

{$R *.dfm}

uses
{$IFNDEF DELPHI6}
  cxVariants,
{$ENDIF}
  cxClasses, cxFilterControlStrs;

function IsFilterControlDialogNeeded(ACriteria: TcxFilterCriteria): Boolean;
const
  SupportedKinds: TcxFilterOperatorKinds = [foEqual..foNotLike];
var
  AItemLinks: TList;

  function SimpleList(AList: TcxFilterCriteriaItemList): Boolean;
  var
    I: Integer;
    AItem: TcxCustomFilterCriteriaItem;
    AItemLink: TObject;
  begin
    Result := False;
    with AList do
    begin
      if (Count <> 2) or not (BoolOperatorKind in [fboAnd, fboOr]) then
        Exit;
      for I := 0 to Count - 1 do
      begin
        AItem := Items[I];
        if AItem.IsItemList or not (TcxFilterCriteriaItem(AItem).OperatorKind in SupportedKinds) then
          Exit;
      end;
      AItemLink := TcxFilterCriteriaItem(Items[0]).ItemLink;
      Result := (AItemLink = TcxFilterCriteriaItem(Items[1]).ItemLink) and
        (AItemLinks.IndexOf(AItemLink) = -1);
      if Result then
        AItemLinks.Add(AItemLink);
    end;
  end;

  function SimpleItem(AItem: TcxCustomFilterCriteriaItem): Boolean;
  begin
    if AItem.IsItemList then
      Result := SimpleList(TcxFilterCriteriaItemList(AItem))
    else
      with TcxFilterCriteriaItem(AItem) do
      begin
        Result := (OperatorKind in SupportedKinds) and (AItemLinks.IndexOf(ItemLink) = -1);
        if Result then
          AItemLinks.Add(ItemLink);
      end;
  end;

var
  I: Integer;
begin
  AItemLinks := TList.Create;
  try
    Result := ACriteria.Root.BoolOperatorKind <> fboAnd;
    if not Result then
      for I := 0 to ACriteria.Root.Count - 1 do
      begin
        Result := not SimpleItem(ACriteria.Root.Items[I]);
        if Result then Break;
      end;
  finally
    AItemLinks.Free;
  end;
end;

function ShowFilterDialog(ACriteria: TcxFilterCriteria;
  AItemLink: TObject; AEditProperties: TcxCustomEditProperties;
  const ACriteriaItemCaption: string; AValueTypeClass: TcxValueTypeClass;
  ALookAndFeel: TcxLookAndFeel = nil; AFont: TFont = nil): Boolean;
var
  ADialog: TcxFilterDialog;
begin
  Result := False;
  if cxFilterDialogClass.GetFilterEditHelper(AEditProperties) = nil then
    Exit;
  ADialog := cxFilterDialogClass.Create(ACriteria, AItemLink, AEditProperties,
    ACriteriaItemCaption, AValueTypeClass, ALookAndFeel, AFont);
  try
    Result := ADialog.ShowModal = mrOK;
    if Result then ADialog.ApplyChanges;
  finally
    ADialog.Free;
  end;
end;

procedure FillOperatorComboBox(AComboBox: TcxComboBox; AOperators: TcxFilterControlOperators);
var
  AOperator: TcxFilterControlOperator;
begin
  for AOperator := Low(TcxFilterControlOperator) to High(TcxFilterControlOperator) do
    if AOperator in AOperators then
      AComboBox.Properties.Items.AddObject(GetFilterControlOperatorText(AOperator), TObject(AOperator));
end;

function GetOperatorIndex(AComboBox: TcxComboBox; AOperator: TcxFilterControlOperator): Integer;
begin
  Result := AComboBox.Properties.Items.IndexOfObject(TObject(AOperator));
end;

function GetOperator(AComboBox: TcxComboBox): TcxFilterControlOperator;

  function GetStartItemIndex(AComboBox: TcxComboBox): Integer;
  begin
    if AComboBox.Properties.Items[0] = '' then
      Result := 0
    else
      Result := -1;
  end;

begin
  with AComboBox do
    if (ItemIndex = -1) or (ItemIndex = GetStartItemIndex(AComboBox)) then
      Result := fcoNone
    else
      Result := TcxFilterControlOperator(Properties.Items.Objects[ItemIndex])
end;

{ TcxFilterDialog }

constructor TcxFilterDialog.Create(ACriteria: TcxFilterCriteria; AItemLink: TObject;
  AEditProperties: TcxCustomEditProperties; const ACriteriaItemCaption: string;
  AValueTypeClass: TcxValueTypeClass; ALookAndFeel: TcxLookAndFeel; AFont: TFont);
begin
  inherited Create(Application);
  FCriteria := ACriteria;
  FItemLink := AItemLink;
  FEditProperties := AEditProperties;
  FValueTypeClass := AValueTypeClass;

⌨️ 快捷键说明

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