unit wgEd;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  RXSpin, StdCtrls, ExtCtrls, Grids, RXGrids, ComCtrls, ToolWin, ActnList;

type
  TfmWGEd = class(TForm)
    ActionList1: TActionList;
    actInsert: TAction;
    actDelete: TAction;
    actGraph: TAction;
    CoolBar1: TCoolBar;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    actOpenPow: TAction;
    gdPC: TRxDrawGrid;
    Panel1: TPanel;
    Label1: TLabel;
    edModelName: TEdit;
    edPR: TRxSpinEdit;
    Label2: TLabel;
    Label3: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure gdPCDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure actOpenPowExecute(Sender: TObject);
    procedure actDeleteExecute(Sender: TObject);
    procedure actInsertExecute(Sender: TObject);
    procedure gdPCGetEditText(Sender: TObject; ACol, ARow: Integer;
      var Value: String);
    procedure gdPCSetEditText(Sender: TObject; ACol, ARow: Integer;
      const Value: String);
    procedure actGraphExecute(Sender: TObject);
    procedure edModelNameChange(Sender: TObject);
    procedure edPRChange(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure UpdGrid;
  end;

var
  fmWGEd: TfmWGEd;

implementation

uses Data, FormHolder, PwrCurveGr;

{$R *.DFM}

procedure TfmWGEd.FormCreate(Sender: TObject);
begin
  UpdGrid;
  edModelName.Text := wg.ModelName;
  edPR.Value       := wg.RatedPower; 
end;

procedure TfmWGEd.UpdGrid;
begin
  if wg.Count>0 then
     gdPC.RowCount := wg.Count+1
  else
     gdPC.RowCount := 2;
end;

procedure TfmWGEd.gdPCDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
const
  gdTitles : array[0..2] of string = ('Α/Α','Ταχύτητα (m/s)','Ισχύς (watt)');
begin
  with Sender as TRxDrawGrid do
  case ARow of
    0     : DrawStr(rect,gdTitles[ACol],taCenter);
    else  case ACol of
            0 : DrawStr(rect,IntToStr(ARow),taCenter);
            1 : DrawStr(rect,Format('%0.3f',[wg[arow-1].x]),taCenter);
            2 : DrawStr(rect,Format('%0.3f',[wg[arow-1].y]),taCenter);
          end;
  end;
end;

procedure TfmWGEd.actOpenPowExecute(Sender: TObject);
begin
  with dm.SelWGDlg do
  if Execute then
  begin
    wg.LoadFromWaspPow(dm.SelWGDlg.FileName);
    UpdGrid;
    gdPC.Refresh;
  end;
end;

procedure TfmWGEd.actDeleteExecute(Sender: TObject);
begin
   wg.Delete(gdpc.Row-1);
   UpdGrid;
   gdPC.Refresh;
end;

procedure TfmWGEd.actInsertExecute(Sender: TObject);
var
 pt : TPoint2d;
begin
  with wg do
  begin
    FillChar(pt,SizeOf(pt),0);
    if Count>0 then
    begin
      pt.x := wg[Count-1].x + 1;
      pt.y := wg[Count-1].y + 1;
    end;
    wg.AddPoint(pt);
    updGrid;
    gdPC.Row := Count;
    gdPC.Refresh;
  end;
end;

procedure TfmWGEd.gdPCGetEditText(Sender: TObject; ACol, ARow: Integer;
  var Value: String);
begin
  case ACol of
    1 : value := Format('%0.3f',[wg[arow-1].x]);
    2 : value := Format('%0.3f',[wg[arow-1].y]);
  end;
end;

procedure TfmWGEd.gdPCSetEditText(Sender: TObject; ACol, ARow: Integer;
  const Value: String);
begin
  case ACol of
    1 : wg.x[ARow-1] := StrToFloat(Value);
    2 : wg.y[ARow-1] := StrToFloat(Value);
  end;
end;

procedure TfmWGEd.actGraphExecute(Sender: TObject);
begin
  HoldFormClass(fmHolder.pnlHolder,fmHolder.Panel1,true,false,TFmPwrCurveGr);
end;

procedure TfmWGEd.edModelNameChange(Sender: TObject);
begin
  wg.ModelName := edModelName.Text;
end;

procedure TfmWGEd.edPRChange(Sender: TObject);
begin
  wg.RatedPower := edPr.Value;
end;

end.
