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

unit uViewInformeBaseGrid;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewBase, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, dxPSGlbl, dxPSUtl, dxPSEngn,
  dxPrnPg, dxBkgnd, dxWrap, dxPrnDev, dxPSCompsProvider, dxPSFillPatterns,
  dxPSEdgePatterns, dxPSCore, ActnList, uDADataTable, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGrid, dxPScxCommon, dxPScxGridLnk,
  dxPgsDlg;

type
  IViewInformeBaseGrid = interface(IViewBase)
  ['{35BD610D-3BCF-4496-BA98-B86A7821CEFC}']
    procedure ExpandirTodo;
    procedure ContraerTodo;
    procedure AjustarAncho;
    procedure Preview;
    procedure Print;
    procedure PrintSetup;
    procedure GotoFirst;
    procedure GotoLast;

    function GetColumnByCaption (const ACaption : String): TcxGridDBColumn;

    function GetFocusedView : TcxGridDBTableView;
    property FocusedView : TcxGridDBTableView read GetFocusedView;

    function GetColumnCount: Integer;
    function GetColumns(Index: Integer): TcxGridDBColumn;
    property Columns[Index: Integer] : TcxGridDBColumn read GetColumns;
    property ColumnCount : Integer read GetColumnCount;

    function GetGroupedColumnCount: Integer;
    function GetGroupedColumns(index: Integer): TcxGridColumn;
    property GroupedColumns[Index: Integer] : TcxGridColumn read GetGroupedColumns;
    property GroupedColumnCount : Integer read GetGroupedColumnCount;

    function GetOnViewChanged : TNotifyEvent;
    procedure SetOnViewChanged(const Value : TNotifyEvent);
    property OnViewChanged: TNotifyEvent read GetOnViewChanged write SetOnViewChanged;

    function GetViewProperties: String;
    procedure SetViewProperties(const Value: String);
    property ViewProperties: String read GetViewProperties write SetViewProperties;

    function GetFilterValueList(index: Integer) : TStringList;
    property FilterValueList[Index : Integer]: TStringList read GetFilterValueList;

    function GetOnFilterChanged: TNotifyEvent;
    procedure SetOnFilterChanged(const Value: TNotifyEvent);
    property OnFilterChanged: TNotifyEvent read GetOnFilterChanged write SetOnFilterChanged;
  end;

  TfrViewInformeBaseGrid = class(TfrViewBase, IViewInformeBaseGrid)
    dxPrintStyleManager1: TdxPrintStyleManager;
    dxPrintStyleManager1Style1: TdxPSPrintStyle;
    dxPageSetupDialog1: TdxPageSetupDialog;
    dxPSEngineController1: TdxPSEngineController;
    dxComponentPrinter: TdxComponentPrinter;
    dxPrinterLinkcxGrid: TdxGridReportLink;
    cxStyleRepositoryInforme: TcxStyleRepository;
    cxStyleContentInforme: TcxStyle;
    cxStyleFooterInforme: TcxStyle;
    cxStyleGroupInforme: TcxStyle;
    cxStyleHeaderInforme: TcxStyle;
    cxStyleSelectionInforme: TcxStyle;
  private
  protected
    FOnViewChanged : TNotifyEvent;
    FOnFilterChanged : TNotifyEvent;
    function GetColumnCount: Integer;
    function GetColumns(Index: Integer): TcxGridDBColumn;
    function GetGroupedColumnCount: Integer;
    function GetGroupedColumns(Index: Integer): TcxGridColumn;
    function GetFocusedView : TcxGridDBTableView; virtual; abstract;
    function GetColumnByCaption (const ACaption : String): TcxGridDBColumn;
    function GetOnViewChanged : TNotifyEvent; virtual;
    procedure SetOnViewChanged(const Value : TNotifyEvent); virtual;
    function GetOnFilterChanged: TNotifyEvent; virtual;
    procedure SetOnFilterChanged(const Value: TNotifyEvent); virtual;
    function GetViewProperties: String; virtual;
    procedure SetViewProperties(const Value: String); virtual;
    function GetFilterValueList(index: Integer) : TStringList; virtual;
    procedure ActivarEventos; virtual;
    procedure DesactivarEventos; virtual;
  public
    procedure Preview; virtual;
    procedure Print; virtual;
    procedure PrintSetup; virtual;
    procedure GotoFirst; virtual;
    procedure GotoLast; virtual;
    procedure ExpandirTodo; virtual;
    procedure ContraerTodo; virtual;
    procedure AjustarAncho; virtual;
    property FocusedView : TcxGridDBTableView read GetFocusedView;
    property Columns[Index: Integer] : TcxGridDBColumn read GetColumns;
    property ColumnCount : Integer read GetColumnCount;
    property GroupedColumns[Index: Integer] : TcxGridColumn read GetGroupedColumns;
    property GroupedColumnCount : Integer read GetGroupedColumnCount;
    property OnViewChanged: TNotifyEvent read GetOnViewChanged write SetOnViewChanged;
    property OnFilterChanged: TNotifyEvent read GetOnFilterChanged write SetOnFilterChanged;
    property ViewProperties: String read GetViewProperties write SetViewProperties;
    property FilterValueList[Index : Integer]: TStringList read GetFilterValueList;
    constructor Create(AOwner: TComponent); override;
  end;

implementation

{$R *.dfm}

{ TfrViewInformeBaseGrid }

procedure TfrViewInformeBaseGrid.ActivarEventos;
begin
  //
end;

procedure TfrViewInformeBaseGrid.AjustarAncho;
begin
  FocusedView.ApplyBestFit;
end;

procedure TfrViewInformeBaseGrid.ContraerTodo;
begin
  FocusedView.ViewData.Collapse(True);
end;

constructor TfrViewInformeBaseGrid.Create(AOwner: TComponent);
begin
  inherited;
  FOnViewChanged := nil;
end;

procedure TfrViewInformeBaseGrid.DesactivarEventos;
begin
  //
end;

procedure TfrViewInformeBaseGrid.ExpandirTodo;
begin
  FocusedView.ViewData.Expand(True);
end;

function TfrViewInformeBaseGrid.GetColumnByCaption(
  const ACaption: String): TcxGridDBColumn;
var
  i : integer;
begin
  Result := NIL;
  for i := 0 to FocusedView.ColumnCount - 1 do
    if FocusedView.Columns[i].Caption = ACaption then
    begin
      Result := FocusedView.Columns[i];
      Break;
    end;
end;

function TfrViewInformeBaseGrid.GetColumnCount: Integer;
begin
  Result := FocusedView.ColumnCount;
end;

function TfrViewInformeBaseGrid.GetColumns(Index: Integer): TcxGridDBColumn;
begin
  Result := FocusedView.Columns[Index];
end;

function TfrViewInformeBaseGrid.GetFilterValueList(
  index: Integer): TStringList;
var
  AValueList : TcxDataFilterValueList;
  i : Integer;
begin
  Result := TStringList.Create;
  AValueList := TcxDataFilterValueList.Create(FocusedView.DataController.Filter);
  try
    AValueList.Load(Columns[Index].Index);
    for i := 0 to AValueList.Count - 1 do
      Result.Add(AValueList[i].DisplayText);
  finally
    FreeAndNil(AValueList);
  end;
end;

function TfrViewInformeBaseGrid.GetGroupedColumnCount: Integer;
begin
  Result := FocusedView.GroupedColumnCount;
end;

function TfrViewInformeBaseGrid.GetGroupedColumns(
  Index: Integer): TcxGridColumn;
begin
  Result := FocusedView.GroupedColumns[Index];
end;

function TfrViewInformeBaseGrid.GetOnFilterChanged: TNotifyEvent;
begin
  Result := FOnFilterChanged;
end;

function TfrViewInformeBaseGrid.GetOnViewChanged: TNotifyEvent;
begin
  Result := FOnViewChanged;
end;

function TfrViewInformeBaseGrid.GetViewProperties: String;
var
  AStream : TStringStream;
begin
  DesactivarEventos;
  AStream := TStringStream.Create('');
  try
    FocusedView.StoreToStream(AStream, [gsoUseFilter,gsoUseSummary], FocusedView.Name);
    Result := AStream.DataString;
  finally
    AStream.Free;
    ActivarEventos;
  end;
end;

procedure TfrViewInformeBaseGrid.GotoFirst;
begin
  FocusedView.DataController.GotoFirst;
end;

procedure TfrViewInformeBaseGrid.GotoLast;
begin
  FocusedView.DataController.GotoLast;
end;

procedure TfrViewInformeBaseGrid.Preview;
begin
  dxPrinterLinkcxGrid.Preview;
end;

procedure TfrViewInformeBaseGrid.Print;
begin
  dxPrinterLinkcxGrid.Print(True, nil);
end;

procedure TfrViewInformeBaseGrid.PrintSetup;
begin
  dxPrinterLinkcxGrid.PageSetup;
end;

procedure TfrViewInformeBaseGrid.SetOnFilterChanged(const Value: TNotifyEvent);
begin
  FOnFilterChanged := Value;
end;

procedure TfrViewInformeBaseGrid.SetOnViewChanged(const Value: TNotifyEvent);
begin
  FOnViewChanged := Value;
end;

procedure TfrViewInformeBaseGrid.SetViewProperties(const Value: String);
var
  AStream : TStringStream;
begin
  DesactivarEventos;
  AStream := TStringStream.Create(Value);
  try
    AStream.Position := 0;
    FocusedView.RestoreFromStream(AStream, True, True, [gsoUseFilter,gsoUseSummary],
    FocusedView.Name);
  finally
    AStream.Free;
    ActivarEventos;
  end;
end;

end.
