1 #include "DrawingWindow.h"
8 class DrawingWindow::DrawingThread: public QThread {
10 DrawingThread(DrawingWindow &w, ThreadFunction f)
18 threadFunction(drawingWindow);
22 DrawingWindow &drawingWindow;
23 ThreadFunction threadFunction;
27 DrawingWindow::DrawingWindow(ThreadFunction fun, int width, int height)
30 initialize(fun, width, height);
33 DrawingWindow::DrawingWindow(QWidget *parent,
34 ThreadFunction fun, int width, int height)
37 initialize(fun, width, height);
40 DrawingWindow::DrawingWindow(QWidget *parent, Qt::WindowFlags flags,
41 ThreadFunction fun, int width, int height)
42 : QWidget(parent, flags)
44 initialize(fun, width, height);
47 void DrawingWindow::initialize(ThreadFunction fun, int width, int height)
49 image = new QImage(width, height, QImage::Format_RGB32);
50 image->fill(QColor(Qt::white).rgb());
52 painter = new QPainter(image);
56 setFocusPolicy(Qt::StrongFocus);
57 setFixedSize(image->size());
58 setAttribute(Qt::WA_OpaquePaintEvent);
60 timer.start(paintInterval, this);
62 thread = new DrawingThread(*this, fun);
63 thread_started = false;
68 DrawingWindow::~DrawingWindow()
71 mutex_enabled = false;
84 void DrawingWindow::setColor(const QColor &color)
86 QPen pen(painter->pen());
91 void DrawingWindow::setColor(float red, float green, float blue)
94 color.setRgbF(red, green, blue);
95 this->setColor(color);
98 void DrawingWindow::drawPoint(int x, int y)
101 painter->drawPoint(x, y);
106 void DrawingWindow::drawLine(int x1, int y1, int x2, int y2)
109 painter->drawLine(x1, y1, x2, y2);
110 setDirtyRect(x1, y1, x2, y2);
114 void DrawingWindow::paintEvent(QPaintEvent *ev)
116 QPainter widgetPainter(this);
117 QRect rect = ev->rect();
119 QImage imageCopy(*image);
121 widgetPainter.drawImage(rect, imageCopy, rect);
124 void DrawingWindow::showEvent(QShowEvent *ev)
126 if (!thread_started) {
128 thread_started = true;
130 QWidget::showEvent(ev);
133 void DrawingWindow::timerEvent(QTimerEvent *ev)
135 if (ev->timerId() == timer.timerId()) {
142 timer.start(paintInterval, this);
144 QWidget::timerEvent(ev);
148 void DrawingWindow::setDirtyRect(const QRect &rect)