unit Ssned;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, Grids, RXGrids, ToolWin,dates, ActnList,iniFiles;

type
  TfmSsnEd = class(TForm)
    CoolBar1: TCoolBar;
    ToolBar1: TToolBar;
    gdSnl: TRxDrawGrid;
    ToolButton1: TToolButton;
    ActionList1: TActionList;
    actInsert: TAction;
    ToolButton2: TToolButton;
    actDelete: TAction;
    actGraph: TAction;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    actLoadFromFile: TAction;
    actSaveToFile: TAction;
    procedure gdSnlDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure FormCreate(Sender: TObject);
    procedure actInsertExecute(Sender: TObject);
    procedure gdSnlGetEditText(Sender: TObject; ACol, ARow: Integer;
      var Value: String);
    procedure gdSnlSetEditText(Sender: TObject; ACol, ARow: Integer;
      const Value: String);
    procedure gdSnlGetEditStyle(Sender: TObject; ACol, ARow: Integer;
      var Style: TInplaceEditStyle);
    procedure gdSnlGetPicklist(Sender: TObject; ACol, ARow: Integer;
      PickList: TStrings);
    function gdSnlAcceptEditKey(Sender: TObject; var Key: Char): Boolean;
    procedure gdSnlSelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
    procedure actDeleteExecute(Sender: TObject);
    procedure actGraphExecute(Sender: TObject);
    procedure actLoadFromFileExecute(Sender: TObject);
    procedure actSaveToFileExecute(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure UpdateGrid;
  end;

var
  fmSsnEd: TfmSsnEd;

implementation

uses Data, FormHolder, SsnGr;

{$R *.DFM}

procedure TfmSsnEd.gdSnlDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
const
  gdTitles : array[0..5] of string =
  ('α/α','Περιγραφή','Εναρξη','Λήξη','Ημέρες','Λειτουργία');

begin
  with Sender as TRxDrawGrid do
  case ARow of
   0    : DrawStr(rect,gdTitles[acol],taCenter);
   else if aRow<=snl.Count then
         case ACol of
           0 : DrawStr(rect,IntToStr(ARow),taCenter);
           1 : DrawStr(rect,snl[ARow-1].Description,taLeftJustify);
           2 : DrawStr(rect,sDate(snl[Arow-1].DayStart*24),taCenter);
           3 : DrawStr(rect,sDate(snl[Arow-1].DayStop*24),taCenter);
           4 : DrawStr(rect,IntToStr(snl[ARow-1].days),taCenter);
           5 : DrawStr(rect,wayopdes[snl[ARow-1].wayop],taCenter);
         end;
  end;
end;

procedure TfmSsnEd.FormCreate(Sender: TObject);
begin
  UpdateGrid;
end;

procedure TfmSsnEd.actInsertExecute(Sender: TObject);
var
 last : TSeason;
 i    : integer;
begin
  Last := snl.LastSsn;
  with snl,AddSsn do
  begin
    if (last<>nil) and (last.DayStop<365) then
    begin
      DayStart := last.DayStop + 1;
      DayStop  := 365;
    end;
  end;

  // Ενημέρωση όλων των συσκευών
  // για την περίοδο που προστέθηκε ...

  for i:= 0 to FDevList.Count-1 do
      FDevList[i].SeasonAdded(snl.Count-1);

  UpdateGrid;
  gdSnl.row := snl.Count;
  gdsnl.InvalidateRow(gdSnl.row);
end;

procedure TfmSsnEd.UpdateGrid;
begin
  if snl.Count>0 then
     gdSnl.RowCount := snl.Count + 1
  else
     gdSnl.RowCount := 2;
end;

procedure TfmSsnEd.gdSnlGetEditText(Sender: TObject; ACol, ARow: Integer;
  var Value: String);
begin
  case ACol of
    1 : value := snl[arow-1].Description;
    2 : value := sDate(snl[arow-1].HourStart);
    3 : value := sDate(snl[arow-1].HourStop);
    5 : value := wayopdes[snl[arow-1].wayop];
  end
end;

procedure TfmSsnEd.gdSnlSetEditText(Sender: TObject; ACol, ARow: Integer;
  const Value: String);
var
 i : integer;
begin
  case ACol of
    1 : snl[arow-1].Description := value ;
    2 : snl[arow-1].HourStart   := iDate(value,0);
    3 : snl[arow-1].HourStop    := iDate(value,0);
    5 : for i:=High(wayopdes) downto 0 do
        if value=wayopdes[i] then
        begin
          snl[arow-1].wayop := i;
          break;
        end;
  end;
  gdSnl.InvalidateRow(ARow);
end;

procedure TfmSsnEd.gdSnlGetEditStyle(Sender: TObject; ACol, ARow: Integer;
  var Style: TInplaceEditStyle);
begin
  if Acol=5 then
     Style := iePickList;
end;

procedure TfmSsnEd.gdSnlGetPicklist(Sender: TObject; ACol, ARow: Integer;
  PickList: TStrings);
var
 i : integer;
begin
  if ACol=5 then
  begin
    for i:=0 to High(wayopdes) do
        PickList.Add(wayopdes[i]);
  end;
end;

function TfmSsnEd.gdSnlAcceptEditKey(Sender: TObject;
  var Key: Char): Boolean;
begin
  with Sender as TRxDrawGrid do
   if col in [4,5] then
      result := false
   else
      result := true;
end;

procedure TfmSsnEd.gdSnlSelectCell(Sender: TObject; ACol, ARow: Integer;
  var CanSelect: Boolean);
begin
  with sender as TRxDrawGrid do
  case acol of
    4   : options := options - [goEditing];
    else  options := options + [goEditing];
  end;
  actDelete.Enabled := Arow>1
end;

procedure TfmSsnEd.actDeleteExecute(Sender: TObject);
var
 i ,
 di : integer;
begin
  with gdsnl do
  if snl.Count>1 then
  begin
    di := gdsnl.row-1;
    snl.items[gdsnl.row-1].Free;
    UpdateGrid;
    InvalidateRow(row);

   // Ενημέρωση όλων των συσκευών
   // για την περίοδο που Διαγράφηκε  ...
    for i:= 0 to FDevList.Count-1 do
        FDevList[i].SeasonDeleted(di);
  end;
end;

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

procedure TfmSsnEd.actLoadFromFileExecute(Sender: TObject);
var
  ini : TIniFile;
  fnm : string;
begin
  fnm := OpendialogExec('Αρχείο Περιόδων|*.ssn','');
  if fnm<>'' then
  begin
    ini := TIniFile.Create(Fnm);
    try
      snl.LoadFromIni(ini);
      UpdateGrid;
      gdSnl.Invalidate;
    finally
      ini.Free;
    end;
  end;
end;

procedure TfmSsnEd.actSaveToFileExecute(Sender: TObject);
var
  ini : TIniFile;
  fnm : string;
begin
  fnm := SavedialogExec('Αρχείο Περιόδων|*.ssn','');
  if fnm<>'' then
  begin
    ini := TIniFile.Create(Fnm);
    try
      snl.SaveToIni(ini);
    finally
      ini.Free;
    end;
  end;
end;

end.
