unit FMXTee.Editor.DBSummary;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMXTee.Editor.DB, FMX.ListBox,
  FMX.Edit;

type
  TDBSummaryEditor = class(TDBSourceEditor)
    Label1: TLabel;
    CBAgg: TComboBox;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    CBValue: TComboEdit;
    CBTimeStep: TComboBox;
    CBSort: TComboBox;
    CBGroup: TComboEdit;
    CBSortType: TComboBox;
    ListBoxItem1: TListBoxItem;
    ListBoxItem2: TListBoxItem;
    ListBoxItem3: TListBoxItem;
    ListBoxItem4: TListBoxItem;
    ListBoxItem5: TListBoxItem;
    ListBoxItem6: TListBoxItem;
    ListBoxItem7: TListBoxItem;
    ListBoxItem8: TListBoxItem;
    ListBoxItem9: TListBoxItem;
    ListBoxItem10: TListBoxItem;
    ListBoxItem11: TListBoxItem;
    ListBoxItem12: TListBoxItem;
    ListBoxItem13: TListBoxItem;
    ListBoxItem14: TListBoxItem;
    ListBoxItem15: TListBoxItem;
    ListBoxItem16: TListBoxItem;
    ListBoxItem17: TListBoxItem;
    procedure FormCreate(Sender: TObject);
    procedure CBSortTypeChange(Sender: TObject);
    procedure CBSortChange(Sender: TObject);
    procedure CBSourcesChange(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure BApplyClick(Sender: TObject);
    procedure CBGroupChange(Sender: TObject);
    procedure CBAggChange(Sender: TObject);
  private
    { Private declarations }
    procedure CheckCount;
    Procedure EnableCombos;
    Procedure EnableTimeStep;
    procedure RefreshSummary;
  public
    { Public declarations }
  end;

implementation

{$R *.fmx}

uses
  Data.DB, FMXTee.Constants, FMXTee.Procs, FMXTee.Engine, FMXTee.DBChart,
  FMXTee.Series;

type
  TDBSummarySource=class(TTeeSeriesDBSource)
  public
    class Function Description:String; override;
    class Function Editor:TComponentClass; override;
    class Function HasSeries(ASeries:TChartSeries):Boolean; override;
  end;

const
  TeeSharp='#'; // Do not localize;

Function TeeGetDBGroup(St:String; Var Step:TTeeDBGroup):Boolean;
begin
  result:=False;
  St:=TeeGetDBPart(1,St);

  if St<>'' then
  begin
    result:=True;
    Step:=StrToDBGroup(St);
  end;
end;

{ TDBSummarySource }

class function TDBSummarySource.Description: String;
begin
  result:=TeeMsg_Summary;
end;

class function TDBSummarySource.Editor: TComponentClass;
begin
  result:=TDBSummaryEditor;
end;

class function TDBSummarySource.HasSeries(
  ASeries: TChartSeries): Boolean;
begin
  result:=(ASeries.DataSource is TDataSet) and
          (Copy(ASeries.MandatoryValueList.ValueSource,1,1)=TeeSharp);
end;

procedure TDBSummaryEditor.BApplyClick(Sender: TObject);

  Function TeeDBGroupToStr(Group:TTeeDBGroup):String;
  begin
    Case Group of
      dgHour   : result:='HOUR'; // Do not localize
      dgDay    : result:='DAY'; // Do not localize
      dgWeek   : result:='WEEK'; // Do not localize
      dgWeekDay: result:='WEEKDAY'; // Do not localize
      dgMonth  : result:='MONTH'; // Do not localize
      dgQuarter: result:='QUARTER'; // Do not localize
      dgYear   : result:='YEAR' // Do not localize
    else
      result:='';
    end;
  end;

var tmp   : String;
    tmpSt : String;
begin
  inherited;

  Case CBAgg.ItemIndex of
    0: tmp:='#SUM#'; // Do not localize
    1: tmp:='#COUNT#'; // Do not localize
    2: tmp:='#HIGH#'; // Do not localize
    3: tmp:='#LOW#'; // Do not localize
    4: tmp:='#AVG#'; // Do not localize
  else
       tmp:='';
  end;

  // Pending: Remove all series that belong to us, before adding a new one
  if (not Assigned(TheSeries)) and Assigned(Chart) then
     TheSeries:=Chart.AddSeries(TBarSeries);

  TheSeries.DataSource:=nil;

  With CBValue do { 5.01 }
  if ItemIndex=-1 then
     TheSeries.MandatoryValueList.ValueSource:=tmp+ListBox.Selected.Text
  else
     TheSeries.MandatoryValueList.ValueSource:=tmp+ListBox.ListItems[ItemIndex].Text;

  tmpSt:='';

  if CBGroup.ItemIndex<>-1 then
  begin
    tmpSt:=CBGroup.ListBox.Selected.Text;

    if CBTimeStep.Enabled and (CBTimeStep.ItemIndex<>-1) then
       tmpSt:=TeeSharp+TeeDBGroupToStr(TTeeDBGroup(CBTimeStep.ItemIndex))+
              TeeSharp+tmpSt
    else
    case CBSort.ItemIndex of
      2: begin
           TheSeries.MandatoryValueList.Order:=loNone;

           if CBSortType.ItemIndex=0 then tmpSt:='#SORTASC#'+tmpSt // Do not localize
                                     else tmpSt:='#SORTDES#'+tmpSt; // Do not localize
         end;
      1: begin
           TheSeries.NotMandatoryValueList.Order:=loNone;
           TheSeries.MandatoryValueList.Order:=TChartListOrder(CBSortType.ItemIndex);
         end;
      0: begin
           TheSeries.MandatoryValueList.Order:=loNone;
           TheSeries.NotMandatoryValueList.Order:=loNone;
         end;
    end;
  end;

  With TheSeries do
  begin
    XLabelsSource:=tmpSt;
    DataSource:=Self.DataSet;
  end;

  BApply.Enabled:=False;
end;

procedure TDBSummaryEditor.CBAggChange(Sender: TObject);
begin
  inherited;

  With CBValue do
  begin
    Enabled:=(DataSet<>nil) and (CBAgg.ItemIndex<>1);

//    if ItemIndex<>-1 then Text:=Items[ItemIndex] { 5.01 }
//                     else Text:='';
  end;

  BApply.Enabled:=True
end;

procedure TDBSummaryEditor.CBGroupChange(Sender: TObject);
begin
  inherited;
  EnableTimeStep;
  BApply.Enabled:=True;
end;

Procedure TDBSummaryEditor.EnableTimeStep;

  Function TheFieldType(Const AName:String):TFieldType;
  begin
    With DataSet do
    if FieldCount>0 then
       result:=FieldByName(AName).DataType
    else
    begin
      FieldDefs.Update;
      result:=FieldDefs[FieldDefs.IndexOf(AName)].DataType;
    end;
  end;

var tmpName : String;
begin
  if CBGroup.ItemIndex=-1 then
  begin
    CBTimeStep.ItemIndex:=-1;
    CBTimeStep.Enabled:=False;
    CBSort.Enabled:=False;
    CBSortType.Enabled:=False;
  end
  else
  begin
    tmpName:=CBGroup.ListBox.Selected.Text;

    CBTimeStep.Enabled:=TeeFieldType(TheFieldType(tmpName))=tftDateTime;
    CBSort.Enabled:=not CBTimeStep.Enabled;

    if not CBSort.Enabled then CBSort.ItemIndex:=0;

    CBSortType.Enabled:=CBSort.ItemIndex>0;
  end;
end;

procedure TDBSummaryEditor.CBSortChange(Sender: TObject);
begin
  CBSortType.Enabled:=CBSort.ItemIndex>0;
  BApply.Enabled:=True;
end;

procedure TDBSummaryEditor.CBSortTypeChange(Sender: TObject);
begin
  BApply.Enabled:=True;
end;

procedure TDBSummaryEditor.CBSourcesChange(Sender: TObject);
begin
  inherited;
  EnableCombos;
  FillFields([CBValue, CBGroup]);
end;

procedure TDBSummaryEditor.FormActivate(Sender: TObject);
begin
  inherited;
  RefreshSummary;
end;

procedure TDBSummaryEditor.RefreshSummary;
var tmp       : TTeeDBGroup;
    tmpSource : String;
    tmpOrder  : TChartListOrder;
begin
  inherited;

  EnableCombos;
  FillFields([CBValue, CBGroup]);

  if Assigned(TheSeries) then
  begin
    tmpSource:=TheSeries.MandatoryValueList.ValueSource;

    With CBAgg do ItemIndex:=ListBox.Items.IndexOf(TeeGetDBPart(1,tmpSource));

    With CBValue do ItemIndex:=ListBox.Items.IndexOf(TeeGetDBPart(2,tmpSource));

    tmpSource:=TeeGetDBPart(2,TheSeries.XLabelsSource);

    tmpOrder:=loNone;

    if tmpSource='' then
       tmpSource:=TeeGetDBPart(1,TheSeries.XLabelsSource)
    else
       tmpOrder:=StrToDBOrder(TeeGetDBPart(1,TheSeries.XLabelsSource));

    if tmpSource='' then tmpSource:=TheSeries.XLabelsSource;

    With CBGroup do ItemIndex:=ListBox.Items.IndexOf(tmpSource);

    EnableTimeStep;

    With CBTimeStep do
    if Enabled then
    begin
      if TeeGetDBGroup(TheSeries.XLabelsSource,tmp) then
         ItemIndex:=Ord(tmp);
    end;

    CBSort.Enabled:=(not CBTimeStep.Enabled) and (CBGroup.ItemIndex<>-1);

    if CBSort.Enabled then
       if tmpOrder=loNone then
       begin
         if TheSeries.MandatoryValueList.Order<>loNone then
         begin
           CBSort.ItemIndex:=1;
           CBSortType.ItemIndex:=Ord(TheSeries.MandatoryValueList.Order);
         end
         else CBSort.ItemIndex:=0;
       end
       else
       begin
         CBSort.ItemIndex:=2;
         CBSortType.ItemIndex:=Ord(tmpOrder);
       end;

    CBSortType.Enabled:=CBSort.Enabled and (CBSort.ItemIndex>0);

    CheckCount;
  end;

  TeeTranslateControl(Self);
end;

procedure TDBSummaryEditor.FormCreate(Sender: TObject);
begin
  inherited;
  CBSortType.ItemIndex:=0;
  CBSort.ItemIndex:=0;
end;

Procedure TDBSummaryEditor.EnableCombos;
begin
  EnableControls(DataSet<>nil,[CBAgg,CBValue,CBTimeStep,CBGroup]);
end;

procedure TDBSummaryEditor.CheckCount;
begin
  CBValue.Enabled:=(DataSet<>nil) and (CBAgg.ItemIndex<>1);

  if CBValue.Enabled then
     With CBValue do
     begin
       ItemIndex:=ListBox.Items.IndexOf(TeeGetDBPart(2,
                                TheSeries.MandatoryValueList.ValueSource));

//       if ItemIndex<>-1 then Text:=Items[ItemIndex]
//                        else Text:='';
     end
  else
    CBValue.ItemIndex:=-1; //Text:='';
end;

initialization
  TeeSources.Add(TDBSummarySource);
finalization
  TeeSources.Remove(TDBSummarySource);
end.
