1 #include "DrawingWindow.h"
6 class DrawingWindow::DrawingThread: public QThread {
8 DrawingThread(DrawingWindow &w, ThreadFunction f)
16 exit(threadFunction(drawingWindow));
20 DrawingWindow &drawingWindow;
21 ThreadFunction threadFunction;
25 DrawingWindow::DrawingWindow(ThreadFunction fun, int width, int height)
28 initialize(fun, width, height);
31 DrawingWindow::DrawingWindow(QWidget *parent,
32 ThreadFunction fun, int width, int height)
35 initialize(fun, width, height);
38 DrawingWindow::DrawingWindow(QWidget *parent, Qt::WindowFlags flags,
39 ThreadFunction fun, int width, int height)
40 : QWidget(parent, flags)
42 initialize(fun, width, height);
45 void DrawingWindow::initialize(ThreadFunction fun, int width, int height)
47 image = new QImage(width, height, QImage::Format_RGB32);
48 image->fill(QColor(Qt::white).rgb());
50 painter = new QPainter(image);
54 setFocusPolicy(Qt::StrongFocus);
55 setFixedSize(image->size());
56 setAttribute(Qt::WA_OpaquePaintEvent);
58 timer.start(paintInterval, this);
60 thread = new DrawingThread(*this, fun);
61 thread_started = false;
64 DrawingWindow::~DrawingWindow()
71 void DrawingWindow::setColor(const QColor &color)
73 QPen pen(painter->pen());
78 void DrawingWindow::setColor(float red, float green, float blue)
81 color.setRgbF(red, green, blue);
82 this->setColor(color);
85 void DrawingWindow::drawPoint(int x, int y)
88 painter->drawPoint(x, y);
93 void DrawingWindow::drawLine(int x1, int y1, int x2, int y2)
96 painter->drawLine(x1, y1, x2, y2);
97 setDirtyRect(x1, y1, x2, y2);
101 void DrawingWindow::paintEvent(QPaintEvent *ev)
103 QPainter widgetPainter(this);
104 QRect rect = ev->rect();
106 QImage imageCopy(*image);
108 widgetPainter.drawImage(rect, imageCopy, rect);
111 void DrawingWindow::showEvent(QShowEvent *ev)
113 if (!thread_started) {
115 thread_started = true;
117 QWidget::showEvent(ev);
120 void DrawingWindow::timerEvent(QTimerEvent *ev)
122 if (ev->timerId() == timer.timerId()) {
129 timer.start(paintInterval, this);
131 QWidget::timerEvent(ev);
135 void DrawingWindow::setDirtyRect(const QRect &rect)