#include <QApplication>
-#include <QImage>
-#include <QLabel>
-#include <QPaintEvent>
-#include <QPainter>
-#include <QPixmap>
-#include <QThread>
-#include <Qt>
+#include <DrawingWindow.h>
-#include <cmath>
#include <iostream>
-#include <string>
-//============================================================
-// DrawingAreaInterface
-
-class DrawingAreaInterface {
-public:
- static const int DEFAULT_WIDTH = 640;
- static const int DEFAULT_HEIGHT = 480;
-
- virtual ~DrawingAreaInterface() { }
-
- virtual void setColor(float red, float green, float blue) = 0;
- virtual void drawPoint(int x, int y) = 0;
- virtual void drawLine(int x1, int y1, int x2, int y2) = 0;
-
- virtual void wait() = 0;
- virtual void waitAll() = 0;
-};
-
-//============================================================
-// WindowCore
-
-class WindowCore {
-private:
- static int instanceCount;
- static int DEFAULT_ARGC;
- static char *DEFAULT_ARGV[];
-
-protected:
- WindowCore(int &argc = DEFAULT_ARGC, char *argv[] = DEFAULT_ARGV);
- ~WindowCore();
-};
-
-int WindowCore::instanceCount = 0;
-int WindowCore::DEFAULT_ARGC = 1;
-char *WindowCore::DEFAULT_ARGV[] = { "class WindowCore", NULL };
-
-WindowCore::WindowCore(int &argc, char *argv[])
+void flip(DrawingWindow &w)
{
- if (!qApp)
- new QApplication(argc, argv);
- else
- WindowCore::instanceCount++;
-}
-
-WindowCore::~WindowCore()
-{
- if (WindowCore::instanceCount == 0)
- delete qApp;
- else
- WindowCore::instanceCount--;
-}
-
-//============================================================
-// QtDrawingArea
-
-class QtDrawingArea: public DrawingAreaInterface,
- public WindowCore, public QWidget {
-private:
- static int visibleCount;
-
- QImage *image;
- QPainter *painter;
-
-private:
- void init(int width, int height, const char *title);
-
-protected:
- void paintEvent(QPaintEvent *)
- {
- QPainter painter(this);
- painter.drawImage(0, 0, *image);
- }
-
- void closeEvent(QCloseEvent *event)
- {
- QtDrawingArea::visibleCount--;
- event->accept();
- }
-
- void keyPressEvent(QKeyEvent *event)
- {
- if (event->key() == Qt::Key_Escape) {
- event->accept();
- close();
- } else
- event->ignore();
- }
-
-public:
- QtDrawingArea(int argc, char *argv[],
- int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
- const char *title = NULL);
-
- QtDrawingArea(QWidget *parent, Qt::WindowFlags flags,
- int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
- const char *title = NULL);
-
- QtDrawingArea(int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT,
- const char *title = NULL);
-
- ~QtDrawingArea();
-
- int width() const
- {
- return image->width();
- }
-
- int height() const
- {
- return image->height();
- }
-
- void setColor(const QColor &color)
- {
- QPen pen(painter->pen());
- pen.setColor(color);
- painter->setPen(pen);
- }
- void setColor(float red, float green, float blue)
- {
- QColor color;
- color.setRgbF(red, green, blue);
- this->setColor(color);
- }
-
- void drawPoint(int x, int y)
- {
- painter->drawPoint(x, y);
- this->update();
- }
-
- void drawLine(int x1, int y1, int x2, int y2)
- {
- painter->drawLine(x1, y1, x2, y2);
- this->update();
- }
-
- void flush()
- {
- qApp->sendPostedEvents(this, 0);
- qApp->processEvents();
- qApp->flush();
- }
-
- void wait()
- {
- if (QtDrawingArea::visibleCount > 1)
- while (this->isVisible())
- qApp->processEvents(QEventLoop::WaitForMoreEvents);
- else if (QtDrawingArea::visibleCount > 0)
- qApp->exec();
- }
-
- void waitAll()
- {
- if (QtDrawingArea::visibleCount > 0)
- qApp->exec();
- }
-};
-
-int QtDrawingArea::visibleCount = 0;
-
-QtDrawingArea::QtDrawingArea(int argc, char *argv[],
- int width, int height, const char *title)
- : WindowCore(argc, argv)
- , QWidget()
-{
- init(width, height, title);
-}
-
-QtDrawingArea::QtDrawingArea(QWidget *parent, Qt::WindowFlags flags,
- int width, int height, const char *title)
- : WindowCore()
- , QWidget(parent, flags)
-{
- init(width, height, title);
-}
-
-QtDrawingArea::QtDrawingArea(int width, int height, const char *title)
- : WindowCore()
- , QWidget()
-{
- init(width, height, title);
-}
-
-QtDrawingArea::~QtDrawingArea()
-{
- delete painter;
- delete image;
-}
-
-void QtDrawingArea::init(int width, int height, const char *title)
-{
- if (title)
- this->setWindowTitle(title);
- this->setFocusPolicy(Qt::StrongFocus);
- image = new QImage(width, height, QImage::Format_RGB32);
- image->fill(QColor(Qt::white).rgb());
- this->setFixedSize(image->size());
- QtDrawingArea::visibleCount++;
- this->show();
- painter = new QPainter(image);
-}
-
-
-//============================================================
-class DrawingArea: public DrawingAreaInterface {
-private:
- QImage *image;
- QPainter *painter;
-
-public:
- DrawingArea(int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT);
- ~DrawingArea();
-
- int width() const
- {
- return image->width();
- }
-
- int height() const
- {
- return image->height();
- }
-
- void setColor(const QColor &color)
- {
- QPen pen(painter->pen());
- pen.setColor(color);
- painter->setPen(pen);
- }
-
- void setColor(float red, float green, float blue)
- {
- QColor color;
- color.setRgbF(red, green, blue);
- this->setColor(color);
- }
-
- void drawPoint(int x, int y)
- {
- painter->drawPoint(x, y);
- }
-
- void drawLine(int x1, int y1, int x2, int y2)
- {
- painter->drawLine(x1, y1, x2, y2);
+ std::cout << "[ " << w.width << " x " << w.height << " ]\n";
+
+ int c = 0;
+ int y = 0;
+ int count = 50;//1 << 31;
+ while (1) {
+ w.setColor(c, c, c);
+ for (int yy = y; yy < y + 10; yy++) {
+ for (int x = 0; x < w.width; x++)
+ w.drawPoint(x, yy);
+ }
+ if ((y += 10) >= w.height) {
+ w.sync();
+ y = 0;
+ c = !c;
+ if (!--count) break;
+ }
}
-
-};
-
-DrawingArea::DrawingArea(int width, int height)
-{
- image = new QImage(width, height, QImage::Format_RGB32);
- image->fill(QColor(Qt::white).rgb());
- painter = new QPainter(image);
}
-DrawingArea::~DrawingArea()
+void mandel(DrawingWindow &w)
{
- delete painter;
- delete image;
-}
-
-//============================================================
-class DrawingThreadCore: public QThread {
-public:
- void run();
- virtual void runForReal() = 0;
-};
+ /* paramètres par défaut */
+ int larg = w.width;
+ int haut = w.height;
+ float Rmin = -2.05;
+ float Rmax = 0.55;
+ float Imin = -1.3;
+ float Imax = 1.3;
-void DrawingThreadCore::run()
-{
- this->runForReal();
-}
+ int maxiter = 100;
-//============================================================
-class MainDrawinThread: public DrawingThreadCore {
-public:
- void runForReal();
-};
-
-void MainDrawingThread::runForReal()
-{
- // >>> insert main drawing code here <<<
-}
-
-//============================================================
-int main(int argc, char *argv[])
-{
- QApplication application(argc, argv);
- MainDrawingThread mainDrawingThread;
-
- mainDrawingThread.start();
-
- return application.exec();
-}
-
-//============================================================
-#if 0
-
-/* paramètres par défaut */
-int larg = 600;
-int haut = 600;
-float Rmin = -2.05;
-float Rmax = 0.55;
-float Imin = -1.3;
-float Imax = 1.3;
-
-int maxiter = 100;
-
-void DrawingThread::run()
-{
int x, y; /* le pixel considéré */
float cr, ci; /* le complexe correspondant */
float zr, zi; /* pour calculer la suite */
float rouge, vert, bleu;
int i;
- // QtDrawingArea fen(argc, argv, larg, haut);
-
pr = (Rmax - Rmin) / larg;
- pi = (Imax - Imin) / larg;
+ pi = (Imax - Imin) / haut;
cr = Rmin;
for (x = 0; x < larg; x++) {
ci = Imin;
- for (y = 0; y < larg; y++) {
+ for (y = 0; y < haut; y++) {
/* z_1 = c */
zr = cr;
zi = ci;
zi = 2*zr*zi + ci;
zr = zr2 - zi2 + cr;
}
- /* on est sorti trop t\e-bôt du for(...):\e-A
+ /* on est sorti trop tôt du for(...):
on affiche le pixel d'un couleur en fonction
de i */
if (i <= maxiter / 2) {
vert = 1.0 - bleu;
} else /* (i > maxiter) */
rouge = vert = bleu = 0.0;
- fen.setColor(rouge, vert, bleu);
- fen.drawPoint(x, y);
+ w.setColor(rouge, vert, bleu);
+ w.drawPoint(x, y);
ci += pi;
}
cr += pr;
- // fen.flush();
+// if (x % 10 == 0)
+// w.sync();
}
- // fen.wait();
}
-#endif
+void lines(DrawingWindow &w)
+{
+ int n = 100000;
+ int xmax = w.width;
+ int ymax = w.height;
+ while (n-- > 0) {
+ double r = rand() / (float )RAND_MAX;
+ double g = rand() / (float )RAND_MAX;
+ double b = rand() / (float )RAND_MAX;
+ int x1 = rand() % xmax;
+ int y1 = rand() % ymax;
+ int x2 = rand() % xmax;
+ int y2 = rand() % ymax;
+ w.setColor(r, g, b);
+ w.drawLine(x1, y1, x2, y2);
+ //if (n % 100 == 0)
+ w.sync();
+ }
+}
+
+void rectangles(DrawingWindow &w)
+{
+ int d = 5;
+ int z = (w.width > w.height ? w.height : w.width) / 2;
+ z = d * (z / d);
+ while (z > 0) {
+ w.drawRect(z, z, w.width - 1 - z, w.height - 1 - z);
+ z -= d;
+ }
+}
+
+int main(int argc, char *argv[])
+{
+ const int w = 700;
+ const int h = 700;
+ QApplication application(argc, argv);
+
+ const int nf = 1;
+ const int nm = 1;
+ DrawingWindow *dw[nf + nm];
+
+ for (int i = 0; i < nf; ++i)
+ dw[i] = new DrawingWindow(flip, w, h);
+ for (int i = nf; i < nf + nm; ++i)
+ dw[i] = new DrawingWindow(mandel, w, h);
+
+ for (int i = 0; i < nf + nm; ++i)
+ dw[i]->show();
+
+ DrawingWindow dr(rectangles, w, h);
+ dr.show();
+
+ DrawingWindow dl(lines, w, h);
+ dl.show();
+
+ return application.exec();
+}