Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d2ee6de412349978f54a4b0cb7b1f2d175cf0b96
[graphlib.git] / DrawingWindow.h
1 #ifndef DRAWING_WINDOW_H
2 #define DRAWING_WINDOW_H
3
4 #define USE_PIXMAP_CACHE
5
6 #include <QBasicTimer>
7 #include <QColor>
8 #include <QImage>
9 #include <QMutex>
10 #include <QPainter>
11 #ifdef USE_PIXMAP_CACHE
12 #  include <QPixmap>
13 #endif
14 #include <QRect>
15 #include <QWidget>
16 #include <Qt>
17
18 class DrawingWindow: public QWidget {
19 /*     Q_OBJECT */
20
21 public:
22     typedef int (*ThreadFunction)(DrawingWindow &);
23
24     static const int DEFAULT_WIDTH = 640;
25     static const int DEFAULT_HEIGHT = 480;
26
27     DrawingWindow(ThreadFunction fun,
28                   int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT);
29     DrawingWindow(QWidget *parent,
30                   ThreadFunction fun,
31                   int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT);
32     DrawingWindow(QWidget *parent, Qt::WindowFlags flags,
33                   ThreadFunction fun,
34                   int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT);
35
36     ~DrawingWindow();
37
38     int width() const;
39     int height() const;
40
41     void setColor(float red, float green, float blue);
42     void setColor(const QColor &color);
43
44     void drawPoint(int x, int y);
45     void drawLine(int x1, int y1, int x2, int y2);
46
47 protected:
48     void paintEvent(QPaintEvent *ev);
49     void showEvent(QShowEvent *ev);
50     void timerEvent(QTimerEvent *ev);
51
52 private:
53     class DrawingThread;
54
55     static const int paintInterval = 33;
56
57     QBasicTimer timer;
58
59     QImage *image;
60     QPainter *painter;
61 #ifdef USE_PIXMAP_CACHE
62     QPixmap *pixmap;
63 #endif
64
65     DrawingThread *thread;
66     bool thread_started;
67
68     bool dirtyFlag;
69     QRect dirtyRect;
70
71     QMutex mutex;
72
73     void initialize(ThreadFunction fun, int width, int height);
74
75     void lock();
76     void unlock();
77
78     void setDirtyRect();
79     void setDirtyRect(int x, int y);
80     void setDirtyRect(int x1, int y1, int x2, int y2);
81     void setDirtyRect(const QRect &rect);
82 };
83
84 inline
85 int DrawingWindow::width() const
86 {
87     return image->width();
88 }
89
90 inline
91 int DrawingWindow::height() const
92 {
93     return image->height();
94 }
95
96 inline
97 void DrawingWindow::lock()
98 {
99     mutex.lock();
100 }
101
102 inline
103 void DrawingWindow::unlock()
104 {
105     mutex.unlock();
106 }
107
108 #endif // !DRAWING_WINDOW_H