/* Timer GUI by Chuck Liang, Hofstra University Computer Science. Educational program under Gnu general public license and Qt Free Edition license. No part of this code may be used for commercial purposes. */ #include #include #include #include #include #include #include #include #include #include #define BSIZE 100 #define WGAP 20 void displayUpdate(int,int,int,int); void delay(int ms) // delay by ms milliseconds. { /* int t; t = 4; QTime curTime; curTime.start(); while ( curTime.elapsed() < ms ) {t++; t--; } */ struct timeval tv; tv.tv_sec = ms / 1000; ms = ms % 1000; tv.tv_usec = ms * 1000; select(0,NULL,NULL,NULL,&tv); } // end delay /* gui *********************** */ class mywindow : public QWidget { Q_OBJECT private: QLineEdit *m0; QLineEdit *m1; QLineEdit *s0; QLineEdit *s1; QPushButton *resetB; QPushButton *startB; QPushButton *stopB; QPushButton *reverseB; QLabel *colonL; timer *thetimer; public slots: void starthandler() { int tm1, tm0, ts1, ts0; tm1 = (m1->text()).toInt(); tm0 = (m0->text()).toInt(); ts1 = (s1->text()).toInt(); ts0 = (s0->text()).toInt(); m1->clearFocus(); m0->clearFocus(); s1->clearFocus(); s0->clearFocus(); // setUpdatesEnabled( FALSE ); thetimer = new timer(tm1,tm0,ts1,ts0); thetimer->start(); // setUpdatesEnabled( TRUE ); } void resethandler() { thetimer->reset(); } public: QApplication *theapp; mywindow() { setGeometry(100,100,BSIZE*4+WGAP*6,2*BSIZE); m1 = new QLineEdit("0",this); m1->setGeometry(WGAP,WGAP,BSIZE,BSIZE); m0 = new QLineEdit("0",this); m0->setGeometry((WGAP*2+BSIZE),WGAP,BSIZE,BSIZE); colonL = new QLabel(":",this); colonL->setGeometry((WGAP*3)+BSIZE+BSIZE,WGAP,WGAP,BSIZE); colonL->setFont(QFont("Times",48,QFont::Bold)); colonL->setAlignment(Qt::AlignHCenter); s1 = new QLineEdit("0",this); s1->setGeometry((WGAP*4)+BSIZE+BSIZE,WGAP,BSIZE,BSIZE); s0 = new QLineEdit("0",this); s0->setGeometry((WGAP*5)+BSIZE+BSIZE+BSIZE,WGAP,BSIZE,BSIZE); m1->setFont(QFont("Times",BSIZE-2,QFont::Bold)); m0->setFont(QFont("Times",BSIZE-2,QFont::Bold)); s1->setFont(QFont("Times",BSIZE-2,QFont::Bold)); s0->setFont(QFont("Times",BSIZE-2,QFont::Bold)); s0->setAlignment(Qt::AlignHCenter); m0->setAlignment(Qt::AlignHCenter); s1->setAlignment(Qt::AlignHCenter); m1->setAlignment(Qt::AlignHCenter); /* m1->setReadOnly(true); m0->setReadOnly(true); s1->setReadOnly(true); s0->setReadOnly(true); */ startB = new QPushButton("Start",this); // stopB = new QPushButton("Stop",this); resetB = new QPushButton("Reset",this); startB->setGeometry(2*WGAP,WGAP*2+BSIZE,6*BSIZE/3,BSIZE/2); // stopB->setGeometry(WGAP*3+(6*BSIZE/3),WGAP*2+BSIZE,4*BSIZE/3,BSIZE/2); resetB->setGeometry(WGAP*4+(6*BSIZE/3),WGAP*2+BSIZE,6*BSIZE/3,BSIZE/2); connect(startB, SIGNAL( clicked() ), this, SLOT( starthandler() )); // connect(stopB, SIGNAL( clicked() ), this, SLOT( stophandler() )); connect(resetB, SIGNAL( clicked() ), this, SLOT( resethandler() )); } // end of constructor void updateDisplay(int mm1, int mm0, int ss1, int ss0) { /* */ s0->setText(QString::number(ss0)); s1->setText(QString::number(ss1)); m0->setText(QString::number(mm0)); m1->setText(QString::number(mm1)); theapp->flushX(); /* paintEvent(new QPaintEvent(QRect(0,0,BSIZE*4+WGAP*6,2*BSIZE))); setUpdatesEnabled( TRUE ); repaint(false); setUpdatesEnabled( FALSE ); */ } // end updateDisplay }; // end of class mywindow //use of global necessitated by lack of headers (not there in the semester yet) mywindow *thewindow; void displayUpdate(int m1, int m0, int s1, int s0) { thewindow->updateDisplay(m1,m0,s1,s0); } int main(int argc, char **argv) { QApplication mainapp(argc, argv); thewindow = new mywindow(); thewindow->setMinimumSize(100,50); mainapp.setMainWidget(thewindow); thewindow->theapp = &mainapp; thewindow->show(); return mainapp.exec(); // This should always be the last line } // of a Qt enhanced C++ program #include "timergui.moc" // Don't move this line!