Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
.
[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 #include <QThread>
14
15 class DrawingWindow: public QWidget {
16 /*     Q_OBJECT */
17
18 public:
19     typedef void (*ThreadFunction)(DrawingWindow &);
20
21     static const int DEFAULT_WIDTH = 640;
22     static const int DEFAULT_HEIGHT = 480;
23
24     DrawingWindow(ThreadFunction fun,
25                   int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT);
26     DrawingWindow(QWidget *parent,
27                   ThreadFunction fun,
28                   int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT);
29     DrawingWindow(QWidget *parent, Qt::WindowFlags flags,
30                   ThreadFunction fun,
31                   int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT);
32
33     ~DrawingWindow();
34
35     int width() const;
36     int height() const;
37
38     void setColor(float red, float green, float blue);
39     void setColor(const QColor &color);
40
41     void drawPoint(int x, int y);
42     void drawLine(int x1, int y1, int x2, int y2);
43
44 protected:
45     void closeEvent(QCloseEvent *ev);
46     void paintEvent(QPaintEvent *ev);
47     void showEvent(QShowEvent *ev);
48     void timerEvent(QTimerEvent *ev);
49
50 private:
51     class DrawingThread: public QThread {
52     public:
53         DrawingThread(DrawingWindow &w, ThreadFunction f);
54         void run();
55
56         void enableTerminate();
57         void disableTerminate();
58
59     private:
60         DrawingWindow &drawingWindow;
61         ThreadFunction threadFunction;
62         
63     };
64
65     static const int paintInterval = 33;
66
67     QBasicTimer timer;
68
69     QImage *image;
70     QPainter *painter;
71
72     DrawingThread *thread;
73     bool thread_started;
74
75     bool dirtyFlag;
76     QRect dirtyRect;
77
78     QMutex mutex;
79
80     void initialize(ThreadFunction fun, int width, int height);
81
82     void lock();
83     void unlock();
84
85     void setDirtyRect();
86     void setDirtyRect(int x, int y);
87     void setDirtyRect(int x1, int y1, int x2, int y2);
88     void setDirtyRect(const QRect &rect);
89 };
90
91 inline
92 int DrawingWindow::width() const
93 {
94     return image->width();
95 }
96
97 inline
98 int DrawingWindow::height() const
99 {
100     return image->height();
101 }
102
103 inline
104 void DrawingWindow::lock()
105 {
106     thread->disableTerminate();
107     mutex.lock();
108 }
109
110 inline
111 void DrawingWindow::unlock()
112 {
113     mutex.unlock();
114     thread->enableTerminate();
115 }
116
117 inline
118 void DrawingWindow::setDirtyRect()
119 {
120     dirtyFlag = true;
121     dirtyRect.setRect(0, 0, width(), height());
122 }
123
124 inline
125 void DrawingWindow::setDirtyRect(int x, int y)
126 {
127     setDirtyRect(QRect(x, y, 1, 1));
128 }
129
130 inline
131 void DrawingWindow::setDirtyRect(int x1, int y1, int x2, int y2)
132 {
133     QRect r;
134     r.setCoords(x1, y1, x2, y2);
135     setDirtyRect(r.normalized());
136 }
137
138 inline
139 void DrawingWindow::DrawingThread::enableTerminate()
140 {
141     setTerminationEnabled(true);
142 }
143
144 inline
145 void DrawingWindow::DrawingThread::disableTerminate()
146 {
147     setTerminationEnabled(false);
148 }
149
150 #endif // !DRAWING_WINDOW_H