unit uUniShowCaseFrame;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, uniGUITypes, uniGUIAbstractClasses,
  uniGUIClasses, uniGUIFrame,System.Contnrs;

type
  TUniShowCaseFrame = class(TUniFrame)
  private
    { Private declarations }

    FProductsOL:TObjectList;
    FNameCount:integer;

    FMaxWidth:integer;

    FProductsOnPage:integer;
    FCurrentPageNumber:integer;
    FPagesCount:integer;



    procedure DisposeProductFrames(i: Integer; PageNumber: Integer);



  public
    { Public declarations }

    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    procedure AddProductFrameSinglePage;
    {< this method create TProductFrame, add it to ProductsOL and paint on
    Self  }


    procedure DeleteSelectedProductFrames;

    procedure AddProductFrameMultiPages;
    {< this method just create TProductFrame and add it to
    FProductsOL:TObjectList}

    procedure OpenPage(PageNumber:integer);
    {< this method paints virtual page on Self}

    procedure OpenLastPage();
    {< just opens last page}

    procedure HideAllProductFrames;
    {< just hides all frames}

    procedure LoadAllProductsFromDB;
    {<just loads all products from DB to showcase frame}

    property MaxWidth:integer read FMaxWidth write FMaxWidth;

    property ProductsOnPage:Integer read FProductsOnPage write FProductsOnPage;
    property PageNumber:Integer read FCurrentPageNumber write FCurrentPageNumber;

    property PagesCount:integer read FPagesCount write FPagesCount;

    property ProductsOL:TObjectList read FProductsOL write FProductsOL;
    {< this element is key in all unit - there is a container for all ProductFrames}




  end;

implementation

{$R *.dfm}

uses uProductFrame,uDataBaseUnit,MainModule;



{ TUniShowCaseFrame }

procedure TUniShowCaseFrame.AddProductFrameMultiPages;
var   i:integer;
      Product:TProductFrame;

begin

  i:=FProductsOL.Add( TProductFrame.Create(Self) );

  FPagesCount:=trunc(i/FProductsOnPage);


  with TProductFrame(FProductsOL[i]) do begin

  Parent:=Self;

  Inc(FNameCount);
  Name:='SomeName'+FNameCount.ToString;

  lProductName.Text:=Name;

  //add some properties here...


  end;


end;



procedure TUniShowCaseFrame.AddProductFrameSinglePage;
var   i:integer;
      Product:TProductFrame;

begin

  i:=FProductsOL.Add( TProductFrame.Create(Self) );




  with TProductFrame(FProductsOL[i]) do begin

  Parent:=Self;

  Inc(FNameCount);
  Name:='SomeName'+FNameCount.ToString;

  //In which row to put? In current or next?

   if i>0 then
     begin
       if TProductFrame(FProductsOL[i-1]).Left+
       2*TProductFrame(FProductsOL[i-1]).width>FMaxWidth-10 then

        begin
        Top:=TProductFrame(FProductsOL[i-1]).Top+
        TProductFrame(FProductsOL[i-1]).Height;
        Left:=0;

        Self.Height:=Self.Height+TProductFrame(FProductsOL[i-1]).Height;
        end else

        begin

        Top:=TProductFrame(FProductsOL[i-1]).Top;
        Left:=TProductFrame(FProductsOL[i-1]).Left+
        TProductFrame(FProductsOL[i-1]).width;

        end;



     end;


  Show;
  PaintBorder;
  end;


end;

constructor TUniShowCaseFrame.Create(AOwner: TComponent);




begin
  inherited;

  FProductsOL:=TObjectList.Create(true);
  FNameCount:=-1;
  FCurrentPageNumber:=0;

  FPagesCount:=0;



end;


procedure TUniShowCaseFrame.OpenLastPage;
var
  i: Integer;
begin

HideAllProductFrames;


// fill new ones


  for i:= FPagesCount*FProductsOnPage to FProductsOL.Count-1  do
  DisposeProductFrames(i,FPagesCount)


end;

procedure TUniShowCaseFrame.OpenPage(PageNumber: integer);
var
  i: Integer;
begin

// clear all product frames

HideAllProductFrames;


// fill new ones


        // if this last page?


  if PageNumber=FPagesCount then
  for i:= PageNumber*FProductsOnPage to FProductsOL.Count-1  do
  DisposeProductFrames(i,PageNumber)

   else

       // if not this last page?
  for i := PageNumber*FProductsOnPage to PageNumber*FProductsOnPage+FProductsOnPage-1 do
  DisposeProductFrames(i,PageNumber);



end;

procedure TUniShowCaseFrame.DeleteSelectedProductFrames;
var
  I,J: Integer;
begin


for i := 0 to UniMainModule.SelectedProductsIdList.Count-1 do
 begin
 //Delete from DB

   with DataBaseUnit.qDeleteProduct do begin

     Params.ParamValues['id']:=UniMainModule.SelectedProductsIdList[i].ToInteger();
     ExecSQL;
   end;
  end;

 //Delete from UniShowCase

 for i := UniMainModule.SelectedProductsIdList.Count-1 downto 0  do
 begin


   for j :=FProductsOL.Count-1  downto 0 do

    begin
    if UniMainModule.SelectedProductsIdList[i].ToInteger=

       TProductFrame(FProductsOL[j]).idDataBase

      then


     begin
     TProductFrame(FProductsOL[j]).Hide;

     end;


    end;



 end;

 UniMainModule.SelectedProductsIdList.Clear;


LoadAllProductsFromDB;
OpenPage(0);

end;



destructor TUniShowCaseFrame.Destroy;
begin



  inherited;
end;

procedure TUniShowCaseFrame.DisposeProductFrames(i: Integer; PageNumber: Integer);
begin
//
        if i=PageNumber*FProductsOnPage then

        begin
        TProductFrame(FProductsOL[i]).Left:=0;
        TProductFrame(FProductsOL[i]).Top:=0;
        TProductFrame(FProductsOL[i]).Show;
        end else


         begin

           // Adding next row
           if TProductFrame(FProductsOL[i-1]).Left+

             2*TProductFrame(FProductsOL[i-1]).width>FMaxWidth-10 then

              begin

              TProductFrame(FProductsOL[i]).Top:=TProductFrame(FProductsOL[i-1]).Top+
              TProductFrame(FProductsOL[i-1]).Height;
              TProductFrame(FProductsOL[i]).Left:=0;

              Self.Height:=Self.Height+TProductFrame(FProductsOL[i-1]).Height;
              end else

              begin

              TProductFrame(FProductsOL[i]).Top:=TProductFrame(FProductsOL[i-1]).Top;
              TProductFrame(FProductsOL[i]).Left:=TProductFrame(FProductsOL[i-1]).Left+
              TProductFrame(FProductsOL[i-1]).width;

              end;

         TProductFrame(FProductsOL[i]).Show;

         end;




end;

procedure TUniShowCaseFrame.HideAllProductFrames;
var
  i: Integer;
begin

// clear all product frames

if FProductsOL.Count>0 then
Self.Height:=TProductFrame(FProductsOL[0]).Height;

 for i := 0 to FProductsOL.Count-1 do
  begin

  TProductFrame(FProductsOL[i]).Left:=0;
  TProductFrame(FProductsOL[i]).Top:=0;
  TProductFrame(FProductsOL[i]).Hide;
  end;



end;



procedure TUniShowCaseFrame.LoadAllProductsFromDB;
var
  i: integer;
  thumbnailLink:string;

begin
//

HideAllProductFrames;
ProductsOL.Clear;

with DataBaseUnit.qSelectAll do begin

Open();

while not EOF do begin


  i:=FProductsOL.Add( TProductFrame.Create(Self) );
  FPagesCount:=trunc(i/FProductsOnPage);


  with TProductFrame(FProductsOL[i]) do begin

  Parent:=Self;

  Inc(FNameCount);
  Name:='SomeName'+FNameCount.ToString;

  lProductName.Text:=Name;

    lProductName.Text:='';
    lCountry.Text:='';
    lPrice.Text:='';

    lProductName.Left:=10;
    lCountry.Left:=10;
    lPrice.Left:=10;

   IndexInObjectList:=i;

  //add some properties here...

  if not FieldByName('id').IsNull then
  idDataBase:=FieldByName('id').AsInteger;

  if not FieldByName('price_begin').IsNull then
  lPrice.Text:=FieldByName('price_begin').AsInteger.ToString;

  if not FieldByName('name').IsNull then

  begin

  if FieldByName('name').AsString.Length>17 then
  ShortProductName:=FieldByName('name').AsString.Substring(0,17)+'...'
  else ShortProductName:=FieldByName('name').AsString;

  FullProductName:=FieldByName('name').AsString;

  lProductName.Text:=ShortProductName;

  end;



  if not FieldByName('country').IsNull then
  lCountry.Text:=FieldByName('country').AsString;

  thumbnailLink:='';
  if not FieldByName('ImageThumbnailLink').IsNull then
  thumbnailLink:=FieldByName('ImageThumbnailLink').AsString;

  if thumbnailLink<>'' then
  UniImageProductFrame.Picture.LoadFromFile(thumbnailLink);

  end;


Next;

end;



Close();

end;


end;

end.
