unit uViewAgrupaciones;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ActnList, Menus, dxLayoutLookAndFeels, dxLayoutControl, uViewBase,
  TBXDkPanels, cxControls, StdCtrls, ImgList, uViewInformeBaseGrid,
  PngImageList, uViewInformeBaseParametros, JvExControls, JvComponent,
  JvArrowButton, cxLookAndFeelPainters, cxButtons;

type
  IViewAgrupaciones = interface(IViewInformeBaseParametros)
  ['{42030B8F-B12D-43D2-AEB2-F642F11D78DA}']
    function GetAgrupaciones: TStringList;  
  end;

  TfrViewAgrupaciones = class(TfrViewInformeBaseParametros, IViewAgrupaciones)
    TBXButton1: TTBXButton;
    TBXButton2: TTBXButton;
    TBXButton3: TTBXButton;
    PopupMenu1: TPopupMenu;
    PopupMenu2: TPopupMenu;
    PopupMenu3: TPopupMenu;
    ActionList1: TActionList;
    actBoton2: TAction;
    actBoton3: TAction;
    actBoton1: TAction;
    actMenuClick: TAction;
    TBXLabel1: TTBXLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    pnlAgrupaciones: TTBXAlignmentPanel;
    procedure actBoton2Update(Sender: TObject);
    procedure actBoton3Update(Sender: TObject);
    procedure actBoton2Execute(Sender: TObject);
    procedure actBoton3Execute(Sender: TObject);
    procedure actBoton1Execute(Sender: TObject);
    procedure AMenuItemClick(Sender: TObject);
  protected
    FColumns : TStringList;

    FBotonPulsado : TTBXButton;
    procedure MarcarItem(AMenuItem : TMenuItem); overload;
    procedure MarcarItem(AMenu : TPopupMenu; const ACaption : String); overload;
    procedure DesmarcarItem(AMenu : TPopupMenu; const ACaption : String);
    procedure DesmarcarItems(AMenu : TPopupMenu);

    procedure OnButtonPopup(Sender: TTBXCustomButton; var AllowDropDown: Boolean);
    procedure RellenarListaColumnas;
    procedure RellenarAgrupaciones;
    procedure SetModificable(const Value: Boolean); override;
  public
    function GetAgrupaciones: TStringList;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Refresh; override;
    procedure Execute; override;
  end;

implementation

uses
  cxGridDBTableView, JclStrings;

{$R *.dfm}

const
  SIN_AGRUPACION = '(Quitar agrupación)';

{ TfrAgrupaciones }

procedure TfrViewAgrupaciones.MarcarItem(AMenuItem : TMenuItem);
var
  AItem : TMenuItem;
begin
  AMenuItem.Checked := True;

  AItem := PopupMenu1.Items.Find(AMenuItem.Caption);
  if Assigned(AItem) then
    AItem.Enabled := False;

  AItem := PopupMenu2.Items.Find(AMenuItem.Caption);
  if Assigned(AItem) then
    AItem.Enabled := False;

  AItem := PopupMenu3.Items.Find(AMenuItem.Caption);
  if Assigned(AItem) then
    AItem.Enabled := False;
end;

procedure TfrViewAgrupaciones.MarcarItem(AMenu : TPopupMenu; const ACaption : String);
var
  AItem : TMenuItem;
begin
  AItem := AMenu.Items.Find(ACaption);
  if Assigned(AItem) then
    MarcarItem(AItem);
end;

procedure TfrViewAgrupaciones.DesmarcarItem(AMenu : TPopupMenu; const ACaption : String);
var
  AItem : TMenuItem;
begin
  AItem := AMenu.Items.Find(ACaption);
  if Assigned(AItem) then
  begin
    AItem.Enabled := True;
    AItem.Checked := False;
  end;
end;

procedure TfrViewAgrupaciones.DesmarcarItems(AMenu : TPopupMenu);
var
  i : integer;
begin
  for i := 0 to AMenu.Items.Count - 1 do
  begin
    AMenu.Items[i].Enabled := True;
    AMenu.Items[i].Checked := False;
  end;
end;


function TfrViewAgrupaciones.GetAgrupaciones: TStringList;
var
  aux : String;
  AIndex : Integer;
begin
  Result := TStringList.Create;
  if actBoton1.Enabled then
  begin
    aux := TBXButton1.Caption;
    aux := StringReplace(aux, '&', '', [rfReplaceAll, rfIgnoreCase]);
    if Length(Trim(aux)) > 0 then
      Result.Add(aux);

    if actBoton2.Enabled then
    begin
      aux := TBXButton2.Caption;
      aux := StringReplace(aux, '&', '', [rfReplaceAll, rfIgnoreCase]);
      if Length(Trim(aux)) > 0 then
        Result.Add(aux);

      if actBoton3.Enabled then
      begin
        aux := TBXButton3.Caption;
        aux := StringReplace(aux, '&', '', [rfReplaceAll, rfIgnoreCase]);
        if Length(Trim(aux)) > 0 then
          Result.Add(aux);
      end;
    end;
  end;
end;

procedure TfrViewAgrupaciones.actBoton2Update(Sender: TObject);
begin
  (Sender as TAction).Enabled := Length(TBXButton1.Caption) > 0;
end;

procedure TfrViewAgrupaciones.actBoton3Update(Sender: TObject);
begin
  (Sender as TAction).Enabled := Length(TBXButton2.Caption) > 0;
end;

procedure TfrViewAgrupaciones.actBoton2Execute(Sender: TObject);
begin
  //
end;

procedure TfrViewAgrupaciones.actBoton3Execute(Sender: TObject);
begin
  //
end;

procedure TfrViewAgrupaciones.actBoton1Execute(Sender: TObject);
begin
  //
end;

procedure TfrViewAgrupaciones.AMenuItemClick(Sender: TObject);
var
  i : integer;
begin
  if (Sender as TMenuItem).MenuIndex > 0 then // El 0 es 'Sin agrupacion'
  begin
    DesmarcarItem(PopupMenu1, FBotonPulsado.Caption);
    DesmarcarItem(PopupMenu2, FBotonPulsado.Caption);
    DesmarcarItem(PopupMenu3, FBotonPulsado.Caption);
    FBotonPulsado.Caption := (Sender as TMenuItem).Caption;
    MarcarItem(Sender as TMenuItem);
  end
  else begin
    DesmarcarItems(PopupMenu1);
    DesmarcarItems(PopupMenu2);
    DesmarcarItems(PopupMenu3);
    // Anular los botones que estén por debajo del actual
    for i := 3 downto FBotonPulsado.Tag do
      TTBXButton(FindComponent('TBXButton' + IntToStr(i))).Caption := '';
  end;
end;

constructor TfrViewAgrupaciones.Create(AOwner: TComponent);
begin
  inherited;
  FColumns := TStringList.Create;

  TBXButton1.OnDropDown := OnButtonPopup;
  TBXButton2.OnDropDown := OnButtonPopup;
  TBXButton3.OnDropDown := OnButtonPopup;
end;

procedure TfrViewAgrupaciones.OnButtonPopup(Sender: TTBXCustomButton;
  var AllowDropDown: Boolean);
begin
  FBotonPulsado := (Sender as TTBXButton);
end;

procedure TfrViewAgrupaciones.Execute;
var
  AgrBotones : TStringList;
  i : Integer;
begin
  AgrBotones := GetAgrupaciones;
  for i := 0 to FColumns.Count - 1 do
    (FColumns.Objects[i] as TcxGridDBColumn).GroupIndex := AgrBotones.IndexOf(FColumns.Strings[i]);
end;

procedure TfrViewAgrupaciones.Refresh;
begin
  RellenarListaColumnas;
  RellenarAgrupaciones;
end;

procedure TfrViewAgrupaciones.RellenarListaColumnas;
var
  i : integer;

  function DarItem (const ACaption : string):TMenuItem;
  begin
    Result := NewItem(ACaption, 0, false, true, AMenuItemClick, 0, '');
  end;

begin
  PopupMenu1.Items.Clear;
  PopupMenu2.Items.Clear;
  PopupMenu3.Items.Clear;

  PopupMenu1.Items.Add(DarItem(SIN_AGRUPACION));
  PopupMenu2.Items.Add(DarItem(SIN_AGRUPACION));
  PopupMenu3.Items.Add(DarItem(SIN_AGRUPACION));

  for i := 0 to ViewInformeBaseGrid.ColumnCount - 1 do
  begin
    FColumns.AddObject(ViewInformeBaseGrid.Columns[i].Caption, ViewInformeBaseGrid.Columns[i]);
    PopupMenu1.Items.Add(DarItem(ViewInformeBaseGrid.Columns[i].Caption));
    PopupMenu2.Items.Add(DarItem(ViewInformeBaseGrid.Columns[i].Caption));
    PopupMenu3.Items.Add(DarItem(ViewInformeBaseGrid.Columns[i].Caption));
  end;
end;

destructor TfrViewAgrupaciones.Destroy;
begin
  FreeAndNIL(FColumns);
  inherited;
end;

procedure TfrViewAgrupaciones.RellenarAgrupaciones;
var
  i : integer;
  AList : TStringList;
begin
  AList := TStringList.Create;

  try
    // Recuperar las agrupaciones del grid
    for i := 0 to ViewInformeBaseGrid.GroupedColumnCount - 1 do
    begin
      if i = 3 then
        Break;  // Sólo meter las 3 primera agrupaciones
      AList.Add(ViewInformeBaseGrid.GroupedColumns[i].Caption);
    end;

    if AList.Count > 0 then
    begin
      DesmarcarItems(PopupMenu1);
      DesmarcarItems(PopupMenu2);
      DesmarcarItems(PopupMenu3);

      // Anular los botones que estén por debajo del actual
      for i := 1 to 3 do
        TTBXButton(FindComponent('TBXButton' + IntToStr(i))).Caption := '';

      TBXButton1.Caption := AList.Strings[0];
      MarcarItem(TBXButton1.DropDownMenu, AList.Strings[0]);

      if AList.Count > 1 then
      begin
        TBXButton2.Caption := AList.Strings[1];
        MarcarItem(TBXButton2.DropDownMenu, AList.Strings[1]);

        if AList.Count > 2 then
        begin
          TBXButton3.Caption := AList.Strings[2];
          MarcarItem(TBXButton3.DropDownMenu, AList.Strings[2]);
        end;
      end;
    end;
  finally
    FreeAndNil(AList);
  end;
end;

procedure TfrViewAgrupaciones.SetModificable(const Value: Boolean);
begin
  inherited;
  pnlAgrupaciones.Enabled := Value;

  if pnlAgrupaciones.Enabled then
  begin
    Label1.Font.Color := clWindowText;
    Label2.Font.Color := clWindowText;
    Label3.Font.Color := clWindowText;
    TBXButton1.Font.Color := clWindowText;
    TBXButton2.Font.Color := clWindowText;
    TBXButton3.Font.Color := clWindowText;
  end
  else begin
    Label1.Font.Color := clBtnShadow;
    Label2.Font.Color := clBtnShadow;
    Label3.Font.Color := clBtnShadow;
    TBXButton1.Font.Color := clBtnShadow;
    TBXButton2.Font.Color := clBtnShadow;
    TBXButton3.Font.Color := clBtnShadow;
  end
end;

end.
