From 7badda6845696cab7209c13aad6ede58aed61acd Mon Sep 17 00:00:00 2001 From: giersch Date: Tue, 6 Nov 2007 20:54:40 +0000 Subject: [PATCH] . --- DrawingWindow.cpp | 44 +++++++++++++++++++++++++++++++------------- DrawingWindow.h | 2 ++ test/hello.cpp | 14 ++++++++------ 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/DrawingWindow.cpp b/DrawingWindow.cpp index 76227ff..b60eb06 100644 --- a/DrawingWindow.cpp +++ b/DrawingWindow.cpp @@ -157,14 +157,7 @@ bool DrawingWindow::sync(unsigned long time) { bool ret; d->safeLock(d->paintMutex); - d->safeLock(d->imageMutex); -#if 1 - d->dirty(); // xxx -#else - QApplication::postEvent(this, new QPaintEvent(this->rect())); - d->dirtyFlag = false; -#endif - d->safeUnlock(d->imageMutex); + QApplication::postEvent(this, new QEvent(QEvent::User)); ret = d->paintCondition.wait(&d->paintMutex, time); d->safeUnlock(d->paintMutex); return ret; @@ -194,6 +187,36 @@ void DrawingWindow::closeEvent(QCloseEvent *ev) d->thread->wait(); } +void DrawingWindow::customEvent(QEvent *) +{ + d->imageMutex.lock(); + if (d->dirtyFlag) { + QRect r = d->dirtyRect; + d->dirtyFlag = false; + d->imageMutex.unlock(); + repaint(r); + } else + d->imageMutex.unlock(); + d->paintMutex.lock(); + d->paintCondition.wakeAll(); + d->paintMutex.unlock(); +} + +void DrawingWindow::keyPressEvent(QKeyEvent *ev) +{ + bool accept = true; + switch (ev->key()) { + case Qt::Key_Escape: + close(); + break; + default: + accept = false; + break; + } + if (accept) + ev->accept(); +} + void DrawingWindow::paintEvent(QPaintEvent *ev) { QPainter widgetPainter(this); @@ -202,11 +225,6 @@ void DrawingWindow::paintEvent(QPaintEvent *ev) d->imageMutex.unlock(); QRect rect = ev->rect(); widgetPainter.drawImage(rect, imageCopy, rect); - if (rect == this->rect()) { - d->paintMutex.lock(); - d->paintCondition.wakeAll(); - d->paintMutex.unlock(); - } } void DrawingWindow::showEvent(QShowEvent *ev) diff --git a/DrawingWindow.h b/DrawingWindow.h index c6aaa0d..66fc445 100644 --- a/DrawingWindow.h +++ b/DrawingWindow.h @@ -44,6 +44,8 @@ public: protected: void closeEvent(QCloseEvent *ev); + void customEvent(QEvent *ev); + void keyPressEvent(QKeyEvent *ev); void paintEvent(QPaintEvent *ev); void showEvent(QShowEvent *ev); void timerEvent(QTimerEvent *ev); diff --git a/test/hello.cpp b/test/hello.cpp index 8cc7b50..bc1a62e 100644 --- a/test/hello.cpp +++ b/test/hello.cpp @@ -42,6 +42,7 @@ void flip(DrawingWindow &w) for (int yy = y; yy < y + 10; yy++) { for (int x = 0; x < w.width; x++) w.drawPoint(x, yy); + w.sync(); } if ((y += 10) >= w.height) { y = 0; @@ -115,6 +116,7 @@ void mandel(DrawingWindow &w) ci += pi; } cr += pr; +// w.sync(); } } @@ -154,12 +156,6 @@ int main(int argc, char *argv[]) const int h = 700; QApplication application(argc, argv); - DrawingWindow dl(lines, w, h); - dl.show(); - - DrawingWindow dr(rectangles, w, h); - dr.show(); - const int nf = 1; const int nm = 1; DrawingWindow *dw[nf + nm]; @@ -172,5 +168,11 @@ int main(int argc, char *argv[]) for (int i = 0; i < nf + nm; ++i) dw[i]->show(); + DrawingWindow dr(rectangles, w, h); + dr.show(); + + DrawingWindow dl(lines, w, h); + dl.show(); + return application.exec(); } -- 2.20.1