unit FMXTee.Editor.Series.Map;
{$I TeeDefs.inc}

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  {$IFDEF D17}
  System.Rtti,
  {$ENDIF}
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMXTee.Chart.ListBox,
  FMX.TabControl, FMXTee.Series.Map, FMX.Edit, FMX.ListBox, FMX.Colors,
  FMX.Layouts, FMX.Grid, FMXTee.Chart.Grid;

type
  TMapSeriesEditor = class(TForm)
    TabControl1: TTabControl;
    TabGlobal: TTabItem;
    TabShadow: TTabItem;
    TabEmboss: TTabItem;
    CBTransparent: TCheckBox;
    BBorder: TButton;
    BBrush: TButton;
    UDMapTransp: TSpinBox;
    Label6: TLabel;
    UDPointSize: TSpinBox;
    Label7: TLabel;
    TabShapes: TTabItem;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    TabControl2: TTabControl;
    Splitter1: TSplitter;
    TabValues: TTabItem;
    TabFormat: TTabItem;
    TabPoints: TTabItem;
    EText: TEdit;
    EditZ: TEdit;
    Label2: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    ShapeIndex: TLabel;
    CBColor: TComboColorBox;
    BFormatBorder: TButton;
    Label9: TLabel;
    BFormatBrush: TButton;
    CBGlobalPen: TCheckBox;
    CBGlobalBrush: TCheckBox;
    CBDefaultColor: TCheckBox;
    BGradient: TButton;
    CBClosed: TCheckBox;
    CBVisible: TCheckBox;
    UDTransp: TSpinBox;
    Label10: TLabel;
    SBAdd: TButton;
    SBDelete: TButton;
    TabGrid3D: TTabItem;
    ChartListBox1: TChartListBox;
    procedure FormActivate(Sender: TObject);
    procedure CBTransparentChange(Sender: TObject);
    procedure BBorderClick(Sender: TObject);
    procedure BBrushClick(Sender: TObject);
    procedure UDMapTranspChange(Sender: TObject);
    procedure UDPointSizeChange(Sender: TObject);
    procedure ETextChange(Sender: TObject);
    procedure EditZChange(Sender: TObject);
    procedure ChartListBox1Click(Sender: TObject);
    procedure BFormatBorderClick(Sender: TObject);
    procedure BFormatBrushClick(Sender: TObject);
    procedure CBColorChange(Sender: TObject);
    procedure BGradientClick(Sender: TObject);
    procedure TabControl1Change(Sender: TObject);
    procedure CBGlobalPenChange(Sender: TObject);
    procedure CBGlobalBrushChange(Sender: TObject);
    procedure SBDeleteClick(Sender: TObject);
    procedure SBAddClick(Sender: TObject);
    procedure CBDefaultColorChange(Sender: TObject);
    procedure CBClosedChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CBVisibleChange(Sender: TObject);
    procedure UDTranspChange(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ChartListBox1ChangeCheck(Sender: TObject);
  private
    { Private declarations }
    ChangingColor,
    ChangingText : Boolean;
    Grid3DForm : TCustomForm;
    FChartGrid : TChartGrid;
    IReady     : Boolean;

    procedure CheckDefaultColor;
    Procedure EnableTabs;
    procedure FillShapes;
    Function SelectedShape:TTeePolygon;
    procedure ChartGridSetEditText(Sender: TObject; const ACol,
      ARow: Integer; const Value: {$IFDEF D17}TValue{$ELSE}Variant{$ENDIF});
  protected
    Map : TMapSeries;
  public
    { Public declarations }
    InitialShape : TObject;
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Editor.Stroke, FMXTee.Editor.Brush, FMXTee.Editor.Gradient,
  FMXTee.Editor.Shadow, FMXTee.Editor.Series.Grid, FMXTee.Procs, FMXTee.Engine;

procedure TMapSeriesEditor.CBClosedChange(Sender: TObject);
begin
  if SelectedShape<>nil then
    SelectedShape.Closed:=CBClosed.IsChecked;
end;

procedure TMapSeriesEditor.CBColorChange(Sender: TObject);
begin
  CheckDefaultColor;
  ChartListBox1.Repaint;
end;

procedure TMapSeriesEditor.CBDefaultColorChange(Sender: TObject);
begin
  if (SelectedShape<>nil) and not ChangingColor then
  begin
    SelectedShape.Color:=clTeeColor;
    ChartListBox1.Repaint;
  end;
end;

procedure TMapSeriesEditor.CBGlobalBrushChange(Sender: TObject);
begin
  if SelectedShape<>nil then
    SelectedShape.ParentBrush:=CBGlobalBrush.IsChecked;
end;

procedure TMapSeriesEditor.CBGlobalPenChange(Sender: TObject);
begin
  if SelectedShape<>nil then
    SelectedShape.ParentPen:=CBGlobalPen.IsChecked;
end;

procedure TMapSeriesEditor.CBTransparentChange(Sender: TObject);
begin
  if assigned(Map) then
    Map.Transparent:=CBTransparent.IsChecked;
end;

procedure TMapSeriesEditor.CBVisibleChange(Sender: TObject);
begin
  if SelectedShape<>nil then
  begin
    SelectedShape.Points.Visible:=CBVisible.IsChecked;
    ChartListBox1.SelectedSeries.Visible:=CBVisible.IsChecked;
    if ChartListBox1.Selected.IsChecked<>CBVisible.IsChecked then
      ChartListBox1.Selected.IsChecked:=CBVisible.IsChecked;
    ChartListBox1.Repaint;
  end;
end;

procedure TMapSeriesEditor.ChartListBox1ChangeCheck(Sender: TObject);
begin
  ChartListBox1.ItemIndex:=(Sender as TListBoxItem).Index;
  ChartListBox1.SelectRange((Sender as TListBoxItem),(Sender as TListBoxItem));
  SelectedShape.Points.Visible:=(Sender as TListBoxItem).IsChecked;
  ChartListBox1Click(ChartListBox1);
  ChartListBox1.Repaint;
end;

procedure TMapSeriesEditor.ChartListBox1Click(Sender: TObject);
begin
  EnableTabs;

  if SelectedShape<>nil then
  begin

    With SelectedShape do
    begin
      ShapeIndex.Text:=IntToStr(Index);

      CBColor.Color:=SelectedShape.Color;
      CBGlobalPen.IsChecked:=ParentPen;
      CheckDefaultColor;
      CBGlobalBrush.IsChecked:=ParentBrush;

      ChangingText:=True;
      try
        EText.Text:=Text;
        EditZ.Text:=FloatToStr(Z);
      finally
        ChangingText:=False;
      end;

      CBClosed.IsChecked:=Closed;
      CBVisible.IsChecked:=Points.Visible;
      UDTransp.Value:=Transparency;

      FChartGrid.Series:=Points;
    end;

    SBDelete.Enabled:=True;

  end
  else
  begin
    ShapeIndex.Text:='';
    ChangingText:=True;
    EText.Text:='';
    EditZ.Text:='0';
    ChangingText:=False;
  end;
end;

procedure TMapSeriesEditor.BBrushClick(Sender: TObject);
begin
  if Assigned (Map) then
    TBrushEditor.Edit(Self, Map.Brush)
end;

procedure TMapSeriesEditor.BFormatBorderClick(Sender: TObject);
begin
  if SelectedShape<>nil then
  begin
    TStrokeEditor.Edit(Self, SelectedShape.Pen);
    CBGlobalPen.IsChecked:=False;
    Map.Repaint;
  end;
end;

procedure TMapSeriesEditor.BFormatBrushClick(Sender: TObject);
begin
  if SelectedShape<>nil then
  begin
    TBrushEditor.Edit(Self, SelectedShape.Brush);
    CBGlobalBrush.IsChecked:=False;
    Map.Repaint;
  end;
end;

procedure TMapSeriesEditor.BGradientClick(Sender: TObject);
begin
  if SelectedShape<>nil then
  begin
    TGradientEditor.Edit(Self, SelectedShape.Gradient);
    ChartListBox1.Repaint;
  end;
end;

procedure TMapSeriesEditor.BBorderClick(Sender: TObject);
begin
  if Assigned(Map) then
    TStrokeEditor.Edit(Self, Map.Pen);
end;

type
  TSeriesAccess=class(TChartSeries);

procedure TMapSeriesEditor.CheckDefaultColor;
var tmp : TAlphaColor;
begin
  ChangingColor:=True;

  tmp:=TSeriesAccess(SelectedShape.ParentSeries).InternalColor(SelectedShape.Index);

  CBDefaultColor.IsChecked:=tmp=clTeeColor;
  ChangingColor:=False;

  CBDefaultColor.Enabled:=not CBDefaultColor.IsChecked;
end;

procedure TMapSeriesEditor.FormActivate(Sender: TObject);
begin
  if not Assigned(Map) then
  begin
    Map:=TMapSeries(TagObject);

    if Assigned(Map) then
    begin
      ChangingText:=False;
      UDMapTransp.Value:=Map.Transparency;
      CBTransparent.IsChecked:=Map.Transparent;
      UDPointSize.Value:=Map.PointSize;

      if not Assigned(FChartGrid) then
      begin
        FChartGrid:=TChartGrid.Create(Self);
        FChartGrid.Align:=TAlignLayout.alClient;
        FChartGrid.OnSetEditText:=ChartGridSetEditText;
        TabPoints.AddObject(FChartGrid);
      end;
      TGrid3DSeriesEditor.CreateEmbedd(Self,Map,TabGrid3D);
      TShadowEditor.CreateEmbedd(Self,Map.Shadow,TabShadow);
      TShadowEditor.CreateEmbedd(Self,Map.Emboss,TabEmboss);
    end;

    TabControl1.ActiveTab:=TabGlobal;
    FChartGrid.ShowFields:=False;
    EnableTabs;

    //ChartGridNavigator1.Buttons[nbInsert].Visible:=True;
    //ChartGridNavigator1.Buttons[nbDelete].Visible:=True;

    TabControl2.ActiveTab:=TabFormat;

    if Assigned(Map) and (Map.Shapes.Count>0) then
    begin
      FillShapes; // 7.07

      if Assigned(InitialShape) then
      begin
        TabControl1.ActiveTab:=TabShapes;
        TabControl1Change(Self);
        ChartListBox1.ItemIndex:=ChartListBox1.Items.IndexOfObject(InitialShape);
      end
      else
         ChartListBox1.ItemIndex:=0;

      ChartListBox1Click(Self);
    end;

    TabControl1.ActiveTab:=TabGlobal;
    TabControl2.ActiveTab:=TabValues;
  end;
end;

procedure TMapSeriesEditor.FormCreate(Sender: TObject);
begin
  IReady:=True;
  InitialShape:=nil;
end;

procedure TMapSeriesEditor.FormDestroy(Sender: TObject);
begin
  FreeAndNil(Grid3DForm);
end;

procedure TMapSeriesEditor.EditZChange(Sender: TObject);
var tmp : Double;
begin
  if IReady and (not ChangingText) then
  begin
    tmp:=StrToFloat(EditZ.Text);
    if SelectedShape.Z<>tmp then
    begin
      SelectedShape.Z:=tmp;
      Map.Repaint;
    end;
  end;
end;

procedure TMapSeriesEditor.EnableTabs;

  procedure EnableControls(Tab:TTabItem; Value:Boolean);
  var t : Integer;
  begin
    for t:=0 to Tab. ChildrenCount-1 do
      if Tab.Children[t] is TControl then
         (Tab.Children[t] as TControl).Enabled:=Value;
  end;

var tmp : Boolean;
begin
  tmp:=Assigned(Map) and (Map.Shapes.Count>0); { 5.02 }

  EnableControls(TabValues,tmp);
  EnableControls(TabFormat,tmp);
  EnableControls(TabPoints,tmp);
end;

procedure TMapSeriesEditor.SBAddClick(Sender: TObject);
begin
  with Map.Shapes.Add do { 5.02 }
       Text:=IntToStr(Map.Count);

  FillShapes;

  with ChartListBox1 do
  begin
    ItemIndex:=Items.Count-1;
    SelectRange(ListItems[ItemIndex],ListItems[ItemIndex]);
    SetFocus;
  end;

  ChartListBox1Click(Self);
  EnableTabs;
end;

procedure TMapSeriesEditor.SBDeleteClick(Sender: TObject);
var tmpLast : Boolean;
    tmp     : Integer;
begin
  tmp:=ChartListBox1.ItemIndex;
  tmpLast:=ChartListBox1.ItemIndex=ChartListBox1.Items.Count-1;
  Map.Delete(ChartListBox1.ItemIndex);
  SBDelete.Enabled:=Map.Shapes.Count>0;

  FillShapes;

  if Map.Shapes.Count>0 then
  begin
    if tmpLast then tmp:=ChartListBox1.Items.Count-1;
  end
  else tmp:=-1;

  with ChartListBox1 do
  begin
    ItemIndex:=tmp;
    if tmp>=0 then
       SelectRange(ListItems[tmp],ListItems[tmp]);
  end;

  ChartListBox1Click(Self);
  EnableTabs;
end;

function TMapSeriesEditor.SelectedShape: TTeePolygon;
begin
  if ChartListBox1.ItemIndex>=0 then
     result:=Map.Shapes[ChartListBox1.ItemIndex]
  else
     result:=nil;
end;

procedure TMapSeriesEditor.TabControl1Change(Sender: TObject);
begin
  if Assigned(Map) and (ChartListBox1.Items.Count<>Map.Shapes.Count) then
  begin
    FillShapes;
    ChartListBox1Click(Self);
  end;
end;

procedure TMapSeriesEditor.ETextChange(Sender: TObject);
var tmp : String;
begin
  if not ChangingText then
  begin
    tmp:=SelectedShape.Text;
    if tmp<>EText.Text then
    begin
      SelectedShape.Text:=EText.Text;
      With ChartListBox1 do Items[ItemIndex]:=SelectedShape.Text;
    end;
  end;
end;

Procedure TMapSeriesEditor.FillShapes;
var t : Integer;
    tmp : TListBoxItem;
begin
  With ChartListBox1 do
  begin
    Items.BeginUpdate;
    try
      Clear;
      for t:=0 to Map.Shapes.Count-1 do
      begin
        tmp:=TListBoxItem.Create(Owner);
        tmp.Text:=Map.Labels[t];
        if tmp.Text='' then
          tmp.Text:=IntToStr(t);
        tmp.Data:=Map.Shapes[t].Points;
        tmp.IsChecked:=Map.Shapes[t].Points.Visible;
        AddObject(tmp);
      end;
    finally
      Items.EndUpdate;
    end;
  end;
end;

procedure TMapSeriesEditor.ChartGridSetEditText(Sender: TObject; const ACol,
  ARow: Integer; const Value: {$IFDEF D17}TValue{$ELSE}Variant{$ENDIF});
begin
  if Assigned(Map) then
    Map.Repaint;
end;

procedure TMapSeriesEditor.UDMapTranspChange(Sender: TObject);
begin
  if Assigned(Map) then
    Map.Transparency:=Round(UDMapTransp.Value);
end;

procedure TMapSeriesEditor.UDPointSizeChange(Sender: TObject);
begin
  if Assigned(Map) then
    Map.PointSize:=Round(UDPointSize.Value);
end;

procedure TMapSeriesEditor.UDTranspChange(Sender: TObject);
begin
  if SelectedShape<>nil then
     SelectedShape.Transparency:=Round(UDTransp.Value);
end;

initialization
  RegisterSeriesEditor(TMapSeriesEditor);
end.
