// @(#)root/html:$Id$
// Author: OttoSchaile
// * Copyright Marabou
#include "TROOT.h"
#include "TSystem.h"
#include "TDatime.h"
#include "TStopwatch.h"
#include "TTimer.h"
#include "TGMrbValuesAndText.h"
// #include "SetColor.h"
#include "mywatch.h"
#include
static const char setblack[] = "\033[39m"; // set "default" color - may be other than "black"
static const char setred[] = "\033[31m";
static const char setgreen[] = "\033[32m";
static const char setyellow[] = "\033[33m";
static const char setblue[] = "\033[34m";
static const char setmagenta[] = "\033[35m";
static const char setcyan[] = "\033[36m";
static const char bell[] = "\007\007\007";
ClassImp(MyWatch)
MyWatch::MyWatch()
//____________________________________________________________________//
// //
// This programs implements //
// a wallclock, //
// stopwatch //
// an egg-timer //
// //
////////////////////////////////////////////////////////////////////////
{
fVerbose = 0;
fDialog = NULL;
fRealTimeDisplay = "";
fRunTimeDisplay= "";
fElapsedTimeDisplay= "";
fPresetEggTime = 10.; // seconds
fElapsedTime = 0;
fStopwatchRunning = 0;
fRealTimeDisplayRate = 2;
fRealTimeCounter = 0;
fStopwatch = NULL;
fEggTimer = NULL;
fDatime = new TDatime();
UpdateWallclock();
StartGui();
fTimer = new TTimer();
fTimer->Connect("Timeout()", "MyWatch", this, "HandleTimerEvents()");
fTimer->Start(1000, kFALSE); // 1 second continuos
};
//_____________________________________________________________________
MyWatch::~MyWatch()
{
if ( fTimer )
fTimer->Stop();
if ( fEggTimer ) {
fEggTimer->Stop();
}
}
//_____________________________________________________________________
void MyWatch::StartGui()
////////////////////////////////////////////////////////////////////////
//
// Build and start a simple graphical user interface
//
////////////////////////////////////////////////////////////////////////
{
static const Char_t helptext[] =
"This program implements:\n\
a wallclock \n\
an egg-timer\n\
a stopwatch\n\
"
;
fRowLab= new TList();
Int_t ind = 0;
TString startwatch("StartStopWatch()");
TString stopwatch("StopStopWatch()");
TString resumewatch("ResumeStopWatch()");
TString starteggtimer("StartEggTimer()");
fRowLab->Add(new TObjString("StringValue_Realtime"));
fBidRealTimeDisplay = ind;
fValp[ind++] = &fRealTimeDisplay;
fRowLab->Add(new TObjString("CommandButt_Start Eggtimer"));
fValp[ind++] = &starteggtimer;
fRowLab->Add(new TObjString("DoubleValue_Preset Eggtimer"));
fBidPresetEggTime= ind;
fValp[ind++] = &fPresetEggTime;
fRowLab->Add(new TObjString("CommandButt_Start Stopwatch"));
fValp[ind++] = &startwatch;
fRowLab->Add(new TObjString("CommandButt_Stop Stopwatch"));
fValp[ind++] = &stopwatch;
fRowLab->Add(new TObjString("CommandButt_Resume Stopwatch"));
fValp[ind++] = &resumewatch;
fRowLab->Add(new TObjString("DoubleValue_Elapsed Time"));
fBidElapsedTime = ind;
fValp[ind++] = &fElapsedTime;
Int_t Ok = 0;
Int_t itemwidth = 250;
fDialog =
new TGMrbValuesAndText("MyWatch", NULL, &Ok,itemwidth, NULL,
NULL, NULL, fRowLab, fValp,
NULL, NULL, helptext, this, this->ClassName());
fDialog->Move(5,455);
}
//____________________________________________________________________
void MyWatch::HandleTimerEvents()
{
fRealTimeCounter++;
if (fRealTimeCounter >= fRealTimeDisplayRate) {
UpdateWallclock();
fRealTimeCounter = 0;
}
}
//____________________________________________________________________
void MyWatch::HandleEggTimerEvents()
{
cout << setblue << ">>>>>>>>>>> Eggs are ready <<<<<<<<<<" << setblack << endl;
gSystem->Exec("play $MARABOU/sounds/Ende_der_Seereise.wav 2>/dev/null");
}
//____________________________________________________________________
void MyWatch::StartEggTimer()
{
if( fEggTimer == NULL ) {
fEggTimer = new TTimer();
fEggTimer->Connect("Timeout()", "MyWatch", this, "HandleEggTimerEvents()");
}
Int_t tms = (Int_t)fPresetEggTime * 1000;
fEggTimer->Start(tms, kTRUE); // single shot
cout << setblue << "Starting Egg timer with: "
<< fPresetEggTime << " seconds" << setblack << endl;
}
//____________________________________________________________________
void MyWatch::StartStopWatch()
{
if( fStopwatch == NULL ) {
fStopwatch = new TStopwatch;
}
fStopwatch->Start(kTRUE);
fStopwatchRunning = 1;
}
//____________________________________________________________________
void MyWatch::StopStopWatch()
{
fStopwatch->RealTime();
fStopwatchRunning = 0;
UpdateWallclock();
}
//____________________________________________________________________
void MyWatch::ResumeStopWatch()
{
fStopwatch->Start(kFALSE);
fStopwatchRunning = 1;
}
//____________________________________________________________________
void MyWatch::UpdateWallclock()
{
fDatime->Set();
fRealTimeDisplay = "";
fRealTimeDisplay += fDatime->GetHour();
fRealTimeDisplay +=":";
fRealTimeDisplay += fDatime->GetMinute();
fRealTimeDisplay +=":";
fRealTimeDisplay += fDatime->GetSecond();
// cout << "UpdateWallclock() " << fRealTimeDisplay << endl;
if ( fStopwatchRunning == 1) {
fElapsedTime = fStopwatch->RealTime();
fStopwatch->Continue();
}
if ( fDialog ) {
fDialog->ReloadValues();
fDialog->DoNeedRedraw();
}
}
//____________________________________________________________________
void MyWatch::CRButtonPressed(Int_t /*wid*/, Int_t bid, TObject */*obj*/)
////////////////////////////////////////////////////////////////////////
//
// Handle button press events
//
////////////////////////////////////////////////////////////////////////
{
// cout << "CRButtonPressed " << bid << " " << obj<< endl;
if ( bid == fBidPresetEggTime ) {
cout << "Preset egg time changed" << endl;
}
if ( bid == fBidRealTimeDisplay ) {
// cout << "Wallclock changed" << endl;
}
if ( bid == fBidRealTimeDisplay ) {
// cout << "Elapsed time changed" << endl;
}
}
//____________________________________________________________________
void MyWatch::CloseDown(Int_t id)
////////////////////////////////////////////////////////////////////////
//
// Handle end of program
// id = -1 : Save and quit pressed
// -2 : Cancel
//
////////////////////////////////////////////////////////////////////////
{
cout << "CloseDown id " << id << endl;
if ( fTimer ) {
fTimer->Stop();
fTimer = NULL;
}
if ( fEggTimer ) {
fEggTimer->Stop();
fEggTimer = NULL;
}
delete this;
}