📄 stopwatch.cpp
字号:
/****************************************************************************** File: 'stopwatch.cpp'**** Created: Sat Dec 2 15:58:32 2006** by: Wya ($Id: edited Sat Dec 2 15:58:32 2006 $)**** WARNING! All changes made by wya******************************************************************************/#include "stopwatch.h"#include "analogclock.h"#include "digitalclock.h"#include "yconfigure.h"#include "ydefine.h"#include <QTimer>#include <QMessageBox>#define VALUE_100MS 110 //how much micro second we add one#define VALUE_OFFSET_SECOND 5 //every how second we offset 100 second#define VALUE_NORMAL_COUNTER 8 //800 micro second to one second#define VALUE_OFFSET_COUNTER 8 //800 micro second to one second#define SaveLog ("stopwatch.log")/***** construction*****/YStopWatch::YStopWatch(QWidget *parent ) : QDialog(parent){ setupUi(this); m_hours=0; m_minutes=0; m_seconds=0; m_microSeconds=0; m_msPerS=9;// default m_lastMicroSeconds=-1; m_lastTimeSecond=0; m_DelayTime=0;//default m_timer=NULL; m_delayTimer=NULL; m_offsetMS=0; m_indication=0;//default is 0 m_SaveLogFile=SaveLog;//default is from the file #ifdef LOG_STOPWATCH m_LogStream=NULL; m_delaySave=0;#endif setTheMsPerS(VALUE_NORMAL_COUNTER); QTime t(0,0,0,0); m_lastTime=t; //timer=NULL; m_StopWatch->setForamtWithMircoSecond(); m_DelayLcdTimer->setForamtWithSecond(); doConnection(); }/***** deconstruction*****/YStopWatch::~YStopWatch(){ }/***** do all the signal connect***/void YStopWatch::doConnection(){ // delayTimeScrollBar connect( startButton, SIGNAL(clicked(bool)), this, SLOT(slotStartButton(bool))); connect(delayTimeScrollBar, SIGNAL(valueChanged (int)), this, SLOT(slotSetDelayTimer(int)));}/*****change the value of delay time*****/void YStopWatch::slotSetDelayTimer(int i){ QString txt= getTimeString(0, 0,i); m_DelayLcdTimer->setDisplayTime(txt); m_DelayTime=i;}/***** click button*****/void YStopWatch::slotStartButton(bool checked ){ if(startButton->text()=="Start" && m_DelayTime==0 ) { startStopWatch();#ifdef LOG_STOPWATCH m_StartStamp=QDateTime::currentDateTime();#endif } else if(startButton->text()=="Start" && m_DelayTime>0) //start delay timer { //m_StopWatch->setForamtWithMircoSecond(); startDelayWatch();#ifdef LOG_STOPWATCH m_StartStamp=QDateTime::currentDateTime(); m_delaySave=m_DelayTime;#endif } else { // stop the watch timer delayTimeScrollBar->setValue(0); m_DelayTime=0; if(m_timer!=NULL) m_timer->stop(); if(m_delayTimer!=NULL) m_delayTimer->stop(); #ifdef LOG_STOPWATCH m_StopStamp=QDateTime::currentDateTime(); writeTimeToTheStream();#endif startButton->setText("Start"); //send signal emit stopStopWatch(false); }}/***** do stop watch event*****/void YStopWatch::slotDisplayStopWatchTimer(){ m_microSeconds=m_microSeconds+1; if(m_microSeconds>=getTheMsPerS()) { m_microSeconds=0; QTime updateTime= QTime::currentTime(); if(updateTime.second()-m_lastTimeSecond >0) //get current timr { qDebug(" how much timer is rest =%d\n",updateTime.second()-m_lastTimeSecond); } m_lastTimeSecond=updateTime.second(); m_seconds = m_seconds+1; //we will offet the micro second if(m_offsetMS <VALUE_OFFSET_SECOND) { m_offsetMS=m_offsetMS+1; setTheMsPerS(VALUE_NORMAL_COUNTER); } else { m_offsetMS=0; setTheMsPerS(VALUE_OFFSET_COUNTER); } if(m_seconds>59) //for minutes { m_seconds = 0; m_minutes=m_minutes+1; if(m_minutes>59) //for hours { m_minutes=0; m_hours=m_hours+1; //we don't think we use more 24 } } } //set value to the display string QString txt= getTimeString(m_hours, m_minutes,m_seconds,m_microSeconds*100); m_StopWatch->setDisplayTime(txt);}/**********/void YStopWatch::slotDelayTimeEvent(){ if(m_DelayTime < 1) { m_delayTimer->stop(); // start the new timer startStopWatch(); } else { m_DelayTime= m_DelayTime-1; qDebug(" m_delay timer is%d \n", m_DelayTime); QString txt= getTimeString(0,0,m_DelayTime); m_DelayLcdTimer->setDisplayTime(txt); }}/***** start the timer of stop watch*****/void YStopWatch::startStopWatch(){ m_hours=0; m_minutes=0; m_seconds=0; m_microSeconds=0; m_lastMicroSeconds=-1; //just a indicate m_lastTimeSecond=0; if(m_timer==NULL) { m_timer = new QTimer(this);} else { delete m_timer ; m_timer=NULL; m_timer = new QTimer(this); } connect(m_timer, SIGNAL(timeout()), this, SLOT(slotDisplayStopWatchTimer())); // connect(m_timer, SIGNAL(timeout()), this, SLOT(ajustWatchTimer())); m_lastTime=QTime::currentTime(); m_lastTimeSecond=m_lastTime.second(); m_timer->start(VALUE_100MS); startButton->setText("Stop");}/***** start delay event*****/void YStopWatch::startDelayWatch(){ if(m_delayTimer==NULL) { m_delayTimer = new QTimer(this);} else { delete m_delayTimer; m_delayTimer=NULL; m_delayTimer= new QTimer(this); } connect(m_delayTimer, SIGNAL(timeout()), this, SLOT(slotDelayTimeEvent())); // connect(m_timer, SIGNAL(timeout()), this, SLOT(ajustWatchTimer())); m_delayTimer->start(1000); startButton->setText("Stop");}/***** return a string like hh:mm:ss.z** ***/QString YStopWatch::getTimeString(int hh,int mm,int ss,int zz){ QTime time (hh,mm,ss,zz); QString txt = time.toString("hh:mm:ss.z"); qDebug("txt length =%d",txt.length()); txt=txt.left(10); qDebug(txt.toAscii()); return txt;}/***** overload function*****/QString YStopWatch::getTimeString(int hh,int mm,int ss){ QTime time (hh,mm,ss); QString txt = time.toString("hh:mm:ss"); qDebug(txt.toAscii()); return txt;}/***** get the offet value *****/void YStopWatch::ajustWatchTimer(){ int value=0; QTime getTime = QTime::currentTime (); //check the value of if((m_lastMicroSeconds >900 || getTime.msec() <100 ) ) { value =getTime.msec()+1000-m_lastMicroSeconds; } else { value =getTime.msec()-m_lastMicroSeconds; } if(value <150 && m_lastMicroSeconds!=-1 ) { m_ajustValue.append(value); } m_lastMicroSeconds=getTime.msec(); }/**********/int YStopWatch::getTheAjustValue(){ int res=0; int i=0; #if 0 qDebug(" get all the value of adjust \n"); for ( i = 0; i <m_ajustValue .size(); ++i) { qDebug("ajust value [%d]=%d\n",i,m_ajustValue[i]); }#endif //get mint value for ( i = 1; i <m_ajustValue .size(); ++i) { if ( m_ajustValue.at(0)>m_ajustValue.at(i) ) { m_ajustValue.swap (0, i); } // } // remove min value m_ajustValue.removeAt(0); //get max value for ( i = 1; i <m_ajustValue .size(); ++i) { if ( m_ajustValue.at(0)<m_ajustValue.at(i) ) { m_ajustValue.swap (0, i); } // } //remove max value m_ajustValue.removeAt(0); for ( i = 0; i <m_ajustValue .size(); ++i) { res=res+m_ajustValue[i]; } res=res/m_ajustValue .size(); qDebug(" this ajust value = %d \n", res); m_msPerS =1000/res; //QMessageBox::about(this, "About <Application>",QString::number(m_msPerS)); return res;}/***** set windows name for synchro stopwatch*****/void YStopWatch::setTitleName(){ QString str="Stop Watch"; int i =getIndication(); str.append(" ["); str.append(QString::number(i)); str.append("]"); this->setWindowTitle(str);}/***** event close windows*****/void YStopWatch::closeEvent( QCloseEvent* ce ){ //stop the time if(m_timer!=NULL) { delete m_timer ; m_timer=NULL; } //send this signal in order to we can do some thing :(. emit closeStopWatch(getIndication()); //start close win ce->accept(); }/***** set text stream from parent *****/#ifdef LOG_STOPWATCHvoid YStopWatch::setTextStream(QTextStream * stream){ m_LogStream = stream;}#endif/***** write Time stamp to log file *****/#ifdef LOG_STOPWATCHvoid YStopWatch::writeTimeToTheStream(){ if(m_LogStream!=NULL) { *m_LogStream<<"######"<<char(0x0d)<<char(0x0a)<<"##"<<" Start Time :"<<m_StartStamp.toString("hh:mm:ss ; yyyy MM dd ")<<char(0x0d)<<char(0x0a); *m_LogStream<<"##"<<" Delay Time :"<<m_delaySave<<char(0x0d)<<char(0x0a);#if 0 *m_LogStream<<"##"<<" Stop Time :"<<m_StopStamp.toString("hh:mm:ss ; yyyy MM dd ")<<char(0x0d)<<char(0x0a);#endif *m_LogStream<<"##"<<" Total Run Time :"<<m_StopWatch->getDisplayTime()<<char(0x0d)<<char(0x0a); *m_LogStream<<"######"<<char(0x0d)<<char(0x0a); m_delaySave=0; m_LogStream->flush(); }}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -