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