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);
52 #ifdef USE_PIXMAP_CACHE
53 pixmap = new QPixmap(image->size());
54 QPainter pixmapPainter(pixmap);
55 pixmapPainter.drawImage(0, 0, *image);
60 setFocusPolicy(Qt::StrongFocus);
61 setFixedSize(image->size());
62 setAttribute(Qt::WA_OpaquePaintEvent);
64 timer.start(paintInterval, this);
66 thread = new DrawingThread(*this, fun);
67 thread_started = false;
70 DrawingWindow::~DrawingWindow()
73 #ifdef USE_PIXMAP_CACHE
80 void DrawingWindow::setColor(const QColor &color)
82 QPen pen(painter->pen());
87 void DrawingWindow::setColor(float red, float green, float blue)
90 color.setRgbF(red, green, blue);
91 this->setColor(color);
94 void DrawingWindow::drawPoint(int x, int y)
97 painter->drawPoint(x, y);
102 void DrawingWindow::drawLine(int x1, int y1, int x2, int y2)
105 painter->drawLine(x1, y1, x2, y2);
106 setDirtyRect(x1, y1, x2, y2);
110 void DrawingWindow::paintEvent(QPaintEvent *ev)
112 QPainter widgetPainter(this);
113 QRect rect = ev->rect();
114 #ifdef USE_PIXMAP_CACHE
115 widgetPainter.drawPixmap(rect, *pixmap, rect);
118 widgetPainter.drawImage(rect, *image, rect);
123 void DrawingWindow::showEvent(QShowEvent *ev)
125 if (!thread_started) {
127 thread_started = true;
129 QWidget::showEvent(ev);
132 void DrawingWindow::timerEvent(QTimerEvent *ev)
134 if (ev->timerId() == timer.timerId()) {
137 #ifdef USE_PIXMAP_CACHE
138 QPainter pixmapPainter(pixmap);
139 pixmapPainter.drawImage(dirtyRect, *image, dirtyRect);
143 timer.start(paintInterval, this);
147 QWidget::timerEvent(ev);
151 void DrawingWindow::setDirtyRect()
153 setDirtyRect(QRect(0, 0, width(), height()));
156 void DrawingWindow::setDirtyRect(int x, int y)
158 setDirtyRect(QRect(x, y, 1, 1));
161 void DrawingWindow::setDirtyRect(int x1, int y1, int x2, int y2)
164 r.setCoords(x1, y1, x2, y2);
165 setDirtyRect(r.normalized());
168 void DrawingWindow::setDirtyRect(const QRect &rect)