unit FMXTee.Editor.Legend;

interface

uses
  SysUtils, Types, Classes, Variants, FMX.Types, FMX.Controls, FMX.Forms,
  FMX.ListBox, FMX.Edit, FMX.Memo,
  FMX.Dialogs, FMX.Layouts, FMX.TabControl, FMXTee.Chart,
  FMXTee.Editor.Symbol, FMXTee.Editor.CustomShape, FMXTee.RadioGroup, FMX.Colors;

type
  TLegendEditor = class(TForm)
    PageControl1: TTabControl;
    TabStyle: TTabItem;
    L12: TLabel;
    L7: TLabel;
    Label1: TLabel;
    CBShow: TCheckBox;
    CBInverted: TCheckBox;
    CBLegendStyle: TComboBox;
    CBLegStyle: TComboBox;
    BDivLines: TButton;
    UDVertSpacing: TSpinBox;
    CBFontColor: TCheckBox;
    CBCheckBoxes: TComboBox;
    TabPosition: TTabItem;
    L10: TLabel;
    L1: TLabel;
    CBResizeChart: TCheckBox;
    UDTopPos: TSpinBox;
    UDMargin: TSpinBox;
    GroupBox1: TGroupBox;
    Label4: TLabel;
    Label5: TLabel;
    UDLeft: TSpinBox;
    UDTop: TSpinBox;
    CBCustPos: TCheckBox;
    CBPosPercent: TCheckBox;
    TabSymbol: TTabItem;
    TabTitle: TTabItem;
    PageControl2: TTabControl;
    TabSheet4: TTabItem;
    Label6: TLabel;
    Label7: TLabel;
    MemoText: TMemo;
    CBTextAlign: TComboBox;
    CBTitleVisible: TCheckBox;
    Button1: TButton;
    TabItems: TTabItem;
    Panel1: TPanel;
    SBAddItem: TButton;
    SBDeleteItem: TButton;
    CBCustom: TCheckBox;
    CBCustPosition: TCheckBox;
    LBItems: TListBox;
    PanelItem: TPanel;
    Label2: TLabel;
    Label3: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    EItemText: TEdit;
    EItemText2: TEdit;
    CBItemAlign: TComboBox;
    UDItemLeft: TSpinBox;
    UDItemTop: TSpinBox;
    Button2: TButton;
    CBDefItemFont: TCheckBox;
    RGPosition: TRadioGroup;
    TabFormat: TTabItem;
    Label11: TLabel;
    TBTransp: TAlphaTrackBar;
    CBBehind: TCheckBox;
    procedure PageControl1Change(Sender: TObject);
    procedure CBLegendStyleChange(Sender: TObject);
    procedure CBLegStyleChange(Sender: TObject);
    procedure CBCheckBoxesChange(Sender: TObject);
    procedure PageControl2Change(Sender: TObject);
    procedure CBTextAlignChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure SBAddItemClick(Sender: TObject);
    procedure SBDeleteItemClick(Sender: TObject);
    procedure LBItemsClick(Sender: TObject);
    procedure CBItemAlignChange(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure CBShowChange(Sender: TObject);
    procedure CBFontColorChange(Sender: TObject);
    procedure CBInvertedChange(Sender: TObject);
    procedure UDVertSpacingChange(Sender: TObject);
    procedure CBTitleVisibleChange(Sender: TObject);
    procedure CBResizeChartChange(Sender: TObject);
    procedure BDivLinesClick(Sender: TObject);
    procedure MemoTextChangeTracking(Sender: TObject);
    procedure RGPositionChange(Sender: TObject);
    procedure UDLeftChange(Sender: TObject);
    procedure UDTopChange(Sender: TObject);
    procedure UDMarginChange(Sender: TObject);
    procedure UDTopPosChange(Sender: TObject);
    procedure CBCustPosChange(Sender: TObject);
    procedure CBPosPercentChange(Sender: TObject);
    procedure CBCustomChange(Sender: TObject);
    procedure CBCustPositionChange(Sender: TObject);
    procedure CBDefItemFontChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure EItemTextTyping(Sender: TObject);
    procedure EItemText2Typing(Sender: TObject);
    procedure UDItemLeftChangeTracking(Sender: TObject);
    procedure UDItemTopChangeTracking(Sender: TObject);
    procedure CBBehindChange(Sender: TObject);
    procedure TBTranspChange(Sender: TObject);
  private
   { Private declarations }
     Legend : TChartLegend;
     Symbol : TSymbolEditor;

     FTitleEditor : TTeeShapeEditor;
     ITeeObject   : TTeeShapeEditor;
     IReady       : Boolean;

     function LegendItem:TLegendItem;
     procedure RefreshLegend(ALegend:TChartLegend);
  public
   { Public declarations }
     class function CreateEditor(AOwner:TComponent; ALegend:TChartLegend):TForm;
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Editor.Strings, FMXTee.Editor.Stroke, FMXTee.Engine,
  FMXTee.Editor.Font, FMXTee.Procs;

procedure TLegendEditor.PageControl1Change(Sender: TObject);
begin
  if (PageControl1.ActiveTab=TabSymbol) and (not Assigned(Symbol)) then
  begin
    Symbol:=TSymbolEditor.Create(Self);
    EmbeddForm(Symbol,TabSymbol);
    Symbol.RefreshSymbol(Legend.Symbol);
  end
  else
  if (PageControl1.ActiveTab=TabTitle) and (not Assigned(FTitleEditor)) then
  begin
    FTitleEditor:=TTeeShapeEditor.Embedd(Self, PageControl2);
    FTitleEditor.Name:=TeeGetUniqueName(Self, 'LegendTitleEditor'); // Do not localize
    FTitleEditor.RefreshShape(Legend.Title);
  end
  else
  if (PageControl1.ActiveTab=TabFormat) and (not Assigned(ITeeObject)) then
  begin
    ITeeObject:=TTeeShapeEditor.CreateEditor(Self, Legend);
    ITeeObject.Name:=TeeGetUniqueName(Self, 'LegendShapeEditor'); // Do not localize
    EmbeddForm(ITeeObject, TabFormat);
  end;
end;

procedure TLegendEditor.UDItemLeftChangeTracking(Sender: TObject);
begin
  if Assigned(Legend) then
     LegendItem.Left:=Round(UDItemLeft.Value);
end;

procedure TLegendEditor.UDItemTopChangeTracking(Sender: TObject);
begin
  if Assigned(Legend) then
     LegendItem.Top:=Round(UDItemTop.Value);
end;

procedure TLegendEditor.UDLeftChange(Sender: TObject);
begin
  if IReady then
    if Legend.PositionUnits=muPercent then
       Legend.LeftPercent:=Round(UDLeft.Value)
    else
       Legend.Left:=Round(UDLeft.Value);
end;

procedure TLegendEditor.UDMarginChange(Sender: TObject);
begin
  Legend.HorizMargin:=Round(UDMargin.Value);
end;

procedure TLegendEditor.UDTopChange(Sender: TObject);
begin
  if IReady then
    if Legend.PositionUnits=muPercent then
       Legend.TopPercent:=Round(UDTop.Value)
    else
       Legend.Top:=Round(UDTop.Value);
end;

procedure TLegendEditor.UDTopPosChange(Sender: TObject);
begin
  Legend.TopPos:=Round(UDTopPos.Value);
end;

procedure TLegendEditor.UDVertSpacingChange(Sender: TObject);
begin
  Legend.VertSpacing:=Round(UDVertSpacing.Value);
end;

procedure TLegendEditor.CBShowChange(Sender: TObject);
begin
  Legend.Visible:=CBShow.IsChecked;
end;

procedure TLegendEditor.CBLegendStyleChange(Sender: TObject);
begin
  Legend.LegendStyle:=TLegendStyle(CBLegendStyle.ItemIndex);
end;

procedure TLegendEditor.CBLegStyleChange(Sender: TObject);
begin
  Legend.TextStyle:=TLegendTextStyle(CBLegStyle.ItemIndex);
end;

procedure TLegendEditor.CBFontColorChange(Sender: TObject);
begin
  Legend.FontSeriesColor:=CBFontColor.IsChecked;
end;

procedure TLegendEditor.CBBehindChange(Sender: TObject);
begin
  Legend.DrawBehind:=CBBehind.IsChecked;
end;

procedure TLegendEditor.CBCheckBoxesChange(Sender: TObject);
begin
  Legend.CheckBoxes:=(CBCheckBoxes.ItemIndex<>0);

  if Legend.CheckBoxes then
     if CBCheckBoxes.ItemIndex=1 then
        Legend.CheckBoxesStyle:=cbsCheck
     else
        Legend.CheckBoxesStyle:=cbsRadio;
end;

procedure TLegendEditor.CBResizeChartChange(Sender: TObject);
begin
  Legend.ResizeChart:=CBResizeChart.IsChecked;
end;

procedure TLegendEditor.RGPositionChange(Sender: TObject);
begin
  Legend.Alignment:=TLegendAlignment(RGPosition.ItemIndex);
end;

procedure TLegendEditor.RefreshLegend(ALegend:TChartLegend);
begin
  Legend:=ALegend;

  IReady:=False;

  CBShow.IsChecked:=Legend.Visible;
  CBInverted.IsChecked:=Legend.Inverted;
  CBFontColor.IsChecked:=Legend.FontSeriesColor;

  if not Legend.CheckBoxes then
     CBCheckBoxes.ItemIndex:=0
  else
  if Legend.CheckBoxesStyle=cbsCheck then
     CBCheckBoxes.ItemIndex:=1
  else
     CBCheckBoxes.ItemIndex:=2;

  UDVertSpacing.Value:=Legend.VertSpacing;

  CBLegendStyle.ItemIndex:=Ord(Legend.LegendStyle);
  CBLegStyle.ItemIndex:=Ord(Legend.TextStyle);

  TBTransp.Value:=Legend.GlobalTransparency*0.01;

  // Position
  RGPosition.ItemIndex:=Ord(Legend.Alignment);
  CBResizeChart.IsChecked:=Legend.ResizeChart;
  UDMargin.Value:=Legend.HorizMargin;
  UDTopPos.Value:=Legend.TopPercent;

  CBCustPos.IsChecked:=Legend.CustomPosition;

  if CBPosPercent.IsChecked then
     UDLeft.Value:=Legend.LeftPercent
  else
     UDLeft.Value:=Legend.Left;

  if CBPosPercent.IsChecked then
     UDTop.Value:=Legend.TopPercent
  else
     UDTop.Value:=Legend.Top;

  CBBehind.IsChecked:=Legend.DrawBehind;

  // Title
  MemoText.Text:=Legend.Title.Text.Text;
  CBTitleVisible.IsChecked:=Legend.Title.Visible;
  CBTextAlign.ItemIndex:=Ord(Legend.Title.TextAlignment);

  if Assigned(FTitleEditor) then
     TTeeShapeEditor(FTitleEditor).RefreshShape(Legend.Title);

  // Symbol
  if Assigned(Symbol) then
     Symbol.RefreshSymbol(Legend.Symbol);

  if Assigned(ITeeObject) then
     ITeeObject.RefreshShape(Legend);

  IReady:=True;
end;

class function TLegendEditor.CreateEditor(AOwner: TComponent; ALegend: TChartLegend):TForm;
begin
  result:=TLegendEditor.Create(AOwner);
  TLegendEditor(result).RefreshLegend(ALegend);
end;

procedure TLegendEditor.CBCustPosChange(Sender: TObject);
begin
  if IReady then
     Legend.CustomPosition:=CBCustPos.IsChecked;
end;

procedure TLegendEditor.CBPosPercentChange(Sender: TObject);
begin
  if CBPosPercent.IsChecked then
     Legend.PositionUnits:=muPercent
  else
     Legend.PositionUnits:=muPixels;
end;

procedure TLegendEditor.PageControl2Change(Sender: TObject);
begin
  if Assigned(FTitleEditor) then
     FTitleEditor.PC1Change(PageControl2);
end;

procedure TLegendEditor.MemoTextChangeTracking(Sender: TObject);
begin
  if Assigned(Legend) then
     Legend.Title.Text.Text:=MemoText.Text;
end;

procedure TLegendEditor.CBTextAlignChange(Sender: TObject);
begin
  Legend.Title.TextAlignment:=TAlignment(CBTextAlign.ItemIndex);
end;

procedure TLegendEditor.CBTitleVisibleChange(Sender: TObject);
begin
  Legend.Title.Visible:=CBTitleVisible.IsChecked;
end;

procedure TLegendEditor.BDivLinesClick(Sender: TObject);
begin
  TStrokeEditor.Edit(Self, Legend.DividingLines);
end;

procedure TLegendEditor.Button1Click(Sender: TObject);
begin
  TStringsEditor.Edit(Self,Legend.Title.Text);
end;

procedure TLegendEditor.SBAddItemClick(Sender: TObject);
begin
// TODO
end;

procedure TLegendEditor.SBDeleteItemClick(Sender: TObject);
var tmp : Integer;
begin
  tmp:=LBItems.ItemIndex;
  LBItems.ListItems[tmp].Free;
  Legend.Items.Delete(tmp);
  LBItemsClick(Self);
end;

procedure TLegendEditor.TBTranspChange(Sender: TObject);
begin
  Legend.GlobalTransparency:=Round(TBTransp.Value*100.0);
end;

procedure TLegendEditor.CBCustomChange(Sender: TObject);
begin
  if IReady then
     Legend.Items.Custom:=CBCustom.IsChecked;
end;

procedure TLegendEditor.CBCustPositionChange(Sender: TObject);
begin
  if IReady then
     Legend.Items.CustomPosition:=CBCustPosition.IsChecked;
end;

procedure TLegendEditor.LBItemsClick(Sender: TObject);
begin
  PanelItem.Enabled:=LBItems.ItemIndex<>-1;

  if PanelItem.Enabled then
  begin
    EItemText.Text:=LegendItem.Text;
    EItemText2.Text:=LegendItem.Text2;
    CBItemAlign.ItemIndex:=LegendItem.Align;

    UDItemLeft.Value:=LegendItem.Left;
    UDItemTop.Value:=LegendItem.Top;

    CBDefItemFont.IsChecked:=not LegendItem.HasFont;
  end;
end;

procedure TLegendEditor.EItemText2Typing(Sender: TObject);
begin
  LegendItem.Text2:=EItemText2.Text;
end;

procedure TLegendEditor.EItemTextTyping(Sender: TObject);
begin
  LegendItem.Text:=EItemText.Text;
end;

procedure TLegendEditor.CBInvertedChange(Sender: TObject);
begin
  Legend.Inverted:=CBInverted.IsChecked;
end;

procedure TLegendEditor.CBItemAlignChange(Sender: TObject);
begin
  LegendItem.Align:=CBItemAlign.ItemIndex;
end;

procedure TLegendEditor.FormCreate(Sender: TObject);
begin
  PageControl1.ActiveTab:=TabStyle;

  {$IFDEF TEELITEEMB}
  TabItems.Visible:=False;
  {$ENDIF}
end;

function TLegendEditor.LegendItem:TLegendItem;
begin
  result:=Legend.Item[LBItems.ItemIndex];
end;

procedure TLegendEditor.Button2Click(Sender: TObject);
begin
  TFontEditor.Edit(Self, LegendItem.Font);
  CBDefItemFont.IsChecked:=False;
end;

procedure TLegendEditor.CBDefItemFontChange(Sender: TObject);
begin
  if CBDefItemFont.IsChecked then
     LegendItem.Font:=nil;
end;

end.
