#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()
{
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)
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);
dirtyRect = rect;
}
}
+
+DrawingWindow::DrawingThread::DrawingThread(DrawingWindow &w,
+ ThreadFunction f)
+ : drawingWindow(w)
+ , threadFunction(f)
+{
+}
+
+void DrawingWindow::DrawingThread::run()
+{
+ threadFunction(drawingWindow);
+}
#include <QWidget>
#include <Qt>
+#include <QThread>
+
class DrawingWindow: public QWidget {
/* Q_OBJECT */
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;
bool dirtyFlag;
QRect dirtyRect;
- bool mutex_enabled;
QMutex mutex;
void initialize(ThreadFunction fun, int width, int height);
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
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