Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
.
authorgiersch <giersch>
Mon, 5 Nov 2007 21:53:33 +0000 (21:53 +0000)
committergiersch <giersch>
Mon, 5 Nov 2007 21:53:33 +0000 (21:53 +0000)
DrawingWindow.cpp
DrawingWindow.h
test/hello.cpp

index 4e34d9e..16405b5 100644 (file)
@@ -5,25 +5,6 @@
 
 #include <iostream>
 
-class DrawingWindow::DrawingThread: public QThread {
-public:
-    DrawingThread(DrawingWindow &w, ThreadFunction f)
-        : drawingWindow(w)
-        , threadFunction(f)
-    {
-    }
-
-    void run()
-    {
-        threadFunction(drawingWindow);
-    }
-
-private:
-    DrawingWindow &drawingWindow;
-    ThreadFunction threadFunction;
-
-};
-
 DrawingWindow::DrawingWindow(ThreadFunction fun, int width, int height)
     : QWidget()
 {
@@ -61,24 +42,13 @@ void DrawingWindow::initialize(ThreadFunction fun, int width, int height)
 
     thread = new DrawingThread(*this, fun);
     thread_started = false;
-
-    mutex_enabled = true;
 }
 
 DrawingWindow::~DrawingWindow()
 {
-    mutex.lock();
-    mutex_enabled = false;
-    mutex.unlock();
-    std::cerr << "A\n";
-    thread->terminate();    
-    std::cerr << "B\n";
-    thread->wait();
-    std::cerr << "C\n";
     delete thread;
     delete painter;
     delete image;
-    std::cerr << "D\n";
 }
 
 void DrawingWindow::setColor(const QColor &color)
@@ -111,6 +81,21 @@ 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);
@@ -154,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);
+}
index 9e86889..0a0863f 100644 (file)
@@ -10,6 +10,8 @@
 #include <QWidget>
 #include <Qt>
 
+#include <QThread>
+
 class DrawingWindow: public QWidget {
 /*     Q_OBJECT */
 
@@ -40,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;
 
@@ -60,7 +75,6 @@ private:
     bool dirtyFlag;
     QRect dirtyRect;
 
-    bool mutex_enabled;
     QMutex mutex;
 
     void initialize(ThreadFunction fun, int width, int height);
@@ -89,16 +103,15 @@ int DrawingWindow::height() const
 inline
 void DrawingWindow::lock()
 {
-    if (mutex_enabled)
-        mutex.lock();
-    if (!mutex_enabled)
-        mutex.unlock();
+    thread->disableTerminate();
+    mutex.lock();
 }
 
 inline
 void DrawingWindow::unlock()
 {
     mutex.unlock();
+    thread->enableTerminate();
 }
 
 inline
@@ -122,4 +135,18 @@ void DrawingWindow::setDirtyRect(int x1, int y1, int x2, int y2)
     setDirtyRect(r.normalized());
 }
 
+inline
+void DrawingWindow::DrawingThread::enableTerminate()
+{
+    if (currentThread() == this)
+        setTerminationEnabled(true);
+}
+
+inline
+void DrawingWindow::DrawingThread::disableTerminate()
+{
+    if (currentThread() == this)
+        setTerminationEnabled(false);
+}
+
 #endif // !DRAWING_WINDOW_H
index 8c1585c..d98d2a6 100644 (file)
@@ -143,7 +143,7 @@ int main(int argc, char *argv[])
 
     DrawingWindow dd(lines, w, h);
     dd.show();
-    return application.exec();
+//     return application.exec();
 
     const int nf = 1;
     const int nm = 1;