- if (!qApp)
- new QApplication(argc, argv);
- else
- WindowCore::instanceCount++;
-}
-
-WindowCore::~WindowCore()
-{
- if (WindowCore::instanceCount == 0)
- delete qApp;
- else
- WindowCore::instanceCount--;
-}
-
-//============================================================
-// QtDrawingArea
-
-class QtDrawingArea: public DrawingAreaInterface,
- public WindowCore, public QWidget {
-private:
- static int visibleCount;
-
- QImage *image;
- QPainter *painter;
-
-private:
- void init(int width, int height, const char *title);
-
-protected:
- void paintEvent(QPaintEvent *)
- {
- QPainter painter(this);
- painter.drawImage(0, 0, *image);
- }
-
- void closeEvent(QCloseEvent *event)
- {
- QtDrawingArea::visibleCount--;
- event->accept();
- }
-
- void keyPressEvent(QKeyEvent *event)
- {
- if (event->key() == Qt::Key_Escape) {
- event->accept();
- close();
- } else
- event->ignore();
- }
-
-public:
- QtDrawingArea(int argc, char *argv[],
- int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
- const char *title = NULL);
-
- QtDrawingArea(QWidget *parent, Qt::WindowFlags flags,
- int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
- const char *title = NULL);
-
- QtDrawingArea(int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
- const char *title = NULL);
-
- ~QtDrawingArea();
-
- int width() const
- {
- return image->width();
- }
-
- int height() const
- {
- return image->height();
- }
-
- void setColor(const QColor &color)
- {
- QPen pen(painter->pen());
- pen.setColor(color);
- painter->setPen(pen);
- }
- void setColor(float red, float green, float blue)
- {
- QColor color;
- color.setRgbF(red, green, blue);
- this->setColor(color);
- }
-
- void drawPoint(int x, int y)
- {
- painter->drawPoint(x, y);
- this->update();
- }
-
- void drawLine(int x1, int y1, int x2, int y2)
- {
- painter->drawLine(x1, y1, x2, y2);
- this->update();
- }
-
- void flush()
- {
- qApp->sendPostedEvents(this, 0);
- qApp->processEvents();
- qApp->flush();
- }
-
- void wait()
- {
- if (QtDrawingArea::visibleCount > 1)
- while (this->isVisible())
- qApp->processEvents(QEventLoop::WaitForMoreEvents);
- else if (QtDrawingArea::visibleCount > 0)
- qApp->exec();
- }
-
- void waitAll()
- {
- if (QtDrawingArea::visibleCount > 0)
- qApp->exec();