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

unit uViewGrid;

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, dxPScxGridLnk, dxPrnDlg,
  cxIntlPrintSys3, dxPSPrvwAdv, uViewGridBase, cxGridCustomPopupMenu,
  cxGridPopupMenu, uViewFiltroBase, ComCtrls, cxPC, ImgList, PngImageList,
  TB2Item, TBX, TB2Dock, TB2Toolbar, TBXDkPanels, dxPgsDlg;

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

  TfrViewGrid = class(TfrViewGridBase, IViewGrid)
    cxGrid: TcxGrid;
    cxGridLevel: TcxGridLevel;
    cxGridView: 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;
    frViewFiltroBase1: TfrViewFiltroBase;
    ActionList1: TActionList;
    PngImageList10: TPngImageList;
    actQuitarAgrupaciones: TAction;
    pnlAgrupaciones: TTBXDockablePanel;
    TBXAlignmentPanel1: TTBXAlignmentPanel;
    TBXToolbar1: TTBXToolbar;
    TBXItem1: TTBXItem;
    cxStyleRepositoryInforme: TcxStyleRepository;
    cxStyleContentInforme: TcxStyle;
    cxStyleFooterInforme: TcxStyle;
    cxStyleGroupInforme: TcxStyle;
    cxStyleHeaderInforme: TcxStyle;
    cxStyleSelectionInforme: TcxStyle;
    dxPrintStyleManager1: TdxPrintStyleManager;
    dxPrintStyleManager1Style1: TdxPSPrintStyle;
    procedure cxGridViewStylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure cxGridViewDblClick(Sender: TObject);
    procedure actQuitarAgrupacionesExecute(Sender: TObject);
    procedure actQuitarAgrupacionesUpdate(Sender: TObject);
  protected
    function GetGrid : TcxGrid; override;
    function GetFocusedView : TcxGridDBTableView; override;
    procedure SetPopupMenu(const Value: TPopupMenu); override;
    procedure FilterChanged(Sender : TObject); override;
    procedure OnChangeValoresFiltro(Sender: TObject);
    procedure SetViewFiltros(const Value: IViewFiltroBase); override;
    function AddFilterGrid(const Operacion: tcxFilterBoolOperatorKind): TcxFilterCriteriaItemList;
  public
    function esSeleccionCeldaDatos: Boolean; override;
    procedure Preview; override;
    procedure Print; override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

implementation

uses
  uDataModuleBase, uDBSelectionListUtils;

{$R *.dfm}

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

{ TfrViewGrid }
function TfrViewGrid.GetFocusedView: TcxGridDBTableView;
begin
  Result := cxGridView;
end;

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

procedure TfrViewGrid.OnChangeValoresFiltro(Sender: TObject);
begin
  cxGridView.DataController.Filter.BeginUpdate;
  RefrescarFiltro;
  cxGridView.DataController.Filter.EndUpdate;
end;

procedure TfrViewGrid.Preview;
begin
  inherited;
  dxComponentPrinter.Preview;
end;

procedure TfrViewGrid.Print;
begin
  inherited;
  dxComponentPrinter.Print(True, nil, nil);
end;

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

procedure TfrViewGrid.SetViewFiltros(const Value: IViewFiltroBase);
begin
  inherited;
  if Assigned(ViewFiltros) then
     ViewFiltros.OnFiltrosChange := OnChangeValoresFiltro
end;

procedure TfrViewGrid.actQuitarAgrupacionesExecute(Sender: TObject);
var
  Columna: TcxGridDBColumn;
  i: Integer;
begin
  inherited;
  for i := 0 to  cxGridView.ColumnCount - 1 do
  begin
    Columna := (cxGridView as TcxGridDBTableView).Columns[i];
    if not (Columna.GroupIndex < 0) then
    begin
      Columna.GroupIndex := -1;
      Columna.Visible := True;
    end;
  end;
end;

procedure TfrViewGrid.actQuitarAgrupacionesUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (cxGridView.GroupedColumnCount > 0);
end;

function TfrViewGrid.AddFilterGrid(const Operacion: tcxFilterBoolOperatorKind): TcxFilterCriteriaItemList;
var
  AItemList: TcxFilterCriteriaItemList;
begin
  AItemList := cxGridView.DataController.Filter.Root;
  Result := AItemList.AddItemList(Operacion);
end;

constructor TfrViewGrid.Create(AOwner: TComponent);
begin
  inherited;
  ViewFiltros := frViewFiltroBase1;
end;

procedure TfrViewGrid.cxGridViewDblClick(Sender: TObject);
begin
  inherited;
  if Assigned(FOnDblClick) then
    FOnDblClick(Sender);
end;

procedure TfrViewGrid.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;

destructor TfrViewGrid.Destroy;
begin
  ViewFiltros := Nil;
  inherited;
end;

function TfrViewGrid.esSeleccionCeldaDatos: Boolean;
begin
  Result := not (cxGridView.Controller.FocusedRecord is TcxGridGroupRow);
end;

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

end.

