Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "drdrawview.h"
- DRDrawView::DRDrawView(QWidget *parent) : QGLWidget(parent), image(1024, 1536, QImage::Format_ARGB32_Premultiplied)
- {
- pen.setColor(Qt::black);
- pen.setWidth(20 * devicePixelRatio());
- pen.setCapStyle(Qt::RoundCap);
- pen.setJoinStyle(Qt::RoundJoin);
- setAutoFillBackground(false);
- setAttribute(Qt::WA_OpaquePaintEvent, true);
- setAttribute(Qt::WA_NoSystemBackground, true);
- clearScreen();
- }
- DRDrawView::~DRDrawView()
- {
- }
- void DRDrawView::save( const QString &filename, const QString &format )
- {
- }
- void DRDrawView::clearScreen()
- {
- QMutexLocker l(&bufferMutex);
- memset(image.bits(), 0xFF, image.byteCount());
- update();
- }
- void DRDrawView::mousePressEvent( QMouseEvent *e )
- {
- mousePressed = true;
- }
- void DRDrawView::mouseReleaseEvent( QMouseEvent * )
- {
- QMutexLocker l(&bufferMutex);
- mousePressed = false;
- polyline.clear();
- }
- void DRDrawView::mouseMoveEvent( QMouseEvent *e )
- {
- if ( mousePressed )
- {
- QMutexLocker l(&bufferMutex);
- QPoint p = e->pos();
- p.setX(p.x() * devicePixelRatio());
- p.setY(p.y() * devicePixelRatio());
- polyline.push_front(p);
- if (polyline.count() == 4)
- {
- polyline.pop_back();
- QPainter painter(&image);
- painter.setRenderHint(QPainter::Antialiasing);
- painter.setPen(pen);
- painter.drawPolyline(polyline);
- QRect r = polyline.boundingRect();
- r.setWidth(r.width() / devicePixelRatio());
- r.setHeight(r.height() / devicePixelRatio());
- r.setX(r.x() / devicePixelRatio());
- r.setY(r.y() / devicePixelRatio());
- r.setLeft( r.left() - penWidth() );
- r.setTop( r.top() - penWidth() );
- r.setRight( r.right() + penWidth() );
- r.setBottom( r.bottom() + penWidth() );
- r = r.normalized();
- qDebug() << "DRAW RECT: " << devicePixelRatio() << r.x() << " " << r.y() << " " << r.width() << " " << r.height();
- update(r);
- }
- }
- }
- void DRDrawView::resizeEvent( QResizeEvent *e )
- {
- QWidget::resizeEvent( e );
- //image = QImage(width() * devicePixelRatio(), height() * devicePixelRatio(), QImage::Format_ARGB32_Premultiplied);
- //clearScreen();
- }
- void DRDrawView::paintEvent( QPaintEvent *e )
- {
- //QGLWidget::paintEvent( e );
- QMutexLocker l(&bufferMutex);
- QVector<QRect> rects = e->region().rects();
- QPainter painter(this);
- painter.setRenderHint(QPainter::Antialiasing);
- for (int i = 0; i < rects.count(); i++)
- {
- QRect r = rects[i];
- QRect r2 = r;
- r2.setWidth(r2.width() * devicePixelRatio());
- r2.setHeight(r2.height() * devicePixelRatio());
- r2.setX(r2.x() * devicePixelRatio());
- r2.setY(r2.y() * devicePixelRatio());
- //painter.drawImage(r.x(), r.y(), &image, r.x(), r.y(), r.width(), r.height(), Qt::AutoColor);
- painter.drawImage(r, image, r2);
- qDebug() << "UPDATE RECT: " << devicePixelRatio() << r.x() << " " << r.y() << " " << r.width() << " " << r.height();
- }
- }
- /*
- Logging:
- UPDATE RECT: 2 0 0 320 568
- UPDATE RECT: 2 0 0 320 568
- DRAW RECT: 2 128 159 266 302
- UPDATE RECT: 2 0 0 320 568
- DRAW RECT: 2 87 171 255 398
- UPDATE RECT: 2 0 0 320 568
- DRAW RECT: 2 75 182 248 431
- DRAW RECT: 2 73 278 207 434
- UPDATE RECT: 2 0 0 320 568
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement