X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/graphlib.git/blobdiff_plain/d9d083393abda0a28eb718de3000466513aa6b91..5cb6b0c2a5bd8569ba18bb274924b2e05598e851:/DrawingWindow.cpp diff --git a/DrawingWindow.cpp b/DrawingWindow.cpp index 9af145f..02df2eb 100644 --- a/DrawingWindow.cpp +++ b/DrawingWindow.cpp @@ -1,7 +1,6 @@ #include "DrawingWindow.h" #include #include -#include #include #include @@ -56,32 +55,40 @@ DrawingWindow::~DrawingWindow() delete image; } -void DrawingWindow::setColor(float red, float green, float blue) +void DrawingWindow::setColor(unsigned int color) { - fgColor.setRgbF(red, green, blue); - applyColor(); + setColor(QColor::fromRgb(color)); } void DrawingWindow::setColor(const char *name) { - fgColor.setNamedColor(name); - applyColor(); + setColor(QColor(name)); } -void DrawingWindow::setBgColor(float red, float green, float blue) +void DrawingWindow::setColor(float red, float green, float blue) +{ + setColor(QColor::fromRgbF(red, green, blue)); +} + +void DrawingWindow::setBgColor(unsigned int color) { - bgColor.setRgbF(red, green, blue); + setBgColor(QColor::fromRgb(color)); } void DrawingWindow::setBgColor(const char *name) { - bgColor.setNamedColor(name); + setBgColor(QColor(name)); +} + +void DrawingWindow::setBgColor(float red, float green, float blue) +{ + setBgColor(QColor::fromRgbF(red, green, blue)); } void DrawingWindow::clearGraph() { safeLock(imageMutex); - painter->fillRect(image->rect(), bgColor); + painter->fillRect(image->rect(), getBgColor()); dirty(); safeUnlock(imageMutex); } @@ -116,7 +123,7 @@ void DrawingWindow::drawRect(int x1, int y1, int x2, int y2) void DrawingWindow::fillRect(int x1, int y1, int x2, int y2) { - painter->setBrush(fgColor); + painter->setBrush(getColor()); drawRect(x1, y1, x2, y2); painter->setBrush(Qt::NoBrush); } @@ -134,21 +141,58 @@ void DrawingWindow::drawCircle(int x, int y, int r) void DrawingWindow::fillCircle(int x, int y, int r) { - painter->setBrush(fgColor); + painter->setBrush(getColor()); drawCircle(x, y, r); painter->setBrush(Qt::NoBrush); } -void DrawingWindow::drawText(int x, int y, const char *text) +void DrawingWindow::drawText(int x, int y, const char *text, int flags) { QRect r(image->rect()); - r.moveTo(x, y); + switch (flags & Qt::AlignHorizontal_Mask) { + case Qt::AlignRight: + r.setRight(x); + break; + case Qt::AlignHCenter: + if (x < width / 2) + r.setLeft(2 * x - width + 1); + else + r.setRight(2 * x); + break; + default: + r.setLeft(x); + } + switch (flags & Qt::AlignVertical_Mask) { + case Qt::AlignBottom: + r.setBottom(y); + break; + case Qt::AlignVCenter: + if (y < height / 2) + r.setTop(2 * y - height + 1); + else + r.setBottom(2 * y); + break; + default: + r.setTop(y); + } safeLock(imageMutex); - painter->drawText(r, 0, text, &r); + painter->drawText(r, flags, text, &r); dirty(r); safeUnlock(imageMutex); } +void DrawingWindow::drawTextBg(int x, int y, const char *text, int flags) +{ + painter->setBackgroundMode(Qt::OpaqueMode); + drawText(x, y, text, flags); + painter->setBackgroundMode(Qt::TransparentMode); +} + +unsigned int DrawingWindow::getPointColor(int x, int y) +{ + return image->pixel(x, y); +} + bool DrawingWindow::sync(unsigned long time) { bool synced; @@ -165,7 +209,7 @@ bool DrawingWindow::sync(unsigned long time) void DrawingWindow::closeGraph() { - qApp->postEvent(this, new QCloseEvent()); + qApp->postEvent(this, new QEvent(QEvent::Type(QEvent::User + 1))); } void DrawingWindow::sleep(unsigned long secs) @@ -185,48 +229,43 @@ void DrawingWindow::usleep(unsigned long usecs) void DrawingWindow::closeEvent(QCloseEvent *ev) { - qDebug(">>>CLOSING<<<\n"); - char x = 'A'; - qDebug(">>> %c <<<\n", x++); timer.stop(); - qDebug(">>> %c <<<\n", x++); thread->terminate(); - qDebug(">>> %c <<<\n", x++); syncMutex.lock(); - qDebug(">>> %c <<<\n", x++); 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() - qDebug(">>> %c <<<\n", x++); syncCondition.wakeAll(); - qDebug(">>> %c <<<\n", x++); syncMutex.unlock(); - qDebug(">>> %c <<<\n", x++); QWidget::closeEvent(ev); - qDebug(">>> %c <<<\n", x++); thread->wait(); - qDebug(">>> %c <<<\n", x++); - qDebug(">>>CLOSED<<<\n"); -} - -void DrawingWindow::customEvent(QEvent *) -{ - 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(); +} + +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) @@ -294,13 +333,31 @@ void DrawingWindow::initialize(DrawingWindow::ThreadFunction f) } inline -void DrawingWindow::applyColor() +void DrawingWindow::setColor(const QColor& color) { QPen pen(painter->pen()); - pen.setColor(fgColor); + pen.setColor(color); painter->setPen(pen); } +inline +void DrawingWindow::setBgColor(const QColor& color) +{ + painter->setBackground(color); +} + +inline +QColor DrawingWindow::getColor() +{ + return painter->pen().color(); +} + +inline +QColor DrawingWindow::getBgColor() +{ + return painter->background().color(); +} + inline void DrawingWindow::safeLock(QMutex &mutex) {