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 /*!
18  * Fenêtre de dessin.
19  *  
20  */
21 class DrawingWindow: public QWidget {
22 public:
23     //! Type de la fonction de dessin, passée en paramètre de construction.
24     typedef void (*ThreadFunction)(DrawingWindow &);
25
26     //! Largeur par défaut de la fenêtre.
27     static const int DEFAULT_WIDTH = 640;
28     //! Hauteur par défaut de la fenêtre.
29     static const int DEFAULT_HEIGHT = 480;
30
31     DrawingWindow(ThreadFunction fun,
32                   int width_ = DEFAULT_WIDTH, int height_ = DEFAULT_HEIGHT);
33     DrawingWindow(QWidget *parent,
34                   ThreadFunction fun,
35                   int width_ = DEFAULT_WIDTH, int height_ = DEFAULT_HEIGHT);
36     DrawingWindow(QWidget *parent, Qt::WindowFlags flags,
37                   ThreadFunction fun,
38                   int width_ = DEFAULT_WIDTH, int height_ = DEFAULT_HEIGHT);
39
40     ~DrawingWindow();
41
42     //! Largeur de la fenêtre.
43     const int width;
44     //! Hauteur de la fenêtre.
45     const int height;
46
47     void setColor(unsigned int color);
48     void setColor(const char *name);
49     void setColor(float red, float green, float blue);
50
51     void setBgColor(unsigned int color);
52     void setBgColor(const char *name);
53     void setBgColor(float red, float green, float blue);
54
55     void clearGraph();
56
57     void drawPoint(int x, int y);
58     void drawLine(int x1, int y1, int x2, int y2);
59     void drawRect(int x1, int y1, int x2, int y2);
60     void fillRect(int x1, int y1, int x2, int y2);
61     void drawCircle(int x, int y, int r);
62     void fillCircle(int x, int y, int r);
63
64     void drawText(int x, int y, const char *text, int flags = 0);
65     void drawTextBg(int x, int y, const char *text, int flags = 0);
66
67     unsigned int getPointColor(int x, int y);
68
69     bool sync(unsigned long time = ULONG_MAX);
70
71     void closeGraph();
72
73     void sleep(unsigned long secs);
74     void msleep(unsigned long msecs);
75     void usleep(unsigned long usecs);
76
77 protected:
78     void closeEvent(QCloseEvent *ev);
79     void customEvent(QEvent *ev);
80     void keyPressEvent(QKeyEvent *ev);
81     void paintEvent(QPaintEvent *ev);
82     void showEvent(QShowEvent *ev);
83     void timerEvent(QTimerEvent *ev);
84
85 private:
86     //! Intervalle de temps entre deux rendus (ms)
87     static const int paintInterval = 33;
88
89     QBasicTimer timer;
90     QMutex imageMutex;
91     QMutex syncMutex;
92     QWaitCondition syncCondition;
93     bool terminateThread;
94     int lockCount;
95
96     QImage *image;
97     QPainter *painter;
98
99     bool dirtyFlag;
100     QRect dirtyRect;
101
102     DrawingThread *thread;
103
104     void initialize(ThreadFunction fun);
105
106     void setColor(const QColor& color);
107     void setBgColor(const QColor& color);
108     QColor getColor();
109     QColor getBgColor();
110
111     void safeLock(QMutex &mutex);
112     void safeUnlock(QMutex &mutex);
113
114     void dirty();
115     void dirty(int x, int y);
116     void dirty(int x1, int y1, int x2, int y2);
117     void dirty(const QRect &rect);
118
119     void mayUpdate();
120     void realSync();
121     void realDrawText(int x, int y, const char *text, int flags);
122 };
123
124 #endif // !DRAWING_WINDOW_H
125
126 // Local variables:
127 // mode: c++
128 // End: