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

unit uEditorGridBase;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorBase, ToolWin, ComCtrls, JvExControls, JvComponent,
  JvNavigationPane, ActnList, TB2Dock, TB2Toolbar, TBX, TB2Item,
  ImgList, PngImageList, StdActns, TB2ExtItems, TBXExtItems, uViewGridBase,
  uEditorDBBase, DB, uDADataTable, Menus, JvFormAutoSize,
  uDAScriptingProvider, uDACDSDataTable, AppEvnts, JvAppStorage,
  JvAppRegistryStorage, JvFormPlacement, pngimage, ExtCtrls,
  JvComponentBase, dxLayoutLookAndFeels, TBXStatusBars, JvExComCtrls,
  JvStatusBar, uDAInterfaces, cxGraphics, StdCtrls, cxControls, cxContainer,
  cxEdit, cxTextEdit, cxMaskEdit, cxDropDownEdit, cxLookupEdit, cxDBLookupEdit,
  cxDBLookupComboBox;

type
  IEditorGridBase = interface(IEditorDBBase)
    ['{CB8CDE00-B225-4A1D-9A5C-EC6FBE2C854B}']

    procedure SetMultiSelect (AValue : Boolean);
    function GetMultiSelect : Boolean;
    property MultiSelect : Boolean read GetMultiSelect write SetMultiSelect;
  end;

  TfEditorGridBase = class(TfEditorDBBase, IEditorGridBase)
    tbxEditFiltro: TTBXEditItem;
    tbxFiltro: TTBXToolbar;
    GridPopupMenu: TPopupMenu;
    Modificar1: TMenuItem;
    Eliminar1: TMenuItem;
    N1: TMenuItem;
    Nuevo1: TMenuItem;
    TBXLabelItem1: TTBXLabelItem;
    actQuitarFiltro: TAction;
    N2: TMenuItem;
    Previsualizar1: TMenuItem;
    Imprimir1: TMenuItem;
    TBXSeparatorItem14: TTBXSeparatorItem;
    TBXItem33: TTBXItem;
    N3: TMenuItem;
    Actualizar1: TMenuItem;
    TBXItem34: TTBXItem;
    actAnchoAuto: TAction;
    TBXSeparatorItem16: TTBXSeparatorItem;
    TBXItem35: TTBXItem;
    TBXItem36: TTBXItem;
    TBXSeparatorItem6: TTBXSeparatorItem;
    TBXItem7: TTBXItem;
    actFiltrar: TAction;
    TBXItem37: TTBXItem;
    TBXTMain2: TTBXToolbar;
    actExportarExcel: TAction;
    ExportaraficheroExcel1: TMenuItem;
    N488: TMenuItem;
    TBXSeparatorItem172: TTBXSeparatorItem;
    TBXItem395: TTBXItem;
    TBXItem384: TTBXItem;
    actExpandir: TAction;
    actContraer: TAction;
    TBXItem938: TTBXItem;
    TBXItem939: TTBXItem;
    cbxListaAnos: TTBXComboBoxItem;
    sepAno: TTBXSeparatorItem;
    lblAno: TTBXLabelItem;
    procedure tbxEditFiltroChange(Sender: TObject; const Text: String);
    procedure FormShow(Sender: TObject);
    procedure actQuitarFiltroExecute(Sender: TObject);
    procedure actDuplicarUpdate(Sender: TObject);
    procedure actModificarUpdate(Sender: TObject);
    procedure actEliminarUpdate(Sender: TObject);
    procedure actNuevoUpdate(Sender: TObject);
    procedure actAnchoAutoExecute(Sender: TObject);
    procedure actRefrescarUpdate(Sender: TObject);
    procedure actFiltrarExecute(Sender: TObject);
    procedure actFiltrarUpdate(Sender: TObject);
    procedure CustomEditorDestroy(Sender: TObject);
    procedure actExportarExcelUpdate(Sender: TObject);
    procedure actExportarExcelExecute(Sender: TObject);
    procedure actExpandirExecute(Sender: TObject);
    procedure actContraerExecute(Sender: TObject);
    procedure actExpandirUpdate(Sender: TObject);
    procedure actContraerUpdate(Sender: TObject);
  protected
    FListaAnos: TStringList;
    FViewGrid : IViewGridBase;
    FDynWhereDataTable: WideString;

    function GetListaAnos: TStringList;
    procedure SetListaAnos(const Value: TStringList);

    function GetDynWhereDataTable: WideString;
    procedure SetDynWhereDataTable(const Value: WideString);

    procedure SetViewGrid(const Value : IViewGridBase); virtual;
    function GetViewGrid: IViewGridBase;

    procedure SetMultiSelect (AValue : Boolean);
    function GetMultiSelect : Boolean;

    procedure PrevisualizarInterno; override;
    procedure ConfPaginaInterno; override;
    procedure ImprimirInterno; override;
    procedure RefrescarInterno; override;

    function GetModified: Boolean; override;

  public
    property ListaAnos: TStringList read GetListaAnos write SetListaAnos;
    property ViewGrid: IViewGridBase read GetViewGrid write SetViewGrid;
    property DynWhereDataTable: WideString read GetDynWhereDataTable write SetDynWhereDataTable;
    property MultiSelect : Boolean read GetMultiSelect write SetMultiSelect;


    constructor Create(AOwner : TComponent); override;
    destructor Destroy; override;
    function ShowModal : Integer; override;
  end;

implementation

uses
  uDMBase, uCustomEditor, cxGridTableView;

{$R *.dfm}

{
********************************* TfEditorGridBase *****************************
}
destructor TfEditorGridBase.Destroy;
begin
  FDynWhereDataTable := '';
  FViewGrid := NIL;
  if Assigned(FListaAnos) then
     FlistaAnos.Free;

  inherited;
end;

function TfEditorGridBase.GetDynWhereDataTable: WideString;
begin
  Result := FDynWhereDataTable;
end;

function TfEditorGridBase.GetListaAnos: TStringList;
begin
  Result := FListaAnos;
end;

function TfEditorGridBase.GetModified: Boolean;
begin
  //inherited:
  Result := False;
end;

function TfEditorGridBase.GetMultiSelect: Boolean;
begin
  Result := False;
  if Assigned(ViewGrid) then
     Result := ViewGrid.MultiSelect;
end;

function TfEditorGridBase.GetViewGrid: IViewGridBase;
begin
  Result := FViewGrid;
end;

procedure TfEditorGridBase.ImprimirInterno;
begin
  inherited;
  ViewGrid.Print;
end;

procedure TfEditorGridBase.SetDynWhereDataTable(const Value: WideString);
begin
  FDynWhereDataTable := Value;
end;

procedure TfEditorGridBase.SetListaAnos(const Value: TStringList);
var
  i: Integer;
  AStringAnterior: String;

begin
  AStringAnterior := '';
  if Assigned(FListaAnos) then
  begin
     AStringAnterior := FListaAnos.ValueFromIndex[cbxListaAnos.ItemIndex];
     FListaAnos.Free;
  end;

  //Se asigna el nuevo TStringList
  FListaAnos := Value;

  if Assigned(FListaAnos) then
  begin
    cbxListaAnos.Strings.BeginUpdate;
    cbxListaAnos.Strings.Clear;
    for i := 0 to FListaAnos.Count - 1 do
        cbxListaAnos.Strings.Append(FListaAnos.Names[i]);

    //Se posiciona en el elemento que habia anteriormente
    if Length(AStringAnterior) > 0 then
    begin
      if FListaAnos.IndexOfName(AStringAnterior) <> -1 then
         cbxListaAnos.ItemIndex := FListaAnos.IndexOfName(AStringAnterior)
      else
         cbxListaAnos.ItemIndex := 0;
    end;

    cbxListaAnos.Strings.EndUpdate;
  end;
end;

procedure TfEditorGridBase.SetMultiSelect(AValue: Boolean);
begin
  if Assigned(ViewGrid) then
     ViewGrid.MultiSelect := AValue;
end;

procedure TfEditorGridBase.SetViewGrid(const Value: IViewGridBase);
begin
  FViewGrid := Value;
  if Assigned(FViewGrid) then
  begin
    FViewGrid.PopupMenu := GridPopupMenu;
    FViewGrid.OnDblClick := actModificar.OnExecute;
  end;
end;

function TfEditorGridBase.ShowModal: Integer;
begin
//  tbxMenu.Visible := True;
  JvNavPanelHeader.Top := TBXDock.Height;
  Result := inherited ShowModal;
end;

procedure TfEditorGridBase.tbxEditFiltroChange(Sender: TObject; const Text: String);
begin
  if Assigned(ViewGrid) then
  begin
    if Length(Text) > 0 then
      ViewGrid.Filter := Text
    else
      actQuitarFiltro.Execute;
  end;
end;

procedure TfEditorGridBase.FormShow(Sender: TObject);
begin
  inherited;

  if Assigned(ViewGrid) then
  begin
    ViewGrid.ShowEmbedded(Self);
    ViewGrid.RestoreFromIniFile;

//    actExpandir.Visible := ViewGrid.AgrupacionesVisibles;
//    actContraer.Visible := ViewGrid.AgrupacionesVisibles; 
  end;

  cbxListaAnos.ItemIndex := 0;    
end;

procedure TfEditorGridBase.actEliminarUpdate(Sender: TObject);
begin
  inherited;

  if HayDatos and Assigned(ViewGrid) then
    (Sender as TAction).Enabled := not (dsDataTable.DataTable.State in dsEditModes)
      and ViewGrid.esSeleccionCeldaDatos
      and (ViewGrid.NumSeleccionados > 0)
  else
    (Sender as TAction).Enabled := False;
end;

procedure TfEditorGridBase.actExpandirExecute(Sender: TObject);
begin
  inherited;
  if Assigned(ViewGrid) then
    ViewGrid.ExpandirTodo
end;

procedure TfEditorGridBase.actExpandirUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := Assigned(dsDataTable.DataTable) and Assigned(ViewGrid);
end;

procedure TfEditorGridBase.actExportarExcelExecute(Sender: TObject);
begin
  inherited;
  if Assigned(ViewGrid) then
     ViewGrid.ExportToExcel;
end;

procedure TfEditorGridBase.actExportarExcelUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := Assigned(ViewGrid);
end;

procedure TfEditorGridBase.actFiltrarExecute(Sender: TObject);
begin
  inherited;
  if Assigned(ViewGrid) then
     ViewGrid.ViewFiltros.VerFiltros := not ViewGrid.ViewFiltros.VerFiltros;

  if Assigned(ViewGrid) then
     if ViewGrid.ViewFiltros.VerFiltros then
     begin
       tbxEditFiltro.Visible := False;
       TBXItem7.Visible := False;
     end
     else
     begin
       tbxEditFiltro.Text := '';
       tbxEditFiltro.Visible := True;
       TBXItem7.Visible := True;
     end;
end;

procedure TfEditorGridBase.actFiltrarUpdate(Sender: TObject);
begin
  inherited;
  if Assigned(ViewGrid) then
    (Sender as TAction).Checked := ViewGrid.ViewFiltros.VerFiltros;
end;

procedure TfEditorGridBase.actModificarUpdate(Sender: TObject);
begin
  inherited;
  if HayDatos and Assigned(ViewGrid) then
    (Sender as TAction).Enabled := not (dsDataTable.DataTable.State in dsEditModes)
                                   and not ViewGrid.IsEmpty
                                   and ViewGrid.esSeleccionCeldaDatos
  else
    (Sender as TAction).Enabled := False;

  //En el caso de que el grid sea multiselección solo se podrá modificar si solo se tiene un elemento seleccionado
  if (Sender as TAction).Enabled then
     if MultiSelect and Assigned(ViewGrid) then
        (Sender as TAction).Enabled := (ViewGrid.NumSeleccionados = 1);
end;

procedure TfEditorGridBase.actNuevoUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := Assigned(dsDataTable.DataTable);
end;

procedure TfEditorGridBase.actQuitarFiltroExecute(Sender: TObject);
begin
  if Assigned(ViewGrid) then
  begin
    tbxEditFiltro.Text := '';
    ViewGrid.Filter := '';
  end;
end;

procedure TfEditorGridBase.actRefrescarUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := Assigned(dsDataTable.DataTable);
end;

procedure TfEditorGridBase.ConfPaginaInterno;
begin
  inherited;
  ViewGrid.PrintSetup;
end;

constructor TfEditorGridBase.Create(AOwner: TComponent);
begin
  inherited;
  FDynWhereDataTable := '';
  actModificar.ShortCut := ShortCut(VK_RETURN, []);
end;

procedure TfEditorGridBase.CustomEditorDestroy(Sender: TObject);
begin
  inherited;
  if Assigned(ViewGrid) then
    ViewGrid.StoreToIniFile;
end;

procedure TfEditorGridBase.actAnchoAutoExecute(Sender: TObject);
begin
  inherited;
  if Assigned(ViewGrid) then
    ViewGrid.AjustarAncho;
end;

procedure TfEditorGridBase.actContraerExecute(Sender: TObject);
begin
  inherited;
  if Assigned(ViewGrid) then
    ViewGrid.ContraerTodo
end;

procedure TfEditorGridBase.actContraerUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := Assigned(dsDataTable.DataTable) and Assigned(ViewGrid);
end;

procedure TfEditorGridBase.actDuplicarUpdate(Sender: TObject);
begin
  inherited;
  if HayDatos and Assigned(ViewGrid) then
    (Sender as TAction).Enabled := not (dsDataTable.DataTable.State in dsEditModes)
      and not ViewGrid.IsEmpty
      and ViewGrid.esSeleccionCeldaDatos
  else
    (Sender as TAction).Enabled := False;

  //En el caso de que el grid sea multiselección solo se podrá modificar si solo se tiene un elemento seleccionado
  if (Sender as TAction).Enabled then
     if MultiSelect and Assigned(ViewGrid) then
       (Sender as TAction).Enabled := (ViewGrid.NumSeleccionados = 1);
end;

procedure TfEditorGridBase.PrevisualizarInterno;
begin
  inherited;
  ViewGrid.Preview;
end;

procedure TfEditorGridBase.RefrescarInterno;
var
  FocusedRow, TopRow : Integer;
begin
  TopRow := ViewGrid._FocusedView.Controller.TopRowIndex;
  FocusedRow := ViewGrid._FocusedView.DataController.FocusedRowIndex;
  ViewGrid._FocusedView.BeginUpdate;
  ShowHourglassCursor;

  try
    // inherited; <- No hacemos lo que hay en el padre
    dsDataTable.DataTable.Refresh;

  finally
    ViewGrid._FocusedView.EndUpdate;
    ViewGrid._FocusedView.DataController.FocusedRowIndex := FocusedRow;
    ViewGrid._FocusedView.Controller.TopRowIndex := TopRow;
    HideHourglassCursor;
  end;
end;

initialization
  RegisterClass(TfEditorGridBase);

finalization
  UnRegisterClass(TfEditorGridBase);

end.

