X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/graphlib.git/blobdiff_plain/f41c1c8ff250f095b47fb6d5f7e578279b0c2844..7e44377ed59d10dcf9f04ff898691b99d5f8a1c8:/DrawingWindow.h?ds=sidebyside diff --git a/DrawingWindow.h b/DrawingWindow.h index 7dd9bb7..9e86889 100644 --- a/DrawingWindow.h +++ b/DrawingWindow.h @@ -1,29 +1,125 @@ #ifndef DRAWING_WINDOW_H #define DRAWING_WINDOW_H -#include "DrawingArea.h" -#include -#include +#include +#include +#include +#include +#include +#include #include +#include class DrawingWindow: public QWidget { /* Q_OBJECT */ public: - DrawingWindow(DrawingArea &a); - DrawingWindow(QWidget *parent, DrawingArea &a); - DrawingWindow(QWidget *parent, Qt::WindowFlags flags, DrawingArea &a); + typedef void (*ThreadFunction)(DrawingWindow &); + + static const int DEFAULT_WIDTH = 640; + static const int DEFAULT_HEIGHT = 480; + + DrawingWindow(ThreadFunction fun, + int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT); + DrawingWindow(QWidget *parent, + ThreadFunction fun, + int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT); + DrawingWindow(QWidget *parent, Qt::WindowFlags flags, + ThreadFunction fun, + int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT); + + ~DrawingWindow(); + + int width() const; + int height() const; + + void setColor(float red, float green, float blue); + void setColor(const QColor &color); + + void drawPoint(int x, int y); + void drawLine(int x1, int y1, int x2, int y2); protected: void paintEvent(QPaintEvent *ev); + void showEvent(QShowEvent *ev); void timerEvent(QTimerEvent *ev); private: - DrawingArea &drawingArea; - QPixmap pixmap; + class DrawingThread; + + static const int paintInterval = 33; + QBasicTimer timer; - void initialize(); + QImage *image; + QPainter *painter; + + DrawingThread *thread; + bool thread_started; + + bool dirtyFlag; + QRect dirtyRect; + + bool mutex_enabled; + QMutex mutex; + + void initialize(ThreadFunction fun, int width, int height); + + void lock(); + void unlock(); + + void setDirtyRect(); + void setDirtyRect(int x, int y); + void setDirtyRect(int x1, int y1, int x2, int y2); + void setDirtyRect(const QRect &rect); }; +inline +int DrawingWindow::width() const +{ + return image->width(); +} + +inline +int DrawingWindow::height() const +{ + return image->height(); +} + +inline +void DrawingWindow::lock() +{ + if (mutex_enabled) + mutex.lock(); + if (!mutex_enabled) + mutex.unlock(); +} + +inline +void DrawingWindow::unlock() +{ + mutex.unlock(); +} + +inline +void DrawingWindow::setDirtyRect() +{ + dirtyFlag = true; + dirtyRect.setRect(0, 0, width(), height()); +} + +inline +void DrawingWindow::setDirtyRect(int x, int y) +{ + setDirtyRect(QRect(x, y, 1, 1)); +} + +inline +void DrawingWindow::setDirtyRect(int x1, int y1, int x2, int y2) +{ + QRect r; + r.setCoords(x1, y1, x2, y2); + setDirtyRect(r.normalized()); +} + #endif // !DRAWING_WINDOW_H