unit LoadConst;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, ComCtrls, ToolWin, Grids, RXGrids,deviceSel, Placemnt,series,data,
  ActnList;

type
  TfmLoadConstr = class(TForm)
    Panel1: TPanel;
    gdDevices: TRxDrawGrid;
    CoolBar1: TCoolBar;
    ToolBar1: TToolBar;
    Splitter1: TSplitter;
    sbDevicesToogle: TToolButton;
    FormStorage: TFormStorage;
    ToolButton2: TToolButton;
    ToolButton4: TToolButton;
    ToolButton1: TToolButton;
    SaveDialog: TSaveDialog;
    ToolButton5: TToolButton;
    ActionList1: TActionList;
    actDelete: TAction;
    ToolButton3: TToolButton;
    procedure sbDevicesToogleClick(Sender: TObject);
    procedure gdDevicesDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure FormCreate(Sender: TObject);
    procedure gdDevicesDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure gdDevicesDragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure ToolButton2Click(Sender: TObject);
    procedure gdDevicesGetEditText(Sender: TObject; ACol, ARow: Integer;
      var Value: String);
    procedure gdDevicesSelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
    procedure gdDevicesSetEditText(Sender: TObject; ACol, ARow: Integer;
      const Value: String);
    procedure ToolButton3Click(Sender: TObject);
    procedure ShowGraph(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormStorageRestorePlacement(Sender: TObject);
    procedure FormStorageSavePlacement(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure actDeleteExecute(Sender: TObject);
    procedure actDeleteUpdate(Sender: TObject);
  private
    { Private declarations }
    fmDevSelDck : TFmDevSel;

  public
    { Public declarations }
    procedure AddLoadDev(Sender: TObject);
  end;

var
  fmLoadConstr: TfmLoadConstr;

implementation

uses  DevEdit, GraphBaseSt, FormHolder;

{$R *.DFM}

procedure TfmLoadConstr.sbDevicesToogleClick(Sender: TObject);
begin
  fmDevSelDck.Visible := TToolButton(Sender).Down;
end;

procedure TfmLoadConstr.gdDevicesDrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);

  procedure IcnDraw(ImgIdx : integer);
  var
   ps : integer;
  begin
    ps := (Rect.Right-Rect.Left-16) div 2;
    with Sender As TRXDrawGrid do
      dm.SmallImages.Draw(Canvas,Rect.Left+ps,Rect.Top+2,ImgIdx);
  end;

begin
  with Sender As TRXDrawGrid do
  if ARow=0 then
  begin
    case ACol of
      0 : CTextRect(Canvas,Rect,'a/a');
      1 : CTextRect(Canvas,Rect,'*');
      2 : CTextRect(Canvas,Rect,'Αρ.Συσ.');
      3 : CTextRect(Canvas,Rect,'Συσκευή');
      4 : CTextRect(Canvas,Rect,'Ισχύς');
    end;
  end
  else
  begin
    if (FDevList.Count>0) then
    with TDeviceEl(FDevList[ARow-1]) do
       case ACol of
         0 : CTextRect(Canvas,Rect,IntToStr(ARow));
         1 : IcnDraw(FImageIndex);
         2 : CTextRect(Canvas,Rect,IntToStr(FDevCount));
         3 : LTextRect(Canvas,Rect,FDevName);
         4 : CTextRect(Canvas,Rect,FloatToStr(FRatedPower));
       end
  end;
end;

procedure TfmLoadConstr.FormCreate(Sender: TObject);
begin

  fmDevSelDck := TFmDevSel.Create(Self);
  
  with fmDevSelDck do
  begin
    FormStorage.Active:=False;
    DragKind := dkDock;
    OnShow := tbnLargeClick;
    ManualDock(Panel1);
    tbnLarge.Down := True;
    tbnList.Down := False;

    Visible := True;
    ListView1.OnDblClick := AddLoadDev;
    ListView1.DragMode := dmAutomatic;
    gdDevices.ControlStyle := gdDevices.ControlStyle + [csDisplayDragImage];
  end;

  if FDevList.Count>0 then
     gdDevices.RowCount := FDevList.Count +1
  else
     gdDevices.RowCount := 2;
end;

procedure TfmLoadConstr.AddLoadDev(Sender: TObject);
var
 ADev ,
 NDev : TDeviceEl;
 i,j  : integer;
begin
  if not Assigned(fmDevSelDck.ListView1.Selected) then
     Exit;

  ADev := TDeviceEl(fmDevSelDck.ListView1.Selected.DAta);

  if ADev<>nil then
  begin
    NDev :=TDeviceEl.AsCopyOff(ADev);
    for i:=1 to snl.Count-1 do
    begin
      ndev.FHourOp.Add(THourOp.Create);
      for j:=0 to 23 do
        ndev.FHourOp[i,j] := ndev.FHourOp[i-1,j];
    end;
    if NDev.FNotKnownOp then
    begin
      with TFmDevEdit.Create(NDev) do
      try
        if ShowModal=mrOk then
        begin
        end
      finally
        Free;
      end;
    end
    else
    if not NDev.FKnownRatedPower then
    begin
      if not InputFloat('Ονομαστική Ισχύς',NDev.FRatedPower) then
      begin
        NDev.Free;
        Exit;
      end;
    end;
    FDevList.Add(NDev);


    gdDevices.RowCount := FDevList.Count+1;
    gdDevices.Refresh;
  end;
end;

procedure TfmLoadConstr.gdDevicesDragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
begin
  Accept := True;
end;

procedure TfmLoadConstr.gdDevicesDragDrop(Sender, Source: TObject; X,
  Y: Integer);
begin
  if Source=fmDevSelDck.ListView1 then
     AddLoadDev(Sender);
end;

procedure TfmLoadConstr.ToolButton2Click(Sender: TObject);
var
 ADev : TDeviceEl;
begin
  if FDevList.Count>0 then
  begin
    ADev:= FDevList[gdDevices.Row-1];
    with TFmDevEdit.Create(ADev) do
    try
      ShowModal
    finally
      Free;
    end;
  end;
end;

procedure TfmLoadConstr.gdDevicesGetEditText(Sender: TObject; ACol,
  ARow: Integer; var Value: String);
begin
  case ACol of
    2 : Value := IntToStr(TDeviceEl(FDevList[ARow-1]).FDevCount);
    3 : Value := TDeviceEl(FDevList[ARow-1]).FDevName;
    4 : Value := FloatToStr(TDeviceEl(FDevList[ARow-1]).FRatedPower);
  end;
end;

procedure TfmLoadConstr.gdDevicesSelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
  with Sender As TRxDrawGrid do
  begin
    if (FDevList.Count>0) and (ACol in [2,3,4]) then
      Options := Options + [goEditing]
    else
      Options := Options - [goEditing];
  end
end;

procedure TfmLoadConstr.gdDevicesSetEditText(Sender: TObject; ACol,
  ARow: Integer; const Value: String);
begin
  case ACol of
    2 : TDeviceEl(FDevList[ARow-1]).FDevCount := StrToInt(Value);
    3 : TDeviceEl(FDevList[ARow-1]).FDevName := Value ;
    4 : TDeviceEl(FDevList[ARow-1]).FRatedPower  := StrToFloat(Value);
  end;
end;

procedure TfmLoadConstr.ToolButton3Click(Sender: TObject);
begin
  if FDevList.Count>0 then
  begin
    FDevList.Delete(gdDevices.Row-1);
    if FDevList.Count>0 then
      gdDevices.RowCount := FDevList.Count+1;
    gdDevices.Refresh;
  end;
end;

procedure TfmLoadConstr.ShowGraph(Sender: TObject);
var
  sr : TBarSeries;
  ch : TFmGraphBaseSt;
  h  ,
  i  : integer;
  FDev : TDeviceEl;
begin
  //ch := TFmGraphBase.Create(Application);
  ch := Sender As TFmGraphBaseSt;
  ch.Caption := 'Κατανομή Φορτίου Ζήτησης';
  with ch  do
  begin
    Chart1.Title.Text.Text := 'Κατανομή Φορτίου Ζήτησης';
    for i:=0 to FDevList.Count-1 do
    begin
      FDev := TDeviceEl(FDevList[i]);
      sr := TBarSeries.Create(ch.Chart1);
      sr.Title := FDev.FDevName;
      if FDev.FDevCount>1 then
         sr.Title := sr.Title+Format(' (%d)',[FDev.FDevCount]);
      sr.ParentChart := ch.Chart1;
      sr.MultiBar := mbStacked;
      sr.Marks.Visible := False;
      with sr do
      begin
        for h:=0 to 23 do
          AddXY(h,FDev.FHourOp[0,h]*FDev.FRatedPower*FDev.FDevCount);
      end;
    end;
    Chart1.Legend.Visible := True;
  end;
end;

procedure TfmLoadConstr.ToolButton1Click(Sender: TObject);
begin
  with SaveDialog do
  if Execute then
     FDevList.SaveToFile(FileName);
end;

procedure TfmLoadConstr.FormStorageRestorePlacement(Sender: TObject);
begin
  SaveDialog.InitialDir := FormStorage.ReadString('InitialDir','');
  DM.SelDevListDlg.InitialDir := SaveDialog.InitialDir;
end;

procedure TfmLoadConstr.FormStorageSavePlacement(Sender: TObject);
begin
  if SaveDialog.FileName<>'' then
     FormStorage.WriteString('InitialDir',ExtractFilePath(SaveDialog.FileName));
end;

procedure TfmLoadConstr.ToolButton5Click(Sender: TObject);
begin
  with DM.SelDevListDlg do
  if Execute then
  begin
    FDevList.LoadFromFile(FileName);
    gdDevices.RowCount := FDevList.Count + 1;
    gdDevices.Refresh;
  end;
end;

procedure TfmLoadConstr.ToolButton4Click(Sender: TObject);
begin
  HoldFormClass(
  FmHolder.PnlHolder,fmHolder.Panel1,True,false,TFmGraphBaseSt,ShowGraph);
end;

procedure TfmLoadConstr.actDeleteExecute(Sender: TObject);
begin
  if FDevList.Count>0 then
  begin
    FDevList.Delete(gdDevices.Row-1);
    if FDevList.Count>0 then
       gdDevices.RowCount := FDevList.Count + 1
    else
       gdDevices.RowCount := 2;

    gdDevices.Refresh;
  end;
end;

procedure TfmLoadConstr.actDeleteUpdate(Sender: TObject);
begin
  TAction(Sender).Enabled := FDevList.Count>0;
end;

end.
