Subversion Repositories ExamClock

Rev

Rev 12 | Blame | Compare with Previous | Last modification | View Log | RSS feed

unit mainunit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
  LCLType, StdCtrls, ExtCtrls, ComCtrls, EditBtn, Buttons, unit1, unit2;

type

  { TMainForm }

  TMainForm = class(TForm)
    StartButton: TBitBtn;
    CurrentTimeLabel: TLabel;
    AdvancedLabel: TLabel;
    AdvancedTimeLabel: TLabel;
    Label1: TLabel;
    FoundationLabel: TLabel;
    FoundationTimeLabel: TLabel;
    IntermediateLabel: TLabel;
    IntermediateTimeLabel: TLabel;
    MainMenu: TMainMenu;
    FileMenu: TMenuItem;
    FileExit: TMenuItem;
    HelpMenu: TMenuItem;
    HelpAbout: TMenuItem;
    CurrentTimePanel: TPanel;
    FoundationPanel: TPanel;
    IntermediatePanel: TPanel;
    AdvancedPanel: TPanel;
    FileExams: TMenuItem;
    FileSeperator1: TMenuItem;
    StatusBar: TStatusBar;
    Timer1: TTimer;
    procedure FileExamsClick(Sender: TObject);
    procedure FileExitClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
    procedure FormCreate(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure HelpAboutClick(Sender: TObject);
    procedure StartButtonClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { private declarations }
    ClockRunning: Boolean;
    FoundationDuration, FoundationHalfTime: TDateTime;
    IntermediateDuration, IntermediateHalfTime: TDateTime;
    AdvancedDuration, AdvancedHalfTime: TDateTime;
    Sec, TenMins, ZeroHour: TDateTime;
    FoundationCaption, IntermediateCaption, AdvancedCaption: String;
  public
    { public declarations }
  end;

var
  MainForm: TMainForm;

implementation

{$R *.lfm}

{ TMainForm }

procedure TMainForm.FileExitClick(Sender: TObject);
begin
  Close;
end;

procedure TMainForm.FileExamsClick(Sender: TObject);
begin
  if Form2.ShowModal = ID_OK then
  begin
    FoundationPanel.Enabled := Form2.FoundationCheckBox.Checked;
    FoundationLabel.Visible := Form2.FoundationCheckBox.Checked;
    FoundationTimeLabel.Visible := Form2.FoundationCheckBox.Checked;
    IntermediatePanel.Enabled := Form2.IntermediateCheckBox.Checked;
    IntermediateLabel.Visible := Form2.IntermediateCheckBox.Checked;
    IntermediateTimeLabel.Visible := Form2.IntermediateCheckBox.Checked;
    AdvancedPanel.Enabled := Form2.AdvancedCheckBox.Checked;
    AdvancedLabel.Visible := Form2.AdvancedCheckBox.Checked;
    AdvancedTimeLabel.Visible := Form2.AdvancedCheckBox.Checked;
  end;
end;

procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: boolean);
begin
  CanClose := Application.MessageBox('Confirm application exit?',
           'Confirm Exit', MB_ICONQUESTION+MB_YESNO) = IDYES;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  // Initalise variables
  ClockRunning := False;
  { Values for testing }
{  FoundationDuration := StrToTime('00:01:00');
  FoundationHalfTime := StrToTime('00:00:30');
  IntermediateDuration := StrToTime('00:02:00');
  IntermediateHalfTime := StrToTime('00:01:00');
  AdvancedDuration := StrToTime('00:03:00');
  AdvancedHalfTime := StrToTime('00:01:30');
  TenMins := StrToTime('00:00:10');}
  { Live values }
  FoundationDuration := StrToTime('01:00:00');
  FoundationHalfTime := StrToTime('00:30:00');
  IntermediateDuration := StrToTime('01:30:00');
  IntermediateHalfTime := StrToTime('00:45:00');
  AdvancedDuration := StrToTime('02:00:00');
  AdvancedHalfTime := StrToTime('01:00:00');
  TenMins := StrToTime('00:10:00');

  Sec := StrToTime('00:00:01');
  ZeroHour := StrToTime('00:00:00');
end;

procedure TMainForm.FormResize(Sender: TObject);
var
  h: Integer;
begin
  // set panel heights to fit screen
  h := (Height - 2 * StatusBar.Height) div 4;
  CurrentTimePanel.Height := h;
  FoundationPanel.Height := h;
  IntermediatePanel.Height := h;
  AdvancedPanel.Height := h;
  Label1.Caption := 'Time Now:';
  FoundationLabel.Caption := FoundationCaption;
  IntermediateLabel.Caption := IntermediateCaption;
  AdvancedLabel.Caption := AdvancedCaption;
  Application.ProcessMessages;
end;

procedure TMainForm.FormShow(Sender: TObject);
var
  h: Integer;
begin
  // set panel heights to fit screen
  h := (Height - 2 * StatusBar.Height) div 4;
  CurrentTimePanel.Height := h;
  FoundationPanel.Height := h;
  IntermediatePanel.Height := h;
  AdvancedPanel.Height := h;

  CurrentTimeLabel.Caption := TimeToStr(Time);
  StatusBar.Panels[0].Text := DateToStr(Date);
  FoundationCaption := 'Foundation:';
  FoundationCaption := FoundationCaption + sLineBreak + 'Duration: ' +
                    TimeToStr(FoundationDuration);
  FoundationCaption := FoundationCaption + sLineBreak + 'Half Time: ' +
                    TimeToStr(FoundationHalfTime);
  FoundationLabel.Caption := FoundationCaption;
  FoundationTimeLabel.Caption := TimeToStr(FoundationDuration);
  IntermediateCaption := 'Intermediate:';
  IntermediateCaption := IntermediateCaption + sLineBreak + 'Duration: ' +
                      TimeToStr(IntermediateDuration);
  IntermediateCaption := IntermediateCaption + sLineBreak + 'Half Time: ' +
                      TimeToStr(IntermediateHalfTime);
  IntermediateLabel.Caption := IntermediateCaption;
  IntermediateTimeLabel.Caption := TimeToStr(IntermediateDuration);
  AdvancedCaption := 'Advanced:';
  AdvancedCaption := AdvancedCaption + sLineBreak + 'Duration: ' +
                  TimeToStr(AdvancedDuration);
  AdvancedCaption := AdvancedCaption + sLineBreak + 'Half Time: ' +
                  TimeToStr(AdvancedHalfTime);
  AdvancedLabel.Caption := AdvancedCaption;
  AdvancedTimeLabel.Caption := TimeToStr(AdvancedDuration);
end;

procedure TMainForm.HelpAboutClick(Sender: TObject);
begin
  Form1.ShowModal;
end;

procedure TMainForm.StartButtonClick(Sender: TObject);
var
  start: TDateTime;
begin
  start := Time;
  FoundationCaption := 'Foundation:';
  FoundationCaption := FoundationCaption + sLineBreak + 'Duration: ' +
                    TimeToStr(FoundationDuration);
  FoundationCaption := FoundationCaption + sLineBreak + 'Half Time: ' +
                    TimeToStr(start + FoundationHalfTime);
  FoundationCaption := FoundationCaption + sLineBreak + 'End Time: ' +
                    TimeToStr(start + FoundationDuration);
  FoundationLabel.Caption := FoundationCaption;
  IntermediateCaption := 'Intermediate:';
  IntermediateCaption := IntermediateCaption + sLineBreak + 'Duration: ' +
                      TimeToStr(IntermediateDuration);
  IntermediateCaption := IntermediateCaption + sLineBreak + 'Half Time: ' +
                      TimeToStr(start + IntermediateHalfTime);
  IntermediateCaption := IntermediateCaption + sLineBreak + 'End Time: ' +
                      TimeToStr(start + IntermediateDuration);
  IntermediateLabel.Caption := IntermediateCaption;
  AdvancedCaption := 'Advanced:';
  AdvancedCaption := AdvancedCaption + sLineBreak + 'Duration: ' +
                  TimeToStr(AdvancedDuration);
  AdvancedCaption := AdvancedCaption + sLineBreak + 'Half Time: ' +
                  TimeToStr(start + AdvancedHalfTime);
  AdvancedCaption := AdvancedCaption + sLineBreak + 'End Time: '+
                  TimeToStr(start + AdvancedDuration);
  AdvancedLabel.Caption := AdvancedCaption;

  // start the clock
  ClockRunning := True;
  StartButton.Enabled := False;
end;

procedure TMainForm.Timer1Timer(Sender: TObject);
begin
  CurrentTimeLabel.Caption := TimeToStr(Time);
  StatusBar.Panels[0].Text := DateToStr(Date);
  if ClockRunning then
  begin
    { update the display
      first the Foundation display }
    if FoundationDuration > ZeroHour then
    begin
      FoundationDuration := FoundationDuration - Sec;
      if FoundationDuration <= TenMins then
           FoundationTimeLabel.Color := clFuchsia
      else
      begin
        if FoundationDuration <= FoundationHalfTime then
          FoundationTimeLabel.Color := clYellow;
      end;
    end
    else
    begin
      FoundationDuration := ZeroHour;
      FoundationTimeLabel.Color := clRed;
      FoundationPanel.Enabled := False;
    end;
    FoundationTimeLabel.Caption := TimeToStr(FoundationDuration);
    { update the Intermediate display }
    if IntermediateDuration > ZeroHour then
    begin
      IntermediateDuration := IntermediateDuration - Sec;
      if IntermediateDuration <= TenMins then
           IntermediateTimeLabel.Color := clFuchsia
      else
      begin
        if IntermediateDuration <= IntermediateHalfTime then
          IntermediateTimeLabel.Color := clYellow;
      end;
    end
    else
    begin
      IntermediateDuration := ZeroHour;
      IntermediateTimeLabel.Color := clRed;
      IntermediatePanel.Enabled := False;
    end;
    IntermediateTimeLabel.Caption := TimeToStr(IntermediateDuration);
    { update the Advanced display }
    if AdvancedDuration > ZeroHour then
    begin
      AdvancedDuration := AdvancedDuration - Sec;
      if AdvancedDuration <= TenMins then
        AdvancedTimeLabel.Color := clFuchsia
      else
      begin
        if AdvancedDuration <= AdvancedHalfTime then
          AdvancedTimeLabel.Color := clYellow;
      end;
    end
    else
    begin
      AdvancedDuration := ZeroHour;
      AdvancedTimeLabel.Color := clRed;
      AdvancedPanel.Enabled := False;
    end;
    AdvancedTimeLabel.Caption := TimeToStr(AdvancedDuration);
  end;
  Label1.Caption := 'Time Now:';
  FoundationLabel.Caption := FoundationCaption;
  IntermediateLabel.Caption := IntermediateCaption;
  AdvancedLabel.Caption := AdvancedCaption;
  Application.ProcessMessages;
end;

end.