You've already forked lazarus-ccr
Decimals/Scale option implemented for ftFloat and ftCurrency. Right use of Size property in ftBCD fields
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@2026 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
@ -1,3 +1,20 @@
|
|||||||
|
{ JDBGridControl
|
||||||
|
|
||||||
|
Copyright (C) 2011 Julio Jiménez Borreguero
|
||||||
|
Contact: jujibo at gmail dot com
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or modify it
|
||||||
|
under the same terms as the Lazarus Component Library (LCL)
|
||||||
|
|
||||||
|
See the file license-jujiboutils.txt and COPYING.LGPL, included in this distribution,
|
||||||
|
for details about the license.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
unit JDBGridControl;
|
unit JDBGridControl;
|
||||||
|
|
||||||
{$mode objfpc}{$H+}
|
{$mode objfpc}{$H+}
|
||||||
@ -149,8 +166,9 @@ begin
|
|||||||
ftDate: Result := dateDbGridControl.Editor(Self);
|
ftDate: Result := dateDbGridControl.Editor(Self);
|
||||||
ftTime: Result := timeDbGridControl.Editor(Self);
|
ftTime: Result := timeDbGridControl.Editor(Self);
|
||||||
ftDateTime: Result := dateTimeDbGridControl.Editor(Self);
|
ftDateTime: Result := dateTimeDbGridControl.Editor(Self);
|
||||||
ftCurrency, ftFloat, ftBCD: Result := doubleDbGridControl.Editor(Self);
|
ftCurrency, ftFloat: Result :=
|
||||||
// TODO: ftDateTime. strings?
|
doubleDbGridControl.Editor(Self, Columns.Items[Column - 1].Decimals);
|
||||||
|
ftBCD: Result := doubleDbGridControl.Editor(Self, aField.Size);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
@ -129,19 +129,19 @@ type
|
|||||||
Field: TField;
|
Field: TField;
|
||||||
updated: boolean;
|
updated: boolean;
|
||||||
theValue: double;
|
theValue: double;
|
||||||
fDecimales: integer;
|
fDecimals: integer;
|
||||||
function getDecimales: integer;
|
function getDecimals: integer;
|
||||||
procedure myEditOnEnter(Sender: TObject);
|
procedure myEditOnEnter(Sender: TObject);
|
||||||
procedure myEditOnEditingDone(Sender: TObject);
|
procedure myEditOnEditingDone(Sender: TObject);
|
||||||
procedure setDecimales(const AValue: integer);
|
procedure setDecimals(const AValue: integer);
|
||||||
procedure OnKeyPress(Sender: TObject; var key: char);
|
procedure OnKeyPress(Sender: TObject; var key: char);
|
||||||
procedure OnKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
|
procedure OnKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
|
||||||
function IsValidFloat(const Value: string): boolean;
|
function IsValidFloat(const Value: string): boolean;
|
||||||
function textNumber(const aValue: string): string;
|
function ScaleTo(const AValue: double; const NDecimals: integer): double;
|
||||||
public
|
public
|
||||||
CellEditor: TStringCellEditor;
|
CellEditor: TStringCellEditor;
|
||||||
theGrid: TDBGrid;
|
theGrid: TDBGrid;
|
||||||
property decimales: integer read getDecimales write setDecimales;
|
property decimals: integer read getDecimals write setDecimals;
|
||||||
constructor Create;
|
constructor Create;
|
||||||
destructor Destroy; override;
|
destructor Destroy; override;
|
||||||
function Editor(aGrid: TDBGrid; aDecimals: integer = 2): TStringCellEditor;
|
function Editor(aGrid: TDBGrid; aDecimals: integer = 2): TStringCellEditor;
|
||||||
@ -587,9 +587,9 @@ end;
|
|||||||
|
|
||||||
{ TJDbGridDoubleCtrl }
|
{ TJDbGridDoubleCtrl }
|
||||||
|
|
||||||
function TJDbGridDoubleCtrl.getDecimales: integer;
|
function TJDbGridDoubleCtrl.getDecimals: integer;
|
||||||
begin
|
begin
|
||||||
Result := fDecimales;
|
Result := fDecimals;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TJDbGridDoubleCtrl.myEditOnEnter(Sender: TObject);
|
procedure TJDbGridDoubleCtrl.myEditOnEnter(Sender: TObject);
|
||||||
@ -613,8 +613,9 @@ begin
|
|||||||
theValue := StrToFloat(CellEditor.Caption);
|
theValue := StrToFloat(CellEditor.Caption);
|
||||||
Field.DataSet.DisableControls;
|
Field.DataSet.DisableControls;
|
||||||
Field.DataSet.Edit;
|
Field.DataSet.Edit;
|
||||||
Field.AsFloat := theValue;
|
if decimals > 0 then
|
||||||
Field.Value := textNumber(Field.DisplayText);
|
theValue := ScaleTo(theValue, fDecimals);
|
||||||
|
Field.Value := theValue;
|
||||||
Field.DataSet.EnableControls;
|
Field.DataSet.EnableControls;
|
||||||
end;
|
end;
|
||||||
end
|
end
|
||||||
@ -625,10 +626,10 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TJDbGridDoubleCtrl.setDecimales(const AValue: integer);
|
procedure TJDbGridDoubleCtrl.setDecimals(const AValue: integer);
|
||||||
begin
|
begin
|
||||||
if (AValue >= 0) and (AValue < 5) then
|
if (AValue >= 0) and (AValue < 11) then
|
||||||
fDecimales := AValue;
|
fDecimals := AValue;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TJDbGridDoubleCtrl.OnKeyPress(Sender: TObject; var key: char);
|
procedure TJDbGridDoubleCtrl.OnKeyPress(Sender: TObject; var key: char);
|
||||||
@ -639,8 +640,8 @@ begin
|
|||||||
key := #0;
|
key := #0;
|
||||||
if not (Key in ['0'..'9', DecimalSeparator, '+', '-', #8, #9]) then
|
if not (Key in ['0'..'9', DecimalSeparator, '+', '-', #8, #9]) then
|
||||||
Key := #0;
|
Key := #0;
|
||||||
if (Key = DecimalSeparator) and (fDecimales = 0) then
|
//if (Key = DecimalSeparator) and (fDecimals = 0) then
|
||||||
Key := #0;
|
// Key := #0; // Note: decimal=0 avoids rounding
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TJDbGridDoubleCtrl.OnKeyDown(Sender: TObject; var Key: word;
|
procedure TJDbGridDoubleCtrl.OnKeyDown(Sender: TObject; var Key: word;
|
||||||
@ -661,7 +662,7 @@ begin
|
|||||||
CellEditor.Text := ''
|
CellEditor.Text := ''
|
||||||
else
|
else
|
||||||
CellEditor.Text := FloatToStr(Field.AsFloat);
|
CellEditor.Text := FloatToStr(Field.AsFloat);
|
||||||
//CellEditor.Text := CurrToStr(redondear(Field.AsCurrency, fDecimales));
|
//CellEditor.Text := CurrToStr(redondear(Field.AsCurrency, fDecimals));
|
||||||
updated := True;
|
updated := True;
|
||||||
theGrid.SetFocus; // No perder el foco
|
theGrid.SetFocus; // No perder el foco
|
||||||
end
|
end
|
||||||
@ -677,9 +678,10 @@ begin
|
|||||||
begin
|
begin
|
||||||
theValue := StrToFloat(CellEditor.Caption);
|
theValue := StrToFloat(CellEditor.Caption);
|
||||||
Field.DataSet.Edit;
|
Field.DataSet.Edit;
|
||||||
Field.AsFloat := theValue;//redondear(theValue, fDecimales);
|
if decimals > 0 then
|
||||||
// normalize value
|
theValue := ScaleTo(theValue, fDecimals);
|
||||||
Field.Value := textNumber(Field.DisplayText);
|
Field.Value := theValue;
|
||||||
|
;
|
||||||
CellEditor.Text := Field.AsString;
|
CellEditor.Text := Field.AsString;
|
||||||
updated := True;
|
updated := True;
|
||||||
end;
|
end;
|
||||||
@ -694,27 +696,10 @@ begin
|
|||||||
Result := True;
|
Result := True;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TJDbGridDoubleCtrl.textNumber(const aValue: string): string;
|
function TJDbGridDoubleCtrl.ScaleTo(const AValue: double;
|
||||||
var
|
const NDecimals: integer): double;
|
||||||
i: integer;
|
|
||||||
aText: string;
|
|
||||||
begin
|
begin
|
||||||
// Because there is no way to know the number scale (or I don't know how
|
Result := round(AValue * power(10, NDecimals)) / power(10, NDecimals);
|
||||||
// use display format to get a valid number with the desired scale (decimals)
|
|
||||||
// so if display format is 0.00 we'll get a number rounded to two decimals
|
|
||||||
// This is a bit tricky but works mostly...
|
|
||||||
aText := '';
|
|
||||||
if Length(aValue) = 0 then
|
|
||||||
aText := aValue
|
|
||||||
else
|
|
||||||
begin
|
|
||||||
if aValue[1] = '-' then
|
|
||||||
aText := '-'; // negative number?
|
|
||||||
for i := 1 to length(aValue) do
|
|
||||||
if aValue[i] in ['0'..'9', DecimalSeparator] then
|
|
||||||
aText += aValue[i];
|
|
||||||
end;
|
|
||||||
Result := aText;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
constructor TJDbGridDoubleCtrl.Create;
|
constructor TJDbGridDoubleCtrl.Create;
|
||||||
@ -724,7 +709,7 @@ begin
|
|||||||
CellEditor.OnEnter := @myEditOnEnter;
|
CellEditor.OnEnter := @myEditOnEnter;
|
||||||
CellEditor.OnKeyDown := @OnKeyDown;
|
CellEditor.OnKeyDown := @OnKeyDown;
|
||||||
CellEditor.OnEditingDone := @myEditOnEditingDone;
|
CellEditor.OnEditingDone := @myEditOnEditingDone;
|
||||||
fDecimales := 2;
|
fDecimals := 2;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
destructor TJDbGridDoubleCtrl.Destroy;
|
destructor TJDbGridDoubleCtrl.Destroy;
|
||||||
@ -736,7 +721,7 @@ end;
|
|||||||
function TJDbGridDoubleCtrl.Editor(aGrid: TDBGrid;
|
function TJDbGridDoubleCtrl.Editor(aGrid: TDBGrid;
|
||||||
aDecimals: integer): TStringCellEditor;
|
aDecimals: integer): TStringCellEditor;
|
||||||
begin
|
begin
|
||||||
decimales := aDecimals;
|
decimals := aDecimals;
|
||||||
theGrid := aGrid;
|
theGrid := aGrid;
|
||||||
Result := CellEditor;
|
Result := CellEditor;
|
||||||
end;
|
end;
|
||||||
|
Reference in New Issue
Block a user