Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
29b81f8f8199fc4b91b82dd59bfee015a32d83b9
[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 <QApplication>
25 #include <QImage>
26 #include <QLabel>
27 #include <QPaintEvent>
28 #include <QPainter>
29 #include <QPixmap>
30 #include <QThread>
31 #include <Qt>
32
33 #include <cmath>
34 #include <iostream>
35 #include <string>
36
37 //============================================================
38 // DrawingAreaInterface
39
40 class DrawingAreaInterface {
41 public:
42     static const int DEFAULT_WIDTH = 640;
43     static const int DEFAULT_HEIGHT = 480;
44
45     virtual ~DrawingAreaInterface() { }
46
47     virtual void setColor(float red, float green, float blue) = 0;
48     virtual void drawPoint(int x, int y) = 0;
49     virtual void drawLine(int x1, int y1, int x2, int y2) = 0;
50     
51     virtual void wait() = 0;
52     virtual void waitAll() = 0;
53 };
54
55 //============================================================
56 // WindowCore
57
58 class WindowCore {
59 private:
60     static int instanceCount;
61     static int DEFAULT_ARGC;
62     static char *DEFAULT_ARGV[];
63
64 protected:
65     WindowCore(int &argc = DEFAULT_ARGC, char *argv[] = DEFAULT_ARGV);
66     ~WindowCore();
67 };
68
69 int WindowCore::instanceCount = 0;
70 int WindowCore::DEFAULT_ARGC = 1;
71 char *WindowCore::DEFAULT_ARGV[] = { "class WindowCore", NULL };
72
73 WindowCore::WindowCore(int &argc, char *argv[])
74 {
75     if (!qApp)
76         new QApplication(argc, argv);
77     else
78         WindowCore::instanceCount++;
79 }
80
81 WindowCore::~WindowCore()
82 {
83     if (WindowCore::instanceCount == 0)
84         delete qApp;
85     else
86         WindowCore::instanceCount--;
87 }
88
89 //============================================================
90 // QtDrawingArea
91
92 class QtDrawingArea: public DrawingAreaInterface,
93                      public WindowCore, public QWidget {
94 private:
95     static int visibleCount;
96
97     QImage *image;
98     QPainter *painter;
99
100 private:
101     void init(int width, int height, const char *title);
102
103 protected:
104     void paintEvent(QPaintEvent *)
105     {
106         QPainter painter(this);
107         painter.drawImage(0, 0, *image);
108     }
109
110     void closeEvent(QCloseEvent *event)
111     {
112         QtDrawingArea::visibleCount--;
113         event->accept();
114     }
115
116     void keyPressEvent(QKeyEvent *event)
117     {
118         if (event->key() == Qt::Key_Escape) {
119             event->accept();
120             close();
121         } else
122             event->ignore();
123     }
124
125 public:
126     QtDrawingArea(int argc, char *argv[],
127                   int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
128                   const char *title = NULL);
129
130     QtDrawingArea(QWidget *parent, Qt::WindowFlags flags,
131                   int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
132                   const char *title = NULL);
133
134     QtDrawingArea(int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
135                   const char *title = NULL);
136
137     ~QtDrawingArea();
138
139     int width() const
140     {
141         return image->width();
142     }
143
144     int height() const
145     {
146         return image->height();
147     }
148
149     void setColor(const QColor &color)
150     {
151         QPen pen(painter->pen());
152         pen.setColor(color);
153         painter->setPen(pen);
154     }
155     void setColor(float red, float green, float blue)
156     {
157         QColor color;
158         color.setRgbF(red, green, blue);
159         this->setColor(color);
160     }
161
162     void drawPoint(int x, int y)
163     {
164         painter->drawPoint(x, y);
165         this->update();
166     }
167
168     void drawLine(int x1, int y1, int x2, int y2)
169     {
170         painter->drawLine(x1, y1, x2, y2);
171         this->update();
172     }
173
174     void flush()
175     {
176         qApp->sendPostedEvents(this, 0);
177         qApp->processEvents();
178         qApp->flush();
179     }
180
181     void wait()
182     {
183         if (QtDrawingArea::visibleCount > 1)
184             while (this->isVisible())
185                 qApp->processEvents(QEventLoop::WaitForMoreEvents);
186         else if (QtDrawingArea::visibleCount > 0)
187             qApp->exec();
188     }
189
190     void waitAll()
191     {
192         if (QtDrawingArea::visibleCount > 0)
193             qApp->exec();
194     }
195 };
196
197 int QtDrawingArea::visibleCount = 0;
198
199 QtDrawingArea::QtDrawingArea(int argc, char *argv[],
200                              int width, int height, const char *title)
201     : WindowCore(argc, argv)
202     , QWidget()
203 {
204     init(width, height, title);
205 }
206
207 QtDrawingArea::QtDrawingArea(QWidget *parent, Qt::WindowFlags flags,
208                              int width, int height, const char *title)
209     : WindowCore()
210     , QWidget(parent, flags)
211 {
212     init(width, height, title);
213 }
214
215 QtDrawingArea::QtDrawingArea(int width, int height, const char *title)
216     : WindowCore()
217     , QWidget()
218 {
219     init(width, height, title);
220 }
221
222 QtDrawingArea::~QtDrawingArea()
223 {
224     delete painter;
225     delete image;
226 }
227
228 void QtDrawingArea::init(int width, int height, const char *title)
229 {
230     if (title)
231         this->setWindowTitle(title);
232     this->setFocusPolicy(Qt::StrongFocus);
233     image = new QImage(width, height, QImage::Format_RGB32);
234     image->fill(QColor(Qt::white).rgb());
235     this->setFixedSize(image->size());
236     QtDrawingArea::visibleCount++;
237     this->show();
238     painter = new QPainter(image);
239 }
240
241
242 //============================================================
243 class DrawingArea: public DrawingAreaInterface {
244 private:
245     QImage *image;
246     QPainter *painter;
247
248 public:
249     DrawingArea(int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT);
250     ~DrawingArea();
251
252     int width() const
253     {
254         return image->width();
255     }
256
257     int height() const
258     {
259         return image->height();
260     }
261
262     void setColor(const QColor &color)
263     {
264         QPen pen(painter->pen());
265         pen.setColor(color);
266         painter->setPen(pen);
267     }
268
269     void setColor(float red, float green, float blue)
270     {
271         QColor color;
272         color.setRgbF(red, green, blue);
273         this->setColor(color);
274     }
275
276     void drawPoint(int x, int y)
277     {
278         painter->drawPoint(x, y);
279     }
280
281     void drawLine(int x1, int y1, int x2, int y2)
282     {
283         painter->drawLine(x1, y1, x2, y2);
284     }
285
286 };
287
288 DrawingArea::DrawingArea(int width, int height)
289 {
290     image = new QImage(width, height, QImage::Format_RGB32);
291     image->fill(QColor(Qt::white).rgb());
292     painter = new QPainter(image);
293 }
294
295 DrawingArea::~DrawingArea()
296 {
297     delete painter;
298     delete image;
299 }
300
301 //============================================================
302 class DrawingThreadCore: public QThread {
303 public:
304     void run();
305     virtual void runForReal() = 0;
306 };
307
308 void DrawingThreadCore::run()
309 {
310     this->runForReal();
311 }
312
313 //============================================================
314 class MainDrawingThread: public DrawingThreadCore {
315 public:
316     void runForReal();
317 };
318
319 void MainDrawingThread::runForReal()
320 {
321     // >>> insert main drawing code here <<<
322 }
323
324 //============================================================
325 int main(int argc, char *argv[])
326 {
327     QApplication application(argc, argv);
328     MainDrawingThread mainDrawingThread;
329
330     mainDrawingThread.start();
331
332     return application.exec();
333 }
334
335 //============================================================
336 #if 0
337
338 /* paramètres par défaut */
339 int larg = 600;
340 int haut = 600;
341 float Rmin = -2.05;
342 float Rmax = 0.55;
343 float Imin = -1.3;
344 float Imax = 1.3;
345
346 int maxiter = 100;
347
348 void DrawingThread::run()
349 {
350     int x, y;                   /* le pixel considéré */
351     float cr, ci;               /* le complexe correspondant */
352     float zr, zi;               /* pour calculer la suite */
353     float zr2, zi2;
354     float pr, pi;               /* taille d'un pixel */
355     float rouge, vert, bleu;
356     int i;
357
358     //    QtDrawingArea fen(argc, argv, larg, haut);
359
360     pr = (Rmax - Rmin) / larg;
361     pi = (Imax - Imin) / larg;
362
363     cr = Rmin;
364     for (x = 0; x < larg; x++) {
365         ci = Imin;
366         for (y = 0; y < larg; y++) {
367             /* z_1 = c */
368             zr = cr;
369             zi = ci;
370             for (i = 1; i <= maxiter; i++) {
371                 zr2 = zr * zr;
372                 zi2 = zi * zi;
373                 if (zr2 + zi2 >= 4) {
374                     /* |z| >= 2 : on sort de la boucle */
375                     break;
376                 }
377                 /* on calcule le z suivant */
378                 zi = 2*zr*zi + ci;
379                 zr = zr2 - zi2 + cr;
380             }
381                 /* on est sorti trop t\e-bôt du for(...):\e-A
382                    on affiche le pixel d'un couleur en fonction 
383                    de i */
384                  if (i <= maxiter / 2) {
385                     /* entre rouge et vert */
386                     vert = (2.0 * i) / maxiter;
387                     rouge = 1.0 - vert;
388                     bleu = 0.0;
389                 } else if (i <= maxiter) {
390                     /* entre vert et bleu */
391                     rouge = 0.0;
392                     bleu = (2.0 * i) / maxiter - 1.0;
393                     vert = 1.0 - bleu;
394                 } else /* (i > maxiter) */
395                     rouge = vert = bleu = 0.0;
396                 fen.setColor(rouge, vert, bleu);
397                 fen.drawPoint(x, y);
398
399             ci += pi;
400         }
401         cr += pr;
402         //        fen.flush();
403     }
404     //    fen.wait();
405 }
406
407 #endif