unit FMXTee.Editor.DBSource;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMXTee.Editor.DB,
  FMX.ListBox, Data.DB, FMXTee.Editor.SelectList, FMX.Layouts;

type
  TDBChartSourceEditor = class(TDBSourceEditor)
    Layout1: TLayout;
    procedure BApplyClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure CBSourcesChange(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    {$IFNDEF TEELITE}
    //INavig   : TDBNavigator;
    {$ENDIF}

    ISources : TSelectListForm;
    Procedure FillFields;
    Function DataSource:TDataSource;
    procedure OnChangeSources(Sender: TObject);
  protected
    Function IsValid(AComponent:TComponent):Boolean; override;
  public
    { Public declarations }
  end;

implementation

{$R *.fmx}

uses
  FMXTee.DBChart, FMXTee.Engine, FMXTee.Constants, FMXTee.Procs,
  FMXTee.Editor.Stroke;

type
  TSingleRecordSeriesSource=class(TTeeSeriesDBSource)
  public
    class Function Description:String; override;
    class Function Editor:TComponentClass; override;
    class Function HasSeries(ASeries:TChartSeries):Boolean; override;
  end;

{ TSingleRecordSeriesSource }

class function TSingleRecordSeriesSource.Description: String;
begin
  result:=TeeMsg_SingleRecord;
end;

class function TSingleRecordSeriesSource.Editor: TComponentClass;
begin
  result:=TDBChartSourceEditor;
end;

class function TSingleRecordSeriesSource.HasSeries(
                                                ASeries: TChartSeries): Boolean;
begin
  result:=ASeries.DataSource is TDataSource;
end;

procedure TDBChartSourceEditor.BApplyClick(Sender: TObject);
var tmpSt : String;
    t     : Integer;
begin
  inherited;

  TheSeries.DataSource:=nil;

  With ISources.ToList do
  if Items.Count>0 then
  begin
    tmpSt:=Items[0];

    for t:=1 to Items.Count-1 do
        tmpSt:=tmpSt+';'+Items[t]; // Do not localize
  end
  else tmpSt:='';

  TheSeries.MandatoryValueList.ValueSource:=tmpSt;
  TheSeries.DataSource:=DataSource;

  {$IFNDEF TEELITE}
  //INavig.DataSource:=DataSource;
  {$ENDIF}

  BApply.Enabled:=False;
end;

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

  FillFields;
  TheSeries.XLabelsSource:='';
end;

function TDBChartSourceEditor.DataSource: TDataSource;
begin
  if CBSources.Selected=nil then
     result:=nil
  else
     result:=TDataSource(CBSources.Selected.Data);
end;

procedure TDBChartSourceEditor.FillFields;

  Procedure AddField(Const tmpSt:String; tmpType:TFieldType);
  begin
    Case TeeFieldType(tmpType) of
    tftNumber,
   tftDateTime: ISources.FromList.Items.Add(tmpSt);
    end;
  end;

  Procedure AddAggregateFields;
  var t : Integer;
  begin
     With DataSource.DataSet do
     for t:=0 to AggFields.Count-1 do
         AddField(AggFields[t].FieldName,ftFloat);
  end;

var tmpSt : String;
    tmpField : String;
    t     : Integer;
begin
  ISources.FromList.Clear;
  ISources.ToList.Clear;

  {$IFNDEF TEELITE}
  //INavig.DataSource:=DataSource;
  {$ENDIF}

  if (DataSource<>nil) and (DataSource.DataSet<>nil) then
  begin
    With DataSource.DataSet do
    if FieldCount>0 then
    begin
       for t:=0 to FieldCount-1 do
           AddField(Fields[t].FieldName,Fields[t].DataType);
       AddAggregateFields;
    end
    else
    begin
      FieldDefs.Update;
      for t:=0 to FieldDefs.Count-1 do
          AddField(FieldDefs[t].Name,FieldDefs[t].DataType);
      AddAggregateFields;
    end;

    tmpSt:=TheSeries.MandatoryValueList.ValueSource;
    for t:=1 to TeeNumFields(tmpSt) do
    begin
      tmpField:=TeeExtractField(tmpSt,t);
      With ISources.FromList.Items do
      if IndexOf(tmpField)<>-1 then
      begin
        ISources.ToList.Items.Add(tmpField);
        Delete(IndexOf(tmpField));
      end;
    end;
  end;

  ISources.EnableButtons;
end;

procedure TDBChartSourceEditor.FormActivate(Sender: TObject);
begin
  inherited;

  ISources.TagObject:=TheSeries;

  {$IFNDEF TEELITE}
  (*
  INavig:=TDBNavigator.Create(Self);
  With INavig do
  begin
    VisibleButtons:=[nbFirst, nbPrior, nbNext, nbLast, nbRefresh];
    Flat:=True;
    Parent:=ISources;
    Align:=alBottom;
  end;
  *)
  {$ENDIF}

  LLabel.Text:=TeeMsg_AskDataSource;
  FillFields;

  ISources.FormActivate(Self);
end;

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

  {$IFNDEF TEELITE}
  //if CanClose then
     //INavig.Free;
  {$ENDIF}
end;

procedure TDBChartSourceEditor.FormCreate(Sender: TObject);
begin
  inherited;

  ISources:=TSelectListForm.Create(Self);
  ISources.OnChange:=OnChangeSources;
  EmbeddForm(ISources,Layout1);

  TeeTranslateControl(ISources);
end;

function TDBChartSourceEditor.IsValid(AComponent: TComponent): Boolean;
begin
  result:=AComponent is TDataSource;
end;

procedure TDBChartSourceEditor.OnChangeSources(Sender: TObject);
begin
  BApply.Enabled:=True;
end;

initialization
  TeeSources.Add(TSingleRecordSeriesSource);
finalization
  TeeSources.Remove(TSingleRecordSeriesSource);
end.
