#include "my_paint_widget.hh" #include #include #include #include #include #include MyPaintWidget::MyPaintWidget() { old_x = -1; old_y = -1; _image = NULL; _painting = NULL; _pen_color = Qt::white; } int MyPaintWidget::loadImage(const char* filename) { _image = new QPixmap(); if (! _image->load(filename)) { std::cout << "Error loading " << filename << std::endl; return 1; // throw exception } std::cout << "width x height: " << _image->width() << " x " << _image->height() << std::endl; #ifdef SLOW _painting = new QImage(_image->size(), QImage::Format_ARGB32_Premultiplied); _painting->fill(QColor(Qt::transparent).value()); #else _painting = new QPixmap(_image->size()); _painting->fill(QColor(Qt::transparent)); // _painting->fill(QColor(Qt::white)); #endif setPixmap(*_image); // this->resize(_image->size()); return 0; } void MyPaintWidget::clearPainting(void) { _painting->fill(QColor(Qt::transparent).value()); update(); } void MyPaintWidget::mousePressEvent(QMouseEvent *e) { old_x = e->x(); old_y = e->y(); } void MyPaintWidget::setPenColor(QColor color) { _pen_color = color; } void MyPaintWidget::mouseMoveEvent(QMouseEvent *e) { if (e->buttons() == Qt::LeftButton) { QPainter painter(_painting); painter.setPen(QPen(QBrush(_pen_color), 3 , Qt::SolidLine)) ; painter.drawLine(old_x, old_y, e->x(), e->y()); old_x = e->x(); old_y = e->y(); update(); } } void MyPaintWidget::paintEvent(QPaintEvent *event) { QPainter painter(this); painter.drawPixmap(0, 0, *_image); #ifdef SLOW painter.drawImage(0, 0, *_painting); #else painter.drawPixmap(0, 0, *_painting); #endif }