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

unit uViewControlGrid;

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
  IViewControlGrid = interface(IViewBase)
  ['{566F315A-70A6-4BCE-8A02-0C7E10FF311A}']
    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;

  TfrViewControlGrid = class(TfrViewBase, IViewControlGrid)
    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}

{ TfrViewControlGrid }

procedure TfrViewControlGrid.ActivarEventos;
begin
  //
end;

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

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

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

procedure TfrViewControlGrid.DesactivarEventos;
begin
  //
end;

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

function TfrViewControlGrid.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 TfrViewControlGrid.GetColumnCount: Integer;
begin
  Result := FocusedView.ColumnCount;
end;

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

function TfrViewControlGrid.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 TfrViewControlGrid.GetGroupedColumnCount: Integer;
begin
  Result := FocusedView.GroupedColumnCount;
end;

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

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

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

function TfrViewControlGrid.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 TfrViewControlGrid.GotoFirst;
begin
  FocusedView.DataController.GotoFirst;
end;

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

procedure TfrViewControlGrid.Preview;
begin
  dxPrinterLinkcxGrid.Preview;
end;

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

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

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

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

procedure TfrViewControlGrid.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.
