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

interface

uses
  System.SysUtils, System.Types, System.Classes, System.Variants, FMX.Types,
  FMX.Controls, FMX.Forms,
  {$IFNDEF UPTOXE3}
  FMX.StdCtrls,
  {$ENDIF}
  {$IFDEF D21}
  FMX.SpinBox,
  {$ENDIF}
  FMX.Edit, FMX.ListBox, FMX.Dialogs, FMX.TabControl, FMXTee.Series,
  FMXTee.Editor.Series.Circled, FMX.Layouts,
  FMXTee.Editor.Stroke, FMXTee.Editor.Gradient;

type
  TPieSeriesEditor = class(TForm)
    PageControl1: TTabControl;
    TabOptions: TTabItem;
    Label4: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    LTransparency: TLabel;
    LEdgeStyle: TLabel;
    Label11: TLabel;
    CBDark3d: TCheckBox;
    UDExpBig: TSpinBox;
    CBPatterns: TCheckBox;
    UDAngleSize: TSpinBox;
    CBMultiple: TComboBox;
    CBEdgeStyle: TComboBox;
    TabGroup: TTabItem;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    CBOther: TComboBox;
    EOtherValue: TEdit;
    EOtherLabel: TEdit;
    Button2: TButton;
    TabSheet1: TTabItem;
    Label9: TLabel;
    CBColorEach: TCheckBox;
    BColor: TButton;
    CBPalette: TComboBox;
    TabSheet2: TTabItem;
    Label8: TLabel;
    LLegSize: TLabel;
    CBMarksAutoPosition: TCheckBox;
    CBVertCenter: TCheckBox;
    SBLegSize: TScrollBar;
    TabGradient: TTabItem;
    TabItem1: TTabItem;
    CBMarksInside: TCheckBox;
    TBTransp: TTrackBar;
    LTransp: TLabel;
    TabControl1: TTabControl;
    TabBevel: TTabItem;
    TabBorder: TTabItem;
    LBevelPercent: TLabel;
    UDBevelPercent: TSpinBox;
    Label12: TLabel;
    TBBevelBright: TTrackBar;
    LBevelBright: TLabel;
    CBBevelBorder: TCheckBox;
    Label1: TLabel;
    UDDarkPen: TSpinBox;
    CBRotated: TCheckBox;
    CBMarksEmpty: TCheckBox;
    Layout1: TLayout;
    Label10: TLabel;
    TBBright: TTrackBar;
    LBright: TLabel;
    TabFrame: TTabItem;
    procedure PageControl1Change(Sender: TObject);
    procedure CBMultipleChange(Sender: TObject);
    procedure CBEdgeStyleChange(Sender: TObject);
    procedure CBOtherChange(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure SBLegSizeChange(Sender: TObject);
    procedure TBBrightChange(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure CBDark3dChange(Sender: TObject);
    procedure CBPatternsChange(Sender: TObject);
    procedure CBColorEachChange(Sender: TObject);
    procedure CBMarksAutoPositionChange(Sender: TObject);
    procedure CBVertCenterChange(Sender: TObject);
    procedure BColorClick(Sender: TObject);
    procedure UDBevelPercentChange(Sender: TObject);
    procedure UDExpBigChange(Sender: TObject);
    procedure UDAngleSizeChange(Sender: TObject);
    procedure UDDarkPenChange(Sender: TObject);
    procedure CBPaletteChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure EOtherValueTyping(Sender: TObject);
    procedure EOtherLabelTyping(Sender: TObject);
    procedure TBBevelBrightChange(Sender: TObject);
    procedure CBBevelBorderChange(Sender: TObject);
    procedure CBMarksInsideChange(Sender: TObject);
    procedure TBTranspChange(Sender: TObject);
    procedure CBRotatedChange(Sender: TObject);
    procedure CBMarksEmptyChange(Sender: TObject);
    procedure TabControl1Change(Sender: TObject);
  private
   { Private declarations }
    Pie : TPieSeries;

    IBorder : TStrokeEditor;
    IGradient : TGradientEditor;

    //IFrame : TFramedBorderEditor;

    FCircledEditor : TCircledSeriesEditor;

    procedure RefreshPie(const APie:TPieSeries);
  public
   { Public declarations }
    class function CreateEditor(const AOwner:TComponent; const APie:TPieSeries):TForm;
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Constants, FMXTee.Canvas, FMXTee.Chart,
  FMXTee.Procs, FMXTee.Editor.Legend;

class function TPieSeriesEditor.CreateEditor(const AOwner: TComponent;
  const APie:TPieSeries): TForm;
begin
  result:=TPieSeriesEditor.Create(AOwner);
  TPieSeriesEditor(result).RefreshPie(APie);
end;

procedure TPieSeriesEditor.FormActivate(Sender: TObject);
begin
  if not Assigned(Pie) then
     RefreshPie(TPieSeries(TagObject));
end;

procedure TPieSeriesEditor.FormCreate(Sender: TObject);
begin
  PageControl1.ActiveTab:=TabOptions;
end;

procedure TPieSeriesEditor.PageControl1Change(Sender: TObject);
begin
  if (PageControl1.ActiveTab=TabGradient) and (not Assigned(IGradient)) then
     IGradient:=TGradientEditor.InsertForm(Pie.Gradient,TabGradient)
  {
  // Pending Circular Frame editor:
  else
  if (PageControl1.ActiveTab=TabFrame) and (not Assigned(IFrame))) then
     IFrame:=TFramedBorderEditor.InsertForm(Pie.Frame,TabFrame);
  }
end;

procedure TPieSeriesEditor.RefreshPie(const APie: TPieSeries);
var TabCircled : TTabItem;
begin
  Pie:=APie;

  UDExpBig.Value:=Pie.ExplodeBiggest;
  UDAngleSize.Value:=Pie.AngleSize;
  UDDarkPen.Value:=Pie.DarkPen;

  TBTransp.Value:=Pie.Transparency;
  LTransp.Text:=IntToStr(Pie.Transparency);

  UDBevelPercent.Value:=Pie.BevelPercent;

  CBDark3d.IsChecked:=Pie.Dark3D;
  CBPatterns.IsChecked:=Pie.UsePatterns;
  CBMultiple.ItemIndex:=Ord(Pie.MultiPie);

  CBOther.ItemIndex:=Ord(Pie.OtherSlice.Style);
  EOtherLabel.Text:=Pie.OtherSlice.Text;
  EOtherValue.Text:=FloatToStr(Pie.OtherSlice.Value);

  CBColorEach.IsChecked:=Pie.ColorEachPoint;

  CBPalette.Clear;
  ColorPalettes.AddTo(CBPalette.ListBox.Items,True);

  CBPalette.Enabled:=Assigned(Pie.ParentChart);

  if Assigned(Pie.ParentChart) then
     CBPalette.ItemIndex:=(Pie.ParentChart as TCustomChart).ColorPaletteIndex+1;

  CBMarksAutoPosition.IsChecked:=Pie.AutoMarkPosition;
  CBVertCenter.IsChecked:=Pie.PieMarks.VertCenter;
  CBMarksInside.IsChecked:=Pie.PieMarks.InsideSlice;

  CBRotated.IsChecked:=Pie.PieMarks.Rotated;
  CBMarksEmpty.IsChecked:=Pie.PieMarks.EmptySlice;

  SBLegSize.Value:=Pie.PieMarks.LegSize;
  LLegSize.Text:=TeeStr(Round(SBLegSize.Value));

  TBBright.Value:=Pie.GradientBright;
  LBright.Text:=IntToStr(Pie.GradientBright);

  TBBevelBright.Value:=Pie.Bevel.Bright;
  LBevelBright.Text:=IntToStr(Pie.Bevel.Bright);

  CBBevelBorder.IsChecked:=Pie.Bevel.UseBorder;

  case Pie.EdgeStyle of
    edNone:   CBEdgeStyle.ItemIndex:=0;
    edFlat:   CBEdgeStyle.ItemIndex:=1;
    edCurved: CBEdgeStyle.ItemIndex:=2;
  end;

  if not Assigned(FCircledEditor) then
  begin
    FCircledEditor:=TCircledSeriesEditor.Create(Self);

    TabCircled:=TTabItem.Create(Self);
    TabCircled.Text:=TeeMsg_GalleryCircled;
    PageControl1.AddObject(TabCircled);

    EmbeddForm(FCircledEditor,TabCircled);

    TabCircled.Index:=1;
  end;

  FCircledEditor.RefreshCircled(Pie);
end;

procedure TPieSeriesEditor.CBDark3dChange(Sender: TObject);
begin
  Pie.Dark3D:=CBDark3d.IsChecked;
end;

procedure TPieSeriesEditor.CBPaletteChange(Sender: TObject);
begin
  TCustomChart(Pie.ParentChart).ColorPaletteIndex:=CBPalette.ItemIndex-1;
end;

procedure TPieSeriesEditor.CBPatternsChange(Sender: TObject);
begin
  Pie.UsePatterns:=CBPatterns.IsChecked;
end;

procedure TPieSeriesEditor.CBRotatedChange(Sender: TObject);
begin
  Pie.PieMarks.Rotated:=CBRotated.IsChecked;
end;

procedure TPieSeriesEditor.CBMultipleChange(Sender: TObject);
begin
  Pie.MultiPie:=TMultiPie(CBMultiple.ItemIndex);
end;

procedure TPieSeriesEditor.CBEdgeStyleChange(Sender: TObject);
begin
  case CBEdgeStyle.ItemIndex of
    0: Pie.EdgeStyle:=edNone;
    1: Pie.EdgeStyle:=edFlat;
    2: Pie.EdgeStyle:=edCurved;
  end;
end;

procedure TPieSeriesEditor.CBOtherChange(Sender: TObject);
begin
  Pie.OtherSlice.Style:=TPieOtherStyle(CBOther.ItemIndex);
end;

procedure TPieSeriesEditor.EOtherLabelTyping(Sender: TObject);
begin
  Pie.OtherSlice.Text:=EOtherLabel.Text;
end;

procedure TPieSeriesEditor.EOtherValueTyping(Sender: TObject);
begin
  Pie.OtherSlice.Value:=StrToFloat(EOtherValue.Text);
end;

procedure TPieSeriesEditor.BColorClick(Sender: TObject);
begin
  Pie.Color:=EditColor(Self, Pie.Color);
end;

procedure TPieSeriesEditor.Button2Click(Sender: TObject);
begin
  with TLegendEditor.CreateEditor(Self, Pie.OtherSlice.Legend) do
  try
    ShowModal;
  finally
    Free;
  end;
end;

procedure TPieSeriesEditor.CBBevelBorderChange(Sender: TObject);
begin
  Pie.Bevel.UseBorder:=CBBevelBorder.IsChecked;
end;

procedure TPieSeriesEditor.CBColorEachChange(Sender: TObject);
begin
  Pie.ColorEachPoint:=CBColorEach.IsChecked;
end;

procedure TPieSeriesEditor.CBMarksAutoPositionChange(Sender: TObject);
begin
  Pie.AutoMarkPosition:=CBMarksAutoPosition.IsChecked;
end;

procedure TPieSeriesEditor.CBMarksEmptyChange(Sender: TObject);
begin
  Pie.PieMarks.EmptySlice:=CBMarksEmpty.IsChecked;
end;

procedure TPieSeriesEditor.CBMarksInsideChange(Sender: TObject);
begin
  Pie.PieMarks.InsideSlice:=CBMarksInside.IsChecked;
end;

procedure TPieSeriesEditor.CBVertCenterChange(Sender: TObject);
begin
  Pie.PieMarks.VertCenter:=CBVertCenter.IsChecked;
end;

procedure TPieSeriesEditor.SBLegSizeChange(Sender: TObject);
begin
  Pie.PieMarks.LegSize:=Round(SBLegSize.Value);
  LLegSize.Text:=IntToStr(Pie.PieMarks.LegSize);
end;

procedure TPieSeriesEditor.TabControl1Change(Sender: TObject);
begin
  if (TabControl1.ActiveTab=TabBorder) and (not Assigned(IBorder)) then
     IBorder:=TStrokeEditor.InsertForm(Pie.PiePen,TabBorder);
end;

procedure TPieSeriesEditor.TBBevelBrightChange(Sender: TObject);
begin
  Pie.Bevel.Bright:=Round(TBBevelBright.Value);
  LBevelBright.Text:=IntToStr(Pie.Bevel.Bright);
end;

procedure TPieSeriesEditor.TBBrightChange(Sender: TObject);
begin
  Pie.GradientBright:=Round(TBBright.Value);
end;

procedure TPieSeriesEditor.TBTranspChange(Sender: TObject);
begin
  Pie.Transparency:=Round(TBTransp.Value);
end;

procedure TPieSeriesEditor.UDAngleSizeChange(Sender: TObject);
begin
  Pie.AngleSize:=Round(UDAngleSize.Value);
end;

procedure TPieSeriesEditor.UDBevelPercentChange(Sender: TObject);
begin
  Pie.BevelPercent:=Round(UDBevelPercent.Value);
end;

procedure TPieSeriesEditor.UDExpBigChange(Sender: TObject);
begin
  Pie.ExplodeBiggest:=Round(UDExpBig.Value);
end;

procedure TPieSeriesEditor.UDDarkPenChange(Sender: TObject);
begin
  Pie.DarkPen:=Round(UDDarkPen.Value);
end;

initialization
  RegisterSeriesEditor(TPieSeriesEditor);
end.
