Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
.
[graphlib.git] / DrawingWindow.cpp
index 9a8d501..16405b5 100644 (file)
@@ -3,24 +3,7 @@
 #include <QThread>
 #include <QTimerEvent>
 
-class DrawingWindow::DrawingThread: public QThread {
-public:
-    DrawingThread(DrawingWindow &w, ThreadFunction f)
-        : drawingWindow(w)
-        , threadFunction(f)
-    {
-    }
-
-    void run()
-    {
-        exit(threadFunction(drawingWindow));
-    }
-
-private:
-    DrawingWindow &drawingWindow;
-    ThreadFunction threadFunction;
-
-};
+#include <iostream>
 
 DrawingWindow::DrawingWindow(ThreadFunction fun, int width, int height)
     : QWidget()
@@ -49,12 +32,6 @@ void DrawingWindow::initialize(ThreadFunction fun, int width, int height)
 
     painter = new QPainter(image);
 
-#ifdef USE_PIXMAP_CACHE
-    pixmap = new QPixmap(image->size());
-    QPainter pixmapPainter(pixmap);
-    pixmapPainter.drawImage(0, 0, *image);
-#endif
-
     dirtyFlag = false;
 
     setFocusPolicy(Qt::StrongFocus);
@@ -70,9 +47,6 @@ void DrawingWindow::initialize(ThreadFunction fun, int width, int height)
 DrawingWindow::~DrawingWindow()
 {
     delete thread;
-#ifdef USE_PIXMAP_CACHE
-    delete pixmap;
-#endif
     delete painter;
     delete image;
 }
@@ -107,17 +81,29 @@ void DrawingWindow::drawLine(int x1, int y1, int x2, int y2)
     unlock();
 }
 
+void DrawingWindow::closeEvent(QCloseEvent *ev)
+{
+    std::cerr << "A\n";
+    lock();
+    std::cerr << "B\n";
+    thread->terminate();
+    std::cerr << "C\n";
+    QWidget::closeEvent(ev);
+    std::cerr << "D\n";
+    thread->wait();
+    std::cerr << "E\n";
+    unlock();
+    std::cerr << "F\n";
+}
+
 void DrawingWindow::paintEvent(QPaintEvent *ev)
 {
     QPainter widgetPainter(this);
     QRect rect = ev->rect();
-#ifdef USE_PIXMAP_CACHE
-    widgetPainter.drawPixmap(rect, *pixmap, rect);
-#else
     lock();
-    widgetPainter.drawImage(rect, *image, rect);
+    QImage imageCopy(*image);
     unlock();
-#endif
+    widgetPainter.drawImage(rect, imageCopy, rect);
 }
 
 void DrawingWindow::showEvent(QShowEvent *ev)
@@ -134,37 +120,16 @@ void DrawingWindow::timerEvent(QTimerEvent *ev)
     if (ev->timerId() == timer.timerId()) {
         lock();
         if (dirtyFlag) {
-#ifdef USE_PIXMAP_CACHE
-            QPainter pixmapPainter(pixmap);
-            pixmapPainter.drawImage(dirtyRect, *image, dirtyRect);
-#endif
-            dirtyFlag = false;
             update(dirtyRect);
-            timer.start(paintInterval, this);
+            dirtyFlag = false;
         }
         unlock();
+        timer.start(paintInterval, this);
     } else {
         QWidget::timerEvent(ev);
     }
 }
 
-void DrawingWindow::setDirtyRect()
-{
-    setDirtyRect(QRect(0, 0, width(), height()));
-}
-
-void DrawingWindow::setDirtyRect(int x, int y)
-{
-    setDirtyRect(QRect(x, y, 1, 1));
-}
-
-void DrawingWindow::setDirtyRect(int x1, int y1, int x2, int y2)
-{
-    QRect r;
-    r.setCoords(x1, y1, x2, y2);
-    setDirtyRect(r.normalized());
-}
-
 void DrawingWindow::setDirtyRect(const QRect &rect)
 {
     if (dirtyFlag) {
@@ -174,3 +139,15 @@ void DrawingWindow::setDirtyRect(const QRect &rect)
         dirtyRect = rect;
     }
 }
+
+DrawingWindow::DrawingThread::DrawingThread(DrawingWindow &w,
+                                            ThreadFunction f)
+    : drawingWindow(w)
+    , threadFunction(f)
+{
+}
+
+void DrawingWindow::DrawingThread::run()
+{
+    threadFunction(drawingWindow);
+}