Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
17253ba11b79026a9552680c50ef7965937e64a1
[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 <QRect>
9 #include <QWaitCondition>
10 #include <QWidget>
11 #include <Qt>
12
13 class DrawingThread;
14
15 class DrawingWindow: public QWidget {
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     const int width;
34     const int height;
35
36     // http://www.w3.org/TR/SVG/types.html#ColorKeywords
37     void setColor(float red, float green, float blue);
38     void setColor(const char *name);
39     void setBgColor(float red, float green, float blue);
40     void setBgColor(const char *name);
41
42     void clearGraph();
43
44     void drawPoint(int x, int y);
45     void drawLine(int x1, int y1, int x2, int y2);
46     void drawRect(int x1, int y1, int x2, int y2);
47     void fillRect(int x1, int y1, int x2, int y2);
48     void drawCircle(int x, int y, int r);
49     void fillCircle(int x, int y, int r);
50
51     void drawText(int x, int y, const char *text);
52
53     bool sync(unsigned long time = ULONG_MAX);
54
55     void closeGraph();
56
57     void sleep(unsigned long secs);
58     void msleep(unsigned long msecs);
59     void usleep(unsigned long usecs);
60
61 protected:
62     void closeEvent(QCloseEvent *ev);
63     void customEvent(QEvent *ev);
64     void keyPressEvent(QKeyEvent *ev);
65     void paintEvent(QPaintEvent *ev);
66     void showEvent(QShowEvent *ev);
67     void timerEvent(QTimerEvent *ev);
68
69 private:
70     static const int paintInterval = 33;
71
72     QBasicTimer timer;
73     QMutex imageMutex;
74     QMutex syncMutex;
75     QWaitCondition syncCondition;
76     bool terminateThread;
77     int lockCount;
78
79     QImage *image;
80     QPainter *painter;
81
82     QColor fgColor;
83     QColor bgColor;
84
85     bool dirtyFlag;
86     QRect dirtyRect;
87
88     DrawingThread *thread;
89
90     void initialize(ThreadFunction f);
91
92     void applyColor();
93
94     void safeLock(QMutex &mutex);
95     void safeUnlock(QMutex &mutex);
96
97     void dirty();
98     void dirty(int x, int y);
99     void dirty(int x1, int y1, int x2, int y2);
100     void dirty(const QRect &rect);
101
102     void mayUpdate();
103 };
104
105 #endif // !DRAWING_WINDOW_H