1 #include "DrawingWindow.h"
8 DrawingWindow::DrawingWindow(ThreadFunction fun, int width, int height)
11 initialize(fun, width, height);
14 DrawingWindow::DrawingWindow(QWidget *parent,
15 ThreadFunction fun, int width, int height)
18 initialize(fun, width, height);
21 DrawingWindow::DrawingWindow(QWidget *parent, Qt::WindowFlags flags,
22 ThreadFunction fun, int width, int height)
23 : QWidget(parent, flags)
25 initialize(fun, width, height);
28 void DrawingWindow::initialize(ThreadFunction fun, int width, int height)
30 image = new QImage(width, height, QImage::Format_RGB32);
31 image->fill(QColor(Qt::white).rgb());
33 painter = new QPainter(image);
37 setFocusPolicy(Qt::StrongFocus);
38 setFixedSize(image->size());
39 setAttribute(Qt::WA_OpaquePaintEvent);
41 timer.start(paintInterval, this);
43 thread = new DrawingThread(*this, fun);
44 thread_started = false;
47 DrawingWindow::~DrawingWindow()
54 void DrawingWindow::setColor(const QColor &color)
56 QPen pen(painter->pen());
61 void DrawingWindow::setColor(float red, float green, float blue)
64 color.setRgbF(red, green, blue);
65 this->setColor(color);
68 void DrawingWindow::drawPoint(int x, int y)
71 painter->drawPoint(x, y);
76 void DrawingWindow::drawLine(int x1, int y1, int x2, int y2)
79 painter->drawLine(x1, y1, x2, y2);
80 setDirtyRect(x1, y1, x2, y2);
84 void DrawingWindow::closeEvent(QCloseEvent *ev)
91 QWidget::closeEvent(ev);
99 void DrawingWindow::paintEvent(QPaintEvent *ev)
101 QPainter widgetPainter(this);
102 QRect rect = ev->rect();
104 QImage imageCopy(*image);
106 widgetPainter.drawImage(rect, imageCopy, rect);
109 void DrawingWindow::showEvent(QShowEvent *ev)
111 if (!thread_started) {
113 thread_started = true;
115 QWidget::showEvent(ev);
118 void DrawingWindow::timerEvent(QTimerEvent *ev)
120 if (ev->timerId() == timer.timerId()) {
127 timer.start(paintInterval, this);
129 QWidget::timerEvent(ev);
133 void DrawingWindow::setDirtyRect(const QRect &rect)
143 DrawingWindow::DrawingThread::DrawingThread(DrawingWindow &w,
150 void DrawingWindow::DrawingThread::run()
152 threadFunction(drawingWindow);