X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/graphlib.git/blobdiff_plain/58868f34882be4712e0eb1439993cd8929c1a947..a7346f12e37f808491a76b2465de8ae8cd951d51:/DrawingWindow.cpp diff --git a/DrawingWindow.cpp b/DrawingWindow.cpp index 1f5bb6b..c68b7b8 100644 --- a/DrawingWindow.cpp +++ b/DrawingWindow.cpp @@ -1,134 +1,339 @@ #include "DrawingWindow.h" +#include #include +#include #include #include -#include +class DrawingThread: public QThread { +public: + DrawingThread(DrawingWindow &w, DrawingWindow::ThreadFunction f); + void start_once(Priority priority = InheritPriority); -/* - TODO : - class DrawingWindowPrivate { ... } - externalize class DrawingThread -*/ +protected: + void run(); -DrawingWindow::DrawingWindow(ThreadFunction fun, int width, int height) +private: + DrawingWindow &drawingWindow; + DrawingWindow::ThreadFunction threadFunction; + bool started_once; + + friend class DrawingWindow; +}; + +//--- DrawingWindow ---------------------------------------------------- + +DrawingWindow::DrawingWindow(ThreadFunction f, int w, int h) : QWidget() + , width(w) + , height(h) { - initialize(fun, width, height); + initialize(f); } DrawingWindow::DrawingWindow(QWidget *parent, - ThreadFunction fun, int width, int height) + ThreadFunction f, int w, int h) : QWidget(parent) + , width(w) + , height(h) { - initialize(fun, width, height); + initialize(f); } DrawingWindow::DrawingWindow(QWidget *parent, Qt::WindowFlags flags, - ThreadFunction fun, int width, int height) + ThreadFunction f, int w, int h) : QWidget(parent, flags) + , width(w) + , height(h) { - initialize(fun, width, height); + initialize(f); } -void DrawingWindow::initialize(ThreadFunction fun, int width, int height) +DrawingWindow::~DrawingWindow() { - image = new QImage(width, height, QImage::Format_RGB32); - image->fill(QColor(Qt::white).rgb()); - - painter = new QPainter(image); - - dirtyFlag = false; + delete thread; + delete painter; + delete image; +} - setFocusPolicy(Qt::StrongFocus); - setFixedSize(image->size()); - setAttribute(Qt::WA_OpaquePaintEvent); - setFocus(); - timer.start(paintInterval, this); +void DrawingWindow::setColor(float red, float green, float blue) +{ + fgColor.setRgbF(red, green, blue); + applyColor(); +} - thread = new DrawingThread(*this, fun); - thread_started = false; +void DrawingWindow::setColor(const char *name) +{ + fgColor.setNamedColor(name); + applyColor(); } -DrawingWindow::~DrawingWindow() +void DrawingWindow::setBgColor(float red, float green, float blue) { - delete thread; - delete painter; - delete image; + bgColor.setRgbF(red, green, blue); } -void DrawingWindow::setColor(const QColor &color) +void DrawingWindow::setBgColor(const char *name) { - QPen pen(painter->pen()); - pen.setColor(color); - painter->setPen(pen); + bgColor.setNamedColor(name); } -void DrawingWindow::setColor(float red, float green, float blue) +void DrawingWindow::clearGraph() { - QColor color; - color.setRgbF(red, green, blue); - this->setColor(color); + safeLock(imageMutex); + painter->fillRect(image->rect(), bgColor); + dirty(); + safeUnlock(imageMutex); } void DrawingWindow::drawPoint(int x, int y) { - lock(); + safeLock(imageMutex); painter->drawPoint(x, y); - setDirtyRect(x, y); - unlock(); + dirty(x, y); + safeUnlock(imageMutex); } void DrawingWindow::drawLine(int x1, int y1, int x2, int y2) { - lock(); + safeLock(imageMutex); painter->drawLine(x1, y1, x2, y2); - setDirtyRect(x1, y1, x2, y2); - unlock(); + dirty(x1, y1, x2, y2); + safeUnlock(imageMutex); +} + +void DrawingWindow::drawRect(int x1, int y1, int x2, int y2) +{ + QRect r; + r.setCoords(x1, y1, x2 - 1, y2 - 1); + r = r.normalized(); + safeLock(imageMutex); + painter->drawRect(r); + r.adjust(0, 0, 1, 1); + dirty(r); + safeUnlock(imageMutex); +} + +void DrawingWindow::fillRect(int x1, int y1, int x2, int y2) +{ + painter->setBrush(fgColor); + drawRect(x1, y1, x2, y2); + painter->setBrush(Qt::NoBrush); +} + +void DrawingWindow::drawCircle(int x, int y, int r) +{ + QRect rect; + rect.setCoords(x - r, y - r, x + r - 1, y + r - 1); + safeLock(imageMutex); + painter->drawEllipse(rect); + rect.adjust(0, 0, 1, 1); + dirty(rect); + safeUnlock(imageMutex); +} + +void DrawingWindow::fillCircle(int x, int y, int r) +{ + painter->setBrush(fgColor); + drawCircle(x, y, r); + painter->setBrush(Qt::NoBrush); +} + +void DrawingWindow::drawText(int x, int y, const char *text) +{ + QRect r(image->rect()); + r.moveTo(x, y); + safeLock(imageMutex); + painter->drawText(r, 0, text, &r); + dirty(r); + safeUnlock(imageMutex); +} + +bool DrawingWindow::sync(unsigned long time) +{ + bool synced; + safeLock(syncMutex); + if (terminateThread) { + synced = false; + } else { + qApp->postEvent(this, new QEvent(QEvent::User)); + synced = syncCondition.wait(&syncMutex, time); + } + safeUnlock(syncMutex); + return synced; +} + +void DrawingWindow::closeGraph() +{ + qApp->postEvent(this, new QEvent(QEvent::Type(QEvent::User + 1))); +} + +void DrawingWindow::sleep(unsigned long secs) +{ + DrawingThread::sleep(secs); +} + +void DrawingWindow::msleep(unsigned long msecs) +{ + DrawingThread::msleep(msecs); +} + +void DrawingWindow::usleep(unsigned long usecs) +{ + DrawingThread::usleep(usecs); } void DrawingWindow::closeEvent(QCloseEvent *ev) { + timer.stop(); thread->terminate(); + syncMutex.lock(); + terminateThread = true; // this flag is needed for the case + // where the following wakeAll() call + // occurs between the + // setTerminationEnable(false) and the + // mutex lock in safeLock() called + // from sync() + syncCondition.wakeAll(); + syncMutex.unlock(); QWidget::closeEvent(ev); thread->wait(); } +void DrawingWindow::customEvent(QEvent *ev) +{ + switch ((int )ev->type()) { + case QEvent::User: + mayUpdate(); + qApp->sendPostedEvents(this, QEvent::UpdateLater); + qApp->sendPostedEvents(this, QEvent::UpdateRequest); + qApp->sendPostedEvents(this, QEvent::Paint); + qApp->processEvents(QEventLoop::ExcludeUserInputEvents | + QEventLoop::ExcludeSocketNotifiers | + QEventLoop::DeferredDeletion | + QEventLoop::X11ExcludeTimers); + qApp->flush(); + qApp->syncX(); + syncMutex.lock(); + syncCondition.wakeAll(); + syncMutex.unlock(); + break; + case QEvent::User + 1: + close(); + break; + } +} + +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); - QRect rect = ev->rect(); - mutex.lock(); + imageMutex.lock(); QImage imageCopy(*image); - mutex.unlock(); + imageMutex.unlock(); + QRect rect = ev->rect(); widgetPainter.drawImage(rect, imageCopy, rect); } void DrawingWindow::showEvent(QShowEvent *ev) { - if (!thread_started) { - thread->start(); - thread_started = true; - } + timer.start(paintInterval, this); + thread->start_once(QThread::IdlePriority); QWidget::showEvent(ev); } void DrawingWindow::timerEvent(QTimerEvent *ev) { if (ev->timerId() == timer.timerId()) { - mutex.lock(); - if (dirtyFlag) { - update(dirtyRect); - dirtyFlag = false; - } - mutex.unlock(); + mayUpdate(); timer.start(paintInterval, this); } else { QWidget::timerEvent(ev); } } -void DrawingWindow::setDirtyRect(const QRect &rect) +//--- DrawingWindow (private methods) ---------------------------------- + +void DrawingWindow::initialize(DrawingWindow::ThreadFunction f) +{ + terminateThread = false; + lockCount = 0; + image = new QImage(width, height, QImage::Format_RGB32); + painter = new QPainter(image); + thread = new DrawingThread(*this, f); + + setFocusPolicy(Qt::StrongFocus); + setFixedSize(image->size()); + setAttribute(Qt::WA_OpaquePaintEvent); + setFocus(); + + setColor("black"); + setBgColor("white"); + clearGraph(); + + dirtyFlag = false; +} + +inline +void DrawingWindow::applyColor() +{ + QPen pen(painter->pen()); + pen.setColor(fgColor); + painter->setPen(pen); +} + +inline +void DrawingWindow::safeLock(QMutex &mutex) +{ + if (lockCount++ == 0) + thread->setTerminationEnabled(false); + mutex.lock(); +} + +inline +void DrawingWindow::safeUnlock(QMutex &mutex) +{ + mutex.unlock(); + if (--lockCount == 0) + thread->setTerminationEnabled(true); +} + +inline +void DrawingWindow::dirty() +{ + dirtyFlag = true; + dirtyRect = image->rect(); +} + +inline +void DrawingWindow::dirty(int x, int y) +{ + dirty(QRect(x, y, 1, 1)); +} + +inline +void DrawingWindow::dirty(int x1, int y1, int x2, int y2) +{ + QRect r; + r.setCoords(x1, y1, x2, y2); + dirty(r.normalized()); +} + +void DrawingWindow::dirty(const QRect &rect) { if (dirtyFlag) { dirtyRect |= rect; @@ -138,14 +343,35 @@ void DrawingWindow::setDirtyRect(const QRect &rect) } } -DrawingWindow::DrawingThread::DrawingThread(DrawingWindow &w, - ThreadFunction f) +void DrawingWindow::mayUpdate() +{ + imageMutex.lock(); + bool dirty = dirtyFlag; + QRect rect = dirtyRect; + dirtyFlag = false; + imageMutex.unlock(); + if (dirty) + update(rect); +} + +//--- DrawingThread ---------------------------------------------------- + +DrawingThread::DrawingThread(DrawingWindow &w, DrawingWindow::ThreadFunction f) : drawingWindow(w) , threadFunction(f) + , started_once(false) { } -void DrawingWindow::DrawingThread::run() +void DrawingThread::start_once(Priority priority) +{ + if (!started_once) { + started_once = true; + start(priority); + } +} + +void DrawingThread::run() { threadFunction(drawingWindow); }