/* Basic Qt animation template by Chuck Liang, Hofstra University Computer Science. This program may not be used in commercial software. */ // student will define animate function in a separate file #include #include #include #include #include #include #include #include #include #include #include #define XBOUND 600 #define YBOUND 500 class mywindow : public QWidget /* a template for a type of window */ { Q_OBJECT private: QPainter * brush; /* an object that can do some graphics */ QPainter * dbrush; /* for drawing to the real screen */ QPainter * pbrush; // printing QPrinter printer; QPixmap * buffer; // double buffer of animation and printing int proceed; QMenuBar *menu; QPopupMenu *printop; void animate(); // to be defined by student void delay(int ms) // delay by ms milliseconds. { struct timeval tv; tv.tv_sec = ms / 1000; ms = ms % 1000; tv.tv_usec = ms * 1000; select(0,NULL,NULL,NULL,&tv); } void clear() { brush->setBrush(QBrush(white,SolidPattern)); // clears buffer brush->drawRect(0,0,XBOUND,YBOUND); // restart with white background brush->setBrush(QBrush(NoBrush)); } void refresh() // updates display { brush->flush(); dbrush->drawPixmap(0,0,*buffer); dbrush->flush(); } /* a function that Qt expects to be defined: */ void paintEvent(QPaintEvent *) { dbrush->begin(this); // prepares the brush brush->begin(buffer); // prepares the brush brush->setBrush(QBrush(white,SolidPattern)); brush->drawRect(0,0,XBOUND,YBOUND); // restart with white background brush->setBrush(QBrush(NoBrush)); animate(); brush->flush(); // bitBlt(buffer,0,0,this,0,0,XBOUND,YBOUND); // printing brush->end(); // stops the "brush" session dbrush->end(); // stops the "brush" session } public slots: void printpic() { proceed = printer.setup(); if (proceed) { // pbrush->setPen(red); pbrush->drawEllipse(10,20,100,100); pbrush->begin(&printer); pbrush->drawPixmap(0,0,*buffer); pbrush->end(); } } // end printpic public: mywindow() /* constructor for the window template */ { setGeometry(100, 100, XBOUND, YBOUND); /* set position and size of program */ brush = new QPainter; // creates the brush object dbrush = new QPainter; // creates the brush object pbrush = new QPainter; buffer = new QPixmap(XBOUND,YBOUND); /* // no print menu in animation version of program menu = new QMenuBar(this); printop = new QPopupMenu(); printop->insertItem("Print Pict", this, SLOT(printpic())); printop->insertItem("quit", qApp, SLOT(quit())); menu->insertItem("file",printop); */ } }; int main(int argc, char **argv) // main is entry point of entire program { QApplication theprogram(argc, argv); // but the real program is a Qt one mywindow thewindow; // that uses a window! theprogram.setMainWidget(&thewindow); // sets the main window thewindow.show(); // shows and executes the Qt app. theprogram.exec(); return 0; // exits gracefully }