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