unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, uniGUITypes, uniGUIAbstractClasses,
  uniGUIClasses, uniGUIRegClasses, uniGUIForm,uUniShowCaseFrame,
  uniButton, uniGUIBaseClasses, uniPanel, uniScrollBox,
  uniBitBtn, uniMultiItem, uniComboBox, uniImage,MAth, uniLabel,
  ServerModule;

type
  TMainForm = class(TUniForm)
    TopPanel: TUniPanel;
    bAddProduct: TUniButton;
    bDeleteProduct: TUniButton;
    UniScrollBox: TUniScrollBox;
    bAddManyProducts: TUniButton;
    bTest: TUniBitBtn;
    cbMode: TUniComboBox;
    OpenDialog: TOpenDialog;
    pBottomPanel: TUniPanel;
    bPrevious: TUniBitBtn;
    bNext: TUniBitBtn;
    lPages: TUniLabel;
    bLoadAllFromDB: TUniBitBtn;
    procedure UniFormCreate(Sender: TObject);
    procedure bAddProductClick(Sender: TObject);
    procedure bAddManyProductsClick(Sender: TObject);
    procedure bTestClick(Sender: TObject);

    procedure bNextClick(Sender: TObject);
    procedure bPreviousClick(Sender: TObject);
    procedure cbModeChange(Sender: TObject);
    procedure bDeleteProductClick(Sender: TObject);
    procedure cbModeSelect(Sender: TObject);
    procedure UniFormResize(Sender: TObject);
    procedure bLoadAllFromDBClick(Sender: TObject);
  private
    { Private declarations }

    FCurrentPage:integer;

    FUniShowCaseFrame:TUniShowCaseFrame;



    procedure CenterElements;

    procedure FUniShowCaseFrameAlign;
    procedure ProportionalDiminishPicSize(FilePath:string; var uniimageSource, uniimageDest: TUniImage; maxWidth, maxHeight: Integer);

    procedure OpenLastPage(Sender:TObject);

  public
    { Public declarations }
  end;

function MainForm: TMainForm;

implementation

{$R *.dfm}

uses
  uniGUIVars, MainModule, uniGUIApplication,uDataBaseUnit,
  uAddProductForm,uUploadPicture,uDiminishPic;

function MainForm: TMainForm;
begin
  Result := TMainForm(UniMainModule.GetFormInstance(TMainForm));
end;

procedure TMainForm.bAddProductClick(Sender: TObject);
begin



{
FUniShowCaseFrame.MaxWidth:=Self.Width;
FUniShowCaseFrame.Width:=Self.Width;

FUniShowCaseFrame.AddProductFrameSinglePage;
}


fAddProductForm.UnishowCaseFrame:=FUniShowCaseFrame;

fAddProductForm.OpenLastPage:=OpenLastPage;

fAddProductForm.MainForm:=Self;

fAddProductForm.Show();




end;

procedure TMainForm.bDeleteProductClick(Sender: TObject);
begin


 FUniShowCaseFrame.
 DeleteSelectedProductFrames;

 FCurrentPage:=0;
 lPages.Text:=FCurrentPage.ToString+ ' / '+FUniShowCaseFrame.PagesCount.ToString;

end;

procedure TMainForm.bLoadAllFromDBClick(Sender: TObject);
begin

//

FUniShowCaseFrame.Width:=Self.Width;
FUniShowCaseFrame.ProductsOnPage:=10;

FUniShowCaseFrame.MaxWidth:=Self.Width;
FUniShowCaseFrame.LoadAllProductsFromDB;
FUniShowCaseFrame.OpenPage(0);

lPages.Text:=FCurrentPage.ToString+ ' / '+FUniShowCaseFrame.PagesCount.ToString;


end;

procedure TMainForm.bPreviousClick(Sender: TObject);
begin

if FCurrentPage>0 then

 begin

 Dec(FCurrentPage);
 FUniShowCaseFrame.OpenPage(FCurrentPage);

 lPages.Text:=FCurrentPage.ToString+ ' / '+FUniShowCaseFrame.PagesCount.ToString;
 end;


end;



procedure TMainForm.bTestClick(Sender: TObject);
var s:string;
  i: Integer;
begin

//

for i := 0 to UniMainModule.SelectedProductsIdList.Count-1 do
s:=s+UniMainModule.SelectedProductsIdList[i]+#13#10;

ShowMessage(s);


end;


procedure TMainForm.ProportionalDiminishPicSize(
FilePath:string;
var uniimageSource:TUniImage;
var uniimageDest:TUniImage;
maxWidth:Integer;
maxHeight:Integer

);
var
  currentWidth: integer;
  currentHeight: integer;
  koeff: Extended;

begin

// Checks...

if FilePath='' then exit;
if not ( Assigned(uniimageSource) ) or not ( Assigned(uniimageDest) ) then exit;

 
//Proportional diminishing
uniimageSource.Picture.LoadFromFile(FilePath);


currentWidth:=uniimageSource.Picture.Width;
currentHeight:=uniimageSource.Picture.Height;

if (currentWidth>maxWidth) or (currentHeight>maxHeight) then
koeff:=Min(maxWidth/currentWidth,maxHeight/currentHeight) else
koeff:=1;

if koeff<>1 then

 begin


  uniimageDest.Width:=MaxWidth;
  uniimageDest.Height:=MaxHeight;

  uniimageDest.Proportional:=true;
  uniimageDest.Stretch:=true;
  uniimageDest.Assign(uniimageSource);

 
 end else

 begin

  uniimageDest.Width:=uniimageSource.Width;
  uniimageDest.Height:=uniimageSource.Height;
  
  uniimageDest.Assign(uniimageSource);

 
 end;


end;



procedure TMainForm.cbModeChange(Sender: TObject);
begin
  //
end;

procedure TMainForm.cbModeSelect(Sender: TObject);
var
  i: Integer;
begin



 case cbMode.ItemIndex of

  0: begin

  ShowMessage('Only Multipage supported');


  cbMode.ItemIndex:=1;
  cbModeSelect(UniApplication);

  {
  //clearing object List;
  FUniShowCaseFrame.ProductsOL.Clear;


  bAddProduct.Enabled:=true;

  bAddManyProducts.Enabled:=false;
  bNext.Enabled:=false;
  bPrevious.Enabled:=false;
  }
  end;


    1: begin


     //clearing object List;
  {   FUniShowCaseFrame.ProductsOL.Clear;

  bAddProduct.Enabled:=false;

  bAddManyProducts.Enabled:=true;
  bNext.Enabled:=true;
  bPrevious.Enabled:=true;
  }
  end;


 end;

end;

procedure TMainForm.CenterElements;
begin

lPages.Left:=Trunc(Self.Width/2 - lPages.Width/2);

bPrevious.Left:=Trunc( lPages.Left- bPrevious.Width-15 );
bNext.Left:=Trunc( lPages.Left+lPages.Width+15 );

end;

procedure TMainForm.FUniShowCaseFrameAlign;
begin

FUniShowCaseFrame.Width:=Self.Width;
FUniShowCaseFrame.Height:=Self.Height;

end;

procedure TMainForm.OpenLastPage(Sender: TObject);
begin

FCurrentPage:=FUniShowCaseFrame.PagesCount;
lPages.Text:=FCurrentPage.ToString+ ' / '+FUniShowCaseFrame.PagesCount.ToString;

end;

procedure TMainForm.bAddManyProductsClick(Sender: TObject);
var
  i: Integer;
begin

 ShowMessage('To do');

{
for i := 0 to 20 do
 begin

 FUniShowCaseFrame.MaxWidth:=Self.Width;
 FUniShowCaseFrame.Width:=Self.Width;
 FUniShowCaseFrame.AddProductFrameSinglePage;


 end;

}

{
// lets create 21 elements with 10 per page

FUniShowCaseFrame.MaxWidth:=Self.Width;
FUniShowCaseFrame.Width:=Self.Width;
FUniShowCaseFrame.ProductsOnPage:=10;

for I := 0 to 20 do FUniShowCaseFrame.AddProductFrameMultiPages;

FUniShowCaseFrame.OpenPage(FCurrentPage);

}
end;

procedure TMainForm.bNextClick(Sender: TObject);
begin

if FCurrentPage<FUniShowCaseFrame.PagesCount then

 begin

 Inc(FCurrentPage);
 FUniShowCaseFrame.OpenPage(FCurrentPage);

 lPages.Text:=FCurrentPage.ToString+ ' / '+FUniShowCaseFrame.PagesCount.ToString;
 end;



end;

procedure TMainForm.UniFormCreate(Sender: TObject);
begin

FUniShowCaseFrame:=TUniShowCaseFrame.Create(Self);
FUniShowCaseFrame.Parent:=UniScrollBox;// Self;
//FUniShowCaseFrame.Align:=alClient;

FCurrentPage:=0;

FUniShowCaseFrame.MaxWidth:=Self.Width;
FUniShowCaseFrameAlign;


CenterElements;

end;

procedure TMainForm.UniFormResize(Sender: TObject);
begin

//ShowMessage('Resize');

CenterElements;


FUniShowCaseFrame.Width:=Self.Width;
FUniShowCaseFrame.ProductsOnPage:=10;

FUniShowCaseFrame.MaxWidth:=Self.Width;
//FUniShowCaseFrame.LoadAllProductsFromDB;
FUniShowCaseFrame.OpenPage(FCurrentPage);

lPages.Text:=FCurrentPage.ToString+ ' / '+FUniShowCaseFrame.PagesCount.ToString;




end;

initialization
  RegisterAppFormClass(TMainForm);

end.
