{********************************************}
{    TPolarSeries Editor Dialog              }
{ Copyright (c) 1996-2020 by Steema Software }
{********************************************}
unit TeePolarEditor;
{$I TeeDefs.inc}

interface

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

     {$IFDEF D16}
     System.UITypes,
     {$ENDIF}

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

     Chart, Series, TeePolar, TeEngine, TeCanvas, TeePenDlg, TeeProcs,
     TeeEdiFont, TeeBrushDlg;

type
  TPolarSeriesEditor = class(TForm)
    PageControl1: TPageControl;
    TabOptions: TTabSheet;
    TabSheet2: TTabSheet;
    LAngleInc: TLabel;
    Label8: TLabel;
    Label1: TLabel;
    CBClose: TCheckBox;
    SEAngleInc: TEdit;
    SERadiusInc: TEdit;
    BColor: TButtonColor;
    CBColorEach: TCheckBox;
    Label3: TLabel;
    CBNulls: TComboFlat;
    TabBack: TTabSheet;
    TabBorder: TTabSheet;
    TBTransp: TTrackBar;
    LTransp: TLabel;
    TabPen: TTabSheet;
    TabPattern: TTabSheet;
    CBDefColor: TCheckBox;
    PageControl3: TPageControl;
    TabSheet3: TTabSheet;
    TabLabelsFormat: TTabSheet;
    Label2: TLabel;
    CBAngleLabels: TCheckBox;
    CBLabelsRot: TCheckBox;
    CBClockWise: TCheckBox;
    CBInside: TCheckBox;
    EMargin: TEdit;
    UDMargin: TUpDown;
    CBAntiOverlap: TCheckBox;
    CBEachLine: TCheckBox;
    CBPointerBehind: TCheckBox;
    LDrawStyle: TLabel;
    CBDrawStyle: TComboFlat;
    procedure FormShow(Sender: TObject);
    procedure CBCloseClick(Sender: TObject);
    procedure SEAngleIncChange(Sender: TObject);
    procedure SERadiusIncChange(Sender: TObject);
    procedure CBAngleLabelsClick(Sender: TObject);
    procedure CBLabelsRotClick(Sender: TObject);
    procedure CBClockWiseClick(Sender: TObject);
    procedure CBInsideClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CBColorEachClick(Sender: TObject);
    procedure EMarginChange(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure CBNullsChange(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure CBAntiOverlapClick(Sender: TObject);
    procedure TBTranspChange(Sender: TObject);
    procedure CBDefColorClick(Sender: TObject);
    procedure BColorClick(Sender: TObject);
    procedure PageControl3Change(Sender: TObject);
    procedure CBEachLineClick(Sender: TObject);
    procedure CBPointerBehindClick(Sender: TObject);
    procedure CBDrawStyleChange(Sender: TObject);
  private
    { Private declarations }
    PointerForm  : TCustomForm;
    FCircledForm : TCustomForm;

    procedure EnableLabels;
  protected
    Polar : TCustomPolarSeries;

    function CreatePolar:TCustomPolarSeries; virtual;
  public
    { Public declarations }
  end;

implementation

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

Uses
  TeePoEdi, TeeEdiSeri, TeeCircledEdit, TeeConst, TeeCustomShapeEditor;

procedure TPolarSeriesEditor.FormShow(Sender: TObject);
begin
  Polar:=CreatePolar;

  if Assigned(Polar) then
  begin
    With Polar do
    begin
      CBClose.Checked       :=CloseCircle;

      SEAngleInc.Text       :=FloatToStr(AngleIncrement);
      SERadiusInc.Text      :=FloatToStr(RadiusIncrement);

      UDMargin.Position     :=AngleLabels.Margin;
      CBClockWise.Checked   :=AngleLabels.ClockWise;
      CBAngleLabels.Checked :=AngleLabels.Visible;
      CBLabelsRot.Checked   :=AngleLabels.Rotated;
      CBInside.Checked      :=AngleLabels.Inside;
      CBAntiOverlap.Checked :=AngleLabels.AntiOverlap;

      TBTransp.Position     :=Transparency;
      TBTranspChange(Self);

      CBColorEach.Checked   :=ColorEachPoint;
      CBEachLine.Checked    :=ColorEachLine;
      CBEachLine.Enabled    :=ColorEachPoint;

      CBPointerBehind.Checked:=PointerBehind;

      CBNulls.ItemIndex     :=Ord(TreatNulls);

      CBDrawStyle.ItemIndex :=Ord(DrawStyle);
    end;

    BColor.LinkProperty(Polar,'SeriesColor');

    BColorClick(Self);

    EnableLabels;

    if not Assigned(FCircledForm) then
       FCircledForm:=TCircledSeriesEditor.CreateForm(Self,Polar);

    if not Assigned(PointerForm) then
       PointerForm:=TeeInsertPointerForm(Parent,Polar.Pointer,TeeMsg_GalleryPoint);
  end;
end;

procedure TPolarSeriesEditor.CBCloseClick(Sender: TObject);
begin
  Polar.CloseCircle:=CBClose.Checked;
end;

procedure TPolarSeriesEditor.SEAngleIncChange(Sender: TObject);
begin
  if Showing then
     Polar.AngleIncrement:=StrToFloatDef(SEAngleInc.Text,Polar.AngleIncrement);
end;

procedure TPolarSeriesEditor.SERadiusIncChange(Sender: TObject);
begin
  if Showing then
     Polar.RadiusIncrement:=StrToFloatDef(SERadiusInc.Text,Polar.RadiusIncrement);
end;

procedure TPolarSeriesEditor.CBAngleLabelsClick(Sender: TObject);
begin
  Polar.AngleLabels.Visible:=CBAngleLabels.Checked;
  EnableLabels;
end;

procedure TPolarSeriesEditor.EnableLabels;
begin
  EnableControls(CBAngleLabels.Checked,[CBLabelsRot,CBClockWise])
end;

procedure TPolarSeriesEditor.CBLabelsRotClick(Sender: TObject);
begin
  Polar.AngleLabels.Rotated:=CBLabelsRot.Checked;
end;

procedure TPolarSeriesEditor.CBClockWiseClick(Sender: TObject);
begin
  Polar.AngleLabels.ClockWise:=CBClockWise.Checked;
end;

procedure TPolarSeriesEditor.CBInsideClick(Sender: TObject);
begin
  Polar.AngleLabels.Inside:=CBInside.Checked;
end;

procedure TPolarSeriesEditor.FormCreate(Sender: TObject);
begin
  Align:=alClient;
  PageControl1.ActivePage:=TabOptions;
end;

procedure TPolarSeriesEditor.CBColorEachClick(Sender: TObject);
begin
  Polar.ColorEachPoint:=CBColorEach.Checked;
  CBEachLine.Enabled:=Polar.ColorEachPoint;
end;

procedure TPolarSeriesEditor.EMarginChange(Sender: TObject);
begin
  if Showing and Assigned(Polar) then
     Polar.AngleLabels.Margin:=UDMargin.Position;
end;

procedure TPolarSeriesEditor.FormDestroy(Sender: TObject);
begin
  FreeAndNil(PointerForm);
  FreeAndNil(FCircledForm);
end;

procedure TPolarSeriesEditor.CBNullsChange(Sender: TObject);
begin
  Polar.TreatNulls:=TTreatNullsStyle(CBNulls.ItemIndex);
end;

function TPolarSeriesEditor.CreatePolar: TCustomPolarSeries;
begin
  result:=TCustomPolarSeries(Tag);
end;

procedure TPolarSeriesEditor.PageControl1Change(Sender: TObject);
begin
  if (PageControl1.ActivePage=TabBack) and (TabBack.ControlCount=0) then
     TBrushDialog.AddDialog(Polar.CircleBrush,TabBack)
  else
  if (PageControl1.ActivePage=TabBorder) and (TabBorder.ControlCount=0) then
     TPenDialog.InsertForm(Polar.CirclePen,TabBorder)
  else
  if (PageControl1.ActivePage=TabPen) and (TabPen.ControlCount=0) then
     TPenDialog.InsertForm(Polar.Pen,TabPen)
  else
  if (PageControl1.ActivePage=TabPattern) and (TabPattern.ControlCount=0) then
     TBrushDialog.AddDialog(Polar.Brush,TabPattern);
end;

procedure TPolarSeriesEditor.CBAntiOverlapClick(Sender: TObject);
begin
  Polar.AngleLabels.AntiOverlap:=CBAntiOverlap.Checked;
end;

procedure TPolarSeriesEditor.TBTranspChange(Sender: TObject);
begin
  if Showing then
     Polar.Transparency:=TBTransp.Position;

  LTransp.Caption:=IntToStr(Polar.Transparency)+' %';
end;

type
  TSeriesAccess=class(TChartSeries);

procedure TPolarSeriesEditor.CBDefColorClick(Sender: TObject);
begin
  if CBDefColor.Checked then
  begin
    TSeriesAccess(Polar).SetDefaultColor;
    BColor.Invalidate;
    CBDefColor.Enabled:=False;
  end;
end;

procedure TPolarSeriesEditor.BColorClick(Sender: TObject);
begin
  CBDefColor.Checked:=not TSeriesAccess(Polar).IsColorStored;
  CBDefColor.Enabled:=not CBDefColor.Checked;
end;

procedure TPolarSeriesEditor.PageControl3Change(Sender: TObject);
begin
  if (PageControl3.ActivePage=TabLabelsFormat) and (TabLabelsFormat.ControlCount=0) then
     TFormTeeShape.CreateForm(Self,Polar.AngleLabels,TabLabelsFormat);
end;

procedure TPolarSeriesEditor.CBEachLineClick(Sender: TObject);
begin
  Polar.ColorEachLine:=CBEachLine.Checked;
end;

procedure TPolarSeriesEditor.CBPointerBehindClick(Sender: TObject);
begin
  Polar.PointerBehind:=CBPointerBehind.Checked;
end;

procedure TPolarSeriesEditor.CBDrawStyleChange(Sender: TObject);
begin
  if Showing then
  case CBDrawStyle.ItemIndex of
    0: Polar.DrawStyle:=dsSegments;
    1: Polar.DrawStyle:=dsAll;
  else
    Polar.DrawStyle:=dsCurve;
  end;
end;

initialization
  RegisterClass(TPolarSeriesEditor);
end.
