From f635c93904ead3811e1dec240480ff86ed7b5726 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Tue, 26 May 2020 20:58:55 +0000 Subject: [PATCH] LazStats: Refactor DataSmoothUnit git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7474 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../lazstats/source/forms/mainunit.pas | 2 +- .../source/forms/tools/datasmoothunit.lfm | 62 +++++--- .../source/forms/tools/datasmoothunit.pas | 143 ++++++++++-------- 3 files changed, 121 insertions(+), 86 deletions(-) diff --git a/applications/lazstats/source/forms/mainunit.pas b/applications/lazstats/source/forms/mainunit.pas index ca808812c..4a1b5204b 100644 --- a/applications/lazstats/source/forms/mainunit.pas +++ b/applications/lazstats/source/forms/mainunit.pas @@ -1602,7 +1602,7 @@ begin end else if Command = HELP_COMMAND then begin - topic := Application.HelpFile + '::/' + PChar(Data); + topic := UnicodeString(Application.HelpFile + '::/' + {%H-}PChar(Data)); res := htmlhelp.HtmlHelpW(0, PWideChar(topic), HH_DISPLAY_TOPIC, 0); end; diff --git a/applications/lazstats/source/forms/tools/datasmoothunit.lfm b/applications/lazstats/source/forms/tools/datasmoothunit.lfm index 6b58b565c..3bd7c7877 100644 --- a/applications/lazstats/source/forms/tools/datasmoothunit.lfm +++ b/applications/lazstats/source/forms/tools/datasmoothunit.lfm @@ -3,6 +3,7 @@ object SmoothDataForm: TSmoothDataForm Height = 386 Top = 190 Width = 387 + AutoSize = True Caption = 'Data Smoothing' ClientHeight = 386 ClientWidth = 387 @@ -86,6 +87,7 @@ object SmoothDataForm: TSmoothDataForm Anchors = [akTop, akLeft, akRight] BorderSpacing.Left = 8 BorderSpacing.Right = 8 + ReadOnly = True TabOrder = 3 Text = 'VariableEdit' end @@ -135,38 +137,23 @@ object SmoothDataForm: TSmoothDataForm end object ResetBtn: TButton AnchorSideTop.Control = ComputeBtn - AnchorSideRight.Control = CancelBtn - Left = 163 + AnchorSideRight.Control = ComputeBtn + Left = 174 Height = 25 Top = 353 Width = 54 Anchors = [akTop, akRight] AutoSize = True - BorderSpacing.Right = 12 + BorderSpacing.Right = 8 Caption = 'Reset' OnClick = ResetBtnClick - TabOrder = 5 - end - object CancelBtn: TButton - AnchorSideTop.Control = ComputeBtn - AnchorSideRight.Control = ComputeBtn - Left = 229 - Height = 25 - Top = 353 - Width = 62 - Anchors = [akTop, akRight] - AutoSize = True - BorderSpacing.Right = 12 - Caption = 'Cancel' - ModalResult = 2 TabOrder = 6 end object ComputeBtn: TButton - AnchorSideRight.Control = Owner - AnchorSideRight.Side = asrBottom + AnchorSideRight.Control = CloseBtn AnchorSideBottom.Control = Owner AnchorSideBottom.Side = asrBottom - Left = 303 + Left = 236 Height = 25 Top = 353 Width = 76 @@ -176,7 +163,6 @@ object SmoothDataForm: TSmoothDataForm BorderSpacing.Right = 8 BorderSpacing.Bottom = 8 Caption = 'Compute' - ModalResult = 1 OnClick = ComputeBtnClick TabOrder = 7 end @@ -184,16 +170,16 @@ object SmoothDataForm: TSmoothDataForm Tag = 119 AnchorSideTop.Control = ComputeBtn AnchorSideRight.Control = ResetBtn - Left = 100 + Left = 115 Height = 25 Top = 353 Width = 51 Anchors = [akTop, akRight] AutoSize = True - BorderSpacing.Right = 12 + BorderSpacing.Right = 8 Caption = 'Help' OnClick = HelpBtnClick - TabOrder = 8 + TabOrder = 5 end object Bevel1: TBevel AnchorSideLeft.Control = InBtn @@ -234,4 +220,32 @@ object SmoothDataForm: TSmoothDataForm ParentColor = False WordWrap = True end + object CloseBtn: TButton + AnchorSideRight.Control = Owner + AnchorSideRight.Side = asrBottom + AnchorSideBottom.Control = Owner + AnchorSideBottom.Side = asrBottom + Left = 320 + Height = 25 + Top = 353 + Width = 55 + Anchors = [akRight, akBottom] + BorderSpacing.Right = 12 + BorderSpacing.Bottom = 8 + Caption = 'Close' + ModalResult = 11 + TabOrder = 8 + end + object Label4: TLabel + AnchorSideLeft.Control = VariableEdit + AnchorSideBottom.Control = VariableEdit + Left = 215 + Height = 15 + Top = 114 + Width = 128 + Anchors = [akLeft, akBottom] + BorderSpacing.Bottom = 2 + Caption = 'Variable to be smoothed' + ParentColor = False + end end diff --git a/applications/lazstats/source/forms/tools/datasmoothunit.pas b/applications/lazstats/source/forms/tools/datasmoothunit.pas index a2c8c6b91..46383f97b 100644 --- a/applications/lazstats/source/forms/tools/datasmoothunit.pas +++ b/applications/lazstats/source/forms/tools/datasmoothunit.pas @@ -16,10 +16,11 @@ type TSmoothDataForm = class(TForm) Bevel1: TBevel; Bevel2: TBevel; - CancelBtn: TButton; + CloseBtn: TButton; HelpBtn: TButton; Label3: TLabel; ComputeBtn: TButton; + Label4: TLabel; Memo1: TLabel; RepeatEdit: TEdit; Label1: TLabel; @@ -39,6 +40,7 @@ type private { private declarations } FAutoSized: Boolean; + procedure UpdateBtnStates; public { public declarations } end; @@ -54,66 +56,79 @@ uses { TSmoothDataForm } procedure TSmoothDataForm.ResetBtnClick(Sender: TObject); -VAR i : integer; +var + i: integer; begin - VarList.Clear; - for i := 1 to NoVariables do - VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]); - RepeatEdit.Text := '1'; - VariableEdit.Text := ''; - InBtn.Enabled := true; - OutBtn.Enabled := false; + VarList.Clear; + for i := 1 to NoVariables do + VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]); + RepeatEdit.Text := '1'; + VariableEdit.Text := ''; + UpdateBtnStates; end; procedure TSmoothDataForm.InBtnClick(Sender: TObject); -VAR index : integer; +var + index: integer; begin - index := VarList.ItemIndex; - VariableEdit.Text := VarList.Items.Strings[index]; - VarList.Items.Delete(index); - InBtn.Enabled := false; - OutBtn.Enabled := true; + index := VarList.ItemIndex; + if (index > -1) and (VariableEdit.Text = '') then + begin + VariableEdit.Text := VarList.Items.Strings[index]; + VarList.Items.Delete(index); + UpdateBtnStates; + end; end; procedure TSmoothDataForm.ComputeBtnClick(Sender: TObject); -VAR - DataPts, OutPts : DblDyneVec; - value, avg : double; - VarCol, N, Reps, i, j, col : integer; - varlabel, strvalue : string; +var + DataPts, OutPts: DblDyneVec; + value, avg: double; + VarCol, N, Reps, i, j, col: integer; + varlabel, strvalue: string; begin - N := NoCases; - SetLength(DataPts,N); - SetLength(OutPts,N); - Reps := StrToInt(RepeatEdit.Text); - varlabel := VariableEdit.Text; - for i := 1 to NoVariables do - if varlabel = OS3MainFrm.DataGrid.Cells[i,0] then VarCol := i; - for i := 1 to N do - begin - value := StrToFloat(OS3MainFrm.DataGrid.Cells[VarCol,i]); - DataPts[i-1] := value; - end; - // repeat smoothing for Reps times - OutPts[0] := DataPts[0]; - OutPts[N-1] := DataPts[N-1]; - for j := 1 to Reps do - begin - for i := 1 to N-2 do - begin - avg := (DataPts[i-1] + DataPts[i] + DataPts[i+1]) / 3.0; - OutPts[i] := avg; - end; - if j < Reps then - for i := 0 to N-1 do DataPts[i] := OutPts[i]; - end; - // Create a new variable and copy smoothed data into it. - strvalue := 'Smoothed'; - col := NoVariables + 1; - DictionaryFrm.NewVar(NoVariables+1); - DictionaryFrm.DictGrid.Cells[1,NoVariables] := strvalue; - OS3MainFrm.DataGrid.Cells[NoVariables,0] := strvalue; - for i := 0 to N-1 do OS3MainFrm.DataGrid.Cells[col,i+1] := FloatToStr(OutPts[i]); + N := NoCases; + SetLength(DataPts, N); + SetLength(OutPts, N); + if not TryStrToInt(RepeatEdit.Text, Reps) or (Reps <= 0) then + begin + RepeatEdit.SetFocus; + MessageDlg('Valid positive number required.', mtError, [mbOK], 0); + exit; + end; + + varlabel := VariableEdit.Text; + for i := 1 to NoVariables do + if varlabel = OS3MainFrm.DataGrid.Cells[i,0] then VarCol := i; + + for i := 1 to N do + begin + value := StrToFloat(OS3MainFrm.DataGrid.Cells[VarCol,i]); + DataPts[i-1] := value; + end; + + // repeat smoothing for Reps times + OutPts[0] := DataPts[0]; + OutPts[N-1] := DataPts[N-1]; + for j := 1 to Reps do + begin + for i := 1 to N-2 do + begin + avg := (DataPts[i-1] + DataPts[i] + DataPts[i+1]) / 3.0; + OutPts[i] := avg; + end; + if j < Reps then + for i := 0 to N-1 do DataPts[i] := OutPts[i]; + end; + + // Create a new variable and copy smoothed data into it. + strvalue := 'Smoothed'; + col := NoVariables + 1; + DictionaryFrm.NewVar(NoVariables+1); + DictionaryFrm.DictGrid.Cells[1, NoVariables] := strvalue; + OS3MainFrm.DataGrid.Cells[NoVariables, 0] := strvalue; + for i := 0 to N-1 do + OS3MainFrm.DataGrid.Cells[col, i+1] := FloatToStr(OutPts[i]); end; procedure TSmoothDataForm.FormActivate(Sender: TObject); @@ -123,13 +138,13 @@ begin if FAutoSized then exit; - w := MaxValue([HelpBtn.Width, ComputeBtn.Width, ResetBtn.Width, CancelBtn.Width]); + w := MaxValue([HelpBtn.Width, ComputeBtn.Width, ResetBtn.Width, CloseBtn.Width]); HelpBtn.Constraints.MinWidth := w; ComputeBtn.Constraints.MinWidth := w; ResetBtn.Constraints.MinWidth := w; - CancelBtn.Constraints.MinWidth := w; + CloseBtn.Constraints.MinWidth := w; - Constraints.MinWidth := Width; + Constraints.MinWidth := (Label1.Width + RepeatEdit.Width + Label3.Width) * 2; Constraints.MinHeight := Height; FAutoSized := true; @@ -139,8 +154,6 @@ procedure TSmoothDataForm.FormCreate(Sender: TObject); begin Assert(OS3MainFrm <> nil); - if ContextHelpForm = nil then - Application.CreateForm(TContextHelpForm, ContextHelpForm); if DictionaryFrm = nil then Application.CreateForm(TDictionaryFrm, DictionaryFrm); end; @@ -154,10 +167,18 @@ end; procedure TSmoothDataForm.OutBtnClick(Sender: TObject); begin - VarList.Items.Add(VariableEdit.Text); - VariableEdit.Text := ''; - OutBtn.Enabled := false; - InBtn.Enabled := true; + if VariableEdit.Text <> '' then + begin + VarList.Items.Add(VariableEdit.Text); + VariableEdit.Text := ''; + UpdateBtnStates; + end; +end; + +procedure TSmoothDataForm.UpdateBtnStates; +begin + InBtn.Enabled := (VarList.ItemIndex > -1) and (VariableEdit.Text = ''); + OutBtn.Enabled := (VariableEdit.Text <> ''); end; initialization