Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9e868897902b4f8f42907a0dd70a3ede017aa52b
[graphlib.git] / DrawingWindow.h
1 #ifndef DRAWING_WINDOW_H
2 #define DRAWING_WINDOW_H
3
4 #include <QBasicTimer>
5 #include <QColor>
6 #include <QImage>
7 #include <QMutex>
8 #include <QPainter>
9 #include <QRect>
10 #include <QWidget>
11 #include <Qt>
12
13 class DrawingWindow: public QWidget {
14 /*     Q_OBJECT */
15
16 public:
17     typedef void (*ThreadFunction)(DrawingWindow &);
18
19     static const int DEFAULT_WIDTH = 640;
20     static const int DEFAULT_HEIGHT = 480;
21
22     DrawingWindow(ThreadFunction fun,
23                   int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT);
24     DrawingWindow(QWidget *parent,
25                   ThreadFunction fun,
26                   int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT);
27     DrawingWindow(QWidget *parent, Qt::WindowFlags flags,
28                   ThreadFunction fun,
29                   int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT);
30
31     ~DrawingWindow();
32
33     int width() const;
34     int height() const;
35
36     void setColor(float red, float green, float blue);
37     void setColor(const QColor &color);
38
39     void drawPoint(int x, int y);
40     void drawLine(int x1, int y1, int x2, int y2);
41
42 protected:
43     void paintEvent(QPaintEvent *ev);
44     void showEvent(QShowEvent *ev);
45     void timerEvent(QTimerEvent *ev);
46
47 private:
48     class DrawingThread;
49
50     static const int paintInterval = 33;
51
52     QBasicTimer timer;
53
54     QImage *image;
55     QPainter *painter;
56
57     DrawingThread *thread;
58     bool thread_started;
59
60     bool dirtyFlag;
61     QRect dirtyRect;
62
63     bool mutex_enabled;
64     QMutex mutex;
65
66     void initialize(ThreadFunction fun, int width, int height);
67
68     void lock();
69     void unlock();
70
71     void setDirtyRect();
72     void setDirtyRect(int x, int y);
73     void setDirtyRect(int x1, int y1, int x2, int y2);
74     void setDirtyRect(const QRect &rect);
75 };
76
77 inline
78 int DrawingWindow::width() const
79 {
80     return image->width();
81 }
82
83 inline
84 int DrawingWindow::height() const
85 {
86     return image->height();
87 }
88
89 inline
90 void DrawingWindow::lock()
91 {
92     if (mutex_enabled)
93         mutex.lock();
94     if (!mutex_enabled)
95         mutex.unlock();
96 }
97
98 inline
99 void DrawingWindow::unlock()
100 {
101     mutex.unlock();
102 }
103
104 inline
105 void DrawingWindow::setDirtyRect()
106 {
107     dirtyFlag = true;
108     dirtyRect.setRect(0, 0, width(), height());
109 }
110
111 inline
112 void DrawingWindow::setDirtyRect(int x, int y)
113 {
114     setDirtyRect(QRect(x, y, 1, 1));
115 }
116
117 inline
118 void DrawingWindow::setDirtyRect(int x1, int y1, int x2, int y2)
119 {
120     QRect r;
121     r.setCoords(x1, y1, x2, y2);
122     setDirtyRect(r.normalized());
123 }
124
125 #endif // !DRAWING_WINDOW_H