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>
27 int main_thread(int, char **)
29 // >>> insert main drawing code here <<<
33 //============================================================
35 //**********************************************************************
36 //**********************************************************************
37 //**********************************************************************
38 //**********************************************************************
39 //**********************************************************************
42 //============================================================
47 static int instanceCount;
48 static int DEFAULT_ARGC;
49 static char *DEFAULT_ARGV[];
52 WindowCore(int &argc = DEFAULT_ARGC, char *argv[] = DEFAULT_ARGV);
56 int WindowCore::instanceCount = 0;
57 int WindowCore::DEFAULT_ARGC = 1;
58 char *WindowCore::DEFAULT_ARGV[] = { "class WindowCore", NULL };
60 WindowCore::WindowCore(int &argc, char *argv[])
63 new QApplication(argc, argv);
65 WindowCore::instanceCount++;
68 WindowCore::~WindowCore()
70 if (WindowCore::instanceCount == 0)
73 WindowCore::instanceCount--;
76 //============================================================
79 class QtDrawingArea: public DrawingAreaInterface,
80 public WindowCore, public QWidget {
82 static int visibleCount;
88 void init(int width, int height, const char *title);
91 void paintEvent(QPaintEvent *)
93 QPainter painter(this);
94 painter.drawImage(0, 0, *image);
97 void closeEvent(QCloseEvent *event)
99 QtDrawingArea::visibleCount--;
103 void keyPressEvent(QKeyEvent *event)
105 if (event->key() == Qt::Key_Escape) {
113 QtDrawingArea(int argc, char *argv[],
114 int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
115 const char *title = NULL);
117 QtDrawingArea(QWidget *parent, Qt::WindowFlags flags,
118 int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
119 const char *title = NULL);
121 QtDrawingArea(int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
122 const char *title = NULL);
128 return image->width();
133 return image->height();
136 void setColor(const QColor &color)
138 QPen pen(painter->pen());
140 painter->setPen(pen);
142 void setColor(float red, float green, float blue)
145 color.setRgbF(red, green, blue);
146 this->setColor(color);
149 void drawPoint(int x, int y)
151 painter->drawPoint(x, y);
155 void drawLine(int x1, int y1, int x2, int y2)
157 painter->drawLine(x1, y1, x2, y2);
163 qApp->sendPostedEvents(this, 0);
164 qApp->processEvents();
170 if (QtDrawingArea::visibleCount > 1)
171 while (this->isVisible())
172 qApp->processEvents(QEventLoop::WaitForMoreEvents);
173 else if (QtDrawingArea::visibleCount > 0)
179 if (QtDrawingArea::visibleCount > 0)
184 int QtDrawingArea::visibleCount = 0;
186 QtDrawingArea::QtDrawingArea(int argc, char *argv[],
187 int width, int height, const char *title)
188 : WindowCore(argc, argv)
191 init(width, height, title);
194 QtDrawingArea::QtDrawingArea(QWidget *parent, Qt::WindowFlags flags,
195 int width, int height, const char *title)
197 , QWidget(parent, flags)
199 init(width, height, title);
202 QtDrawingArea::QtDrawingArea(int width, int height, const char *title)
206 init(width, height, title);
209 QtDrawingArea::~QtDrawingArea()
215 void QtDrawingArea::init(int width, int height, const char *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++;
225 painter = new QPainter(image);
229 //============================================================
231 /* paramètres par défaut */
241 void DrawingThread::run()
243 int x, y; /* le pixel considéré */
244 float cr, ci; /* le complexe correspondant */
245 float zr, zi; /* pour calculer la suite */
247 float pr, pi; /* taille d'un pixel */
248 float rouge, vert, bleu;
251 // QtDrawingArea fen(argc, argv, larg, haut);
253 pr = (Rmax - Rmin) / larg;
254 pi = (Imax - Imin) / larg;
257 for (x = 0; x < larg; x++) {
259 for (y = 0; y < larg; y++) {
263 for (i = 1; i <= maxiter; i++) {
266 if (zr2 + zi2 >= 4) {
267 /* |z| >= 2 : on sort de la boucle */
270 /* on calcule le z suivant */
274 /* on est sorti trop t
\e-bôt du for(...):
\e-A
275 on affiche le pixel d'un couleur en fonction
277 if (i <= maxiter / 2) {
278 /* entre rouge et vert */
279 vert = (2.0 * i) / maxiter;
282 } else if (i <= maxiter) {
283 /* entre vert et bleu */
285 bleu = (2.0 * i) / maxiter - 1.0;
287 } else /* (i > maxiter) */
288 rouge = vert = bleu = 0.0;
289 fen.setColor(rouge, vert, bleu);