Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e44ea3b5c782e2b9151fec54f93fc776e26ca569
[graphlib.git] / DrawingWindow.h
1 #ifndef DRAWING_WINDOW_H
2 #define DRAWING_WINDOW_H
3
4 #include <QBasicTimer>
5 #include <QColor>
6 #include <QFont>
7 #include <QImage>
8 #include <QMutex>
9 #include <QPainter>
10 #include <QPen>
11 #include <QRect>
12 #include <QWaitCondition>
13 #include <QWidget>
14 #include <Qt>
15 #include <string>
16
17 class DrawingThread;
18
19 class DrawingWindow: public QWidget {
20 public:
21     typedef void (*ThreadFunction)(DrawingWindow &);
22
23     static const int DEFAULT_WIDTH = 640;
24     static const int DEFAULT_HEIGHT = 480;
25
26     DrawingWindow(ThreadFunction fun,
27                   int width_ = DEFAULT_WIDTH, int height_ = DEFAULT_HEIGHT);
28     DrawingWindow(QWidget *parent,
29                   ThreadFunction fun,
30                   int width_ = DEFAULT_WIDTH, int height_ = DEFAULT_HEIGHT);
31     DrawingWindow(QWidget *parent, Qt::WindowFlags flags,
32                   ThreadFunction fun,
33                   int width_ = DEFAULT_WIDTH, int height_ = DEFAULT_HEIGHT);
34
35     ~DrawingWindow();
36
37     const int width;
38     const int height;
39
40     void setColor(unsigned int color);
41     void setColor(const char *name);
42     void setColor(float red, float green, float blue);
43
44     void setBgColor(unsigned int color);
45     void setBgColor(const char *name);
46     void setBgColor(float red, float green, float blue);
47
48     const QFont &getFont() const;
49     void setFont(const QFont &font);
50
51     void clearGraph();
52
53     void drawPoint(int x, int y);
54     void drawLine(int x1, int y1, int x2, int y2);
55     void drawRect(int x1, int y1, int x2, int y2);
56     void fillRect(int x1, int y1, int x2, int y2);
57     void drawCircle(int x, int y, int r);
58     void fillCircle(int x, int y, int r);
59     void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3);
60     void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3);
61
62     void drawText(int x, int y, const char *text, int flags = 0);
63     void drawText(int x, int y, const std::string &text, int flags = 0);
64     void drawTextBg(int x, int y, const char *text, int flags = 0);
65     void drawTextBg(int x, int y, const std::string &text, int flags = 0);
66
67     unsigned int getPointColor(int x, int y);
68
69     bool waitMousePress(int &x, int &y, int &button,
70                         unsigned long time = ULONG_MAX);
71     bool sync(unsigned long time = ULONG_MAX);
72
73     void closeGraph();
74
75     static void sleep(unsigned long secs);
76     static void msleep(unsigned long msecs);
77     static void usleep(unsigned long usecs);
78
79 protected:
80     void closeEvent(QCloseEvent *ev);
81     void customEvent(QEvent *ev);
82     void mousePressEvent(QMouseEvent *ev);
83     void keyPressEvent(QKeyEvent *ev);
84     void paintEvent(QPaintEvent *ev);
85     void showEvent(QShowEvent *ev);
86     void timerEvent(QTimerEvent *ev);
87
88 private:
89     //! Intervalle de temps entre deux rendus (ms)
90     static const int paintInterval = 33;
91
92     QBasicTimer timer;
93     QMutex imageMutex;
94     QMutex mouseMutex;
95     QWaitCondition mouseCondition;
96     QMutex syncMutex;
97     QWaitCondition syncCondition;
98     bool terminateThread;
99     int lockCount;
100
101     QImage *image;
102     QPainter *painter;
103
104     QPoint mousePos;
105     Qt::MouseButton mouseButton;
106
107     bool dirtyFlag;
108     QRect dirtyRect;
109
110     DrawingThread *thread;
111
112     void initialize(ThreadFunction fun);
113
114     void setColor(const QColor &color);
115     void setBgColor(const QColor &color);
116     QColor getColor();
117     QColor getBgColor();
118
119     void safeLock(QMutex &mutex);
120     void safeUnlock(QMutex &mutex);
121
122     void dirty();
123     void dirty(int x, int y);
124     void dirty(int x1, int y1, int x2, int y2);
125     void dirty(const QRect &rect);
126
127     void mayUpdate();
128     void realSync();
129     void realDrawText(int x, int y, const char *text, int flags);
130 };
131
132 #endif // !DRAWING_WINDOW_H
133
134 // Local variables:
135 // mode: c++
136 // End: