Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Main_Form;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics, cxControls, cxLookAndFeels, Dateutils,
- cxLookAndFeelPainters, cxStyles, cxCustomData, cxFilter, cxData, cxFilterDialog,
- cxDataStorage, cxEdit, cxNavigator, Data.DB, cxDBData, cxGridCustomTableView,
- cxGridTableView, cxGridDBTableView, dxmdaset, cxGridLevel, cxClasses,
- cxGridCustomView, cxGrid, cxContainer, cxTextEdit, cxLabel, cxGroupBox,
- cxDropDownEdit, cxFontNameComboBox;
- const
- msecPerHour: Integer = MSecsPerDay;
- msecPerMinute: Integer = SecsPerMin * MSecsPerSec;
- msecPerSecond: Integer = MSecsPerSec;
- type
- TmyFilterComboBoxHelper = class(TcxFilterComboBoxHelper)
- private
- class function TryLongDateFormatToDate(const S: string; out Value: TDateTime): Boolean;
- class function TryStringToMilliseconds(const S: string; out Value: Int64): Boolean;
- public
- class procedure GetFilterValue(AEdit: TcxCustomEdit; AEditProperties: TcxCustomEditProperties; var V: Variant; var S: TCaption); override;
- end;
- TMainForm = class(TForm)
- memData: TdxMemData;
- DataSource1: TDataSource;
- memDatatime_field: TIntegerField;
- memDatadate_field: TDateField;
- cxGroupBox1: TcxGroupBox;
- grid: TcxGrid;
- gridDBTableView1: TcxGridDBTableView;
- gridDBTableView1time_field: TcxGridDBColumn;
- gridDBTableView1date_field: TcxGridDBColumn;
- gridLevel1: TcxGridLevel;
- cxLabel1: TcxLabel;
- cxTextEdit1: TcxTextEdit;
- cxLabel2: TcxLabel;
- cxTextEdit2: TcxTextEdit;
- cxGroupBox2: TcxGroupBox;
- cxLabel3: TcxLabel;
- cxTextEdit3: TcxTextEdit;
- cxLabel4: TcxLabel;
- cxTextEdit4: TcxTextEdit;
- procedure FormCreate(Sender: TObject);
- procedure gridDBTableView1time_fieldGetDataText(Sender: TcxCustomGridTableItem; ARecordIndex: Integer; var AText: string);
- procedure gridDBTableView1date_fieldGetDataText(Sender: TcxCustomGridTableItem; ARecordIndex: Integer; var AText: string);
- procedure gridDBTableView1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
- private
- procedure BuildProperties(const cxGridDBColumn: TcxGridDBColumn);
- function FormatMilliseconds(AValue: Integer): string;
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- MainForm: TMainForm;
- implementation
- uses
- cxFilterControlUtils, Math;
- {$R *.dfm}
- procedure TMainForm.FormCreate(Sender: TObject);
- var
- i: Integer;
- curTime: Integer;
- curDate: TDateTime;
- BeginOfYear: TDateTime;
- begin
- Randomize;
- FormatSettings := TFormatSettings.Create(1030); // HARDCODED TO DANISH
- BeginOfYear := EncodeDate(2015, 1, 1);
- gridDBTableView1time_field.PropertiesClass := TcxComboboxProperties;
- gridDBTableView1date_field.PropertiesClass := TcxComboboxProperties;
- memData.Active := True;
- memData.DisableControls;
- try
- for i := 0 to 5 do
- begin
- curDate := Random(Trunc(Date - BeginOfYear)) + BeginOfYear;
- curTime := Random(MSecsPerDay);
- memData.AppendRecord([i, curTime, curDate]);
- end;
- memData.Active := True;
- finally
- memData.EnableControls;
- end;
- BuildProperties(gridDBTableView1time_field);
- BuildProperties(gridDBTableView1date_field);
- FilterEditsController.Register(TcxComboboxProperties, TmyFilterComboBoxHelper);
- end;
- procedure TMainForm.gridDBTableView1date_fieldGetDataText(Sender: TcxCustomGridTableItem; ARecordIndex: Integer; var AText: string);
- var
- lDateTime: TDateTime;
- begin
- if TryStrToDate(AText, lDateTime) then
- AText := FormatDateTime(FormatSettings.LongDateFormat, lDateTime);
- end;
- procedure TMainForm.gridDBTableView1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
- var
- Index: Integer;
- begin
- if AFocusedRecord = nil then
- exit;
- Index := gridDBTableView1time_field.Index;
- cxTextEdit1.Text := AFocusedRecord.Values[Index];
- cxTextEdit2.Text := AFocusedRecord.DisplayTexts[Index];
- Index := gridDBTableView1date_field.Index;
- cxTextEdit3.Text := AFocusedRecord.Values[Index];
- cxTextEdit4.Text := AFocusedRecord.DisplayTexts[Index];
- end;
- procedure TMainForm.gridDBTableView1time_fieldGetDataText(Sender: TcxCustomGridTableItem; ARecordIndex: Integer; var AText: string);
- var
- lMsecs: Integer;
- begin
- if TryStrToInt(AText, lMsecs) then
- AText := FormatMilliseconds(lMsecs);
- end;
- procedure TMainForm.BuildProperties(const cxGridDBColumn: TcxGridDBColumn);
- var
- FilterStrings: TStringList;
- ValueList: TcxGridFilterValueList;
- i: Integer;
- begin
- if cxGridDBColumn = nil then
- exit;
- FilterStrings := TStringList.Create;
- ValueList := cxGridDBColumn.GridView.ViewData.CreateFilterValueList;
- TcxComboboxProperties(cxGridDBColumn.Properties).Items.Clear;
- try
- cxGridDBColumn.DataBinding.GetFilterStrings(FilterStrings, ValueList);
- for i := 0 to FilterStrings.Count - 1 do
- if ValueList[i].Kind = fviValue then
- TcxComboboxProperties(cxGridDBColumn.Properties).Items.Add(ValueList[i].DisplayText);
- finally
- FreeAndNil(ValueList);
- FreeAndNil(FilterStrings);
- end;
- end;
- function TMainForm.FormatMilliseconds(AValue: Integer): string;
- var
- lHours: Integer;
- lMinutes: Integer;
- lSeconds: Integer;
- begin
- lHours := Trunc(AValue / msecPerHour);
- AValue := AValue - (lHours * msecPerHour);
- lMinutes := Trunc(AValue / msecPerMinute);
- AValue := AValue - (lMinutes * msecPerMinute);
- lSeconds := Trunc(AValue / msecPerSecond);
- if (lHours > 0) then
- Result := Format('%.1d:%.2d:%.2d', [lHours, lMinutes, lSeconds])
- else if (lMinutes > 0) then
- Result := Format('%.1d:%.2d', [lMinutes, lSeconds])
- else
- Result := Format(':%.2d', [lSeconds]);
- end;
- class procedure TmyFilterComboBoxHelper.GetFilterValue(AEdit: TcxCustomEdit; AEditProperties: TcxCustomEditProperties; var V: Variant; var S: TCaption);
- var
- Dt: TDateTime;
- Tmp: Int64;
- begin
- inherited;
- if V = null then
- V := 0
- else if TryLongDateFormatToDate(V, Dt) then
- V := DateToStr(Dt)
- else if TryStringToMilliseconds(V, Tmp) then
- V := Tmp;
- end;
- class function TmyFilterComboBoxHelper.TryLongDateFormatToDate(const S: string; out Value: TDateTime): Boolean;
- var
- i: Integer;
- Day, Month, Year: Integer;
- Tokens: TArray<string>;
- begin
- // hardcoded to 'd. MMMM yyyy'
- Tokens := S.Split(['.', ' '], TStringSplitOptions.ExcludeEmpty);
- if length(Tokens) <> 3 then
- exit(false);
- if not TryStrToInt(Tokens[0], Day) then
- exit(false);
- Month := 0;
- for i := Low(FormatSettings.LongMonthNames) to high(FormatSettings.LongMonthNames) do
- if FormatSettings.LongMonthNames[i] = Tokens[1] then
- begin
- Month := i;
- Break;
- end;
- if Month = 0 then
- exit(false);
- if not TryStrToInt(Tokens[2], Year) then
- exit(false);
- Result := TryEncodeDate(Year, Month, Day, Value);
- end;
- class function TmyFilterComboBoxHelper.TryStringToMilliseconds(const S: string; out Value: Int64): Boolean;
- var
- Tokens: TArray<string>;
- Tmp: Int64;
- begin
- Tokens := S.Split([FormatSettings.TimeSeparator], TStringSplitOptions.ExcludeEmpty);
- if length(Tokens) <> 2 then
- exit(false);
- if not TryStrToInt64(Tokens[0], Tmp) then
- exit(false);
- Value := Tmp * msecPerHour;
- if not TryStrToInt64(Tokens[1], Tmp) then
- exit(false);
- Value := Value + Tmp * msecPerMinute;
- Result := True;
- end;
- end.
- **** DFM ***
- object MainForm: TMainForm
- Left = 0
- Top = 0
- ActiveControl = grid
- Caption = 'ExpressQuantumGrid Filter Test'
- ClientHeight = 290
- ClientWidth = 498
- Color = clBtnFace
- Font.Charset = DEFAULT_CHARSET
- Font.Color = clWindowText
- Font.Height = -11
- Font.Name = 'Tahoma'
- Font.Style = []
- OldCreateOrder = False
- OnCreate = FormCreate
- PixelsPerInch = 96
- TextHeight = 13
- object cxGroupBox1: TcxGroupBox
- Left = 311
- Top = 49
- Caption = 'Time field'
- TabOrder = 0
- Height = 105
- Width = 170
- object cxLabel1: TcxLabel
- Left = 7
- Top = 15
- Caption = 'Dataset value'
- end
- object cxTextEdit1: TcxTextEdit
- Left = 7
- Top = 32
- TabOrder = 1
- Text = 'cxTextEdit1'
- Width = 121
- end
- object cxLabel2: TcxLabel
- Left = 7
- Top = 56
- Caption = 'Display value'
- end
- object cxTextEdit2: TcxTextEdit
- Left = 7
- Top = 73
- TabOrder = 3
- Text = 'cxTextEdit1'
- Width = 121
- end
- end
- object grid: TcxGrid
- Left = 0
- Top = 0
- Width = 305
- Height = 290
- TabOrder = 1
- object gridDBTableView1: TcxGridDBTableView
- Navigator.Buttons.CustomButtons = <>
- OnFocusedRecordChanged = gridDBTableView1FocusedRecordChanged
- DataController.DataSource = DataSource1
- DataController.Filter.Active = True
- DataController.Summary.DefaultGroupSummaryItems = <>
- DataController.Summary.FooterSummaryItems = <>
- DataController.Summary.SummaryGroups = <>
- object gridDBTableView1time_field: TcxGridDBColumn
- DataBinding.FieldName = 'time_field'
- PropertiesClassName = 'TcxComboBoxProperties'
- OnGetDataText = gridDBTableView1time_fieldGetDataText
- Width = 76
- end
- object gridDBTableView1date_field: TcxGridDBColumn
- DataBinding.FieldName = 'date_field'
- PropertiesClassName = 'TcxComboBoxProperties'
- Properties.DropDownSizeable = True
- OnGetDataText = gridDBTableView1date_fieldGetDataText
- Width = 205
- end
- end
- object gridLevel1: TcxGridLevel
- GridView = gridDBTableView1
- end
- end
- object cxGroupBox2: TcxGroupBox
- Left = 311
- Top = 160
- Caption = 'Date field'
- TabOrder = 2
- Height = 105
- Width = 170
- object cxLabel3: TcxLabel
- Left = 7
- Top = 15
- Caption = 'Dataset value'
- end
- object cxTextEdit3: TcxTextEdit
- Left = 7
- Top = 32
- TabOrder = 1
- Text = 'cxTextEdit1'
- Width = 121
- end
- object cxLabel4: TcxLabel
- Left = 7
- Top = 56
- Caption = 'Display value'
- end
- object cxTextEdit4: TcxTextEdit
- Left = 7
- Top = 73
- TabOrder = 3
- Text = 'cxTextEdit1'
- Width = 121
- end
- end
- object memData: TdxMemData
- Indexes = <>
- SortOptions = []
- Left = 224
- Top = 120
- object memDatatime_field: TIntegerField
- FieldName = 'time_field'
- end
- object memDatadate_field: TDateField
- FieldName = 'date_field'
- end
- end
- object DataSource1: TDataSource
- DataSet = memData
- Left = 80
- Top = 176
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement