{*******************************************************}
{                                                       }
{           FastCube 2 dimension editor unit            }
{                                                       }
{               Copyright (c) 2001-2016                 }
{           by Oleg Pryalkov, Paul Ishenin              }
{                  Fast Reports Inc.                    }
{*******************************************************}

unit fcxDimensionEditor;

{$I fcx.inc}

interface

uses
{$IFDEF FPC}
  LCLType,
{$ELSE}
  Windows,
{$ENDIF}
{$IFDEF DELPHI_6UP}
  Variants,
{$ENDIF}
  SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls,
  fcxTypes, fcxSlice, Spin, ExtCtrls, CheckLst, ActnList,
  fcxFormatFrame, fcxCalculationFrame;

type

  { TfcxDimensionEditorForm }

  TfcxDimensionEditorForm = class(TForm)
    cefTotal: TfcxCalculationEditorFrame;
    Pages: TPageControl;
    PageGeneral: TTabSheet;
    OkBtn: TButton;
    CancelBtn: TButton;
    ApplyBtn: TButton;
    lblCaption: TLabel;
    edCaption: TEdit;
    PageFormat: TTabSheet;
    FormatFrame: TfcxFormatEditorFrame;
    lblTotalPosition: TLabel;
    cbTotalPosition: TComboBox;
    cbUseTotalPositionFromMeasure: TCheckBox;
    lblSortDirection: TLabel;
    cbSortDirection: TComboBox;
    lblAdditionalTotals: TLabel;
    clbAdditionalTotals: TCheckListBox;
    lblFunctionTotal: TLabel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ApplyBtnClick(Sender: TObject);
  private
    FSlice: TfcxSlice;
    FDimension: TfcxAxisField;
    procedure Localize;
    function GetCalculationType(const Expression, MethodName: string): TfcxCalculationType;
  protected
    procedure SetupGeneralPage;
    procedure SetupFormatPage;

    procedure ApplyGeneralPage;
    procedure ApplyFormatPage;
    procedure Apply;

  public
    function Execute(ASlice: TfcxSlice; ADimension: TfcxAxisField; const PageIndex: Integer = 0): Boolean;
  end;

const
  fcxDimensionEditorGeneralPageIndex = 0;
  fcxDimensionEditorFormatPageIndex = 1;

implementation

uses
  fcxRes;

{$IFDEF FPC}
  {$R *.lfm}
{$ELSE}
  {$R *.dfm}
{$ENDIF}

{ TDimensionEditor }

procedure TfcxDimensionEditorForm.Apply;
begin
  FDimension.BeginUpdate;
  ApplyGeneralPage;
  ApplyFormatPage;
  FDimension.EndUpdate;
end;

function TfcxDimensionEditorForm.Execute(ASlice: TfcxSlice; ADimension: TfcxAxisField; const PageIndex: Integer = 0): Boolean;
begin
  FSlice := ASlice;
  FDimension := ADimension;
  Pages.ActivePageIndex := PageIndex;

  Localize;

  SetupGeneralPage;
  SetupFormatPage;
  Result := ShowModal = mrOk;
  if Result then
    Apply;
end;

procedure TfcxDimensionEditorForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfcxDimensionEditorForm.ApplyGeneralPage;
var
  TotalFunctions: TfcxSetAgrFunc;
  I: Integer;
begin
  FDimension.Caption := ControlToString(edCaption.Text);
  FDimension.TotalPosition := TfcxTotalPosition(cbTotalPosition.ItemIndex);
  FDimension.UseTotalPositionFromMeasure := cbUseTotalPositionFromMeasure.Checked;
  FDimension.SortDirection := TfcxSortDirection(cbSortDirection.ItemIndex);
  case (cefTotal.CalculationType) of
    fcctExpression:
    begin
      FDimension.AdditionalTotalExpression := cefTotal.Value;
      FDimension.AdditionalTotalScriptFunction := '';
    end;
    fcctScriptFunction:
    begin
      FDimension.AdditionalTotalExpression := '';
      FDimension.AdditionalTotalScriptFunction := cefTotal.Value;
    end;
  end;

  TotalFunctions := [];
  for I := 0 to clbAdditionalTotals.Items.Count - 1 do
    if clbAdditionalTotals.Checked[I] then
      Include(TotalFunctions, TfcxAgrFunc(PtrInt(clbAdditionalTotals.Items.Objects[I])));
  FDimension.AdditionalTotalFunctions := TotalFunctions;
end;

procedure TfcxDimensionEditorForm.SetupGeneralPage;
var
  I: Integer;
begin
  edCaption.Text := StringToControl(FDimension.Caption);
  cbTotalPosition.ItemIndex := Ord(FDimension.TotalPosition);
  cbUseTotalPositionFromMeasure.Checked := FDimension.UseTotalPositionFromMeasure;
  cbSortDirection.ItemIndex := Ord(FDimension.SortDirection);
  cefTotal.Slice := FSlice;
  cefTotal.ItemName := FDimension.Name;
  cefTotal.Suffix := 'OnGetAddTotalValue';
  cefTotal.CalculationType := GetCalculationType(FDimension.AdditionalTotalExpression, FDimension.AdditionalTotalScriptFunction);
  cefTotal.AllowTypeChange := True;
  if cefTotal.CalculationType = fcctExpression then
    cefTotal.Value := FDimension.AdditionalTotalExpression
  else
    cefTotal.Value := FDimension.AdditionalTotalScriptFunction;
  for I := 0 to clbAdditionalTotals.Items.Count - 1 do
    clbAdditionalTotals.Checked[I] := TfcxAgrFunc(PtrInt(clbAdditionalTotals.Items.Objects[I])) in FDimension.AdditionalTotalFunctions;
end;

procedure TfcxDimensionEditorForm.ApplyBtnClick(Sender: TObject);
begin
  Apply;
end;

procedure TfcxDimensionEditorForm.Localize;
const
  TotalsItems: array[TfcxTotalPosition] of String = (
 { fctp_Before } 'sBefore',
 { fctp_After  } 'sAfter',
 { fctp_Hide   } 'sHide'
  );
  SortItems: array[TfcxSortDirection] of String = (
  { fcsd_Asc  } 'sAscending',
  { fcsd_Desc } 'sDescending'
  );
var
  TotalPos: TfcxTotalPosition;
  SortDirection: TfcxSortDirection;
  AgrFunc: TfcxAgrFunc;
  DimensionCaption: TfcxString;
begin
  DimensionCaption := FDimension.Caption;
  if DimensionCaption = '' then
    DimensionCaption := FDimension.Name;
  Caption := fcxResources.Get('sDimensionEditor') + Format(' [%s]', [StringToControl(DimensionCaption)]);
  // buttons
  OkBtn.Caption := fcxResources.Get('sOkBtn');
  CancelBtn.Caption := fcxResources.Get('sCancelBtn');
  ApplyBtn.Caption := fcxResources.Get('sApplyBtn');

  // general page
  PageGeneral.Caption := fcxResources.Get('sGeneral');
  lblCaption.Caption := fcxResources.Get('sCaption') + ':';
  lblTotalPosition.Caption := fcxResources.Get('sTotalPosition') + ':';
  for TotalPos := Low(TfcxTotalPosition) to High(TfcxTotalPosition) do
    cbTotalPosition.Items.Add(fcxResources.Get(TotalsItems[TotalPos]));
  cbUseTotalPositionFromMeasure.Caption := fcxResources.Get('sUseTotalPositionFromMeasure');
  lblSortDirection.Caption := fcxResources.Get('sSortDirection') + ':';
  for SortDirection := Low(TfcxSortDirection) to High(TfcxSortDirection) do
    cbSortDirection.Items.Add(fcxResources.Get(SortItems[SortDirection]));
  lblFunctionTotal.Caption := fcxResources.Get('sCalculation') + ':';
  lblAdditionalTotals.Caption := fcxResources.Get('sAdditionalTotals') + ':';
  for AgrFunc := Succ(Low(TfcxAgrFunc)) to High(TfcxAgrFunc) do
    clbAdditionalTotals.Items.AddObject(fcxResources.Get(sFuncCaptions[AgrFunc]), TObject(PtrInt(AgrFunc)));

  // format page
  PageFormat.Caption := fcxResources.Get('sDisplayFormat');
end;

function TfcxDimensionEditorForm.GetCalculationType(const Expression, MethodName: string): TfcxCalculationType;
begin
  if ((Expression <> EmptyStr) or (MethodName = EmptyStr)) then
    Result := fcctExpression
  else
    Result := fcctScriptFunction;
end;

procedure TfcxDimensionEditorForm.SetupFormatPage;
begin
  FormatFrame.Format := FDimension.DisplayFormat;
end;

procedure TfcxDimensionEditorForm.ApplyFormatPage;
begin
  FDimension.DisplayFormat := FormatFrame.Format;
end;

end.
