Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
.
authorgiersch <giersch>
Tue, 23 Oct 2007 15:14:47 +0000 (15:14 +0000)
committergiersch <giersch>
Tue, 23 Oct 2007 15:14:47 +0000 (15:14 +0000)
DrawingArea.cpp
DrawingArea.h
DrawingWindow.cpp
DrawingWindow.h
test/hello.cpp
test/hello.pro

index cd957da..6c3e3cc 100644 (file)
@@ -5,6 +5,7 @@ DrawingArea::DrawingArea(int width, int height)
     image = new QImage(width, height, QImage::Format_RGB32);
     image->fill(QColor(Qt::white).rgb());
     painter = new QPainter(image);
     image = new QImage(width, height, QImage::Format_RGB32);
     image->fill(QColor(Qt::white).rgb());
     painter = new QPainter(image);
+    setDirty();
 }
 
 DrawingArea::~DrawingArea()
 }
 
 DrawingArea::~DrawingArea()
@@ -13,16 +14,6 @@ DrawingArea::~DrawingArea()
     delete image;
 }
 
     delete image;
 }
 
-int DrawingArea::width() const
-{
-    return image->width();
-}
-
-int DrawingArea::height() const
-{
-    return image->height();
-}
-
 void DrawingArea::setColor(const QColor &color)
 {
     QPen pen(painter->pen());
 void DrawingArea::setColor(const QColor &color)
 {
     QPen pen(painter->pen());
@@ -39,18 +30,40 @@ void DrawingArea::setColor(float red, float green, float blue)
 
 void DrawingArea::drawPoint(int x, int y)
 {
 
 void DrawingArea::drawPoint(int x, int y)
 {
+    lock();
     painter->drawPoint(x, y);
     painter->drawPoint(x, y);
-    emit update();
+    setDirty(QRect(x, y, 1, 1));
+    unlock();
 }
 
 void DrawingArea::drawLine(int x1, int y1, int x2, int y2)
 {
 }
 
 void DrawingArea::drawLine(int x1, int y1, int x2, int y2)
 {
+    lock();
     painter->drawLine(x1, y1, x2, y2);
     painter->drawLine(x1, y1, x2, y2);
-    emit update();
+    if (x1 > x2)
+        std::swap(x1, x2);
+    if (y1 > y2)
+        std::swap(y1, y2);
+    setDirty(QRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1));
+    unlock();
+}
+
+void DrawingArea::setDirty()
+{
+    setDirty(QRect(0, 0, width(), height()));
+}
+
+void DrawingArea::setDirty(const QRect &rect)
+{
+    if (dirtyFlag)
+        dirtyRect |= rect;
+    else
+        dirtyRect = rect;
+    dirtyFlag = true;
 }
 
 }
 
-const QImage &DrawingArea::getImage() const
+void DrawingArea::setClean()
 {
 {
-    return *image;
+    dirtyFlag = false;
 }
 
 }
 
index 874de0f..51ff5a8 100644 (file)
@@ -5,9 +5,12 @@
 #include <QImage>
 #include <QObject>
 #include <QPainter>
 #include <QImage>
 #include <QObject>
 #include <QPainter>
+#include <QRect>
+#include <QSize>
+#include <QMutex>
 
 class DrawingArea: public QObject {
 
 class DrawingArea: public QObject {
-    Q_OBJECT
+/*     Q_OBJECT */
 
 public:
     static const int DEFAULT_WIDTH = 640;
 
 public:
     static const int DEFAULT_WIDTH = 640;
@@ -18,6 +21,7 @@ public:
 
     int width() const;
     int height() const;
 
     int width() const;
     int height() const;
+    const QSize size() const;
 
     void setColor(const QColor &color);
     void setColor(float red, float green, float blue);
 
     void setColor(const QColor &color);
     void setColor(float red, float green, float blue);
@@ -25,18 +29,72 @@ public:
     void drawPoint(int x, int y);
     void drawLine(int x1, int y1, int x2, int y2);
 
     void drawPoint(int x, int y);
     void drawLine(int x1, int y1, int x2, int y2);
 
-    const QImage &getImage() const;
+    QImage &getImage();
 
 
-signals:
-    void update();
+    bool isDirty() const;
+    void setDirty();
+    void setDirty(const QRect &rect);
+    void setClean();
+
+    QRect getDirtyRect() const;
+
+    void lock();
+    void unlock();
 
 private:
     QImage *image;
     QPainter *painter;
 
 private:
     QImage *image;
     QPainter *painter;
-
+    bool dirtyFlag;
+    QRect dirtyRect;
+    QMutex mutex;
 };
 
 };
 
-#endif // !DRAWING_AREA_H
+inline
+int DrawingArea::width() const
+{
+    return image->width();
+}
+
+inline
+int DrawingArea::height() const
+{
+    return image->height();
+}
 
 
+inline
+const QSize DrawingArea::size() const
+{
+    return image->size();
+}
 
 
+inline
+QImage &DrawingArea::getImage()
+{
+    return *image;
+}
 
 
+inline
+bool DrawingArea::isDirty() const
+{
+    return dirtyFlag;
+}
+
+inline
+QRect DrawingArea::getDirtyRect() const
+{
+    return dirtyRect;
+}
+
+inline
+void DrawingArea::lock()
+{
+    mutex.lock();
+}
+
+inline
+void DrawingArea::unlock()
+{
+    mutex.unlock();
+}
+
+#endif // !DRAWING_AREA_H
index 6c49e12..04033ca 100644 (file)
@@ -1,35 +1,58 @@
 #include "DrawingWindow.h"
 #include <QPainter>
 #include "DrawingWindow.h"
 #include <QPainter>
+#include <QPaintEvent>
 
 #include <iostream>
 
 
 #include <iostream>
 
-DrawingWindow::DrawingWindow(const DrawingArea &a)
+DrawingWindow::DrawingWindow(DrawingArea &a)
     : QWidget()
     , drawingArea(a)
     : QWidget()
     , drawingArea(a)
+    , pixmap(a.size())
 {
     initialize();
 }
 
 {
     initialize();
 }
 
-DrawingWindow::DrawingWindow(QWidget *parent, const DrawingArea &a)
+DrawingWindow::DrawingWindow(QWidget *parent, DrawingArea &a)
     : QWidget(parent)
     , drawingArea(a)
     : QWidget(parent)
     , drawingArea(a)
+    , pixmap(a.size())
 {
     initialize();
 }
 
 DrawingWindow::DrawingWindow(QWidget *parent, Qt::WindowFlags flags,
 {
     initialize();
 }
 
 DrawingWindow::DrawingWindow(QWidget *parent, Qt::WindowFlags flags,
-                             const DrawingArea &a)
+                             DrawingArea &a)
     : QWidget(parent, flags)
     , drawingArea(a)
     : QWidget(parent, flags)
     , drawingArea(a)
+    , pixmap(a.size())
 {
     initialize();
 }
 
 {
     initialize();
 }
 
-void DrawingWindow::paintEvent(QPaintEvent *)
+void DrawingWindow::paintEvent(QPaintEvent *ev)
 {
 {
-    std::cerr << "paint!\n";
+    QRect rect = ev->rect();
+    drawingArea.lock();
+    if (drawingArea.isDirty()) {
+        QPainter pixmapPainter(&pixmap);
+        pixmapPainter.drawImage(drawingArea.getDirtyRect(),
+                                drawingArea.getImage(),
+                                drawingArea.getDirtyRect());
+        drawingArea.setClean();
+        rect |= drawingArea.getDirtyRect();
+    }
+    drawingArea.unlock();
     QPainter painter(this);
     QPainter painter(this);
-    painter.drawImage(0, 0, drawingArea.getImage());
+    painter.drawPixmap(0, 0, pixmap);
+}
+
+void DrawingWindow::timerEvent(QTimerEvent *ev)
+{
+    if (ev->timerId() == timer.timerId()) {
+        update();
+    } else {
+        QWidget::timerEvent(ev);
+    }
 }
 
 void DrawingWindow::initialize()
 }
 
 void DrawingWindow::initialize()
@@ -37,6 +60,5 @@ void DrawingWindow::initialize()
     setFocusPolicy(Qt::StrongFocus);
     setFixedSize(drawingArea.getImage().size());
     setAttribute(Qt::WA_OpaquePaintEvent);
     setFocusPolicy(Qt::StrongFocus);
     setFixedSize(drawingArea.getImage().size());
     setAttribute(Qt::WA_OpaquePaintEvent);
-    connect(&drawingArea, SIGNAL(update()), this, SLOT(update()));
-
+    timer.start(50, this);
 }
 }
index c89c845..7dd9bb7 100644 (file)
@@ -2,20 +2,26 @@
 #define DRAWING_WINDOW_H
 
 #include "DrawingArea.h"
 #define DRAWING_WINDOW_H
 
 #include "DrawingArea.h"
+#include <QPixmap>
+#include <QTimer>
 #include <QWidget>
 
 class DrawingWindow: public QWidget {
 #include <QWidget>
 
 class DrawingWindow: public QWidget {
+/*     Q_OBJECT */
+
 public:
 public:
-    DrawingWindow(const DrawingArea &a);
-    DrawingWindow(QWidget *parent, const DrawingArea &a);
-    DrawingWindow(QWidget *parent, Qt::WindowFlags flags,
-                  const DrawingArea &a);
+    DrawingWindow(DrawingArea &a);
+    DrawingWindow(QWidget *parent, DrawingArea &a);
+    DrawingWindow(QWidget *parent, Qt::WindowFlags flags, DrawingArea &a);
 
 protected:
 
 protected:
-    void paintEvent(QPaintEvent *e);
+    void paintEvent(QPaintEvent *ev);
+    void timerEvent(QTimerEvent *ev);
 
 private:
 
 private:
-    const DrawingArea &drawingArea;
+    DrawingArea &drawingArea;
+    QPixmap pixmap;
+    QBasicTimer timer;
 
     void initialize();
 };
 
     void initialize();
 };
index 8acf518..033253a 100644 (file)
@@ -22,7 +22,6 @@
 
 
 #include <QApplication>
 
 
 #include <QApplication>
-
 #include <DrawingArea.h>
 #include <DrawingThread.h>
 #include <DrawingWindow.h>
 #include <DrawingArea.h>
 #include <DrawingThread.h>
 #include <DrawingWindow.h>
@@ -35,18 +34,23 @@ int main_drawing_thread(DrawingArea &a)
 
     int c = 0;
     int y = 0;
 
     int c = 0;
     int y = 0;
+    int h = a.height();
+    int w = a.width();
+    int count = 10;
     while (1) {
     while (1) {
-        std::cerr << "loooooooooooooooooooooop "
-                  << y << " (" << c << ")\n";
+//         std::cerr << "loooooooooooooooooooooop "
+//                   << y << " (" << c << ")\n";
         a.setColor(c, c, c);
         for (int yy = y; yy < y + 10; yy++)
         a.setColor(c, c, c);
         for (int yy = y; yy < y + 10; yy++)
-            for (int x = 0; x < a.width(); x++)
+            for (int x = 0; x < w; x++)
                 a.drawPoint(x, yy);
                 a.drawPoint(x, yy);
-        if ((y += 10) >= a.height()) {
+        if ((y += 10) >= h) {
             y = 0;
             c = !c;
             y = 0;
             c = !c;
+            if (!--count) break;
+            std::cerr << "loooooooooooooooooooooop "
+                      << y << " (" << c << ")\n";
         }
         }
-        sleep(1);
     }
     return 0;
 }
     }
     return 0;
 }
index fb8fdb0..68487a8 100644 (file)
@@ -3,6 +3,13 @@ TARGET = hello
 
 CONFIG += qt debug
 
 
 CONFIG += qt debug
 
+QMAKE_CFLAGS += -O2
+QMAKE_CXXFLAGS += -O2
+
+QMAKE_CFLAGS += -pg
+QMAKE_CXXFLAGS += -pg
+QMAKE_LFLAGS += -pg
+
 HEADERS += DrawingArea.h \
            DrawingThread.h \
            DrawingWindow.h
 HEADERS += DrawingArea.h \
            DrawingThread.h \
            DrawingWindow.h