{*********************************************}
{  TeeChart Charting Library for FireMonkey   }
{  Copyright (c) 2012-2020 by Steema Software }
{  All Rights Reserved                        }
{*********************************************}
unit FMXTee.Editor.Source;
{$I TeeDefs.inc}

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.ListBox, FMX.Dialogs,
  {$IFNDEF UPTOXE3}
  FMX.StdCtrls,
  {$ENDIF}
  FMXTee.Chart, FMXTee.Engine;

type
  TBaseSourceEditor = class(TForm)
    Pan: TPanel;
    LLabel: TLabel;
    CBSources: TComboBox;
    PanelApply: TPanel;
    BApply: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure CBSourcesChange(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormActivate(Sender: TObject);
  private
   { Private declarations }
  protected
    Chart     : TCustomChart;
    TheSeries : TChartSeries;
    SkipValidation : Boolean;

    IDataSource : TTeeSeriesSource;

    Procedure AddComponentDataSource(const AComponent:TComponent;
                                     const AItems:TCustomListBox;
                                     AddCurrent:Boolean);
    procedure CheckDataSource(const SourceClass:TTeeSeriesSourceClass);
    procedure CheckReplaceSource(const NewSource:TTeeSeriesSource);
    Function IsValid(AComponent:TComponent):Boolean; virtual;
  public
    { Public declarations }
    procedure Translate; dynamic;
  end;

  { special case for TFormDesigner in TeeCharteg unit }
  TAddComponentDataSource=Procedure( const AComponent:TComponent;
                                     const AItems:TCustomListBox;
                                     AddCurrent:Boolean) of object;

  TOnGetDesignerNamesEvent=Procedure( AProc:TAddComponentDataSource;
                                      ASeries:TChartSeries;
                                      AItems:TCustomListBox;
                                      AddCurrent:Boolean) of Object;

implementation

{$R *.fmx}

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

procedure TBaseSourceEditor.CheckDataSource(const SourceClass: TTeeSeriesSourceClass);
begin
  if (not Assigned(IDataSource)) and Assigned(TheSeries) then
  begin
    if Assigned(TheSeries.DataSource) and
       (TheSeries.DataSource is SourceClass) then
          IDataSource:=TTeeSeriesSource(TheSeries.DataSource);
  end;

  if not Assigned(IDataSource) then
  begin
    if Assigned(TheSeries) then
       IDataSource:=SourceClass.Create(TheSeries.Owner)
    else
       IDataSource:=SourceClass.Create(Self);

    IDataSource.Name:=TeeGetUniqueName(IDataSource.Owner,
         Copy(SourceClass.ClassName,2,Length(SourceClass.ClassName)));
  end;
end;

procedure TBaseSourceEditor.CheckReplaceSource(const NewSource: TTeeSeriesSource);
var tmpSource : TTeeSeriesSource;
begin
  // Pending Chart.DataSource:
  if Assigned(Chart) and (not Assigned(TheSeries)) then
     TheSeries:=Chart.AddSeries(TeeSeriesTypes[0].SeriesClass);

  if Assigned(TheSeries) and (TheSeries.DataSource<>NewSource) then
  begin
    { if the datasource is an internal "Series Source",
      remove and free it ! }
    if Assigned(TheSeries.DataSource) and
       (TheSeries.DataSource is TTeeSeriesSource) and
       (TheSeries.DataSource.Owner=TheSeries) then
    begin
      tmpSource:=TTeeSeriesSource(TheSeries.DataSource);
      tmpSource.Series:=nil;
      tmpSource.Free;
    end;

    TheSeries.DataSource:=nil;

    if not Assigned(NewSource.Series) then
       NewSource.Series:=TheSeries;

    TheSeries.DataSource:=NewSource;
  end;
end;

type
  TButtonAccess=class(TButton);

procedure TBaseSourceEditor.FormActivate(Sender: TObject);
var tmp : TObject;
begin
  // Trick. Use the "TagObject" property to receive either a Series or a Chart
  if (not Assigned(TheSeries)) and (not Assigned(Chart)) then
  begin
    tmp:=TagObject;

    if tmp is TChartSeries then
       TheSeries:=TChartSeries(tmp)
    else
    if tmp is TCustomChart then
       Chart:=TCustomChart(tmp)
    else
    if tmp is TTeeSeriesSource then
    begin
      IDataSource:=TTeeSeriesSource(tmp);
      TheSeries:=IDataSource.Series;
    end;

    BApply.Enabled:=False;
    TeeTranslateControl(Self);
  end;
end;

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

  if BApply.Enabled then
  case YesNoCancel(TeeMsg_SureToApply) of
    mrYes: begin
             TButtonAccess(BApply).Click;
             CanClose:=not BApply.Enabled;
           end;

     mrNo: begin
             BApply.Enabled:=False;
             CanClose:=True;
           end;

 mrCancel: CanClose:=False;
  end;
end;

procedure TBaseSourceEditor.FormCreate(Sender: TObject);
begin
  SkipValidation:=False;
end;

procedure TBaseSourceEditor.FormDestroy(Sender: TObject);
begin
  if Assigned(IDataSource) and
     (not Assigned(IDataSource.Series)) then
       if IDataSource.Owner=Self then
          IDataSource.Free;
end;

procedure TBaseSourceEditor.AddComponentDataSource(const AComponent: TComponent;
  const AItems: TCustomListBox; AddCurrent: Boolean);
var tmp         : String;
    tmpFormName : TComponentName;
    tmpItem     : TListBoxItem;
begin
  if AItems.Items.IndexOfObject(AComponent)=-1 then { 5.01 }
  if AddCurrent or (TheSeries.DataSources.IndexOf(AComponent)=-1) then
  if IsValid(AComponent) then
     if SkipValidation or
        TheSeries.ParentChart.IsValidDataSource(TheSeries,AComponent) then
     begin
       if AComponent is TChartSeries then
          tmp:=SeriesTitleOrName(TChartSeries(AComponent))
       else
          tmp:=AComponent.Name;

       if (TheSeries.Owner<>AComponent.Owner) and
          Assigned(AComponent.Owner) then
       begin
         tmpFormName:=AComponent.Owner.Name;

         if tmpFormName<>'' then
            tmp:=tmpFormName+'.'+tmp;
       end;

       tmpItem:=TListBoxItem.Create(Self);
       tmpItem.Text:=tmp;
       tmpItem.Data:=AComponent;

       AItems.AddObject(tmpItem);
     end;
end;

function TBaseSourceEditor.IsValid(AComponent: TComponent): Boolean;
begin
  result:=False;
end;

procedure TBaseSourceEditor.Translate;
begin
  TeeTranslateControl(Self);
end;

procedure TBaseSourceEditor.CBSourcesChange(Sender: TObject);
begin
  BApply.Enabled:=True;
end;

end.
