unit uViewFiltros;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewParametrosControlGrid, cxStyles, cxGraphics, cxEdit,
  TBXDkPanels, cxControls, cxInplaceContainer, cxVGrid, cxDropDownEdit,
  ImgList, PngImageList, cxFilterControl, uViewControlGrid,
  cxDBFilterControl, StdCtrls;

type
  IViewFiltros = interface(IViewParametrosControlGrid)
  ['{92FA8AF8-D0D8-4BC6-9611-CF3094CD4385}']
  end;

  TfrViewFiltros = class(TfrViewParametrosControlGrid, IViewFiltros)
    vgFiltros: TcxVerticalGrid;
    TBXLabel1: TTBXLabel;
    cxStyleRepository1: TcxStyleRepository;
    cxStyleNombreColumna: TcxStyle;
    cxStyleCategoria: TcxStyle;
    PngImageList1: TPngImageList;
    vgFiltrosEditorRow1: TcxEditorRow;
    cxFilterControl1: TcxFilterControl;
    TBXAlignmentPanel3: TTBXAlignmentPanel;
    bRefrescar: TButton;
    procedure vgFiltrosEditorRow1EditPropertiesEditValueChanged(
      Sender: TObject);
    procedure bRefrescarClick(Sender: TObject);
  protected
    procedure RellenarFiltros;
    procedure SetViewControlGrid(const Value: IViewControlGrid); override;
    procedure SetModificable(const Value: Boolean); override;
  public
    procedure Execute; override;
    procedure Refresh; override;
  end;

implementation

{$R *.dfm}

uses
  cxCustomData, cxFilter, cxGridDBDataDefinitions,
  cxGridDBTableView, cxGridCustomTableView, cxFilterConsts, cxClasses,
  cxDBData;

{ TfrViewFiltros }

procedure TfrViewFiltros.Execute;
begin
  inherited;
  cxFilterControl1.ApplyFilter;
  FViewControlGrid.FocusedView.DataController.Filter.Active := True;
end;

procedure TfrViewFiltros.Refresh;
begin
  inherited;
  cxFilterControl1.UpdateFilter;
  SetModificable(Modificable);
  //RellenarFiltros;
end;

procedure TfrViewFiltros.RellenarFiltros;
var
  i : Integer;
  ACaption : String;
  ARow : TcxEditorRow;
  AColumn : TcxGridDBColumn;
  AValueList : TcxDataFilterValueList;

  procedure GetFilterStrings(AStrings: TStrings;
    AValueList: TcxDataFilterValueList);
  var
    I: Integer;
    S: string;
  begin
    AStrings.BeginUpdate;
    try
      AStrings.Clear;
      for I := 0 to AValueList.Count - 1 do
      begin
        S := AValueList[I].DisplayText;
        AStrings.AddObject(S, TObject(AValueList[I]));
      end;
    finally
      AStrings.EndUpdate;
    end;
  end;

  procedure AddRow;
  begin
    ARow := vgFiltros.Add(TcxEditorRow) as TcxEditorRow;
    ARow.Parent := NIL;
    ARow.Properties.Caption := AColumn.Caption;
    ARow.Properties.EditPropertiesClassName := 'TcxComboBoxProperties';
    with (TcxComboBoxProperties(ARow.Properties.EditProperties)) do
    begin
      ARow.Properties.ImageIndex := 0;
      ImmediateDropDown := True;
      ImmediatePost := True;
      ImmediateUpdateText := True;
      PostPopupValueOnTab := True;
      OnEditValueChanged := vgFiltrosEditorRow1EditPropertiesEditValueChanged;
      GetFilterStrings(Items, AValueList);
    end;
  end;

begin
{  vgFiltros.BeginUpdate;
  try
    vgFiltros.ClearRows;
    for i := 0 to ViewControlGrid.FocusedView.VisibleColumnCount - 1 do
    begin
      AColumn := TcxGridDBColumn(ViewControlGrid.FocusedView.VisibleColumns[i]);
      AValueList := ViewControlGrid.FilterValueList[AColumn.Index];
      AddRow;
    end;
  finally
    vgFiltros.EndUpdate;
  end;}
end;

procedure TfrViewFiltros.SetViewControlGrid(const Value: IViewControlGrid);
begin
  inherited;
  cxFilterControl1.LinkComponent := FViewControlGrid.FocusedView;
end;

procedure TfrViewFiltros.vgFiltrosEditorRow1EditPropertiesEditValueChanged(
  Sender: TObject);
var
  ARow : TcxEditorRow;
  AColumn : TcxGridDBColumn;
  AItemIndex : Integer;
begin
{  ARow := TcxEditorRow(vgFiltros.FocusedRow);

  if not Assigned(ARow) then
    Exit;

  with (TcxComboBoxProperties(ARow.Properties.EditProperties)) do
    AItemIndex := Items.IndexOf(ARow.Properties.Value);

  AColumn := ViewControlGrid.GetColumnByCaption(ARow.Properties.Caption);

  if Assigned(AColumn) and (AItemIndex <> -1) then
  begin
    AColumn.DataBinding.Filter.BeginUpdate;
    try
      with ViewControlGrid.FilterValueList[AColumn.Index][AItemIndex]^ do
      begin
        if not (Kind in [fviAll, fviCustom]) then
          AColumn.DataBinding.Filter.Active := True;
        case Kind of
          fviAll:
            begin
              AColumn.Filtered := False;
              ARow.Properties.Value := '';
            end;
          fviCustom:
            ViewControlGrid.FocusedView.Filtering.RunCustomizeDialog(AColumn);
          fviBlanks:
            AColumn.DataBinding.AddToFilter(nil, foEqual, Value,
              cxGetResourceStringNet(cxSFilterBlankCaption));
          fviNonBlanks:
            AColumn.DataBinding.AddToFilter(nil, foNotEqual, Value,
              cxGetResourceStringNet(cxSFilterBlankCaption));
          fviValue, fviMRU:
            begin
              AColumn.DataBinding.AddToFilter(nil, foEqual, Value, DisplayText);
              AColumn.DataBinding.FilterMRUValueItems.Add(Value, DisplayText);
            end;
        end;
      end;
    finally
      AColumn.DataBinding.Filter.EndUpdate;
    end;
  end;}
end;

procedure TfrViewFiltros.bRefrescarClick(Sender: TObject);
begin
  inherited;
  Execute;
end;

procedure TfrViewFiltros.SetModificable(const Value: Boolean);
begin
  inherited;
  vgFiltros.Enabled := Value;
end;

end.

