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_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 //============================================================
45 //**********************************************************************
46 //**********************************************************************
47 //**********************************************************************
48 //**********************************************************************
49 //**********************************************************************
52 //============================================================
57 static int instanceCount;
58 static int DEFAULT_ARGC;
59 static char *DEFAULT_ARGV[];
62 WindowCore(int &argc = DEFAULT_ARGC, char *argv[] = DEFAULT_ARGV);
66 int WindowCore::instanceCount = 0;
67 int WindowCore::DEFAULT_ARGC = 1;
68 char *WindowCore::DEFAULT_ARGV[] = { "class WindowCore", NULL };
70 WindowCore::WindowCore(int &argc, char *argv[])
73 new QApplication(argc, argv);
75 WindowCore::instanceCount++;
78 WindowCore::~WindowCore()
80 if (WindowCore::instanceCount == 0)
83 WindowCore::instanceCount--;
86 //============================================================
89 class QtDrawingArea: public DrawingAreaInterface,
90 public WindowCore, public QWidget {
92 static int visibleCount;
98 void init(int width, int height, const char *title);
101 void paintEvent(QPaintEvent *)
103 QPainter painter(this);
104 painter.drawImage(0, 0, *image);
107 void closeEvent(QCloseEvent *event)
109 QtDrawingArea::visibleCount--;
113 void keyPressEvent(QKeyEvent *event)
115 if (event->key() == Qt::Key_Escape) {
123 QtDrawingArea(int argc, char *argv[],
124 int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
125 const char *title = NULL);
127 QtDrawingArea(QWidget *parent, Qt::WindowFlags flags,
128 int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
129 const char *title = NULL);
131 QtDrawingArea(int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
132 const char *title = NULL);
138 return image->width();
143 return image->height();
146 void setColor(const QColor &color)
148 QPen pen(painter->pen());
150 painter->setPen(pen);
152 void setColor(float red, float green, float blue)
155 color.setRgbF(red, green, blue);
156 this->setColor(color);
159 void drawPoint(int x, int y)
161 painter->drawPoint(x, y);
165 void drawLine(int x1, int y1, int x2, int y2)
167 painter->drawLine(x1, y1, x2, y2);
173 qApp->sendPostedEvents(this, 0);
174 qApp->processEvents();
180 if (QtDrawingArea::visibleCount > 1)
181 while (this->isVisible())
182 qApp->processEvents(QEventLoop::WaitForMoreEvents);
183 else if (QtDrawingArea::visibleCount > 0)
189 if (QtDrawingArea::visibleCount > 0)
194 int QtDrawingArea::visibleCount = 0;
196 QtDrawingArea::QtDrawingArea(int argc, char *argv[],
197 int width, int height, const char *title)
198 : WindowCore(argc, argv)
201 init(width, height, title);
204 QtDrawingArea::QtDrawingArea(QWidget *parent, Qt::WindowFlags flags,
205 int width, int height, const char *title)
207 , QWidget(parent, flags)
209 init(width, height, title);
212 QtDrawingArea::QtDrawingArea(int width, int height, const char *title)
216 init(width, height, title);
219 QtDrawingArea::~QtDrawingArea()
225 void QtDrawingArea::init(int width, int height, const char *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++;
235 painter = new QPainter(image);
239 //============================================================
241 /* paramètres par défaut */
251 void DrawingThread::run()
253 int x, y; /* le pixel considéré */
254 float cr, ci; /* le complexe correspondant */
255 float zr, zi; /* pour calculer la suite */
257 float pr, pi; /* taille d'un pixel */
258 float rouge, vert, bleu;
261 // QtDrawingArea fen(argc, argv, larg, haut);
263 pr = (Rmax - Rmin) / larg;
264 pi = (Imax - Imin) / larg;
267 for (x = 0; x < larg; x++) {
269 for (y = 0; y < larg; y++) {
273 for (i = 1; i <= maxiter; i++) {
276 if (zr2 + zi2 >= 4) {
277 /* |z| >= 2 : on sort de la boucle */
280 /* on calcule le z suivant */
284 /* on est sorti trop t
\e-bôt du for(...):
\e-A
285 on affiche le pixel d'un couleur en fonction
287 if (i <= maxiter / 2) {
288 /* entre rouge et vert */
289 vert = (2.0 * i) / maxiter;
292 } else if (i <= maxiter) {
293 /* entre vert et bleu */
295 bleu = (2.0 * i) / maxiter - 1.0;
297 } else /* (i > maxiter) */
298 rouge = vert = bleu = 0.0;
299 fen.setColor(rouge, vert, bleu);