cxwwgridconverter.pas

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

PAS
1,078
字号
{*******************************************************************}
{                                                                   }
{       Developer Express Cross Platform Component Library          }
{       ExpressQuantumGrid                                          }
{                                                                   }
{       Copyright (c) 2001-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 AND CLX 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 cxWWGridConverter;

interface

{$I cxVer.inc}

uses
  Classes, SysUtils, cxConverterFactory, cxGridConverter, cxGridDBBandedTableView, cxGridCustomView,
  DB, cxGraphics, cxCheckBox, Graphics, cxHyperLinkEdit, cxTextEdit, cxImage, cxSpinEdit,
  StdCtrls, cxImageComboBox, cxButtonEdit, cxEdit, cxDBLookupComboBox, cxGrid, cxLookupDBGrid,
  cxDropDownEdit, cxMRUEdit, cxMemo, cxCalendar, Controls, cxGridCustomTableView, cxStyles,
  cxConverterUtils, cxCustomConverter;

type
  { TcxWWGridConverter }
  TcxWWGridConverter = class(TcxCustomGridConverter)
  private
    FColor: Integer;
    FFont: TFont;
    FTitleColor: Integer;
    FTitleFont: TFont;
    function ExtractData(const AData: string; var AResultData: string;
      AStartIndex: Integer; ASeparator: Char): Integer;
    function GetCharCase(const ACharCase: string): TEditCharCase;
    function GetcxGridView: TcxGridDBBandedTableView;
    procedure GetFieldNames(AList: TStringList);
    function GetFontWidth(AFont: TFont): Integer;
    procedure ImportColumnCheckBox(AcxColumn: TcxGridDBBandedColumn; const AColumnData: string;
      const AReadOnly: Boolean);
    procedure ImportColumnCustomEdit(AcxColumn: TcxGridDBBandedColumn; const AColumnData: string);
    procedure ImportColumnImage(AcxColumn: TcxGridDBBandedColumn; const AColumnData: string;
      const AReadOnly: Boolean);
    procedure ImportColumnRichEdit(AcxColumn: TcxGridDBBandedColumn; const AColumnData: string);
    procedure ImportColumnText(AcxColumn: TcxGridDBBandedColumn; const AReadOnly: Boolean);
    procedure ImportColumnUrlLink(AcxColumn: TcxGridDBBandedColumn; const AReadOnly: Boolean);
    procedure ImportBands;
    procedure ImportColumns;
    procedure ImportGrid;
    procedure ImportGridStyles;
    procedure ImportWWCheckBox(AcxColumn: TcxGridDBBandedColumn; AComponent: TComponent);
    procedure ImportWWComboBox(AcxColumn: TcxGridDBBandedColumn; AComponent: TComponent);
    procedure ImportWWComboDlg(AcxColumn: TcxGridDBBandedColumn; AComponent: TComponent);
    procedure ImportWWDateTimePicker(AcxColumn: TcxGridDBBandedColumn; AComponent: TComponent);
    procedure ImportWWExpandButton(AcxColumn: TcxGridDBBandedColumn; AComponent: TComponent);
    procedure ImportWWLookupCombo(AcxColumn: TcxGridDBBandedColumn; AComponent: TComponent);
    procedure ImportWWLookupComboDlg(AcxColumn: TcxGridDBBandedColumn; AComponent: TComponent);
    procedure ImportWWMonthCalendar(AcxColumn: TcxGridDBBandedColumn; AComponent: TComponent);
    procedure ImportWWOptions;
    procedure ImportWWRadioGroup(AcxColumn: TcxGridDBBandedColumn; AComponent: TComponent);
    procedure ImportWWSpinEdit(AcxColumn:  TcxGridDBBandedColumn; AComponent: TComponent);
    procedure ImportWWTextEdit(AcxColumn:  TcxGridDBBandedColumn; AComponent: TComponent);
    function IsColumnDefault(const AFieldName: string): string;
    function Pass(const AData: string; AStartIndex: Integer; AFieldCount: Integer;
      ASeparator: Char): Integer;
  protected
    procedure DoRealImport; override;
    procedure DoImportStyles; override;
    function GetGridViewClass: TcxCustomGridViewClass; override;
    property cxGridView: TcxGridDBBandedTableView read GetcxGridView;
  public
    class function GetSourceClassName: string; override;
  end;

implementation

uses cxGridTableView;

{ TcxWWGridConverter }

class function TcxWWGridConverter.GetSourceClassName: string;
begin
  Result := 'TwwDBGrid';
end;

procedure TcxWWGridConverter.DoRealImport;
begin
  cxGridView.DataController.DataSource := GetClassProperty(nil, 'DataSource') as TDataSource;
  ImportBands;
  ImportColumns;
  ImportGrid;
end;

procedure TcxWWGridConverter.DoImportStyles;
begin
  ImportGridStyles;
end;

function TcxWWGridConverter.GetGridViewClass: TcxCustomGridViewClass;
begin
  Result := TcxGridDBBandedTableView;
end;

function TcxWWGridConverter.ExtractData(const AData: string; var AResultData: string;
  AStartIndex: Integer; ASeparator: Char): Integer;
begin
  Result := AStartIndex;
  AResultData := '';
  while Result <= Length(AData) do
  begin
    if AData[Result] = ASeparator then
    begin
      Inc(Result);
      Break;
    end;
    AResultData := AResultData + AData[Result];
    Inc(Result);
  end;
end;

function TcxWWGridConverter.GetCharCase(const ACharCase: string): TEditCharCase;
begin
  if ACharCase = 'ecNormal' then
    Result := ecNormal
  else if ACharCase = 'ecUpperCase' then
    Result := ecUpperCase
  else if ACharCase = 'ecLowerCase' then
    Result := ecLowerCase
  else
    Result := ecNormal;
end;

function TcxWWGridConverter.GetcxGridView: TcxGridDBBandedTableView;
begin
  Result := inherited cxGridView as TcxGridDBBandedTableView;
end;

procedure TcxWWGridConverter.GetFieldNames(AList: TStringList);
var
  ADataSource: TDataSource;
  I: Integer;
begin
  ADataSource := GetClassProperty(nil, 'DataSource') as TDataSource;
  if ADataSource <> nil then
    if ADataSource.DataSet <> nil then
    with ADataSource.DataSet do
      for I := 0 to Fields.Count - 1 do
        AList.Add(Fields[I].FieldName + ';' + IntToStr(Fields[I].DisplayWidth));
end;

function TcxWWGridConverter.GetFontWidth(AFont: TFont): Integer;
var
  APrevFont: TFont;
begin
  Result := 0;
  if AFont = nil then
    Exit;
  with Destination.Canvas do
  begin
    APrevFont := TFont.Create;
    try
      APrevFont.Assign(Font);
      Canvas.Font := AFont;
      Result := Canvas.TextWidth('X');
      Canvas.Font := APrevFont;
    finally
      APrevFont.Free;
    end;
  end;
end;

procedure TcxWWGridConverter.ImportColumnCheckBox(AcxColumn: TcxGridDBBandedColumn;
  const AColumnData: string; const AReadOnly: Boolean);
var
  AValueChecked: string;
  AValueUnchecked: string;
  AIndex: Integer;
begin
  AcxColumn.PropertiesClass := TcxCheckBoxProperties;
  with TcxCheckBoxProperties(AcxColumn.Properties) do
  begin
    ReadOnly := AReadOnly;
    AIndex := Pass(AColumnData, 1, 2, ';');
    AIndex := ExtractData(AColumnData, AValueChecked, AIndex, ';');
    ExtractData(AColumnData, AValueUnchecked, AIndex, ';');
    ValueChecked := AValueChecked;
    ValueUnchecked := AValueUnchecked;
    NullStyle := nssUnchecked;
  end;
end;

procedure TcxWWGridConverter.ImportColumnCustomEdit(AcxColumn: TcxGridDBBandedColumn; const AColumnData: string);
var
  AComponentName: string;
  AComponent: TComponent;
  AIndex: Integer;
begin
  AIndex := Pass(AColumnData, 1, 2, ';');
  ExtractData(AColumnData, AComponentName, AIndex, ';');
  /////////////////////////////////////////////////////////////////////
  //AComponent := Source.Owner.FindComponent(AComponentName);
  /////////////////////////////////////////////////////////////////////
  AComponent := Designer_.GetComponent(AComponentName);
  if AComponent <> nil then
  begin
    if AComponent.ClassName = 'TwwDBSpinEdit' then
      ImportWWSpinEdit(AcxColumn, AComponent)
    else if AComponent.ClassName = 'TwwDBEdit' then
      ImportWWTextEdit(AcxColumn, AComponent)
    else if AComponent.ClassName = 'TwwDBComboBox' then
      ImportWWComboBox(AcxColumn, AComponent)
    else if AComponent.ClassName = 'TwwDBComboDlg' then
      ImportWWComboDlg(AcxColumn, AComponent)
    else if AComponent.ClassName = 'TwwDBLookupCombo' then
      ImportWWLookupCombo(AcxColumn, AComponent)
    else if AComponent.ClassName = 'TwwDBLookupComboDlg' then
      ImportWWLookupComboDlg(AcxColumn, AComponent)
    else if AComponent.ClassName = 'TwwDBMonthCalendar' then
      ImportWWMonthCalendar(AcxColumn, AComponent)
    else if AComponent.ClassName = 'TwwDBDateTimePicker' then
      ImportWWDateTimePicker(AcxColumn, AComponent)
    else if AComponent.ClassName = 'TwwCheckBox' then
      ImportWWCheckBox(AcxColumn, AComponent)
    else if AComponent.ClassName = 'TwwExpandButton' then
      ImportWWExpandButton(AcxColumn, AComponent)
    else if AComponent.ClassName = 'TwwRadioGroup' then
      ImportWWRadioGroup(AcxColumn, AComponent);
  end;
end;

procedure TcxWWGridConverter.ImportColumnImage(AcxColumn: TcxGridDBBandedColumn;
  const AColumnData: string; const AReadOnly: Boolean);
var
  AIndex: Integer;
  AValue1: string;
begin
  AcxColumn.PropertiesClass := TcxImageProperties;
  with TcxImageProperties(AcxColumn.Properties) do
  begin
    ReadOnly := AReadOnly;
    AIndex := Pass(AColumnData, 1, 2, ';');
    ExtractData(AColumnData, AValue1, AIndex, ';');
    Stretch := AValue1 = 'Stretch To Fit';
  end;
end;

procedure TcxWWGridConverter.ImportColumnRichEdit(AcxColumn: TcxGridDBBandedColumn;
  const AColumnData: string);
var
  AComponentName: string;
  AComponent: TComponent;
  AIndex: Integer;
begin
  AIndex := Pass(AColumnData, 1, 2, ';');
  ExtractData(AColumnData, AComponentName, AIndex, ';');
  /////////////////////////////////////////////////////////////////////
  //AComponent := Source.Owner.FindComponent(AComponentName);
  /////////////////////////////////////////////////////////////////////
  AComponent := Designer_.GetComponent(AComponentName);
  if AComponent <> nil then
  begin
    AcxColumn.PropertiesClass := TcxMemoProperties;
    with TcxMemoProperties(AcxColumn.Properties) do
    begin
      Alignment := ConvertAlignment(GetEnumProperty(AComponent, 'Alignment'));
      ReadOnly := GetBooleanProperty(AComponent, 'ReadOnly', ReadOnly);
      AutoSelect := GetBooleanProperty(AComponent, 'AutoSelect', AutoSelect);
      HideSelection := GetBooleanProperty(AComponent, 'HideSelection', HideSelection);
      MaxLength := GetIntegerProperty(AComponent, 'MaxLength', MaxLength);
      ScrollBars := ConvertScrollStyle(GetEnumProperty(AComponent, 'ScrollBars'));
      WantReturns := GetBooleanProperty(AComponent, 'WantReturns', WantReturns);
      WantTabs := GetBooleanProperty(AComponent, 'WantTabs', WantTabs);
      WordWrap := GetBooleanProperty(AComponent, 'WordWrap', WordWrap);
    end;
  end;
end;

procedure TcxWWGridConverter.ImportColumnText(AcxColumn: TcxGridDBBandedColumn;
  const AReadOnly: Boolean);
begin
  AcxColumn.PropertiesClass := TcxTextEditProperties;
  with TcxTextEditProperties(AcxColumn.Properties) do
    ReadOnly := AReadOnly;
end;

procedure TcxWWGridConverter.ImportColumnUrlLink(AcxColumn: TcxGridDBBandedColumn;
  const AReadOnly: Boolean);
begin
  AcxColumn.PropertiesClass := TcxHyperLinkEditProperties;
  with TcxHyperLinkEditProperties(AcxColumn.Properties) do
    ReadOnly := AReadOnly;
end;

procedure TcxWWGridConverter.ImportBands;
begin
  cxGridView.OptionsView.BandHeaders := False;
  cxGridView.Bands.Add;
end;

procedure TcxWWGridConverter.ImportColumns;
var
  AColumns: TStringList;
  AColumnsNeedFree: Boolean;
  AcxColumn: TcxGridDBBandedColumn;
  I: Integer;
  AFieldName: string;
  AWidth: string;
  AHeader: string;
  AReadOnlyString: string;
  AReadOnly: Boolean;
  AColumnData: string;
  AControlType: string;
  AIndex: Integer;
  AFontWidth: Integer;
  AFieldColumns: Boolean;
  ASelected: TStringList;
begin
  AColumnsNeedFree := False;
  ASelected := GetClassProperty(nil, 'Selected') as TStringList;
  if ASelected <> nil then
  begin
    if ASelected.Count = 0 then
    begin
      AColumns := TStringList.Create;
      AColumnsNeedFree := True;
      GetFieldNames(AColumns);
      AFieldColumns := True;
    end
    else
    begin
      AFieldColumns := False;
      if cxGridView.DataController.DataSource <> nil then
        AColumns := ASelected
      else
      begin
        AColumns := TStringList.Create;
        AColumnsNeedFree := True;

⌨️ 快捷键说明

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