5 * 1. Créer le fichier hello.pro :
6 * +------------------------------------------------------------+
8 * |CONFIG += qt debug |
9 * |SOURCES += hello.cc |
10 * +------------------------------------------------------------+
12 * 2. Créer le fichier Makefile avec la commande :
13 * $ qmake -makefile hello.pro
14 * ou tout simplement :
17 * 3. Compiler avec la commande :
24 #include <DrawingArea.h>
25 #include <MainDrawingThread.h>
29 int main_drawing_thread(int argc, char **argv)
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";
37 std::cout << '.' << std::flush;
43 MAIN_DRAWING_THREAD(main_drawing_thread);
45 //============================================================
47 //**********************************************************************
48 //**********************************************************************
49 //**********************************************************************
50 //**********************************************************************
51 //**********************************************************************
54 //============================================================
59 static int instanceCount;
60 static int DEFAULT_ARGC;
61 static char *DEFAULT_ARGV[];
64 WindowCore(int &argc = DEFAULT_ARGC, char *argv[] = DEFAULT_ARGV);
68 int WindowCore::instanceCount = 0;
69 int WindowCore::DEFAULT_ARGC = 1;
70 char *WindowCore::DEFAULT_ARGV[] = { "class WindowCore", NULL };
72 WindowCore::WindowCore(int &argc, char *argv[])
75 new QApplication(argc, argv);
77 WindowCore::instanceCount++;
80 WindowCore::~WindowCore()
82 if (WindowCore::instanceCount == 0)
85 WindowCore::instanceCount--;
88 //============================================================
91 class QtDrawingArea: public DrawingAreaInterface,
92 public WindowCore, public QWidget {
94 static int visibleCount;
100 void init(int width, int height, const char *title);
103 void paintEvent(QPaintEvent *)
105 QPainter painter(this);
106 painter.drawImage(0, 0, *image);
109 void closeEvent(QCloseEvent *event)
111 QtDrawingArea::visibleCount--;
115 void keyPressEvent(QKeyEvent *event)
117 if (event->key() == Qt::Key_Escape) {
125 QtDrawingArea(int argc, char *argv[],
126 int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
127 const char *title = NULL);
129 QtDrawingArea(QWidget *parent, Qt::WindowFlags flags,
130 int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
131 const char *title = NULL);
133 QtDrawingArea(int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
134 const char *title = NULL);
140 return image->width();
145 return image->height();
148 void setColor(const QColor &color)
150 QPen pen(painter->pen());
152 painter->setPen(pen);
154 void setColor(float red, float green, float blue)
157 color.setRgbF(red, green, blue);
158 this->setColor(color);
161 void drawPoint(int x, int y)
163 painter->drawPoint(x, y);
167 void drawLine(int x1, int y1, int x2, int y2)
169 painter->drawLine(x1, y1, x2, y2);
175 qApp->sendPostedEvents(this, 0);
176 qApp->processEvents();
182 if (QtDrawingArea::visibleCount > 1)
183 while (this->isVisible())
184 qApp->processEvents(QEventLoop::WaitForMoreEvents);
185 else if (QtDrawingArea::visibleCount > 0)
191 if (QtDrawingArea::visibleCount > 0)
196 int QtDrawingArea::visibleCount = 0;
198 QtDrawingArea::QtDrawingArea(int argc, char *argv[],
199 int width, int height, const char *title)
200 : WindowCore(argc, argv)
203 init(width, height, title);
206 QtDrawingArea::QtDrawingArea(QWidget *parent, Qt::WindowFlags flags,
207 int width, int height, const char *title)
209 , QWidget(parent, flags)
211 init(width, height, title);
214 QtDrawingArea::QtDrawingArea(int width, int height, const char *title)
218 init(width, height, title);
221 QtDrawingArea::~QtDrawingArea()
227 void QtDrawingArea::init(int width, int height, const char *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++;
237 painter = new QPainter(image);
241 //============================================================
243 /* paramètres par défaut */
253 void DrawingThread::run()
255 int x, y; /* le pixel considéré */
256 float cr, ci; /* le complexe correspondant */
257 float zr, zi; /* pour calculer la suite */
259 float pr, pi; /* taille d'un pixel */
260 float rouge, vert, bleu;
263 // QtDrawingArea fen(argc, argv, larg, haut);
265 pr = (Rmax - Rmin) / larg;
266 pi = (Imax - Imin) / larg;
269 for (x = 0; x < larg; x++) {
271 for (y = 0; y < larg; y++) {
275 for (i = 1; i <= maxiter; i++) {
278 if (zr2 + zi2 >= 4) {
279 /* |z| >= 2 : on sort de la boucle */
282 /* on calcule le z suivant */
286 /* on est sorti trop t
\e-bôt du for(...):
\e-A
287 on affiche le pixel d'un couleur en fonction
289 if (i <= maxiter / 2) {
290 /* entre rouge et vert */
291 vert = (2.0 * i) / maxiter;
294 } else if (i <= maxiter) {
295 /* entre vert et bleu */
297 bleu = (2.0 * i) / maxiter - 1.0;
299 } else /* (i > maxiter) */
300 rouge = vert = bleu = 0.0;
301 fen.setColor(rouge, vert, bleu);