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 class DrawingWindow: public QWidget {
14 /*     Q_OBJECT */
15
16 public:
17     typedef int (*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     QMutex mutex;
64
65     void initialize(ThreadFunction fun, int width, int height);
66
67     void lock();
68     void unlock();
69
70     void setDirtyRect();
71     void setDirtyRect(int x, int y);
72     void setDirtyRect(int x1, int y1, int x2, int y2);
73     void setDirtyRect(const QRect &rect);
74 };
75
76 inline
77 int DrawingWindow::width() const
78 {
79     return image->width();
80 }
81
82 inline
83 int DrawingWindow::height() const
84 {
85     return image->height();
86 }
87
88 inline
89 void DrawingWindow::lock()
90 {
91     mutex.lock();
92 }
93
94 inline
95 void DrawingWindow::unlock()
96 {
97     mutex.unlock();
98 }
99
100 inline
101 void DrawingWindow::setDirtyRect()
102 {
103     dirtyFlag = true;
104     dirtyRect.setRect(0, 0, width(), height());
105 }
106
107 inline
108 void DrawingWindow::setDirtyRect(int x, int y)
109 {
110     setDirtyRect(QRect(x, y, 1, 1));
111 }
112
113 inline
114 void DrawingWindow::setDirtyRect(int x1, int y1, int x2, int y2)
115 {
116     QRect r;
117     r.setCoords(x1, y1, x2, y2);
118     setDirtyRect(r.normalized());
119 }
120
121 #endif // !DRAWING_WINDOW_H