{**********************************************}
{   TeeChart Linear Gauge style                }
{   Copyright (c) 2011-2020 by Steema Software }
{**********************************************}
unit TeeLinearGaugeEditor;
{$I TeeDefs.inc}

interface

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

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

  TeeNumericGauge, TeCanvas, TeeCustomShapeEditor, Chart, TeeProCo,
  TeeProcs, TeEngine, TeePenDlg, TeeEdiGrad, TeeNumericGaugeEditor,
  TeeLinearGauge, TeePoEdi;

type
  TLinearGaugeEditor = class(TNumericGaugeEditor)
    TabAxis: TTabSheet;
    TabRanges: TTabSheet;
    PageControl3: TPageControl;
    TabTicks: TTabSheet;
    TabLabels: TTabSheet;
    TabTitle: TTabSheet;
    PanelRanges: TPanel;
    Panel3: TPanel;
    Label6: TLabel;
    EFormat: TEdit;
    CBLabels: TCheckBox;
    TabArea: TTabSheet;
    TabMax: TTabSheet;
    TabHand: TTabSheet;
    TabAxisPen: TTabSheet;
    TabMinor: TTabSheet;
    Panel4: TPanel;
    EMinor: TEdit;
    UDMinorCount: TUpDown;
    EMinLength: TEdit;
    UDMinorSize: TUpDown;
    Label3: TLabel;
    Label18: TLabel;
    TabMinMax: TTabSheet;
    Label4: TLabel;
    Label5: TLabel;
    Label17: TLabel;
    EMax: TEdit;
    EMin: TEdit;
    EIncr: TEdit;
    Panel5: TPanel;
    ETicks: TEdit;
    UDTicks: TUpDown;
    ETicksDist: TEdit;
    UDTicksDist: TUpDown;
    Label19: TLabel;
    Label20: TLabel;
    PanelRange: TPanel;
    LBRanges: TListBox;
    Panel6: TPanel;
    PanelRangeValues: TPanel;
    Label8: TLabel;
    Label9: TLabel;
    EStartRange: TEdit;
    EEndRange: TEdit;
    UDStartRange: TUpDown;
    UDEndRange: TUpDown;
    SBAddRange: TSpeedButton;
    BDeleteRange: TSpeedButton;
    CBSpiralled: TCheckBox;
    Label12: TLabel;
    ERangePos: TEdit;
    UDRangePos: TUpDown;
    Panel2: TPanel;
    Label7: TLabel;
    EAxisTitle: TEdit;
    CBUseColorPalette: TCheckBox;
    procedure FormShow(Sender: TObject);
    procedure CBLabelsClick(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure EFormatChange(Sender: TObject);
    procedure EStartRangeChange(Sender: TObject);
    procedure EEndRangeChange(Sender: TObject);
    procedure EMinChange(Sender: TObject);
    procedure EMaxChange(Sender: TObject);
    procedure EMinLengthChange(Sender: TObject);
    procedure EMinorChange(Sender: TObject);
    procedure ETicksChange(Sender: TObject);
    procedure ETicksDistChange(Sender: TObject);
    procedure EIncrChange(Sender: TObject);
    procedure PageControl3Change(Sender: TObject);
    procedure PageControl2Change(Sender: TObject);
    procedure LBRangesClick(Sender: TObject);
    procedure SBAddRangeClick(Sender: TObject);
    procedure BDeleteRangeClick(Sender: TObject);
    procedure CBSpiralledClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ERangePosChange(Sender: TObject);
    procedure EAxisTitleChange(Sender: TObject);
    procedure CBUseColorPaletteClick(Sender: TObject);
  private
    { Private declarations }

    IRangeEditor : TSeriesPointerEditor;

    function CustomGauge:TCustomGaugeMinMax;
  public
    { Public declarations }
  end;

implementation

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

uses
  Math, TeeEdiFont, TeeEdiAxis, TeeConst;

function TLinearGaugeEditor.CustomGauge:TCustomGaugeMinMax;
begin
  result:=TCustomGaugeMinMax(Gauge);
end;

procedure TLinearGaugeEditor.FormShow(Sender: TObject);

  procedure AddAxisEditor(const Axis:TChartAxis);
  var tmp : TFormTeeAxis;
  begin
    tmp:=TFormTeeAxis.CreateAxis(Self,Axis);
    tmp.PageTitle.Parent:=TabTitle;
    tmp.PageAxis.ActivePage:=tmp.TabTitle;
    tmp.PageAxisChange(tmp);

    TeeTranslateControl(tmp);
  end;

var t : Integer;
begin
  inherited;

  if Assigned(Gauge) then
  with CustomGauge do
  begin
    EMax.Text:=FloatToStr(Maximum);
    EMin.Text:=FloatToStr(Minimum);
    EFormat.Text:=ValueFormat;

    if Axis<>nil then
    begin
      EIncr.Text:=FloatToStr(Axis.Increment);
      CBLabels.Checked:=Axis.Labels;

      AddAxisEditor(Axis);

      UDMinorCount.Position:=Axis.MinorTickCount;
    end;

    UDMinorSize.Position:=MinorTicks.Size;
    UDTicks.Position:=Ticks.VertSize;
    UDTicksDist.Position:=MinorTickDistance;

    LBRanges.Items.Clear;

    for t:=0 to CustomGauge.Ranges.Count-1 do
        LBRanges.Items.Add(IntToStr(t));
  end;

  if Assigned(Gauge) then
     if (Gauge is TLinearGauge) and (Gauge.GetHorizAxis<>nil) then
	 begin
        EAxisTitle.Text:=Gauge.GetHorizAxis.Title.Caption;
        PanelDigitalFont.Visible:=False;
	 end
     else
     begin
       TabArea.TabVisible:=False;
       TabArea.Hide;
       TabMax.TabVisible:=False;
       TabMax.Hide;
     end;

  CBUseColorPalette.Visible:=Assigned(Gauge) and (Gauge is TLinearGauge);

  if CBUseColorPalette.Visible then
     CBUseColorPalette.Checked:=TLinearGauge(Gauge).UseValueColorPalette;

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

procedure TLinearGaugeEditor.CBLabelsClick(Sender: TObject);
begin
  CustomGauge.Axis.Labels:=CBLabels.Checked;
end;

procedure TLinearGaugeEditor.Button3Click(Sender: TObject);
begin
  EditTeeFontEx(Self,CustomGauge.Axis.Texts.Font);
end;

procedure TLinearGaugeEditor.EFormatChange(Sender: TObject);
begin
  if Showing and Assigned(Gauge) then
     CustomGauge.ValueFormat:=EFormat.Text;
end;

procedure TLinearGaugeEditor.EStartRangeChange(Sender: TObject);
var tmp : Double;
begin
  if Showing and (LBRanges.ItemIndex<>-1) then
     if TryStrToFloat(EStartRange.Text,tmp) then
        CustomGauge.Ranges[LBRanges.ItemIndex].StartValue:=tmp;
end;

procedure TLinearGaugeEditor.EEndRangeChange(Sender: TObject);
var tmp : Double;
begin
  if Showing and (LBRanges.ItemIndex<>-1)  then
     if TryStrToFloat(EEndRange.Text,tmp) then
        CustomGauge.Ranges[LBRanges.ItemIndex].EndValue:=tmp;
end;

procedure TLinearGaugeEditor.EMinChange(Sender: TObject);
var tmp : Double;
begin
  if Showing then
     if TryStrToFloat(EMin.Text,tmp) then
        CustomGauge.Minimum:=tmp;
end;

procedure TLinearGaugeEditor.EMaxChange(Sender: TObject);
var tmp : Double;
begin
  if Showing then
     if TryStrToFloat(EMax.Text,tmp) then
        CustomGauge.Maximum:=tmp;
end;

procedure TLinearGaugeEditor.EMinLengthChange(Sender: TObject);
begin
  if Showing then
     CustomGauge.MinorTicks.Size:=UDMinorSize.Position;
end;

procedure TLinearGaugeEditor.EMinorChange(Sender: TObject);
begin
  if Showing then
     CustomGauge.Axis.MinorTickCount:=UDMinorCount.Position;
end;

procedure TLinearGaugeEditor.ETicksChange(Sender: TObject);
begin
  if Showing then
     CustomGauge.Ticks.VertSize:=UDTicks.Position;
end;

procedure TLinearGaugeEditor.ETicksDistChange(Sender: TObject);
begin
  if Showing then
     CustomGauge.MinorTickDistance:=UDTicksDist.Position;
end;

procedure TLinearGaugeEditor.EIncrChange(Sender: TObject);
var tmp : Double;
begin
  if Showing then
     if TryStrToFloat(EIncr.Text,tmp) then
        CustomGauge.Axis.Increment:=tmp;
end;

procedure TLinearGaugeEditor.PageControl3Change(Sender: TObject);
begin
  inherited;

  if (PageControl3.ActivePage=TabLabels) and (TabLabels.ControlCount<2) then
     InsertTeeFontEditor(TabLabels).RefreshControls(CustomGauge.Axis.Texts.Font)
  else
  if (PageControl3.ActivePage=TabAxisPen) and (TabAxisPen.ControlCount=0) then
     TPenDialog.InsertForm(CustomGauge.Axis.Axis, TabAxisPen)
  else
  if (PageControl3.ActivePage=TabTicks) and (TabTicks.ControlCount<2) then
     TSeriesPointerEditor.InsertForm(CustomGauge.Ticks,TabTicks)
  else
  if (PageControl3.ActivePage=TabMinor) and (TabMinor.ControlCount<2) then
     TSeriesPointerEditor.InsertForm(CustomGauge.MinorTicks,TabMinor);
end;

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

  if Gauge is TLinearGauge then
  begin
    if (PageControl2.ActivePage=TabArea) and (TabArea.ControlCount=0) then
       TFormTeeShape.CreateForm(Self,(Gauge as TLinearGauge).ValueArea,TabArea)
    else
    if (PageControl2.ActivePage=TabMax) and (TabMax.ControlCount=0) then
       TSeriesPointerEditor.InsertForm((Gauge as TLinearGauge).MaxValueIndicator,TabMax)
    else
    if (PageControl2.ActivePage=TabHand) and (TabHand.ControlCount=0) then
       TSeriesPointerEditor.InsertForm(CustomGauge.Hand,TabHand);
  end;
end;

procedure TLinearGaugeEditor.LBRangesClick(Sender: TObject);
var tmp : TGaugePointerRange;
    tmpIndex : Integer;
begin
  if Assigned(IRangeEditor) then
     tmpIndex:=IRangeEditor.PageControl1.ActivePageIndex
  else
     tmpIndex:=0;

  FreeAndNil(IRangeEditor);

  if LBRanges.ItemIndex<>-1 then
  begin
    tmp:=CustomGauge.Ranges[LBRanges.ItemIndex].Format;

    IRangeEditor:=TSeriesPointerEditor.InsertForm(tmp,PanelRange);

    UDStartRange.Position:=Round(tmp.StartValue);
    UDEndRange.Position:=Round(tmp.EndValue);
    CBSpiralled.Checked:=tmp.Spiralled;
    UDRangePos.Position:=tmp.Position;

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

  BDeleteRange.Enabled:=LBRanges.ItemIndex>1;
  PanelRangeValues.Enabled:=Assigned(IRangeEditor);
end;

procedure TLinearGaugeEditor.SBAddRangeClick(Sender: TObject);
begin
  CustomGauge.Ranges.Add(0,0,clYellow);
  LBRanges.Items.Add(IntToStr(CustomGauge.Ranges.Count-1));
  LBRanges.ItemIndex:=CustomGauge.Ranges.Count-1;
  LBRangesClick(Self);
end;

procedure TLinearGaugeEditor.BDeleteRangeClick(Sender: TObject);
var tmp : Integer;
begin
  tmp:=LBRanges.ItemIndex;
  CustomGauge.Ranges.Delete(tmp);
  LBRanges.Items.Delete(tmp);

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

  LBRanges.ItemIndex:=tmp;
  LBRangesClick(Self);
end;

procedure TLinearGaugeEditor.CBSpiralledClick(Sender: TObject);
begin
  if Showing and (LBRanges.ItemIndex<>-1)  then
     CustomGauge.Ranges[LBRanges.ItemIndex].Format.Spiralled:=CBSpiralled.Checked;
end;

procedure TLinearGaugeEditor.FormCreate(Sender: TObject);
begin
  inherited;
  PageControl1.ActivePage:=TabSheet1;
end;

procedure TLinearGaugeEditor.ERangePosChange(Sender: TObject);
begin
  if Showing and (LBRanges.ItemIndex<>-1)  then
     CustomGauge.Ranges[LBRanges.ItemIndex].Format.Position:=UDRangePos.Position;
end;

procedure TLinearGaugeEditor.EAxisTitleChange(Sender: TObject);
begin
  if Showing then
     CustomGauge.GetHorizAxis.Title.Caption:=EAxisTitle.Text;
end;

procedure TLinearGaugeEditor.CBUseColorPaletteClick(Sender: TObject);
begin
  if Showing and (Gauge is TLinearGauge) then
     TLinearGauge(Gauge).UseValueColorPalette:=CBUseColorPalette.Checked;
end;

initialization
  RegisterClass(TLinearGaugeEditor);
end.

