Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
28dc14bd6346db562069febe3fe7b474bcd0f44f
[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 sleep(unsigned long secs);
56     void msleep(unsigned long msecs);
57     void usleep(unsigned long usecs);
58
59 protected:
60     void closeEvent(QCloseEvent *ev);
61     void customEvent(QEvent *ev);
62     void keyPressEvent(QKeyEvent *ev);
63     void paintEvent(QPaintEvent *ev);
64     void showEvent(QShowEvent *ev);
65     void timerEvent(QTimerEvent *ev);
66
67 private:
68     static const int paintInterval = 33;
69
70     QBasicTimer timer;
71     QMutex imageMutex;
72     QMutex syncMutex;
73     QWaitCondition syncCondition;
74     bool terminateThread;
75     int lockCount;
76
77     QImage *image;
78     QPainter *painter;
79
80     QColor fgColor;
81     QColor bgColor;
82
83     bool dirtyFlag;
84     QRect dirtyRect;
85
86     DrawingThread *thread;
87
88     void initialize(ThreadFunction f);
89
90     void applyColor();
91
92     void safeLock(QMutex &mutex);
93     void safeUnlock(QMutex &mutex);
94
95     void dirty();
96     void dirty(int x, int y);
97     void dirty(int x1, int y1, int x2, int y2);
98     void dirty(const QRect &rect);
99
100     void mayUpdate();
101 };
102
103 #endif // !DRAWING_WINDOW_H