Advertisement
Borrisholt

Filtering in cxGrid

Apr 29th, 2015
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 10.80 KB | None | 0 0
  1. unit Main_Form;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics, cxControls, cxLookAndFeels, Dateutils,
  8.   cxLookAndFeelPainters, cxStyles, cxCustomData, cxFilter, cxData, cxFilterDialog,
  9.   cxDataStorage, cxEdit, cxNavigator, Data.DB, cxDBData, cxGridCustomTableView,
  10.   cxGridTableView, cxGridDBTableView, dxmdaset, cxGridLevel, cxClasses,
  11.   cxGridCustomView, cxGrid, cxContainer, cxTextEdit, cxLabel, cxGroupBox,
  12.   cxDropDownEdit, cxFontNameComboBox;
  13.  
  14. const
  15.   msecPerHour: Integer = MSecsPerDay;
  16.   msecPerMinute: Integer = SecsPerMin * MSecsPerSec;
  17.   msecPerSecond: Integer = MSecsPerSec;
  18.  
  19. type
  20.   TmyFilterComboBoxHelper = class(TcxFilterComboBoxHelper)
  21.   private
  22.     class function TryLongDateFormatToDate(const S: string; out Value: TDateTime): Boolean;
  23.     class function TryStringToMilliseconds(const S: string; out Value: Int64): Boolean;
  24.   public
  25.     class procedure GetFilterValue(AEdit: TcxCustomEdit; AEditProperties: TcxCustomEditProperties; var V: Variant; var S: TCaption); override;
  26.   end;
  27.  
  28.   TMainForm = class(TForm)
  29.     memData: TdxMemData;
  30.     DataSource1: TDataSource;
  31.     memDatatime_field: TIntegerField;
  32.     memDatadate_field: TDateField;
  33.     cxGroupBox1: TcxGroupBox;
  34.     grid: TcxGrid;
  35.     gridDBTableView1: TcxGridDBTableView;
  36.     gridDBTableView1time_field: TcxGridDBColumn;
  37.     gridDBTableView1date_field: TcxGridDBColumn;
  38.     gridLevel1: TcxGridLevel;
  39.     cxLabel1: TcxLabel;
  40.     cxTextEdit1: TcxTextEdit;
  41.     cxLabel2: TcxLabel;
  42.     cxTextEdit2: TcxTextEdit;
  43.     cxGroupBox2: TcxGroupBox;
  44.     cxLabel3: TcxLabel;
  45.     cxTextEdit3: TcxTextEdit;
  46.     cxLabel4: TcxLabel;
  47.     cxTextEdit4: TcxTextEdit;
  48.     procedure FormCreate(Sender: TObject);
  49.     procedure gridDBTableView1time_fieldGetDataText(Sender: TcxCustomGridTableItem; ARecordIndex: Integer; var AText: string);
  50.     procedure gridDBTableView1date_fieldGetDataText(Sender: TcxCustomGridTableItem; ARecordIndex: Integer; var AText: string);
  51.     procedure gridDBTableView1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
  52.   private
  53.     procedure BuildProperties(const cxGridDBColumn: TcxGridDBColumn);
  54.     function FormatMilliseconds(AValue: Integer): string;
  55.     { Private declarations }
  56.   public
  57.     { Public declarations }
  58.   end;
  59.  
  60. var
  61.   MainForm: TMainForm;
  62.  
  63. implementation
  64.  
  65. uses
  66.   cxFilterControlUtils, Math;
  67. {$R *.dfm}
  68.  
  69. procedure TMainForm.FormCreate(Sender: TObject);
  70. var
  71.   i: Integer;
  72.   curTime: Integer;
  73.   curDate: TDateTime;
  74.   BeginOfYear: TDateTime;
  75. begin
  76.   Randomize;
  77.   FormatSettings := TFormatSettings.Create(1030); // HARDCODED TO DANISH
  78.   BeginOfYear := EncodeDate(2015, 1, 1);
  79.  
  80.   gridDBTableView1time_field.PropertiesClass := TcxComboboxProperties;
  81.   gridDBTableView1date_field.PropertiesClass := TcxComboboxProperties;
  82.  
  83.   memData.Active := True;
  84.   memData.DisableControls;
  85.   try
  86.     for i := 0 to 5 do
  87.     begin
  88.       curDate := Random(Trunc(Date - BeginOfYear)) + BeginOfYear;
  89.       curTime := Random(MSecsPerDay);
  90.       memData.AppendRecord([i, curTime, curDate]);
  91.     end;
  92.     memData.Active := True;
  93.   finally
  94.     memData.EnableControls;
  95.   end;
  96.  
  97.   BuildProperties(gridDBTableView1time_field);
  98.   BuildProperties(gridDBTableView1date_field);
  99.   FilterEditsController.Register(TcxComboboxProperties, TmyFilterComboBoxHelper);
  100. end;
  101.  
  102. procedure TMainForm.gridDBTableView1date_fieldGetDataText(Sender: TcxCustomGridTableItem; ARecordIndex: Integer; var AText: string);
  103. var
  104.   lDateTime: TDateTime;
  105. begin
  106.   if TryStrToDate(AText, lDateTime) then
  107.     AText := FormatDateTime(FormatSettings.LongDateFormat, lDateTime);
  108. end;
  109.  
  110. procedure TMainForm.gridDBTableView1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
  111. var
  112.   Index: Integer;
  113. begin
  114.   if AFocusedRecord = nil then
  115.     exit;
  116.  
  117.   Index := gridDBTableView1time_field.Index;
  118.   cxTextEdit1.Text := AFocusedRecord.Values[Index];
  119.   cxTextEdit2.Text := AFocusedRecord.DisplayTexts[Index];
  120.  
  121.   Index := gridDBTableView1date_field.Index;
  122.   cxTextEdit3.Text := AFocusedRecord.Values[Index];
  123.   cxTextEdit4.Text := AFocusedRecord.DisplayTexts[Index];
  124. end;
  125.  
  126. procedure TMainForm.gridDBTableView1time_fieldGetDataText(Sender: TcxCustomGridTableItem; ARecordIndex: Integer; var AText: string);
  127. var
  128.   lMsecs: Integer;
  129. begin
  130.   if TryStrToInt(AText, lMsecs) then
  131.     AText := FormatMilliseconds(lMsecs);
  132. end;
  133.  
  134. procedure TMainForm.BuildProperties(const cxGridDBColumn: TcxGridDBColumn);
  135. var
  136.   FilterStrings: TStringList;
  137.   ValueList: TcxGridFilterValueList;
  138.   i: Integer;
  139. begin
  140.   if cxGridDBColumn = nil then
  141.     exit;
  142.  
  143.   FilterStrings := TStringList.Create;
  144.   ValueList := cxGridDBColumn.GridView.ViewData.CreateFilterValueList;
  145.  
  146.   TcxComboboxProperties(cxGridDBColumn.Properties).Items.Clear;
  147.   try
  148.     cxGridDBColumn.DataBinding.GetFilterStrings(FilterStrings, ValueList);
  149.  
  150.     for i := 0 to FilterStrings.Count - 1 do
  151.       if ValueList[i].Kind = fviValue then
  152.         TcxComboboxProperties(cxGridDBColumn.Properties).Items.Add(ValueList[i].DisplayText);
  153.   finally
  154.     FreeAndNil(ValueList);
  155.     FreeAndNil(FilterStrings);
  156.   end;
  157. end;
  158.  
  159. function TMainForm.FormatMilliseconds(AValue: Integer): string;
  160. var
  161.   lHours: Integer;
  162.   lMinutes: Integer;
  163.   lSeconds: Integer;
  164. begin
  165.   lHours := Trunc(AValue / msecPerHour);
  166.   AValue := AValue - (lHours * msecPerHour);
  167.   lMinutes := Trunc(AValue / msecPerMinute);
  168.   AValue := AValue - (lMinutes * msecPerMinute);
  169.   lSeconds := Trunc(AValue / msecPerSecond);
  170.  
  171.   if (lHours > 0) then
  172.     Result := Format('%.1d:%.2d:%.2d', [lHours, lMinutes, lSeconds])
  173.   else if (lMinutes > 0) then
  174.     Result := Format('%.1d:%.2d', [lMinutes, lSeconds])
  175.   else
  176.     Result := Format(':%.2d', [lSeconds]);
  177. end;
  178.  
  179. class procedure TmyFilterComboBoxHelper.GetFilterValue(AEdit: TcxCustomEdit; AEditProperties: TcxCustomEditProperties; var V: Variant; var S: TCaption);
  180. var
  181.   Dt: TDateTime;
  182.   Tmp: Int64;
  183. begin
  184.   inherited;
  185.   if V = null then
  186.     V := 0
  187.   else if TryLongDateFormatToDate(V, Dt) then
  188.     V := DateToStr(Dt)
  189.   else if TryStringToMilliseconds(V, Tmp) then
  190.     V := Tmp;
  191. end;
  192.  
  193. class function TmyFilterComboBoxHelper.TryLongDateFormatToDate(const S: string; out Value: TDateTime): Boolean;
  194. var
  195.   i: Integer;
  196.   Day, Month, Year: Integer;
  197.   Tokens: TArray<string>;
  198. begin
  199.   // hardcoded to 'd. MMMM yyyy'
  200.   Tokens := S.Split(['.', ' '], TStringSplitOptions.ExcludeEmpty);
  201.  
  202.   if length(Tokens) <> 3 then
  203.     exit(false);
  204.  
  205.   if not TryStrToInt(Tokens[0], Day) then
  206.     exit(false);
  207.  
  208.   Month := 0;
  209.  
  210.   for i := Low(FormatSettings.LongMonthNames) to high(FormatSettings.LongMonthNames) do
  211.     if FormatSettings.LongMonthNames[i] = Tokens[1] then
  212.     begin
  213.       Month := i;
  214.       Break;
  215.     end;
  216.  
  217.   if Month = 0 then
  218.     exit(false);
  219.  
  220.   if not TryStrToInt(Tokens[2], Year) then
  221.     exit(false);
  222.  
  223.   Result := TryEncodeDate(Year, Month, Day, Value);
  224. end;
  225.  
  226. class function TmyFilterComboBoxHelper.TryStringToMilliseconds(const S: string; out Value: Int64): Boolean;
  227. var
  228.   Tokens: TArray<string>;
  229.   Tmp: Int64;
  230. begin
  231.   Tokens := S.Split([FormatSettings.TimeSeparator], TStringSplitOptions.ExcludeEmpty);
  232.  
  233.   if length(Tokens) <> 2 then
  234.     exit(false);
  235.  
  236.   if not TryStrToInt64(Tokens[0], Tmp) then
  237.     exit(false);
  238.  
  239.   Value := Tmp * msecPerHour;
  240.  
  241.   if not TryStrToInt64(Tokens[1], Tmp) then
  242.     exit(false);
  243.  
  244.   Value := Value + Tmp * msecPerMinute;
  245.   Result := True;
  246. end;
  247.  
  248. end.
  249.  
  250.  
  251. **** DFM ***
  252.  
  253.  
  254. object MainForm: TMainForm
  255.   Left = 0
  256.   Top = 0
  257.   ActiveControl = grid
  258.   Caption = 'ExpressQuantumGrid Filter Test'
  259.   ClientHeight = 290
  260.   ClientWidth = 498
  261.   Color = clBtnFace
  262.   Font.Charset = DEFAULT_CHARSET
  263.   Font.Color = clWindowText
  264.   Font.Height = -11
  265.   Font.Name = 'Tahoma'
  266.   Font.Style = []
  267.   OldCreateOrder = False
  268.   OnCreate = FormCreate
  269.   PixelsPerInch = 96
  270.   TextHeight = 13
  271.   object cxGroupBox1: TcxGroupBox
  272.     Left = 311
  273.     Top = 49
  274.     Caption = 'Time field'
  275.     TabOrder = 0
  276.     Height = 105
  277.     Width = 170
  278.     object cxLabel1: TcxLabel
  279.       Left = 7
  280.       Top = 15
  281.       Caption = 'Dataset value'
  282.     end
  283.     object cxTextEdit1: TcxTextEdit
  284.       Left = 7
  285.       Top = 32
  286.       TabOrder = 1
  287.       Text = 'cxTextEdit1'
  288.       Width = 121
  289.     end
  290.     object cxLabel2: TcxLabel
  291.       Left = 7
  292.       Top = 56
  293.       Caption = 'Display value'
  294.     end
  295.     object cxTextEdit2: TcxTextEdit
  296.       Left = 7
  297.       Top = 73
  298.       TabOrder = 3
  299.       Text = 'cxTextEdit1'
  300.       Width = 121
  301.     end
  302.   end
  303.   object grid: TcxGrid
  304.     Left = 0
  305.     Top = 0
  306.     Width = 305
  307.     Height = 290
  308.     TabOrder = 1
  309.     object gridDBTableView1: TcxGridDBTableView
  310.       Navigator.Buttons.CustomButtons = <>
  311.       OnFocusedRecordChanged = gridDBTableView1FocusedRecordChanged
  312.       DataController.DataSource = DataSource1
  313.       DataController.Filter.Active = True
  314.       DataController.Summary.DefaultGroupSummaryItems = <>
  315.       DataController.Summary.FooterSummaryItems = <>
  316.       DataController.Summary.SummaryGroups = <>
  317.       object gridDBTableView1time_field: TcxGridDBColumn
  318.         DataBinding.FieldName = 'time_field'
  319.         PropertiesClassName = 'TcxComboBoxProperties'
  320.         OnGetDataText = gridDBTableView1time_fieldGetDataText
  321.         Width = 76
  322.       end
  323.       object gridDBTableView1date_field: TcxGridDBColumn
  324.         DataBinding.FieldName = 'date_field'
  325.         PropertiesClassName = 'TcxComboBoxProperties'
  326.         Properties.DropDownSizeable = True
  327.         OnGetDataText = gridDBTableView1date_fieldGetDataText
  328.         Width = 205
  329.       end
  330.     end
  331.     object gridLevel1: TcxGridLevel
  332.       GridView = gridDBTableView1
  333.     end
  334.   end
  335.   object cxGroupBox2: TcxGroupBox
  336.     Left = 311
  337.     Top = 160
  338.     Caption = 'Date field'
  339.     TabOrder = 2
  340.     Height = 105
  341.     Width = 170
  342.     object cxLabel3: TcxLabel
  343.       Left = 7
  344.       Top = 15
  345.       Caption = 'Dataset value'
  346.     end
  347.     object cxTextEdit3: TcxTextEdit
  348.       Left = 7
  349.       Top = 32
  350.       TabOrder = 1
  351.       Text = 'cxTextEdit1'
  352.       Width = 121
  353.     end
  354.     object cxLabel4: TcxLabel
  355.       Left = 7
  356.       Top = 56
  357.       Caption = 'Display value'
  358.     end
  359.     object cxTextEdit4: TcxTextEdit
  360.       Left = 7
  361.       Top = 73
  362.       TabOrder = 3
  363.       Text = 'cxTextEdit1'
  364.       Width = 121
  365.     end
  366.   end
  367.   object memData: TdxMemData
  368.     Indexes = <>
  369.     SortOptions = []
  370.     Left = 224
  371.     Top = 120
  372.     object memDatatime_field: TIntegerField
  373.       FieldName = 'time_field'
  374.     end
  375.     object memDatadate_field: TDateField
  376.       FieldName = 'date_field'
  377.     end
  378.   end
  379.   object DataSource1: TDataSource
  380.     DataSet = memData
  381.     Left = 80
  382.     Top = 176
  383.   end
  384. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement