Logo AND Algorithmique Numérique Distribuée

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