QBasicTimer timer;
QMutex imageMutex;
- QMutex paintMutex;
- QWaitCondition paintCondition;
+ QMutex syncMutex;
+ QWaitCondition syncCondition;
bool painted;
int lockCount;
void dirty(int x1, int y1, int x2, int y2);
void dirty(const QRect &rect);
+ void update();
};
//--- DrawingWindow ----------------------------------------------------
bool DrawingWindow::sync(unsigned long time)
{
- bool ret;
- d->safeLock(d->paintMutex);
- QApplication::postEvent(this, new QEvent(QEvent::User));
- ret = d->paintCondition.wait(&d->paintMutex, time);
- d->safeUnlock(d->paintMutex);
- return ret;
+ d->safeLock(d->syncMutex);
+ qApp->postEvent(this, new QEvent(QEvent::User));
+ bool synced = d->syncCondition.wait(&d->syncMutex, time);
+ d->safeUnlock(d->syncMutex);
+ return synced;
}
void DrawingWindow::sleep(unsigned long secs)
{
d->timer.stop();
d->thread->terminate();
- d->paintCondition.wakeAll();
+ d->syncMutex.lock();
+ d->syncCondition.wakeAll();
+ d->syncMutex.unlock();
QWidget::closeEvent(ev);
d->thread->wait();
}
void DrawingWindow::customEvent(QEvent *)
{
- d->paintMutex.lock();
- d->imageMutex.lock();
- if (d->dirtyFlag) {
- QRect r = d->dirtyRect;
- d->dirtyFlag = false;
- d->imageMutex.unlock();
- repaint(r);
- } else
- d->imageMutex.unlock();
- d->paintCondition.wakeAll();
- d->paintMutex.unlock();
+ d->syncMutex.lock();
+ d->update();
+ qApp->sendPostedEvents(this, QEvent::UpdateLater);
+ qApp->sendPostedEvents(this, QEvent::UpdateRequest);
+ qApp->sendPostedEvents(this, QEvent::Paint);
+ if (0) { // Disabled because this can lead to
+ // dead-lock if a close event is
+ // processed !
+ qApp->processEvents(QEventLoop::ExcludeUserInputEvents |
+ QEventLoop::ExcludeSocketNotifiers |
+ QEventLoop::DeferredDeletion |
+ QEventLoop::X11ExcludeTimers);
+ }
+ qApp->flush();
+ qApp->syncX();
+ d->syncCondition.wakeAll();
+ d->syncMutex.unlock();
}
void DrawingWindow::keyPressEvent(QKeyEvent *ev)
void DrawingWindow::timerEvent(QTimerEvent *ev)
{
if (ev->timerId() == d->timer.timerId()) {
- d->imageMutex.lock();
- if (d->dirtyFlag) {
- update(d->dirtyRect);
- d->dirtyFlag = false;
- }
- d->imageMutex.unlock();
+ d->update();
d->timer.start(d->paintInterval, this);
} else {
QWidget::timerEvent(ev);
}
}
+void DrawingWindowPrivate::update()
+{
+ imageMutex.lock();
+ bool dirty = dirtyFlag;
+ QRect rect = dirtyRect;
+ dirtyFlag = false;
+ imageMutex.unlock();
+ if (dirty)
+ q->update(rect);
+}
+
//--- DrawingThread ----------------------------------------------------
DrawingThread::DrawingThread(DrawingWindow &w, DrawingWindow::ThreadFunction f)
int c = 0;
int y = 0;
-// int h = w.height;
-// int w = w.width;
int count = 50;//1 << 31;
while (1) {
-// std::cerr << "loooooooooooooooooooooop "
-// << y << " (" << c << ")\n";
w.setColor(c, c, c);
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) {
+ w.sync();
y = 0;
c = !c;
if (!--count) break;
-// std::cerr << "loooooooooooooooooooooop "
-// << y << " (" << c << ")\n";
}
}
}
ci += pi;
}
cr += pr;
-// w.sync();
+// if (x % 10 == 0)
+// w.sync();
}
}
int y2 = rand() % ymax;
w.setColor(r, g, b);
w.drawLine(x1, y1, x2, y2);
- w.sync();
+ if (n % 100 == 0)
+ w.sync();
}
}