{**********************************************}
{   TeeChart Database Cross-Tab Source         }
{   Copyright (c) 1996-2020 by Steema Software }
{**********************************************}
unit TeeDBCrossTabEditor;
{$I TeeDefs.inc}

interface

uses
  {$IFNDEF LINUX}
  Windows, Messages,
  {$ENDIF}
  SysUtils, Classes,

  Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Buttons,

  DB, Chart, DBChart, TeEngine, TeeProcs, TeeSourceEdit, TeeDBEdit, TeCanvas,
  TeeDBCrossTab;

type
  TDBChartCrossTabEditor = class(TBaseDBChartEditor)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    CBAgg: TComboFlat;
    CBValue: TComboFlat;
    CBGroup: TComboFlat;
    Label4: TLabel;
    CBLabels: TComboFlat;
    CBActive: TCheckBox;
    CBCase: TCheckBox;
    CBHide: TCheckBox;
    PanelButtons: TPanel;
    Panel2: TPanel;
    BOK: TButton;
    CBKeepData: TCheckBox;
    procedure CBSourcesChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BApplyClick(Sender: TObject);
    procedure CBAggChange(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure CBActiveClick(Sender: TObject);
    procedure CBCaseClick(Sender: TObject);
    procedure CBHideClick(Sender: TObject);
    procedure CBKeepDataClick(Sender: TObject);
  private
    { Private declarations }
    DataSource : TDBCrossTabSource;
    Procedure EnableCombos;
  public
    { Public declarations }
    procedure Translate; override;
  end;

implementation

{$IFNDEF LCL}
{$R *.DFM}
{$ELSE}
{$R *.lfm}
{$ENDIF}

uses
  TeeConst;

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

procedure TDBChartCrossTabEditor.CBSourcesChange(Sender: TObject);
begin
  inherited;
  EnableCombos;
  FillFields([CBValue,CBGroup,CBLabels]);
  BApply.Enabled:=True;
end;

procedure TDBChartCrossTabEditor.FormShow(Sender: TObject);
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;

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

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

  case DataSource.Formula of
      gfSum: CBAgg.ItemIndex:=0;
    gfCount: CBAgg.ItemIndex:=1;
      gfMin: CBAgg.ItemIndex:=2;
  else
    CBAgg.ItemIndex:=3;
  end;

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

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

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

procedure TDBChartCrossTabEditor.BApplyClick(Sender: TObject);

  Function GetFieldCombo(Combo:TComboBox):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;
      1: Formula:=gfCount;
      2: Formula:=gfMin;
    else
      Formula:=gfMax;
    end;

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

    CaseSensitive:=CBCase.Checked;
    HideSeries:=CBHide.Checked;

    CBKeepData.Checked:=KeepDataOnClose;

    Active:=CBActive.Checked;
  end;

  BApply.Enabled:=False;
end;

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

procedure TDBChartCrossTabEditor.FormDestroy(Sender: TObject);
begin
  if Assigned(DataSource) and
     (not Assigned(DataSource.Series)) then
          DataSource.Free;

  inherited;
end;

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

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

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

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

procedure TDBChartCrossTabEditor.CBKeepDataClick(Sender: TObject);
begin
  if Assigned(DataSource) then
     DataSource.KeepDataOnClose:=CBKeepData.Checked;
end;

initialization
  RegisterClass(TDBChartCrossTabEditor);
finalization
end.
