{**********************************************}
{   TeeChart Numeric Gauge style               }
{   Copyright (c) 2007-2020 by Steema Software }
{**********************************************}
unit TeeNumericGaugeEditor;
{$I TeeDefs.inc}

interface

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

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

  TeeProcs, TeEngine, Chart, TeCanvas, TeePenDlg, TeeEditTools, TeeTools, Series,
  TeeEdiGrad, TeeNumericGauge;

type
  TMarkersEditor=class(TFormTeeTools)
  protected
    function CloneTool(const Tool:TTeeCustomTool):TTeeCustomTool; override;
    function GetNewTool:TTeeCustomTool; override;
  end;

  TNumericGaugeEditor = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabMarkers: TTabSheet;
    TabFrame: TTabSheet;
    TabFace: TTabSheet;
    PageControl2: TPageControl;
    TabSheet2: TTabSheet;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    EValue: TEdit;
    CBPalette: TComboFlat;
    PanelDigitalFont: TPanel;
    RGFont: TRadioGroup;
    procedure FormShow(Sender: TObject);
    procedure RGFontClick(Sender: TObject);
    procedure CBPaletteChange(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure EValueChange(Sender: TObject);
    procedure BBrushClick(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
  private
    { Private declarations }
    Tools : TMarkersEditor;
    procedure IncValue(Delta:Integer);
  protected
    Gauge : TCustomGauge;
  public
    { Public declarations }
  end;

implementation

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

uses
  TeeConst, TeeProCo, TeeBrushDlg, TeeCustomShapeEditor, TeeFrames;

procedure TNumericGaugeEditor.FormShow(Sender: TObject);
begin
  Gauge:=TCustomGauge(Tag);

  if Assigned(Gauge) then
  begin
    if Gauge is TNumericGauge then
       RGFont.ItemIndex:=Ord((Gauge as TNumericGauge).DigitalFont)
    else
    begin
      TabMarkers.TabVisible:=False;
      RGFont.Visible:=False;
    end;

    EValue.Text:=FloatToStr(Gauge.Value);
  end;

  CBPalette.Items.Clear;
  CBPalette.Items.Add('LCD');
  CBPalette.Items.Add('LED');

  ColorPalettes.AddTo(CBPalette.Items);

  TeeTranslateControl(self);
end;

{ TMarkersEditor }

type
  TNumericMarkerAccess=class(TNumericMarker);

function TMarkersEditor.CloneTool(const Tool: TTeeCustomTool): TTeeCustomTool;
begin
  result:=CloneChartTool(Tool,Tool.Owner);
  Tools.Add(result);

  TNumericMarkerAccess(result as TNumericMarker).FGauge:=(Tools as TNumericMarkers).Gauge;
end;

function TMarkersEditor.GetNewTool: TTeeCustomTool;
begin
  result:=(Tools as TNumericMarkers).Add('Marker');
end;

type
  TGaugeAccess=class(TCustomGauge);

procedure TNumericGaugeEditor.CBPaletteChange(Sender: TObject);
var tmp : TColorArray;
begin
  {$IFNDEF D7}
  tmp:=nil;
  {$ENDIF}

  case CBPalette.ItemIndex of
    0: TGaugeAccess(Gauge).SetGaugePalette(LCDPalette);
    1: TGaugeAccess(Gauge).SetGaugePalette(LEDPalette);
  else
    begin
      TColorPalettes.GetPalette(CBPalette.ItemIndex-2,tmp);
      TGaugeAccess(Gauge).SetGaugePalette(tmp);
      tmp:=nil;
    end;
  end;
end;

procedure TNumericGaugeEditor.IncValue(Delta:Integer);
begin
  Gauge.Value:=Gauge.Value+Delta;
  EValue.Text:=FloatToStr(Gauge.Value);
  Gauge.Repaint;
end;

procedure TNumericGaugeEditor.RGFontClick(Sender: TObject);
begin
  (Gauge as TNumericGauge).DigitalFont:=TDigitalFont(RGFont.ItemIndex);
end;

procedure TNumericGaugeEditor.SpeedButton1Click(Sender: TObject);
begin
  IncValue(1);
end;

procedure TNumericGaugeEditor.SpeedButton2Click(Sender: TObject);
begin
  IncValue(-1);
end;

procedure TNumericGaugeEditor.EValueChange(Sender: TObject);
begin
  Gauge.Value:=StrToFloatDef(EValue.Text,Gauge.Value);
end;

procedure TNumericGaugeEditor.BBrushClick(Sender: TObject);
begin
  EditChartBrush(Self,Gauge.Frame.Brush);
end;

procedure TNumericGaugeEditor.PageControl1Change(Sender: TObject);
begin
  if PageControl1.ActivePage=TabMarkers then
  begin
     if (TabMarkers.ControlCount=0) and (Gauge is TNumericGauge) then
     begin
       Tools:=TMarkersEditor.Create(Self);
       Tools.Tools:=(Gauge as TNumericGauge).Markers;
       Tools.Align:=alClient;
       AddFormTo(Tools,TabMarkers);
     end;
  end
  else
  if PageControl1.ActivePage=TabFace then
  begin
    if TabFace.ControlCount=0 then
       TFormTeeShape.CreateForm(Self,Gauge.Face,TabFace).TabChildren.TabVisible:=False;
  end
  else
  if PageControl1.ActivePage=TabFrame then
     if TabFrame.ControlCount=0 then
        TFramedBorderEditor.InsertDialog(TabFrame,Gauge.Frame);
end;

initialization
  RegisterClasses([TNumericGaugeEditor]);
end.
