Better implementation to detect if a value has been edited

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@2065 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
jujibo
2011-10-07 15:32:22 +00:00
parent 617d6901f6
commit efbe37b5a8

View File

@ -36,7 +36,6 @@ 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);
@ -55,7 +54,6 @@ 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;
@ -81,7 +79,6 @@ 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;
@ -107,7 +104,6 @@ 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;
@ -133,7 +129,6 @@ 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);
@ -154,7 +149,6 @@ 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;
@ -196,18 +190,21 @@ end;
procedure TJDbGridStringCtrl.myEditOnEditingDone(Sender: TObject); procedure TJDbGridStringCtrl.myEditOnEditingDone(Sender: TObject);
begin begin
if edited and (not updated) then if (not updated) then
begin begin
Field.DataSet.DisableControls; if CellEditor.Text <> Field.AsString then
Field.DataSet.Edit; begin
Field.AsString := CellEditor.Text; Field.DataSet.DisableControls;
field.DataSet.EnableControls; Field.DataSet.Edit;
Field.AsString := CellEditor.Text;
field.DataSet.EnableControls;
end;
end; end;
end; end;
procedure TJDbGridStringCtrl.OnKeyPress(Sender: TObject; var key: char); procedure TJDbGridStringCtrl.OnKeyPress(Sender: TObject; var key: char);
begin begin
edited := True; // nothing right now
end; end;
procedure TJDbGridStringCtrl.OnKeyDown(Sender: TObject; var Key: word; procedure TJDbGridStringCtrl.OnKeyDown(Sender: TObject; var Key: word;
@ -254,7 +251,6 @@ 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;
@ -282,7 +278,7 @@ procedure TJDbGridDateTimeCtrl.myEditOnEditingDone(Sender: TObject);
begin begin
if Length(CellEditor.Caption) = 0 then if Length(CellEditor.Caption) = 0 then
begin begin
if edited then if Field.Value <> Null then
begin begin
Field.DataSet.DisableControls; Field.DataSet.DisableControls;
Field.DataSet.Edit; Field.DataSet.Edit;
@ -297,13 +293,16 @@ begin
CellEditor.Caption := NormalizeDateTime(CellEditor.Caption, theValue); CellEditor.Caption := NormalizeDateTime(CellEditor.Caption, theValue);
if IsValidDateTimeString(CellEditor.Caption) then if IsValidDateTimeString(CellEditor.Caption) then
begin begin
if edited and (not updated) then if (not updated) then
begin begin
Field.DataSet.DisableControls;
theValue := StrToDateTime(CellEditor.Caption); theValue := StrToDateTime(CellEditor.Caption);
Field.DataSet.Edit; if theValue <> Field.AsDateTime then
Field.AsDateTime := theValue; begin
Field.DataSet.EnableControls; Field.DataSet.DisableControls;
Field.DataSet.Edit;
Field.AsDateTime := theValue;
Field.DataSet.EnableControls;
end;
end; end;
end end
else else
@ -330,19 +329,15 @@ 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;
Shift: TShiftState); Shift: TShiftState);
begin begin
if Key in [VK_DELETE, VK_BACK] then
edited := True;
if Length(CellEditor.Caption) = 0 then if Length(CellEditor.Caption) = 0 then
begin begin
if edited then if Field.Value <> Null then
begin begin
Field.DataSet.Edit; Field.DataSet.Edit;
Field.Value := Null; Field.Value := Null;
@ -418,7 +413,6 @@ 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;
@ -445,7 +439,7 @@ procedure TJDbGridTimeCtrl.myEditOnEditingDone(Sender: TObject);
begin begin
if Length(CellEditor.Caption) = 0 then if Length(CellEditor.Caption) = 0 then
begin begin
if edited then if Field.Value <> Null then
begin begin
Field.DataSet.DisableControls; Field.DataSet.DisableControls;
Field.DataSet.Edit; Field.DataSet.Edit;
@ -460,13 +454,16 @@ begin
CellEditor.Caption := NormalizeTime(CellEditor.Caption, theValue); CellEditor.Caption := NormalizeTime(CellEditor.Caption, theValue);
if IsValidTimeString(CellEditor.Caption) then if IsValidTimeString(CellEditor.Caption) then
begin begin
if edited and (not updated) then if (not updated) then
begin begin
Field.DataSet.DisableControls;
theValue := StrToTime(CellEditor.Caption); theValue := StrToTime(CellEditor.Caption);
Field.DataSet.Edit; if theValue <> Field.AsDateTime then
Field.AsDateTime := theValue; begin
Field.DataSet.EnableControls; Field.DataSet.DisableControls;
Field.DataSet.Edit;
Field.AsDateTime := theValue;
Field.DataSet.EnableControls;
end;
end; end;
end end
else else
@ -492,19 +489,15 @@ 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;
Shift: TShiftState); Shift: TShiftState);
begin begin
if Key in [VK_DELETE, VK_BACK] then
edited := True;
if Length(CellEditor.Caption) = 0 then if Length(CellEditor.Caption) = 0 then
begin begin
if edited then if Field.Value <> Null then
begin begin
Field.DataSet.Edit; Field.DataSet.Edit;
Field.Value := Null; Field.Value := Null;
@ -579,7 +572,6 @@ 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;
@ -606,7 +598,7 @@ procedure TJDbGridDateCtrl.myEditOnEditingDone(Sender: TObject);
begin begin
if Length(CellEditor.Caption) = 0 then if Length(CellEditor.Caption) = 0 then
begin begin
if edited then if Field.Value <> Null then
begin begin
Field.DataSet.DisableControls; Field.DataSet.DisableControls;
Field.DataSet.Edit; Field.DataSet.Edit;
@ -621,13 +613,16 @@ begin
CellEditor.Caption := NormalizeDate(CellEditor.Caption, theValue); CellEditor.Caption := NormalizeDate(CellEditor.Caption, theValue);
if IsValidDateString(CellEditor.Caption) then if IsValidDateString(CellEditor.Caption) then
begin begin
if edited and (not updated) then if (not updated) then
begin begin
Field.DataSet.DisableControls;
theValue := StrToDate(CellEditor.Caption); theValue := StrToDate(CellEditor.Caption);
Field.DataSet.Edit; if theValue <> Field.AsDateTime then
Field.AsDateTime := theValue; begin
field.DataSet.EnableControls; Field.DataSet.DisableControls;
Field.DataSet.Edit;
Field.AsDateTime := theValue;
field.DataSet.EnableControls;
end;
end; end;
end end
else else
@ -636,7 +631,6 @@ begin
CellEditor.Text := FormatDateTime(DisplayFormat, Field.AsDateTime); CellEditor.Text := FormatDateTime(DisplayFormat, Field.AsDateTime);
end; end;
end; end;
//formatInput;
end; end;
procedure TJDbGridDateCtrl.formatInput; procedure TJDbGridDateCtrl.formatInput;
@ -654,19 +648,15 @@ 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;
Shift: TShiftState); Shift: TShiftState);
begin begin
if Key in [VK_DELETE, VK_BACK] then
edited := True;
if Length(CellEditor.Caption) = 0 then if Length(CellEditor.Caption) = 0 then
begin begin
if edited then if Field.Value <> null then
begin begin
Field.DataSet.Edit; Field.DataSet.Edit;
Field.Value := Null; Field.Value := Null;
@ -743,7 +733,6 @@ 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;
@ -770,15 +759,18 @@ procedure TJDbGridDoubleCtrl.myEditOnEditingDone(Sender: TObject);
begin begin
if IsValidFloat(CellEditor.Caption) then if IsValidFloat(CellEditor.Caption) then
begin begin
if edited and (not updated) then if (not updated) then
begin begin
theValue := StrToFloat(CellEditor.Caption); theValue := StrToFloat(CellEditor.Caption);
Field.DataSet.DisableControls; if theValue <> Field.AsFloat then
Field.DataSet.Edit; begin
if decimals > 0 then Field.DataSet.DisableControls;
theValue := ScaleTo(theValue, fDecimals); Field.DataSet.Edit;
Field.Value := theValue; if decimals > 0 then
Field.DataSet.EnableControls; theValue := ScaleTo(theValue, fDecimals);
Field.Value := theValue;
Field.DataSet.EnableControls;
end;
end; end;
end end
else else
@ -801,9 +793,7 @@ 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;
@ -886,7 +876,6 @@ 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;
@ -908,9 +897,7 @@ 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;
@ -956,14 +943,17 @@ procedure TJDbGridIntegerCtrl.myEditOnEditingDone(Sender: TObject);
begin begin
if IsValidInteger(CellEditor.Caption) then if IsValidInteger(CellEditor.Caption) then
begin begin
if edited and (not updated) then if (not updated) then
begin begin
Field.DataSet.DisableControls;
theValue := StrToInt(CellEditor.Caption); theValue := StrToInt(CellEditor.Caption);
Field.DataSet.Edit; if theValue <> Field.AsInteger then
Field.AsInteger := theValue; begin
field.DataSet.EnableControls; Field.DataSet.DisableControls;
updated := True; Field.DataSet.Edit;
Field.AsInteger := theValue;
field.DataSet.EnableControls;
updated := True;
end;
end; end;
end end
else else
@ -1000,7 +990,6 @@ 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;