{********************************************}
{ TeeChart Circular Gauge style              }
{ Copyright (c) 2000-2020 by Steema Software }
{********************************************}
unit TeeCircularGaugeEditor;
{$I TeeDefs.inc}

interface

uses
  {$IFNDEF LINUX}
  Windows, Messages,
  {$ENDIF}
  SysUtils, Classes, Math,
  {$IFDEF D10}
  Types,
  {$ENDIF}

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

  TeeLinearGauge, TeEngine, TeCanvas, TeeEdiGrad, TeeNumericGauge,
  TeePenDlg, Series, TeeLinearGaugeEditor, TeeCircularGauge, TeePoEdi;

type
  TCircularGaugeEditor = class(TLinearGaugeEditor)
    Label15: TLabel;
    Label16: TLabel;
    EDisplayRotationAngle: TEdit;
    EDisplayTotalAngle: TEdit;
    UDDisplayRotationAngle: TUpDown;
    UDDisplayTotalAngle: TUpDown;
    TabCircularFrame: TTabSheet;
    TabCenter: TTabSheet;
    TabFace2: TTabSheet;
    TabEndPoint: TTabSheet;
    CBCircled: TCheckBox;
    CBRotateLabels: TCheckBox;
    CBLabelsInside: TCheckBox;
    LBHands: TListBox;
    PanelLeftHands: TPanel;
    PanelTopHands: TPanel;
    BAddHand: TSpeedButton;
    BDeleteHand: TSpeedButton;
    PanelHands: TPanel;
    PanelHand: TPanel;
    LabelHand: TLabel;
    EHandValue: TEdit;
    Label10: TLabel;
    EHandDistance: TEdit;
    UDHandDistance: TUpDown;
    Label21: TLabel;
    EHandOffset: TEdit;
    UDHandOffset: TUpDown;
    CBHandStyle: TComboFlat;
    LRotationAngle: TLabel;
    Edit4: TEdit;
    UDRotAngle: TUpDown;
    ETotalAngle: TEdit;
    UDTotalAngle: TUpDown;
    LTotalAngle: TLabel;
    PanelClock: TPanel;
    Label11: TLabel;
    ComboClock: TComboFlat;
    CBSnap: TCheckBox;
    procedure FormShow(Sender: TObject);
    procedure CBLabelsInsideClick(Sender: TObject);
    procedure CBRotateLabelsClick(Sender: TObject);
    procedure ETotalAngleChange(Sender: TObject);
    procedure CBCircledClick(Sender: TObject);
    procedure Edit4Change(Sender: TObject);
    procedure EDisplayRotationAngleChange(Sender: TObject);
    procedure EDisplayTotalAngleChange(Sender: TObject);
    procedure SetColor(const aColor: TColor);
    procedure PageControl2Change(Sender: TObject);
    procedure LBHandsClick(Sender: TObject);
    procedure EHandValueChange(Sender: TObject);
    procedure BAddHandClick(Sender: TObject);
    procedure BDeleteRangeClick(Sender: TObject);
    procedure EHandDistanceChange(Sender: TObject);
    procedure EHandOffsetChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CBHandStyleChange(Sender: TObject);
    procedure ComboClockChange(Sender: TObject);
    procedure CBSnapClick(Sender: TObject);
  private
    { Private declarations }

    IHandEditor : TSeriesPointerEditor;

    function CircularGauge:TCircularGauge;
  public
    { Public declarations }
  end;

implementation

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

uses
  Chart, TeeProCo, TeeProcs, TeeBrushDlg, TeeNumericGaugeEditor;

procedure TCircularGaugeEditor.FormShow(Sender: TObject);
var t : Integer;
begin
  inherited;

  if Gauge is TCircularGauge then
    with TCircularGauge(Gauge) do
    begin
      UDTotalAngle.Position := Round(TotalAngle);
      UDRotAngle.Position := Round(RotationAngle);
      CBLabelsInside.Checked := LabelsInside;
      CBRotateLabels.Checked := RotateLabels;
      CBCircled.Checked := Circled;
      UDDisplayRotationAngle.Position := DisplayRotationAngle;
      UDDisplayTotalAngle.Position := DisplayTotalAngle;

      LBHands.Items.Clear;

      for t:=0 to Hands.Count-1 do
          LBHands.Items.Add(IntToStr(t));
    end;

  if LBHands.Items.Count>0 then
  begin
    LBHands.ItemIndex:=0;
    LBHandsClick(Self);
  end;

  if Gauge is TClockGauge then
  begin
    ComboClock.ItemIndex:=Ord(TClockGauge(Gauge).Style);
    CBSnap.Checked:=TClockGauge(Gauge).SnapToSeconds;

    Panel2.Hide;
    RGFont.Visible:=False;
  end
  else
    PanelClock.Hide;
end;

function TCircularGaugeEditor.CircularGauge:TCircularGauge;
begin
  result:=Gauge as TCircularGauge;
end;

procedure TCircularGaugeEditor.CBLabelsInsideClick(Sender: TObject);
begin
  CircularGauge.LabelsInside := CBLabelsInside.Checked;
end;

procedure TCircularGaugeEditor.CBRotateLabelsClick(Sender: TObject);
begin
  CircularGauge.RotateLabels := CBRotateLabels.Checked;
end;

procedure TCircularGaugeEditor.ETotalAngleChange(Sender: TObject);
begin
  if Showing then
     CircularGauge.TotalAngle := UDTotalAngle.Position;
end;

procedure TCircularGaugeEditor.CBCircledClick(Sender: TObject);
begin
  CircularGauge.Circled := CBCircled.Checked;
end;

procedure TCircularGaugeEditor.Edit4Change(Sender: TObject);
begin
  if Showing then
     CircularGauge.RotationAngle := UDRotAngle.Position;
end;

procedure TCircularGaugeEditor.EDisplayRotationAngleChange(Sender: TObject);
begin
  if Showing then
    if Gauge is TCircularGauge then
       TCircularGauge(Gauge).DisplayRotationAngle:=UDDisplayRotationAngle.Position;
end;

procedure TCircularGaugeEditor.EDisplayTotalAngleChange(Sender: TObject);
begin
  if Showing then
    if Gauge is TCircularGauge then
       TCircularGauge(Gauge).DisplayTotalAngle:=UDDisplayTotalAngle.Position;
end;

procedure TCircularGaugeEditor.SetColor(const aColor: TColor);
Begin
  TCircularGauge(Gauge).Face.Color:=aColor;
end;

procedure TCircularGaugeEditor.PageControl2Change(Sender: TObject);
begin
  inherited;

  if (PageControl2.ActivePage=TabCenter) and (TabCenter.ControlCount=0) then
     TSeriesPointerEditor.InsertForm(CircularGauge.Center, TabCenter)
  else
  if (PageControl2.ActivePage=TabEndPoint) and (TabEndPoint.ControlCount=0) then
     TSeriesPointerEditor.InsertForm(CircularGauge.EndPoint, TabEndPoint)
  else
  if (PageControl2.ActivePage=TabFace2) and (TabFace2.ControlCount=0) then
     TBrushDialog.AddDialog(TCircularGauge(Gauge).Face.Brush, TabFace2);
end;

procedure TCircularGaugeEditor.LBHandsClick(Sender: TObject);
var tmp : TGaugeHand;
    tmpIndex : Integer;
begin
  if Assigned(IHandEditor) then
     tmpIndex:=IHandEditor.PageControl1.ActivePageIndex
  else
     tmpIndex:=0;

  FreeAndNil(IHandEditor);

  if LBHands.ItemIndex<>-1 then
  begin
    tmp:=CircularGauge.Hands[LBHands.ItemIndex].Format;

    IHandEditor:=TSeriesPointerEditor.InsertForm(tmp,PanelHands);

    EHandValue.Text:=FormatFloat('0.####',tmp.Value);

    UDHandDistance.Position:=tmp.Distance;
    UDHandOffset.Position:=tmp.Offset;

    case tmp.Style of
      psRectangle: CBHandStyle.ItemIndex:=0;
       psTriangle: CBHandStyle.ItemIndex:=1;
    else
      CBHandStyle.ItemIndex:=2;
    end;

    IHandEditor.TabStyle.TabVisible:=False;

    if tmpIndex>0 then
    begin
      IHandEditor.PageControl1.ActivePageIndex:=tmpIndex;
      IHandEditor.PageControl1Change(IHandEditor);
    end;
  end;

  BDeleteHand.Enabled:=LBHands.ItemIndex>0;
  PanelHand.Enabled:=Assigned(IHandEditor);
end;

procedure TCircularGaugeEditor.EHandValueChange(Sender: TObject);
var tmp : Double;
    tmpIndex : Integer;
begin
  if Showing and (LBHands.ItemIndex<>-1) then
     if TryStrToFloat(EHandValue.Text,tmp) then
     begin
       tmpIndex:=LBHands.ItemIndex;
       CircularGauge.Hands[tmpIndex].Value:=tmp;

       if tmpIndex=0 then
          CircularGauge.Value:=tmp;
     end;
end;

procedure TCircularGaugeEditor.BAddHandClick(Sender: TObject);
begin
  CircularGauge.Hands.Add(0);
  LBHands.Items.Add(IntToStr(CircularGauge.Hands.Count-1));
  LBHands.ItemIndex:=CircularGauge.Hands.Count-1;
  LBHandsClick(Self);
end;

procedure TCircularGaugeEditor.BDeleteRangeClick(Sender: TObject);
var tmp : Integer;
begin
  tmp:=LBHands.ItemIndex;
  CircularGauge.Hands.Delete(tmp);
  LBHands.Items.Delete(tmp);

  if tmp>LBHands.Items.Count-1 then
     Dec(tmp);

  LBHands.ItemIndex:=tmp;
  LBHandsClick(Self);
end;

procedure TCircularGaugeEditor.EHandDistanceChange(Sender: TObject);
begin
  if Showing and (LBHands.ItemIndex<>-1) then
     CircularGauge.Hands[LBHands.ItemIndex].Format.Distance:=UDHandDistance.Position;
end;

procedure TCircularGaugeEditor.EHandOffsetChange(Sender: TObject);
begin
  if Showing and (LBHands.ItemIndex<>-1) then
     CircularGauge.Hands[LBHands.ItemIndex].Format.Offset:=UDHandOffset.Position;
end;

procedure TCircularGaugeEditor.FormCreate(Sender: TObject);
begin
  inherited;
  PageControl2.ActivePage:=TabSheet2;
end;

procedure TCircularGaugeEditor.CBHandStyleChange(Sender: TObject);
begin
  if Showing and (LBHands.ItemIndex<>-1) then
     with CircularGauge.Hands[LBHands.ItemIndex].Format do
          case CBHandStyle.ItemIndex of
             0: Style:=psRectangle;
             1: Style:=psTriangle;
          else
             Style:=psArrow;
          end;
end;

procedure TCircularGaugeEditor.ComboClockChange(Sender: TObject);
begin
  TClockGauge(Gauge).Style:=TClockGaugeStyle(ComboClock.ItemIndex);
end;

procedure TCircularGaugeEditor.CBSnapClick(Sender: TObject);
begin
  TClockGauge(Gauge).SnapToSeconds:=CBSnap.Checked;
end;

initialization
  RegisterClass(TCircularGaugeEditor);
end.
