Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
.
[graphlib.git] / test / hello.cpp
1 /*
2  * Pour compiler
3  * =============
4  *
5  * 1. Créer le fichier hello.pro :
6  *      +------------------------------------------------------------+
7  *      |TARGET = hello                                              |
8  *      |CONFIG += qt debug                                          |
9  *      |SOURCES += hello.cc                                         |
10  *      +------------------------------------------------------------+
11  *
12  * 2. Créer le fichier Makefile avec la commande :
13  *      $ qmake -makefile hello.pro
14  *    ou tout simplement :
15  *      $ qmake -makefile
16  *
17  * 3. Compiler avec la commande :
18  *      $ make hello
19  *    ou tou simplement :
20  *      $ make
21  */
22
23
24 #include <DrawingArea.h>
25 #include <MainDrawingThread.h>
26
27 int main_thread(int, char **)
28 {
29     // >>> insert main drawing code here <<<
30     return 0;
31 }
32
33 //============================================================
34
35 //**********************************************************************
36 //**********************************************************************
37 //**********************************************************************
38 //**********************************************************************
39 //**********************************************************************
40 #if 0
41
42 //============================================================
43 // WindowCore
44
45 class WindowCore {
46 private:
47     static int instanceCount;
48     static int DEFAULT_ARGC;
49     static char *DEFAULT_ARGV[];
50
51 protected:
52     WindowCore(int &argc = DEFAULT_ARGC, char *argv[] = DEFAULT_ARGV);
53     ~WindowCore();
54 };
55
56 int WindowCore::instanceCount = 0;
57 int WindowCore::DEFAULT_ARGC = 1;
58 char *WindowCore::DEFAULT_ARGV[] = { "class WindowCore", NULL };
59
60 WindowCore::WindowCore(int &argc, char *argv[])
61 {
62     if (!qApp)
63         new QApplication(argc, argv);
64     else
65         WindowCore::instanceCount++;
66 }
67
68 WindowCore::~WindowCore()
69 {
70     if (WindowCore::instanceCount == 0)
71         delete qApp;
72     else
73         WindowCore::instanceCount--;
74 }
75
76 //============================================================
77 // QtDrawingArea
78
79 class QtDrawingArea: public DrawingAreaInterface,
80                      public WindowCore, public QWidget {
81 private:
82     static int visibleCount;
83
84     QImage *image;
85     QPainter *painter;
86
87 private:
88     void init(int width, int height, const char *title);
89
90 protected:
91     void paintEvent(QPaintEvent *)
92     {
93         QPainter painter(this);
94         painter.drawImage(0, 0, *image);
95     }
96
97     void closeEvent(QCloseEvent *event)
98     {
99         QtDrawingArea::visibleCount--;
100         event->accept();
101     }
102
103     void keyPressEvent(QKeyEvent *event)
104     {
105         if (event->key() == Qt::Key_Escape) {
106             event->accept();
107             close();
108         } else
109             event->ignore();
110     }
111
112 public:
113     QtDrawingArea(int argc, char *argv[],
114                   int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
115                   const char *title = NULL);
116
117     QtDrawingArea(QWidget *parent, Qt::WindowFlags flags,
118                   int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
119                   const char *title = NULL);
120
121     QtDrawingArea(int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
122                   const char *title = NULL);
123
124     ~QtDrawingArea();
125
126     int width() const
127     {
128         return image->width();
129     }
130
131     int height() const
132     {
133         return image->height();
134     }
135
136     void setColor(const QColor &color)
137     {
138         QPen pen(painter->pen());
139         pen.setColor(color);
140         painter->setPen(pen);
141     }
142     void setColor(float red, float green, float blue)
143     {
144         QColor color;
145         color.setRgbF(red, green, blue);
146         this->setColor(color);
147     }
148
149     void drawPoint(int x, int y)
150     {
151         painter->drawPoint(x, y);
152         this->update();
153     }
154
155     void drawLine(int x1, int y1, int x2, int y2)
156     {
157         painter->drawLine(x1, y1, x2, y2);
158         this->update();
159     }
160
161     void flush()
162     {
163         qApp->sendPostedEvents(this, 0);
164         qApp->processEvents();
165         qApp->flush();
166     }
167
168     void wait()
169     {
170         if (QtDrawingArea::visibleCount > 1)
171             while (this->isVisible())
172                 qApp->processEvents(QEventLoop::WaitForMoreEvents);
173         else if (QtDrawingArea::visibleCount > 0)
174             qApp->exec();
175     }
176
177     void waitAll()
178     {
179         if (QtDrawingArea::visibleCount > 0)
180             qApp->exec();
181     }
182 };
183
184 int QtDrawingArea::visibleCount = 0;
185
186 QtDrawingArea::QtDrawingArea(int argc, char *argv[],
187                              int width, int height, const char *title)
188     : WindowCore(argc, argv)
189     , QWidget()
190 {
191     init(width, height, title);
192 }
193
194 QtDrawingArea::QtDrawingArea(QWidget *parent, Qt::WindowFlags flags,
195                              int width, int height, const char *title)
196     : WindowCore()
197     , QWidget(parent, flags)
198 {
199     init(width, height, title);
200 }
201
202 QtDrawingArea::QtDrawingArea(int width, int height, const char *title)
203     : WindowCore()
204     , QWidget()
205 {
206     init(width, height, title);
207 }
208
209 QtDrawingArea::~QtDrawingArea()
210 {
211     delete painter;
212     delete image;
213 }
214
215 void QtDrawingArea::init(int width, int height, const char *title)
216 {
217     if (title)
218         this->setWindowTitle(title);
219     this->setFocusPolicy(Qt::StrongFocus);
220     image = new QImage(width, height, QImage::Format_RGB32);
221     image->fill(QColor(Qt::white).rgb());
222     this->setFixedSize(image->size());
223     QtDrawingArea::visibleCount++;
224     this->show();
225     painter = new QPainter(image);
226 }
227
228
229 //============================================================
230
231 /* paramètres par défaut */
232 int larg = 600;
233 int haut = 600;
234 float Rmin = -2.05;
235 float Rmax = 0.55;
236 float Imin = -1.3;
237 float Imax = 1.3;
238
239 int maxiter = 100;
240
241 void DrawingThread::run()
242 {
243     int x, y;                   /* le pixel considéré */
244     float cr, ci;               /* le complexe correspondant */
245     float zr, zi;               /* pour calculer la suite */
246     float zr2, zi2;
247     float pr, pi;               /* taille d'un pixel */
248     float rouge, vert, bleu;
249     int i;
250
251     //    QtDrawingArea fen(argc, argv, larg, haut);
252
253     pr = (Rmax - Rmin) / larg;
254     pi = (Imax - Imin) / larg;
255
256     cr = Rmin;
257     for (x = 0; x < larg; x++) {
258         ci = Imin;
259         for (y = 0; y < larg; y++) {
260             /* z_1 = c */
261             zr = cr;
262             zi = ci;
263             for (i = 1; i <= maxiter; i++) {
264                 zr2 = zr * zr;
265                 zi2 = zi * zi;
266                 if (zr2 + zi2 >= 4) {
267                     /* |z| >= 2 : on sort de la boucle */
268                     break;
269                 }
270                 /* on calcule le z suivant */
271                 zi = 2*zr*zi + ci;
272                 zr = zr2 - zi2 + cr;
273             }
274                 /* on est sorti trop t\e-bôt du for(...):\e-A
275                    on affiche le pixel d'un couleur en fonction 
276                    de i */
277                  if (i <= maxiter / 2) {
278                     /* entre rouge et vert */
279                     vert = (2.0 * i) / maxiter;
280                     rouge = 1.0 - vert;
281                     bleu = 0.0;
282                 } else if (i <= maxiter) {
283                     /* entre vert et bleu */
284                     rouge = 0.0;
285                     bleu = (2.0 * i) / maxiter - 1.0;
286                     vert = 1.0 - bleu;
287                 } else /* (i > maxiter) */
288                     rouge = vert = bleu = 0.0;
289                 fen.setColor(rouge, vert, bleu);
290                 fen.drawPoint(x, y);
291
292             ci += pi;
293         }
294         cr += pr;
295         //        fen.flush();
296     }
297     //    fen.wait();
298 }
299
300 #endif