From: giersch Date: Thu, 18 Oct 2007 15:23:01 +0000 (+0000) Subject: . X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/graphlib.git/commitdiff_plain/5c05d2a8c6ae75e14df6ec9a7503a7ec6ee01cbf . --- diff --git a/DrawingArea.cpp b/DrawingArea.cpp index f0b2316..cd957da 100644 --- a/DrawingArea.cpp +++ b/DrawingArea.cpp @@ -40,10 +40,17 @@ void DrawingArea::setColor(float red, float green, float blue) void DrawingArea::drawPoint(int x, int y) { painter->drawPoint(x, y); + emit update(); } void DrawingArea::drawLine(int x1, int y1, int x2, int y2) { painter->drawLine(x1, y1, x2, y2); + emit update(); +} + +const QImage &DrawingArea::getImage() const +{ + return *image; } diff --git a/DrawingArea.h b/DrawingArea.h index 76d03ef..874de0f 100644 --- a/DrawingArea.h +++ b/DrawingArea.h @@ -1,18 +1,18 @@ #ifndef DRAWING_AREA_H #define DRAWING_AREA_H -#include - #include #include +#include #include -class DrawingArea: public DrawingAreaInterface { -private: - QImage *image; - QPainter *painter; +class DrawingArea: public QObject { + Q_OBJECT public: + static const int DEFAULT_WIDTH = 640; + static const int DEFAULT_HEIGHT = 480; + DrawingArea(int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT); ~DrawingArea(); @@ -25,6 +25,15 @@ public: void drawPoint(int x, int y); void drawLine(int x1, int y1, int x2, int y2); + const QImage &getImage() const; + +signals: + void update(); + +private: + QImage *image; + QPainter *painter; + }; #endif // !DRAWING_AREA_H diff --git a/DrawingAreaInterface.h b/DrawingAreaInterface.h deleted file mode 100644 index 98190e7..0000000 --- a/DrawingAreaInterface.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef DRAWING_AREA_INTERFACE_H -#define DRAWING_AREA_INTERFACE_H - -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; -}; - -#endif // !DRAWING_AREA_INTERFACE_H diff --git a/DrawingThread.cpp b/DrawingThread.cpp new file mode 100644 index 0000000..3164375 --- /dev/null +++ b/DrawingThread.cpp @@ -0,0 +1,13 @@ +#include "DrawingThread.h" +#include + +DrawingThread::DrawingThread(DrawingArea &a, ThreadFunction f) + : drawingArea(a) + , threadFunction(f) +{ +} + +void DrawingThread::run() +{ + exit(threadFunction(drawingArea)); +} diff --git a/DrawingThread.h b/DrawingThread.h new file mode 100644 index 0000000..43cedbb --- /dev/null +++ b/DrawingThread.h @@ -0,0 +1,21 @@ +#ifndef DRAWING_THREAD_H +#define DRAWING_THREAD_H + +#include "DrawingArea.h" +#include + +class DrawingThread: public QThread { +public: + typedef int (*ThreadFunction)(DrawingArea &); + + DrawingThread(DrawingArea &a, ThreadFunction f); + + void run(); + +private: + DrawingArea &drawingArea; + ThreadFunction threadFunction; + +}; + +#endif // !DRAWING_THREAD_H diff --git a/DrawingThreadCore.cpp b/DrawingThreadCore.cpp deleted file mode 100644 index a55885f..0000000 --- a/DrawingThreadCore.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include - -void DrawingThreadCore::run() -{ - exit(this->runForReal()); -} - diff --git a/DrawingThreadCore.h b/DrawingThreadCore.h deleted file mode 100644 index e295db4..0000000 --- a/DrawingThreadCore.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef DRAWING_THREAD_CORE_H -#define DRAWING_THREAD_CORE_H - -#include - -class DrawingThreadCore: public QThread { -private: - virtual int runForReal() = 0; - -public: - void run(); - -}; - -#endif // !DRAWING_THREAD_CORE_H diff --git a/DrawingWindow.cpp b/DrawingWindow.cpp new file mode 100644 index 0000000..04b5be4 --- /dev/null +++ b/DrawingWindow.cpp @@ -0,0 +1,40 @@ +#include "DrawingWindow.h" +#include + +#include + +DrawingWindow::DrawingWindow(const DrawingArea &a) + : QWidget() + , drawingArea(a) +{ + initialize(); +} + +DrawingWindow::DrawingWindow(QWidget *parent, const DrawingArea &a) + : QWidget(parent) + , drawingArea(a) +{ + initialize(); +} + +DrawingWindow::DrawingWindow(QWidget *parent, Qt::WindowFlags flags, + const DrawingArea &a) + : QWidget(parent, flags) + , drawingArea(a) +{ + initialize(); +} + +void DrawingWindow::paintEvent(QPaintEvent *) +{ + std::cerr << "paint!\n"; + QPainter painter(this); + painter.drawImage(0, 0, drawingArea.getImage()); +} + +void DrawingWindow::initialize() +{ + setFocusPolicy(Qt::StrongFocus); + setFixedSize(drawingArea.getImage().size()); + setAttribute(Qt::WA_OpaquePaintEvent); +} diff --git a/DrawingWindow.h b/DrawingWindow.h new file mode 100644 index 0000000..c89c845 --- /dev/null +++ b/DrawingWindow.h @@ -0,0 +1,23 @@ +#ifndef DRAWING_WINDOW_H +#define DRAWING_WINDOW_H + +#include "DrawingArea.h" +#include + +class DrawingWindow: public QWidget { +public: + DrawingWindow(const DrawingArea &a); + DrawingWindow(QWidget *parent, const DrawingArea &a); + DrawingWindow(QWidget *parent, Qt::WindowFlags flags, + const DrawingArea &a); + +protected: + void paintEvent(QPaintEvent *e); + +private: + const DrawingArea &drawingArea; + + void initialize(); +}; + +#endif // !DRAWING_WINDOW_H diff --git a/MainDrawingThread.cpp b/MainDrawingThread.cpp deleted file mode 100644 index a786f9d..0000000 --- a/MainDrawingThread.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include - -#include - -MainDrawingThread::MainDrawingThread(int argc_, char **argv_) - : DrawingThreadCore() - , argc(argc_) - , argv(argv_) -{ -} - -int MainDrawingThread::runForReal() -{ - return MainDrawingThread::main(argc, argv); -} - -int main(int argc, char *argv[]) -{ - QApplication application(argc, argv); - MainDrawingThread mainDrawingThread(argc, argv); - - mainDrawingThread.start(); - - return application.exec(); -} diff --git a/MainDrawingThread.h b/MainDrawingThread.h deleted file mode 100644 index 846a0c8..0000000 --- a/MainDrawingThread.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef MAIN_DRAWING_THREAD_H -#define MAIN_DRAWING_THREAD_H - -#include - -class MainDrawingThread: public DrawingThreadCore { -private: - int argc; - char **argv; - - template - static int mainWrapper(int, char **) - { - return f(); - } - - template - static int mainWrapper(int argc, char **argv) - { - return f(argc, argv); - } - - static int (*main)(int, char **); - - int runForReal(); - -public: - MainDrawingThread(int argc_, char **argv_); - -}; - -#define MAIN_DRAWING_THREAD(function) \ - int (*MainDrawingThread::main)(int, char **) = \ - MainDrawingThread::mainWrapper; - -#endif // !MAIN_DRAWING_THREAD_H diff --git a/test/hello.cpp b/test/hello.cpp index d18128a..81f25cd 100644 --- a/test/hello.cpp +++ b/test/hello.cpp @@ -21,292 +21,48 @@ */ +#include + #include -#include +#include +#include #include -int main_drawing_thread(int argc, char **argv) +int main_drawing_thread(DrawingArea &a) { - // >>> insert main drawing code here <<< - std::cout << "[" << argc << "]\n"; - for (int i = 0; i < argc; i++) - std::cout << " <" << argv[i] << ">\n"; - while (true) { + std::cout << "[ " << a.width() << " x " << a.height() << " ]\n"; + + int c = 0; + int y = 0; + while (1) { + std::cerr << "loooooooooooooooooooooop " + << y << " (" << c << ")\n"; + a.setColor(c, c, c); + for (int yy = y; yy < y + 10; yy++) + for (int x = 0; x < a.width(); x++) + a.drawPoint(x, yy); + if ((y += 10) >= a.height()) { + y = 0; + c = !c; + } sleep(1); - std::cout << '.' << std::flush; } - return 0; } -MAIN_DRAWING_THREAD(main_drawing_thread); - -//============================================================ - -//********************************************************************** -//********************************************************************** -//********************************************************************** -//********************************************************************** -//********************************************************************** -#if 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[]) -{ - if (!qApp) - new QApplication(argc, argv); - else - WindowCore::instanceCount++; -} - -WindowCore::~WindowCore() +int main(int argc, char *argv[]) { - 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); + QApplication application(argc, argv); + DrawingArea drawingArea(800, 600); + DrawingWindow drawingWindow(drawingArea); + DrawingThread drawingThread(drawingArea, main_drawing_thread); -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(); + QObject::connect(&drawingArea, SIGNAL(update()), + &drawingWindow, SLOT(update())); - int width() const - { - return image->width(); - } - - int height() const - { - return image->height(); - } + drawingWindow.show(); + drawingThread.start(); - 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; + return application.exec(); } - -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); -} - - -//============================================================ - -/* 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 zr2, zi2; - float pr, pi; /* taille d'un pixel */ - float rouge, vert, bleu; - int i; - - // QtDrawingArea fen(argc, argv, larg, haut); - - pr = (Rmax - Rmin) / larg; - pi = (Imax - Imin) / larg; - - cr = Rmin; - for (x = 0; x < larg; x++) { - ci = Imin; - for (y = 0; y < larg; y++) { - /* z_1 = c */ - zr = cr; - zi = ci; - for (i = 1; i <= maxiter; i++) { - zr2 = zr * zr; - zi2 = zi * zi; - if (zr2 + zi2 >= 4) { - /* |z| >= 2 : on sort de la boucle */ - break; - } - /* on calcule le z suivant */ - zi = 2*zr*zi + ci; - zr = zr2 - zi2 + cr; - } - /* on est sorti trop t-bôt du for(...):-A - on affiche le pixel d'un couleur en fonction - de i */ - if (i <= maxiter / 2) { - /* entre rouge et vert */ - vert = (2.0 * i) / maxiter; - rouge = 1.0 - vert; - bleu = 0.0; - } else if (i <= maxiter) { - /* entre vert et bleu */ - rouge = 0.0; - bleu = (2.0 * i) / maxiter - 1.0; - vert = 1.0 - bleu; - } else /* (i > maxiter) */ - rouge = vert = bleu = 0.0; - fen.setColor(rouge, vert, bleu); - fen.drawPoint(x, y); - - ci += pi; - } - cr += pr; - // fen.flush(); - } - // fen.wait(); -} - -#endif diff --git a/test/hello.pro b/test/hello.pro index c1022ab..fb8fdb0 100644 --- a/test/hello.pro +++ b/test/hello.pro @@ -1,13 +1,8 @@ -###################################################################### -# Automatically generated by qmake (2.01a) Thu Oct 18 17:12:48 2007 -###################################################################### - TEMPLATE = app -TARGET = -DEPENDPATH += . -INCLUDEPATH += . +TARGET = hello + +CONFIG += qt debug -# Input HEADERS += DrawingArea.h \ DrawingThread.h \ DrawingWindow.h