Fix: Update only when edited

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@2053 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
jujibo
2011-09-30 20:38:20 +00:00
parent e3213f10f1
commit 7bb64f5146

View File

@ -36,6 +36,7 @@ type
Field: TField; Field: TField;
updated: boolean; updated: boolean;
fMaxLength: integer; fMaxLength: integer;
edited: boolean;
procedure myEditEnter(Sender: TObject); procedure myEditEnter(Sender: TObject);
procedure myEditOnEditingDone(Sender: TObject); procedure myEditOnEditingDone(Sender: TObject);
procedure OnKeyPress(Sender: TObject; var key: char); procedure OnKeyPress(Sender: TObject; var key: char);
@ -54,6 +55,7 @@ type
private private
Field: TField; Field: TField;
updated: boolean; updated: boolean;
edited: boolean;
theValue: TDateTime; theValue: TDateTime;
fFormat: string; fFormat: string;
function getFormat: string; function getFormat: string;
@ -79,6 +81,7 @@ type
private private
Field: TField; Field: TField;
updated: boolean; updated: boolean;
edited: boolean;
theValue: TTime; theValue: TTime;
fFormat: string; fFormat: string;
function getFormat: string; function getFormat: string;
@ -104,6 +107,7 @@ type
private private
Field: TField; Field: TField;
updated: boolean; updated: boolean;
edited: boolean;
theValue: TDateTime; theValue: TDateTime;
fFormat: string; fFormat: string;
function getFormat: string; function getFormat: string;
@ -129,6 +133,7 @@ type
private private
theValue: integer; theValue: integer;
updated: boolean; updated: boolean;
edited: boolean;
Field: TField; Field: TField;
procedure myEditOnEnter(Sender: TObject); procedure myEditOnEnter(Sender: TObject);
procedure OnKeyPress(Sender: TObject; var key: char); procedure OnKeyPress(Sender: TObject; var key: char);
@ -149,6 +154,7 @@ type
private private
Field: TField; Field: TField;
updated: boolean; updated: boolean;
edited: boolean;
theValue: double; theValue: double;
fDecimals: integer; fDecimals: integer;
function getDecimals: integer; function getDecimals: integer;
@ -189,7 +195,7 @@ end;
procedure TJDbGridStringCtrl.myEditOnEditingDone(Sender: TObject); procedure TJDbGridStringCtrl.myEditOnEditingDone(Sender: TObject);
begin begin
if (not updated) then if edited and (not updated) then
begin begin
Field.DataSet.DisableControls; Field.DataSet.DisableControls;
if (fMaxLength > 0) and (UTF8Length(CellEditor.Text) > fMaxLength) then if (fMaxLength > 0) and (UTF8Length(CellEditor.Text) > fMaxLength) then
@ -203,7 +209,9 @@ end;
procedure TJDbGridStringCtrl.OnKeyPress(Sender: TObject; var key: char); procedure TJDbGridStringCtrl.OnKeyPress(Sender: TObject; var key: char);
begin begin
if (fMaxLength > 0) and (UTF8Length(CellEditor.Text) >= fMaxLength) then if (fMaxLength > 0) and (UTF8Length(CellEditor.Text) >= fMaxLength) then
key := #0; key := #0
else
edited := True;
end; end;
procedure TJDbGridStringCtrl.OnKeyDown(Sender: TObject; var Key: word; procedure TJDbGridStringCtrl.OnKeyDown(Sender: TObject; var Key: word;
@ -252,6 +260,7 @@ function TJDbGridStringCtrl.Editor(aGrid: TDBGrid;
aMaxLength: integer): TStringCellEditor; aMaxLength: integer): TStringCellEditor;
begin begin
theGrid := aGrid; theGrid := aGrid;
edited := False;
fMaxLength := aMaxLength; fMaxLength := aMaxLength;
Result := CellEditor; Result := CellEditor;
end; end;
@ -279,6 +288,8 @@ procedure TJDbGridDateTimeCtrl.myEditOnEditingDone(Sender: TObject);
begin begin
CellEditor.Caption := NormalizeDateTime(CellEditor.Caption, theValue); CellEditor.Caption := NormalizeDateTime(CellEditor.Caption, theValue);
if Length(CellEditor.Caption) = 0 then if Length(CellEditor.Caption) = 0 then
begin
if edited then
begin begin
Field.DataSet.DisableControls; Field.DataSet.DisableControls;
Field.DataSet.Edit; Field.DataSet.Edit;
@ -286,11 +297,12 @@ begin
theValue := 0; theValue := 0;
updated := True; updated := True;
Field.DataSet.EnableControls; Field.DataSet.EnableControls;
end;
end end
else else
if IsValidDateTimeString(CellEditor.Caption) then if IsValidDateTimeString(CellEditor.Caption) then
begin begin
if (not updated) then if edited and (not updated) then
begin begin
Field.DataSet.DisableControls; Field.DataSet.DisableControls;
theValue := StrToDateTime(CellEditor.Caption); theValue := StrToDateTime(CellEditor.Caption);
@ -321,7 +333,9 @@ end;
procedure TJDbGridDateTimeCtrl.OnKeyPress(Sender: TObject; var key: char); procedure TJDbGridDateTimeCtrl.OnKeyPress(Sender: TObject; var key: char);
begin begin
if not (Key in ['0'..'9', #8, #9, '.', '-', '/', ':', ' ']) then if not (Key in ['0'..'9', #8, #9, '.', '-', '/', ':', ' ']) then
Key := #0; Key := #0
else
edited := True;
end; end;
procedure TJDbGridDateTimeCtrl.OnKeyDown(Sender: TObject; var Key: word; procedure TJDbGridDateTimeCtrl.OnKeyDown(Sender: TObject; var Key: word;
@ -394,6 +408,7 @@ end;
function TJDbGridDateTimeCtrl.Editor(aGrid: TDBGrid): TStringCellEditor; function TJDbGridDateTimeCtrl.Editor(aGrid: TDBGrid): TStringCellEditor;
begin begin
theGrid := aGrid; theGrid := aGrid;
edited := False;
Result := CellEditor; Result := CellEditor;
end; end;
@ -420,6 +435,8 @@ procedure TJDbGridTimeCtrl.myEditOnEditingDone(Sender: TObject);
begin begin
CellEditor.Caption := NormalizeTime(CellEditor.Caption, theValue); CellEditor.Caption := NormalizeTime(CellEditor.Caption, theValue);
if Length(CellEditor.Caption) = 0 then if Length(CellEditor.Caption) = 0 then
begin
if edited then
begin begin
Field.DataSet.DisableControls; Field.DataSet.DisableControls;
Field.DataSet.Edit; Field.DataSet.Edit;
@ -427,11 +444,12 @@ begin
theValue := 0; theValue := 0;
updated := True; updated := True;
Field.DataSet.EnableControls; Field.DataSet.EnableControls;
end;
end end
else else
if IsValidTimeString(CellEditor.Caption) then if IsValidTimeString(CellEditor.Caption) then
begin begin
if (not updated) then if edited and (not updated) then
begin begin
Field.DataSet.DisableControls; Field.DataSet.DisableControls;
theValue := StrToTime(CellEditor.Caption); theValue := StrToTime(CellEditor.Caption);
@ -462,7 +480,9 @@ end;
procedure TJDbGridTimeCtrl.OnKeyPress(Sender: TObject; var key: char); procedure TJDbGridTimeCtrl.OnKeyPress(Sender: TObject; var key: char);
begin begin
if not (Key in ['0'..'9', #8, #9, ':']) then if not (Key in ['0'..'9', #8, #9, ':']) then
Key := #0; Key := #0
else
edited := True;
end; end;
procedure TJDbGridTimeCtrl.OnKeyDown(Sender: TObject; var Key: word; procedure TJDbGridTimeCtrl.OnKeyDown(Sender: TObject; var Key: word;
@ -542,6 +562,7 @@ end;
function TJDbGridTimeCtrl.Editor(aGrid: TDBGrid): TStringCellEditor; function TJDbGridTimeCtrl.Editor(aGrid: TDBGrid): TStringCellEditor;
begin begin
theGrid := aGrid; theGrid := aGrid;
edited := False;
Result := CellEditor; Result := CellEditor;
end; end;
@ -568,6 +589,8 @@ procedure TJDbGridDateCtrl.myEditOnEditingDone(Sender: TObject);
begin begin
CellEditor.Caption := NormalizeDate(CellEditor.Caption, theValue); CellEditor.Caption := NormalizeDate(CellEditor.Caption, theValue);
if Length(CellEditor.Caption) = 0 then if Length(CellEditor.Caption) = 0 then
begin
if edited then
begin begin
Field.DataSet.DisableControls; Field.DataSet.DisableControls;
Field.DataSet.Edit; Field.DataSet.Edit;
@ -575,11 +598,12 @@ begin
theValue := 0; theValue := 0;
updated := True; updated := True;
Field.DataSet.EnableControls; Field.DataSet.EnableControls;
end;
end end
else else
if IsValidDateString(CellEditor.Caption) then if IsValidDateString(CellEditor.Caption) then
begin begin
if (not updated) then if edited and (not updated) then
begin begin
Field.DataSet.DisableControls; Field.DataSet.DisableControls;
theValue := StrToDate(CellEditor.Caption); theValue := StrToDate(CellEditor.Caption);
@ -611,7 +635,9 @@ end;
procedure TJDbGridDateCtrl.OnKeyPress(Sender: TObject; var key: char); procedure TJDbGridDateCtrl.OnKeyPress(Sender: TObject; var key: char);
begin begin
if not (Key in ['0'..'9', #8, #9, '.', '-', '/']) then if not (Key in ['0'..'9', #8, #9, '.', '-', '/']) then
Key := #0; Key := #0
else
edited := True;
end; end;
procedure TJDbGridDateCtrl.OnKeyDown(Sender: TObject; var Key: word; procedure TJDbGridDateCtrl.OnKeyDown(Sender: TObject; var Key: word;
@ -685,6 +711,7 @@ end;
function TJDbGridDateCtrl.Editor(aGrid: TDBGrid): TStringCellEditor; function TJDbGridDateCtrl.Editor(aGrid: TDBGrid): TStringCellEditor;
begin begin
theGrid := aGrid; theGrid := aGrid;
edited := False;
Result := CellEditor; Result := CellEditor;
end; end;
@ -711,7 +738,7 @@ procedure TJDbGridDoubleCtrl.myEditOnEditingDone(Sender: TObject);
begin begin
if IsValidFloat(CellEditor.Caption) then if IsValidFloat(CellEditor.Caption) then
begin begin
if (not updated) then if edited and (not updated) then
begin begin
theValue := StrToFloat(CellEditor.Caption); theValue := StrToFloat(CellEditor.Caption);
Field.DataSet.DisableControls; Field.DataSet.DisableControls;
@ -742,7 +769,9 @@ begin
if (key = DecimalSeparator) and (Pos(key, CellEditor.Caption) > 0) then if (key = DecimalSeparator) and (Pos(key, CellEditor.Caption) > 0) then
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
else
edited := True;
//if (Key = DecimalSeparator) and (fDecimals = 0) then //if (Key = DecimalSeparator) and (fDecimals = 0) then
// Key := #0; // Note: decimal=0 avoids rounding // Key := #0; // Note: decimal=0 avoids rounding
end; end;
@ -825,6 +854,7 @@ function TJDbGridDoubleCtrl.Editor(aGrid: TDBGrid;
aDecimals: integer): TStringCellEditor; aDecimals: integer): TStringCellEditor;
begin begin
decimals := aDecimals; decimals := aDecimals;
edited := False;
theGrid := aGrid; theGrid := aGrid;
Result := CellEditor; Result := CellEditor;
end; end;
@ -846,7 +876,9 @@ end;
procedure TJDbGridIntegerCtrl.OnKeyPress(Sender: TObject; var key: char); procedure TJDbGridIntegerCtrl.OnKeyPress(Sender: TObject; var key: char);
begin begin
if not (Key in ['0'..'9', #8, #9, '-']) then if not (Key in ['0'..'9', #8, #9, '-']) then
Key := #0; Key := #0
else
edited := True;
end; end;
procedure TJDbGridIntegerCtrl.OnKeyDown(Sender: TObject; var Key: word; procedure TJDbGridIntegerCtrl.OnKeyDown(Sender: TObject; var Key: word;
@ -892,13 +924,14 @@ procedure TJDbGridIntegerCtrl.myEditOnEditingDone(Sender: TObject);
begin begin
if IsValidInteger(CellEditor.Caption) then if IsValidInteger(CellEditor.Caption) then
begin begin
if (not updated) then if edited and (not updated) then
begin begin
Field.DataSet.DisableControls; Field.DataSet.DisableControls;
theValue := StrToInt(CellEditor.Caption); theValue := StrToInt(CellEditor.Caption);
Field.DataSet.Edit; Field.DataSet.Edit;
Field.AsInteger := theValue; Field.AsInteger := theValue;
field.DataSet.EnableControls; field.DataSet.EnableControls;
updated := True;
end; end;
end end
else else
@ -935,6 +968,7 @@ end;
function TJDbGridIntegerCtrl.Editor(aGrid: TDBGrid): TStringCellEditor; function TJDbGridIntegerCtrl.Editor(aGrid: TDBGrid): TStringCellEditor;
begin begin
theGrid := aGrid; theGrid := aGrid;
edited := False;
Result := CellEditor; Result := CellEditor;
end; end;