From: giersch Date: Mon, 5 Nov 2007 21:53:33 +0000 (+0000) Subject: . X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/graphlib.git/commitdiff_plain/87054f0da897634ebfc9b5ae02a7899cc81ceaa4 . --- diff --git a/DrawingWindow.cpp b/DrawingWindow.cpp index 4e34d9e..16405b5 100644 --- a/DrawingWindow.cpp +++ b/DrawingWindow.cpp @@ -5,25 +5,6 @@ #include -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); +} diff --git a/DrawingWindow.h b/DrawingWindow.h index 9e86889..0a0863f 100644 --- a/DrawingWindow.h +++ b/DrawingWindow.h @@ -10,6 +10,8 @@ #include #include +#include + 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 diff --git a/test/hello.cpp b/test/hello.cpp index 8c1585c..d98d2a6 100644 --- a/test/hello.cpp +++ b/test/hello.cpp @@ -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;