Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
.
[graphlib.git] / DrawingWindow.h
index d2ee6de..10f0aa8 100644 (file)
@@ -1,25 +1,22 @@
 #ifndef DRAWING_WINDOW_H
 #define DRAWING_WINDOW_H
 
-#define USE_PIXMAP_CACHE
-
 #include <QBasicTimer>
 #include <QColor>
 #include <QImage>
 #include <QMutex>
 #include <QPainter>
-#ifdef USE_PIXMAP_CACHE
-#  include <QPixmap>
-#endif
 #include <QRect>
 #include <QWidget>
 #include <Qt>
 
+#include <QThread>
+
 class DrawingWindow: public QWidget {
 /*     Q_OBJECT */
 
 public:
-    typedef int (*ThreadFunction)(DrawingWindow &);
+    typedef void (*ThreadFunction)(DrawingWindow &);
 
     static const int DEFAULT_WIDTH = 640;
     static const int DEFAULT_HEIGHT = 480;
@@ -45,12 +42,25 @@ public:
     void drawLine(int x1, int y1, int x2, int y2);
 
 protected:
+    void closeEvent(QCloseEvent *ev);
     void paintEvent(QPaintEvent *ev);
     void showEvent(QShowEvent *ev);
     void timerEvent(QTimerEvent *ev);
 
 private:
-    class DrawingThread;
+    class DrawingThread: public QThread {
+    public:
+        DrawingThread(DrawingWindow &w, ThreadFunction f);
+        void run();
+
+        void enableTerminate();
+        void disableTerminate();
+
+    private:
+        DrawingWindow &drawingWindow;
+        ThreadFunction threadFunction;
+        
+    };
 
     static const int paintInterval = 33;
 
@@ -58,9 +68,6 @@ private:
 
     QImage *image;
     QPainter *painter;
-#ifdef USE_PIXMAP_CACHE
-    QPixmap *pixmap;
-#endif
 
     DrawingThread *thread;
     bool thread_started;
@@ -96,6 +103,7 @@ int DrawingWindow::height() const
 inline
 void DrawingWindow::lock()
 {
+    thread->disableTerminate();
     mutex.lock();
 }
 
@@ -103,6 +111,40 @@ inline
 void DrawingWindow::unlock()
 {
     mutex.unlock();
+    thread->enableTerminate();
+}
+
+inline
+void DrawingWindow::setDirtyRect()
+{
+    dirtyFlag = true;
+    dirtyRect.setRect(0, 0, width(), height());
+}
+
+inline
+void DrawingWindow::setDirtyRect(int x, int y)
+{
+    setDirtyRect(QRect(x, y, 1, 1));
+}
+
+inline
+void DrawingWindow::setDirtyRect(int x1, int y1, int x2, int y2)
+{
+    QRect r;
+    r.setCoords(x1, y1, x2, y2);
+    setDirtyRect(r.normalized());
+}
+
+inline
+void DrawingWindow::DrawingThread::enableTerminate()
+{
+    setTerminationEnabled(true);
+}
+
+inline
+void DrawingWindow::DrawingThread::disableTerminate()
+{
+    setTerminationEnabled(false);
 }
 
 #endif // !DRAWING_WINDOW_H