Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4e961053eee43f7c0e8b5d82825ab7ad6a8e25a6
[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     void setColor(unsigned int color);
39     void setColor(const char *name);
40     void setColor(float red, float green, float blue);
41
42     void setBgColor(unsigned int color);
43     void setBgColor(const char *name);
44     void setBgColor(float red, float green, float blue);
45
46     void clearGraph();
47
48     void drawPoint(int x, int y);
49     void drawLine(int x1, int y1, int x2, int y2);
50     void drawRect(int x1, int y1, int x2, int y2);
51     void fillRect(int x1, int y1, int x2, int y2);
52     void drawCircle(int x, int y, int r);
53     void fillCircle(int x, int y, int r);
54
55     void drawText(int x, int y, const char *text, int flags = 0);
56     void drawTextBg(int x, int y, const char *text, int flags = 0);
57
58     unsigned int getPointColor(int x, int y);
59
60     bool waitMousePress(int &x, int &y, int &button,
61                         unsigned long time = ULONG_MAX);
62     bool sync(unsigned long time = ULONG_MAX);
63
64     void closeGraph();
65
66     static void sleep(unsigned long secs);
67     static void msleep(unsigned long msecs);
68     static void usleep(unsigned long usecs);
69
70 protected:
71     void closeEvent(QCloseEvent *ev);
72     void customEvent(QEvent *ev);
73     void mousePressEvent(QMouseEvent *ev);
74     void keyPressEvent(QKeyEvent *ev);
75     void paintEvent(QPaintEvent *ev);
76     void showEvent(QShowEvent *ev);
77     void timerEvent(QTimerEvent *ev);
78
79 private:
80     //! Intervalle de temps entre deux rendus (ms)
81     static const int paintInterval = 33;
82
83     QBasicTimer timer;
84     QMutex imageMutex;
85     QMutex mouseMutex;
86     QWaitCondition mouseCondition;
87     QMutex syncMutex;
88     QWaitCondition syncCondition;
89     bool terminateThread;
90     int lockCount;
91
92     QImage *image;
93     QPainter *painter;
94
95     QPoint mousePos;
96     Qt::MouseButton mouseButton;
97
98     bool dirtyFlag;
99     QRect dirtyRect;
100
101     DrawingThread *thread;
102
103     void initialize(ThreadFunction fun);
104
105     void setColor(const QColor& color);
106     void setBgColor(const QColor& color);
107     QColor getColor();
108     QColor getBgColor();
109
110     void safeLock(QMutex &mutex);
111     void safeUnlock(QMutex &mutex);
112
113     void dirty();
114     void dirty(int x, int y);
115     void dirty(int x1, int y1, int x2, int y2);
116     void dirty(const QRect &rect);
117
118     void mayUpdate();
119     void realSync();
120     void realDrawText(int x, int y, const char *text, int flags);
121 };
122
123 #endif // !DRAWING_WINDOW_H
124
125 // Local variables:
126 // mode: c++
127 // End: