Logo AND Algorithmique Numérique Distribuée

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