You've already forked lazarus-ccr
Added: ftTime support to TJDBGridControl
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@1984 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
@ -28,8 +28,11 @@ function CountChar(const s: string; ch: char): integer;
|
||||
procedure Split(const Delimiter: char; Input: string; Strings: TStrings);
|
||||
function NormalizeDate(const Value: string; theValue: TDateTime;
|
||||
const theFormat: string = ''): string;
|
||||
function NormalizeTime(const Value: string; theValue: TTime;
|
||||
const theFormat: string = ''): string;
|
||||
function NormalizeDateSeparator(const s: string): string;
|
||||
function IsValidDateString(const Value: string): boolean;
|
||||
function IsValidTimeString(const Value: string): boolean;
|
||||
|
||||
implementation
|
||||
|
||||
@ -55,7 +58,6 @@ function NormalizeDate(const Value: string; theValue: TDateTime;
|
||||
const theFormat: string): string;
|
||||
var
|
||||
texto: string;
|
||||
i: integer;
|
||||
d, m, y: word;
|
||||
ds, ms, ys: string;
|
||||
aDate: TDateTime;
|
||||
@ -112,7 +114,6 @@ begin
|
||||
texto := Value;
|
||||
texto := NormalizeDateSeparator(texto);
|
||||
Result := texto; // default value
|
||||
i := countchar(texto, DateSeparator);
|
||||
tokens := TStringList.Create;
|
||||
Split(DateSeparator, texto, tokens);
|
||||
if tokens.Count > 0 then
|
||||
@ -133,6 +134,72 @@ begin
|
||||
tokens.Free;
|
||||
end;
|
||||
|
||||
function NormalizeTime(const Value: string; theValue: TTime;
|
||||
const theFormat: string): string;
|
||||
var
|
||||
hh, mm, ss, ms: word;
|
||||
hhs, mms, sss, mss: string;
|
||||
texto: string;
|
||||
aTimeFormat: string;
|
||||
aTime: TTime;
|
||||
tokens: TStringList;
|
||||
|
||||
function TimeString: string;
|
||||
begin
|
||||
Result := hhs + TimeSeparator + mms + TimeSeparator + sss;// + TimeSeparator + mss;
|
||||
end;
|
||||
|
||||
procedure TimeForm;
|
||||
begin
|
||||
if tokens[0] <> '' then
|
||||
hhs := tokens[0];
|
||||
if (tokens.Count > 1) and (tokens[1] <> '') then
|
||||
mms := tokens[1];
|
||||
if (tokens.Count > 2) and (tokens[2] <> '') then
|
||||
sss := tokens[2];
|
||||
if (tokens.Count > 3) and (tokens[3] <> '') then
|
||||
mss := tokens[3];
|
||||
texto := TimeString;
|
||||
end;
|
||||
|
||||
begin
|
||||
if theFormat = '' then
|
||||
aTimeFormat := ShortTimeFormat
|
||||
else
|
||||
aTimeFormat := theFormat;
|
||||
if theValue = 0 then
|
||||
begin
|
||||
DecodeTime(Now, hh, mm, ss, ms);
|
||||
//ShowMessage('sin hora');
|
||||
end
|
||||
else
|
||||
begin
|
||||
DecodeTime(theValue, hh, mm, ss, ms);
|
||||
//ShowMessage('con hora');
|
||||
end;
|
||||
|
||||
hhs := IntToStr(hh);
|
||||
mms := IntToStr(mm);
|
||||
sss := IntToStr(ss);
|
||||
mss := IntToStr(ms);
|
||||
//ShowMessage(TimeString);
|
||||
texto := Value;
|
||||
Result := texto; // default value
|
||||
tokens := TStringList.Create;
|
||||
Split(TimeSeparator, texto, tokens);
|
||||
if tokens.Count > 0 then
|
||||
begin
|
||||
TimeForm;
|
||||
if IsValidTimeString(texto) then
|
||||
begin
|
||||
aTime := StrToTime(texto);
|
||||
Result := TimeString;// FormatDateTime(aTimeFormat, aTime);
|
||||
end;
|
||||
end;
|
||||
tokens.Free;
|
||||
//ShowMessage('Hora normalizada: ' + Result);
|
||||
end;
|
||||
|
||||
function NormalizeDateSeparator(const s: string): string;
|
||||
var
|
||||
i: integer;
|
||||
@ -151,5 +218,14 @@ begin
|
||||
Result := True;
|
||||
end;
|
||||
|
||||
function IsValidTimeString(const Value: string): boolean;
|
||||
var
|
||||
bTime: TDateTime;
|
||||
begin
|
||||
Result := TryStrToTime(Value, bTime);
|
||||
//if Result = False then
|
||||
// ShowMessage('Hora incorrecta: ' + Value);
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
|
@ -16,6 +16,7 @@ type
|
||||
private
|
||||
{ Private declarations }
|
||||
dateDbGridControl: TJDbGridDateCtrl;
|
||||
timeDbGridControl: TJDbGridTimeCtrl;
|
||||
integerDbGridControl: TJDbGridIntegerCtrl;
|
||||
doubleDbGridControl: TJDbGridDoubleCtrl;
|
||||
protected
|
||||
@ -73,8 +74,9 @@ begin
|
||||
case aField.DataType of
|
||||
ftSmallint, ftInteger: Result := integerDbGridControl.Editor(Self);
|
||||
ftDate: Result := dateDbGridControl.Editor(Self);
|
||||
ftTime: Result := timeDbGridControl.Editor(Self);
|
||||
ftCurrency, ftFloat, ftBCD: Result := doubleDbGridControl.Editor(Self);
|
||||
// TODO: ftDateTime and ftTime. strings?
|
||||
// TODO: ftDateTime. strings?
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
@ -83,6 +85,7 @@ constructor TJDBGridControl.Create(TheOwner: TComponent);
|
||||
begin
|
||||
inherited Create(TheOwner);
|
||||
dateDbGridControl := TJDbGridDateCtrl.Create;
|
||||
timeDbGridControl := TJDbGridTimeCtrl.Create;
|
||||
integerDbGridControl := TJDbGridIntegerCtrl.Create;
|
||||
doubleDbGridControl := TJDbGridDoubleCtrl.Create;
|
||||
end;
|
||||
@ -90,6 +93,7 @@ end;
|
||||
destructor TJDBGridControl.Destroy;
|
||||
begin
|
||||
dateDbGridControl.Free;
|
||||
timeDbGridControl.Free;
|
||||
integerDbGridControl.Free;
|
||||
doubleDbGridControl.Free;
|
||||
inherited Destroy;
|
||||
|
@ -27,6 +27,31 @@ uses
|
||||
|
||||
type
|
||||
|
||||
{ TJDbGridTimeCtrl }
|
||||
|
||||
TJDbGridTimeCtrl = class(TObject)
|
||||
private
|
||||
Field: TField;
|
||||
updated: boolean;
|
||||
theValue: TTime;
|
||||
fFormat: string;
|
||||
function getFormat: string;
|
||||
procedure myEditEnter(Sender: TObject);
|
||||
procedure myEditOnEditingDone(Sender: TObject);
|
||||
procedure formatInput;
|
||||
procedure setFormat(const AValue: string);
|
||||
procedure OnKeyPress(Sender: TObject; var key: char);
|
||||
procedure OnKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
|
||||
public
|
||||
CellEditor: TStringCellEditor;
|
||||
theGrid: TDBGrid;
|
||||
function isNull: boolean;
|
||||
property format: string read getFormat write setFormat;
|
||||
constructor Create;
|
||||
destructor Destroy; override;
|
||||
function Editor(aGrid: TDBGrid): TStringCellEditor;
|
||||
end;
|
||||
|
||||
{ TJDbGridDateCtrl }
|
||||
|
||||
TJDbGridDateCtrl = class(TObject)
|
||||
@ -103,6 +128,138 @@ implementation
|
||||
uses
|
||||
Math;
|
||||
|
||||
{ TJDbGridTimeCtrl }
|
||||
|
||||
function TJDbGridTimeCtrl.getFormat: string;
|
||||
begin
|
||||
Result := fFormat;
|
||||
end;
|
||||
|
||||
procedure TJDbGridTimeCtrl.myEditEnter(Sender: TObject);
|
||||
begin
|
||||
Field := theGrid.SelectedField;
|
||||
CellEditor.BoundsRect := theGrid.SelectedFieldRect;
|
||||
CellEditor.Text := Field.AsString;
|
||||
CellEditor.OnKeyPress := @OnKeyPress; // Recuperamos el control :-p
|
||||
CellEditor.OnKeyDown := @OnKeyDown;
|
||||
theValue := Field.AsDateTime;
|
||||
updated := False;
|
||||
CellEditor.SelectAll;
|
||||
end;
|
||||
|
||||
procedure TJDbGridTimeCtrl.myEditOnEditingDone(Sender: TObject);
|
||||
begin
|
||||
CellEditor.Caption := NormalizeTime(CellEditor.Caption, theValue);
|
||||
if Length(CellEditor.Caption) = 0 then
|
||||
theValue := 0
|
||||
else
|
||||
if IsValidTimeString(CellEditor.Caption) then
|
||||
begin
|
||||
if (not updated) then
|
||||
begin
|
||||
theValue := StrToTime(CellEditor.Caption);
|
||||
Field.DataSet.Edit;
|
||||
Field.AsDateTime := theValue;
|
||||
end;
|
||||
end
|
||||
else
|
||||
begin
|
||||
ShowMessage(CellEditor.Caption + ' no es una hora válida: Editing done');
|
||||
CellEditor.Text := FormatDateTime(format, theValue);
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TJDbGridTimeCtrl.formatInput;
|
||||
begin
|
||||
if theValue <> 0 then
|
||||
CellEditor.Caption := FormatDateTime(format, theValue);
|
||||
end;
|
||||
|
||||
procedure TJDbGridTimeCtrl.setFormat(const AValue: string);
|
||||
begin
|
||||
fFormat := AValue;
|
||||
formatInput;
|
||||
end;
|
||||
|
||||
procedure TJDbGridTimeCtrl.OnKeyPress(Sender: TObject; var key: char);
|
||||
begin
|
||||
if not (Key in ['0'..'9', #8, #9, ':']) then
|
||||
Key := #0;
|
||||
end;
|
||||
|
||||
procedure TJDbGridTimeCtrl.OnKeyDown(Sender: TObject; var Key: word;
|
||||
Shift: TShiftState);
|
||||
begin
|
||||
if Length(CellEditor.Caption) <> 0 then
|
||||
if (Key in [VK_RETURN, VK_TAB, VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT]) and
|
||||
(not IsValidTimeString(NormalizeTime(CellEditor.Caption, theValue))) then
|
||||
begin
|
||||
ShowMessage(CellEditor.Caption + ' no es una hora válida OnKeyDown');
|
||||
CellEditor.Text := FormatDateTime(format, theValue);
|
||||
CellEditor.SelectAll;
|
||||
Key := VK_UNKNOWN;
|
||||
end
|
||||
else
|
||||
if key = VK_ESCAPE then
|
||||
begin
|
||||
if Field.IsNull then
|
||||
CellEditor.Text := ''
|
||||
else
|
||||
CellEditor.Text := FormatDateTime(format, Field.AsDateTime);
|
||||
updated := True;
|
||||
theGrid.SetFocus; // No perder el foco
|
||||
end
|
||||
else
|
||||
if Key in [VK_UP, VK_DOWN] then
|
||||
begin
|
||||
Key := VK_UNKNOWN;
|
||||
end
|
||||
else
|
||||
if Key in [VK_RETURN, VK_TAB, VK_RIGHT, VK_LEFT] then
|
||||
begin
|
||||
CellEditor.Caption := NormalizeTime(CellEditor.Caption, theValue);
|
||||
if Length(CellEditor.Caption) = 0 then
|
||||
theValue := 0
|
||||
else
|
||||
if IsValidTimeString(CellEditor.Caption) then
|
||||
begin
|
||||
theValue := StrToTime(CellEditor.Caption);
|
||||
Field.DataSet.Edit;
|
||||
Field.AsDateTime:= theValue ; ShowMessage('TAB/Enter/right/left key-> ' + Field.AsString);//Field.AsDateTime := theValue;
|
||||
CellEditor.SelectAll;
|
||||
updated := True;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
function TJDbGridTimeCtrl.isNull: boolean;
|
||||
begin
|
||||
Result := theValue = 0;
|
||||
end;
|
||||
|
||||
constructor TJDbGridTimeCtrl.Create;
|
||||
begin
|
||||
inherited Create;
|
||||
CellEditor := TStringCellEditor.Create(nil);
|
||||
CellEditor.OnEnter := @myEditEnter;
|
||||
CellEditor.OnKeyDown := @OnKeyDown;
|
||||
CellEditor.OnEditingDone := @myEditOnEditingDone;
|
||||
CellEditor.OnKeyPress := @OnKeyPress; // se sobreescribe por el Grid :(
|
||||
format := ShortTimeFormat;
|
||||
end;
|
||||
|
||||
destructor TJDbGridTimeCtrl.Destroy;
|
||||
begin
|
||||
CellEditor.Free;
|
||||
inherited Destroy;
|
||||
end;
|
||||
|
||||
function TJDbGridTimeCtrl.Editor(aGrid: TDBGrid): TStringCellEditor;
|
||||
begin
|
||||
theGrid := aGrid;
|
||||
Result := CellEditor;
|
||||
end;
|
||||
|
||||
{ TJDbGridDateCtrl }
|
||||
|
||||
function TJDbGridDateCtrl.getFormat: string;
|
||||
|
Reference in New Issue
Block a user