unit DevEdit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids, RXGrids, RXSpin,data, ExtCtrls, TeeProcs, TeEngine,
  Chart, Series, Menus;

type
  TfmDevEdit = class(TForm)
    Label1: TLabel;
    edRATEDPOWER: TRxSpinEdit;
    Label2: TLabel;
    gdFactors: TRxDrawGrid;
    BtnOK: TButton;
    Button1: TButton;
    Label3: TLabel;
    Image1: TImage;
    Bevel1: TBevel;
    edDESCRIPTION: TEdit;
    Label4: TLabel;
    Chart1: TChart;
    Series1: TAreaSeries;
    cbSSn: TComboBox;
    Label5: TLabel;
    Bevel2: TBevel;
    PopupMenu1: TPopupMenu;
    mOpOff: TMenuItem;
    mOpFull: TMenuItem;
    N3: TMenuItem;
    mAsPrev: TMenuItem;
    procedure gdFactorsDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure gdFactorsGetEditText(Sender: TObject; ACol, ARow: Integer;
      var Value: String);
    procedure gdFactorsSetEditText(Sender: TObject; ACol, ARow: Integer;
      const Value: String);
    procedure edDESCRIPTIONChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cbSSnChange(Sender: TObject);
    procedure edRATEDPOWERMouseWheelUp(Sender: TObject; Shift: TShiftState;
      MousePos: TPoint; var Handled: Boolean);
    procedure edRATEDPOWERMouseWheelDown(Sender: TObject;
      Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
    procedure PopupMenu1Popup(Sender: TObject);
    procedure mAsPrevClick(Sender: TObject);
    procedure mOpOffClick(Sender: TObject);
    procedure mOpFullClick(Sender: TObject);
  private
    { Private declarations }
    FDev : TDeviceEl;
  public
    { Public declarations }
    procedure UpdateChart;
    constructor create(ADev : TDeviceEl); reintroduce; overload;
  end;

var
  fmDevEdit: TfmDevEdit;

implementation


{$R *.DFM}

constructor TfmDevEdit.create(ADev: TDeviceEl);
var
 h : integer;
 svColor : TColor;
begin
  inherited Create(Application);
  FDev := ADev;
  if FDev<>nil then
  begin
    edDESCRIPTION.Text := FDev.FDevName;
    edRATEDPOWER.Value := FDev.FRatedPower;
    svColor := Dm.LargeImages.BkColor;

    try
      dm.LargeImages.BkColor := clBtnFace;
      dm.LargeImages.Draw(Image1.Canvas,0,0,FDev.FImageIndex);
    finally
      dm.LargeImages.BkColor := svColor;
    end;

    for h:=0 to 23 do
      Series1.AddY(FDev.FHourOp[0,h]);
  end;
end;

procedure TfmDevEdit.gdFactorsDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  with Sender as TRxDrawGrid do
  begin
    if (ARow=0) and (ACol=0) then
       CTextRect(Canvas,Rect,'Διάστημα')
    else
    if (ARow=0) and (ACol=1) then
       CTextRect(Canvas,Rect,'Πιθανότητα')
    else
    if ACol=0 then
      CTextRect(Canvas,Rect,Format('%0.2d:00-%0.2d:00',[ARow-1,ARow]))
    else
    begin
      CTextRect(Canvas,Rect,
         Format('%5.2f%%',[FDev.FHourOp[cbSSn.ItemIndex,ARow-1]*100]));
    end;
  end
end;

procedure TfmDevEdit.gdFactorsGetEditText(Sender: TObject; ACol,
  ARow: Integer; var Value: String);
begin
  if ARow<>0 then
     Value := FloatToStr(FDev.FHourOp[cbSSn.ItemIndex,ARow-1]);
end;

procedure TfmDevEdit.gdFactorsSetEditText(Sender: TObject; ACol,
  ARow: Integer; const Value: String);
var
 s : string;
 v : Float;
begin
  if Pos('%',Value)<>0 then
  begin
    s:=Copy(Value,1,Pos('%',Value)-1);
    v :=  StrToFloat(s)/100;
  end
  else
    v :=  StrToFloat(Value);
  if (v>=0) and (v<=1) then
  begin
    FDev.FHourOp[cbSSn.ItemIndex,ARow-1] :=  v;;
    Series1.YValues[ARow-1] :=  FDev.FHourOp[cbSSn.ItemIndex,ARow-1];
    Chart1.Refresh;
  end
  else
    raise Exception.Create('Value must be between 0.00 - 1.00');
end;

procedure TfmDevEdit.edDESCRIPTIONChange(Sender: TObject);
begin
  FDev.FDevName := edDESCRIPTION.Text;
end;

procedure TfmDevEdit.FormCreate(Sender: TObject);
var
 i : integer;
begin
  with cbSSn do
  begin
    Items.Clear;
    for i:=0 to snl.Count-1 do
        Items.Add(snl[i].Description);
    ItemIndex := 0;
  end;
end;

procedure TfmDevEdit.cbSSnChange(Sender: TObject);
var
 h : integer;
begin
  gdFactors.Refresh;
  { ... }
  with Chart1 do
  begin
    Series1.Clear;
    for h:=0 to 23 do
       Series1.AddY(FDev.FHourOp[cbSSn.ItemIndex,h]);
  end;
end;

type
  THasUpClick = class(TRXSpinEdit)
  public
    procedure UpClick(Sender : TObject); override;
    procedure DownClick(Sender : TObject); override;
  end;

procedure TfmDevEdit.edRATEDPOWERMouseWheelUp(Sender: TObject;
  Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);


begin
  THasUpClick(edRATEDPOWER).UpClick(Sender);
end;

{ THasUpClick }

procedure THasUpClick.DownClick(Sender: TObject);
begin
  inherited DownClick(Sender);
end;

procedure THasUpClick.UpClick;
begin
  inherited upClick(Sender);
end;

procedure TfmDevEdit.edRATEDPOWERMouseWheelDown(Sender: TObject;
  Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
begin
  THasUpClick(edRATEDPOWER).DownClick(Sender);
end;

procedure TfmDevEdit.PopupMenu1Popup(Sender: TObject);
begin
  masPrev.Enabled := cbSSn.ItemIndex > 0
end;

procedure TfmDevEdit.mAsPrevClick(Sender: TObject);
var
  h : integer;
begin
  if cbSSn.ItemIndex>0 then
   for h:=0 to 23 do
     FDev.FHourOp[cbSSn.ItemIndex,h] := FDev.FHourOp[cbSSn.ItemIndex-1,h];
  gdFactors.Invalidate;
  UpdateChart;
end;

procedure TfmDevEdit.mOpOffClick(Sender: TObject);
var
 h : integer;
begin
  for h:=0 to 23 do
     FDev.FHourOp[cbSSn.ItemIndex,h] := 0;
  gdFactors.Invalidate;
  UpdateChart;
end;

procedure TfmDevEdit.mOpFullClick(Sender: TObject);
var
 h : integer;
begin
  for h:=0 to 23 do
     FDev.FHourOp[cbSSn.ItemIndex,h] := 1;
  gdFactors.Invalidate;
  UpdateChart;
end;

procedure TfmDevEdit.UpdateChart;
var
 h : integer;
begin
  Series1.Clear;
  for h:=0 to 23 do
      Series1.AddY(FDev.FHourOp[0,h]);
end;

end.
