unit FMXTee.Editor.CrossTab;

interface

uses
  System.SysUtils, System.Types, System.Classes, System.Variants, FMX.Types,
  FMX.Controls, FMX.Forms,
  FMX.ListBox, FMX.Dialogs, FMXTee.Editor.Source,
  FMXTee.Chart.CrossTab, FMXTee.Editor.DB, FMX.Edit;

type
  TDBChartCrossTabEditor = class(TDBSourceEditor)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    CBAgg: TComboBox;
    CBValue: TComboEdit;
    CBGroup: TComboEdit;
    CBLabels: TComboEdit;
    CBActive: TCheckBox;
    CBCase: TCheckBox;
    CBHide: TCheckBox;
    PanelButtons: TPanel;
    Panel2: TPanel;
    BOK: TButton;
    procedure BApplyClick(Sender: TObject);
    procedure CBAggChange(Sender: TObject);
    procedure CBHideChange(Sender: TObject);
    procedure CBCaseChange(Sender: TObject);
    procedure CBActiveChange(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure CBSourcesChange(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
   { Private declarations }
    DataSource : TDBCrossTabSource;

    Procedure EnableCombos;
    procedure RefreshSource;
  public
    { Public declarations }
    class procedure Edit(AOwner:TComponent; ACrossTab:TDBCrossTabSource);
    procedure Translate; override;
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Procs;

procedure TDBChartCrossTabEditor.FormActivate(Sender: TObject);
begin
  inherited;
  RefreshSource;
end;

procedure TDBChartCrossTabEditor.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  inherited;

  if CanClose then
  if Assigned(DataSource) and
     (not Assigned(DataSource.Series)) then
          DataSource.Free;
end;

procedure TDBChartCrossTabEditor.RefreshSource;
begin
//  SkipValidation:=True;

//  inherited;

  if not Assigned(TheSeries) then exit;

  if TheSeries.DataSource is TDBCrossTabSource then
     DataSource:=TDBCrossTabSource(TheSeries.DataSource)
  else
  begin
    DataSource:=TDBCrossTabSource.Create(TheSeries.Owner);
    DataSource.Name:=TeeGetUniqueName(DataSource.Owner,'DBCrossTabSource'); // Do not localize
  end;

  CBSources.ItemIndex:=CBSources.ListBox.Items.IndexOfObject(DataSource.DataSet);

  EnableCombos;
  FillFields([CBValue,CBGroup,CBLabels]);

  if DataSource.Formula=gfSum then CBAgg.ItemIndex:=0
                              else CBAgg.ItemIndex:=1;

  With CBValue do ItemIndex:=ListBox.Items.IndexOf(DataSource.ValueField);
  With CBGroup do ItemIndex:=ListBox.Items.IndexOf(DataSource.GroupField);
  With CBLabels do ItemIndex:=ListBox.Items.IndexOf(DataSource.LabelField);

  CBActive.IsChecked:=DataSource.Active;
  CBCase.IsChecked:=DataSource.CaseSensitive;
  CBHide.IsChecked:=DataSource.HideSeries;

  BApply.Enabled:=Assigned(TheSeries) and (DataSource<>TheSeries.DataSource);
  TeeTranslateControl(self,[CBValue,CBGroup,CBLabels]);
end;

procedure TDBChartCrossTabEditor.Translate;
begin
  TeeTranslateControl(Self,[CBValue,CBGroup,CBLabels]);
end;

procedure TDBChartCrossTabEditor.BApplyClick(Sender: TObject);

  Function GetFieldCombo(Combo:TComboEdit):String;
  begin
    With Combo do
    if ItemIndex=-1 then result:=Text
                    else result:=Items[ItemIndex];
  end;

begin
  inherited;

  CheckReplaceSource(DataSource);

  TheSeries.Tag:=0;

  with DataSource do
  begin
    Case CBAgg.ItemIndex of
      0: Formula:=gfSum;
    else
      Formula:=gfCount;
    end;

    ValueField:=GetFieldCombo(CBValue);
    GroupField:=GetFieldCombo(CBGroup);
    LabelField:=GetFieldCombo(CBLabels);
    DataSet:=Self.DataSet;

    CaseSensitive:=CBCase.IsChecked;
    HideSeries:=CBHide.IsChecked;
    Active:=CBActive.IsChecked;
  end;

  BApply.Enabled:=False;
end;

procedure TDBChartCrossTabEditor.CBAggChange(Sender: TObject);
begin
  inherited;
  BApply.Enabled:=True;
end;

procedure TDBChartCrossTabEditor.CBActiveChange(Sender: TObject);
begin
  BApply.Enabled:=True;
end;

procedure TDBChartCrossTabEditor.CBCaseChange(Sender: TObject);
begin
  BApply.Enabled:=True;
end;

procedure TDBChartCrossTabEditor.CBHideChange(Sender: TObject);
begin
  BApply.Enabled:=True;
end;

procedure TDBChartCrossTabEditor.CBSourcesChange(Sender: TObject);
begin
  inherited;

  EnableCombos;
  FillFields([CBValue,CBGroup,CBLabels]);
  BApply.Enabled:=True;
end;

class procedure TDBChartCrossTabEditor.Edit(AOwner: TComponent;
  ACrossTab: TDBCrossTabSource);
begin
  with TDBChartCrossTabEditor.Create(AOwner) do
  try
    TagObject:=ACrossTab.Series;
    PanelButtons.Visible:=True;
    ShowModal;
  finally
    Free;
  end;
end;

Procedure TDBChartCrossTabEditor.EnableCombos;
begin
  EnableControls(DataSet<>nil,[CBAgg,CBValue,CBGroup,CBLabels]);
end;

initialization
  RegisterSeriesEditor(TDBChartCrossTabEditor);
end.
