{*******************************************************}
{                                                       }
{       Administración de puntos de venta               }
{                                                       }
{       Copyright (C) 2006 Rodax Software S.L.          }
{                                                       }
{*******************************************************}

unit uViewGrid2Niveles;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewBase, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, uDADataTable, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGrid, Menus, ActnList, Grids,
  DBGrids, JvComponent, JvFormAutoSize, dxPSGlbl, dxPSUtl, dxPSEngn,
  dxPrnPg, dxBkgnd, dxWrap, dxPrnDev, dxPSCompsProvider, dxPSFillPatterns,
  dxPSEdgePatterns, dxPSCore, dxPScxCommon, dxPrnDlg,
  cxIntlPrintSys3, dxPSPrvwAdv, uViewGridBase, cxGridCustomPopupMenu,
  cxGridPopupMenu, uDAInterfaces, dxPScxGrid6Lnk;

type
  IViewGrid2Niveles = interface(IViewGridBase)
  ['{7EA40980-AD73-4590-A53A-932316C7B121}']
  end;

  TfrViewGrid2Niveles = class(TfrViewGridBase, IViewGrid2Niveles)
    cxGrid: TcxGrid;
    cxGridLevel1N: TcxGridLevel;
    cxGridView1N: TcxGridDBTableView;
    dxComponentPrinter: TdxComponentPrinter;
    dxPSEngineController1: TdxPSEngineController;
    cxStyleRepository1: TcxStyleRepository;
    cxStyleEven: TcxStyle;
    cxStyleOdd: TcxStyle;
    cxStyleSelection: TcxStyle;
    cxStyleSinOrden: TcxStyle;
    cxStyleConOrden: TcxStyle;
    cxViewGridPopupMenu: TcxGridPopupMenu;
    dxComponentPrinterLink: TdxGridReportLink;
    cxStyleFiltered: TcxStyle;
    cxStyleFilteredConOrden: TcxStyle;
    cxGridLevel: TcxGridLevel;
    cxGridView: TcxGridDBTableView;
    procedure cxGridViewStylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure cxGridView1NDblClick(Sender: TObject);
  protected
    function GetGrid : TcxGrid; override;
    function GetFocusedView : TcxGridDBTableView; override;
    procedure SetPopupMenu(const Value: TPopupMenu); override;
    procedure FilterChanged(Sender : TObject); override;
    function GetFiltered: Boolean; override;
    procedure FiltrarGrid(TextoFiltro : String); override;
  public
    procedure AjustarAncho; override;
    procedure ContraerTodo; override;
    procedure ExpandirTodo; override;
    function IsEmpty : Boolean; override;
    procedure RestoreFromRegistry (const Path : String); override;
    procedure StoreToRegistry (const Path : String); override;
  end;

implementation

uses
  uDMBase, uDBSelectionListUtils;

{$R *.dfm}

{
********************************* TfrViewGrid **********************************
}

{ TfrViewGrid }
function TfrViewGrid2Niveles.GetFiltered: Boolean;
begin
  Result := inherited GetFiltered;
  Result := Result OR (cxGridView1N.DataController.Filter.Root.Count > 0);
end;

function TfrViewGrid2Niveles.GetFocusedView: TcxGridDBTableView;
begin
  Result := cxGridView;
end;

function TfrViewGrid2Niveles.GetGrid: TcxGrid;
begin
  Result := cxGrid;
end;

function TfrViewGrid2Niveles.IsEmpty: Boolean;
begin
  Result := (cxGridView1N.ViewData.RowCount < 1);
end;

procedure TfrViewGrid2Niveles.RestoreFromRegistry(const Path: String);
begin
  inherited;
  cxGridView1N.RestoreFromRegistry(Path + '\\GridSettings\\' + Self.Name, False, False, []);
end;

procedure TfrViewGrid2Niveles.SetPopupMenu(const Value: TPopupMenu);
begin
  inherited;
  cxViewGridPopupMenu.PopupMenus[0].PopupMenu := FPopupMenu;
end;

procedure TfrViewGrid2Niveles.StoreToRegistry(const Path: String);
begin
  inherited;
  cxGridView1N.StoreToRegistry(Path + '\\GridSettings\\' + Self.Name, False, []);
end;

procedure TfrViewGrid2Niveles.AjustarAncho;
begin
  inherited;
  cxGridView1N.ApplyBestFit;
end;

procedure TfrViewGrid2Niveles.ContraerTodo;
begin
  inherited;
  cxGridView1N.ViewData.Collapse(True);
end;

procedure TfrViewGrid2Niveles.cxGridView1NDblClick(Sender: TObject);
begin
  inherited;
  if Assigned(FOnDblClick) then
    FOnDblClick(Sender);
end;

procedure TfrViewGrid2Niveles.cxGridViewStylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
begin
  inherited;
  if Assigned(AItem) then
  begin
    if AItem.SortOrder = soNone then
      AStyle := cxStyleSinOrden
    else begin
      AStyle := cxStyleConOrden;
      if Filtered then
        AStyle := cxStyleFilteredConOrden;
    end;
  end;
end;

procedure TfrViewGrid2Niveles.ExpandirTodo;
begin
  inherited;
  cxGridView1N.ViewData.Expand(True);
end;

procedure TfrViewGrid2Niveles.FilterChanged(Sender: TObject);
begin
  inherited;
  if Filtered then
  begin
    _FocusedView.Styles.Content := cxStyleFiltered;
    cxGridView1N.Styles.Content := cxStyleFiltered
  end
  else
  begin
    _FocusedView.Styles.Content := nil;
    cxGridView1N.Styles.Content := nil    
  end;
end;

procedure TfrViewGrid2Niveles.FiltrarGrid(TextoFiltro: String);
var
  Columna: TcxGridDBColumn;
  i: Integer;
  AItemList: TcxFilterCriteriaItemList;
begin
  inherited;

  with cxGridView1N.DataController.Filter do
  begin
    BeginUpdate;
    try
      Options := [fcoCaseInsensitive, fcoSoftCompare];
      Root.Clear;
      if Length(TextoFiltro) > 0 then
      begin
        AItemList := Root.AddItemList(fboAnd);
        AItemList.BoolOperatorKind := fboOr;
        for i:=0 to (cxGridView1N as TcxGridDBTableView).ColumnCount - 1 do
        begin
          Columna := (cxGridView1N as TcxGridDBTableView).Columns[i];
          if (Length(Columna.Caption) > 0) and (Columna.Caption <> 'RecID') then
             AItemList.AddItem(Columna, foLike, '%'+TextoFiltro+'%', IntToStr(i));
        end;
        Active := True;
      end
      else
        Active := False;
    finally
      EndUpdate;
    end;
  end;
end;

end.

