2022-01-10 21:49:06 +01:00
|
|
|
#include "drawingcanvaswidget.h"
|
|
|
|
|
|
|
|
|
|
#include <QPixmap>
|
|
|
|
|
#include <QPainter>
|
2025-11-23 11:13:15 +01:00
|
|
|
#include <QMouseEvent>
|
2022-01-10 21:49:06 +01:00
|
|
|
|
|
|
|
|
void DrawingCanvasWidget::setPixmap(QPixmap &pixmap)
|
|
|
|
|
{
|
|
|
|
|
m_pixmap = &pixmap;
|
|
|
|
|
setFixedSize(m_pixmap->size() * m_scale);
|
|
|
|
|
update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DrawingCanvasWidget::setScale(float scale)
|
|
|
|
|
{
|
|
|
|
|
if (m_scale == scale)
|
|
|
|
|
return;
|
|
|
|
|
emit scaleChanged(m_scale = scale);
|
|
|
|
|
if (m_pixmap)
|
|
|
|
|
setFixedSize(m_pixmap->size() * m_scale);
|
|
|
|
|
update();
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-23 18:53:22 +01:00
|
|
|
void DrawingCanvasWidget::setLeftButtonColor(const QColor &leftButtonColor)
|
|
|
|
|
{
|
|
|
|
|
if (m_leftButtonColor == leftButtonColor)
|
|
|
|
|
return;
|
|
|
|
|
emit leftButtonColorChanged(m_leftButtonColor = leftButtonColor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DrawingCanvasWidget::setRightButtonColor(const QColor &rightButtonColor)
|
|
|
|
|
{
|
|
|
|
|
if (m_rightButtonColor == rightButtonColor)
|
|
|
|
|
return;
|
|
|
|
|
emit rightButtonColorChanged(m_rightButtonColor = rightButtonColor);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-10 21:49:06 +01:00
|
|
|
void DrawingCanvasWidget::paintEvent(QPaintEvent *ev)
|
|
|
|
|
{
|
|
|
|
|
if (!m_pixmap)
|
2022-01-11 02:35:26 +01:00
|
|
|
{
|
|
|
|
|
QWidget::paintEvent(ev);
|
2022-01-10 21:49:06 +01:00
|
|
|
return;
|
2022-01-11 02:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QBrush brush;
|
|
|
|
|
{
|
|
|
|
|
QPixmap pixmap{32, 32};
|
|
|
|
|
{
|
|
|
|
|
QPainter painter{&pixmap};
|
|
|
|
|
painter.setPen(Qt::NoPen);
|
|
|
|
|
painter.setBrush(Qt::gray);
|
|
|
|
|
painter.drawRect(0, 0, 16, 16);
|
|
|
|
|
painter.drawRect(16, 16, 16, 16);
|
|
|
|
|
painter.setBrush(Qt::white);
|
|
|
|
|
painter.drawRect(0, 16, 16, 16);
|
|
|
|
|
painter.drawRect(16, 0, 16, 16);
|
|
|
|
|
}
|
|
|
|
|
brush.setTexture(std::move(pixmap));
|
|
|
|
|
}
|
2022-01-10 21:49:06 +01:00
|
|
|
|
|
|
|
|
QPainter painter{this};
|
2022-01-11 02:35:26 +01:00
|
|
|
|
|
|
|
|
painter.setPen(Qt::NoPen);
|
|
|
|
|
painter.setBrush(std::move(brush));
|
|
|
|
|
painter.drawRect(rect());
|
|
|
|
|
|
|
|
|
|
painter.drawPixmap(rect(), *m_pixmap);
|
2022-01-10 21:49:06 +01:00
|
|
|
}
|
2025-11-23 11:13:15 +01:00
|
|
|
|
|
|
|
|
void DrawingCanvasWidget::mousePressEvent(QMouseEvent *event)
|
|
|
|
|
{
|
|
|
|
|
QWidget::mousePressEvent(event);
|
|
|
|
|
|
2025-11-23 18:53:22 +01:00
|
|
|
switch (event->button())
|
2025-11-23 11:13:15 +01:00
|
|
|
{
|
2025-11-23 18:53:22 +01:00
|
|
|
case Qt::LeftButton:
|
|
|
|
|
case Qt::RightButton:
|
|
|
|
|
{
|
|
|
|
|
m_currentlyDrawing = event->button() == Qt::LeftButton;
|
|
|
|
|
|
2025-11-23 11:13:15 +01:00
|
|
|
setMouseTracking(true);
|
2025-11-23 18:53:22 +01:00
|
|
|
|
|
|
|
|
setPixel(event->pos() / m_scale, event->button() == Qt::LeftButton);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:;
|
2025-11-23 11:13:15 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DrawingCanvasWidget::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
|
{
|
|
|
|
|
QWidget::mouseReleaseEvent(event);
|
|
|
|
|
|
2025-11-23 18:53:22 +01:00
|
|
|
switch (event->button())
|
2025-11-23 11:13:15 +01:00
|
|
|
{
|
2025-11-23 18:53:22 +01:00
|
|
|
case Qt::LeftButton:
|
|
|
|
|
case Qt::RightButton:
|
|
|
|
|
{
|
|
|
|
|
m_currentlyDrawing = std::nullopt;
|
|
|
|
|
|
2025-11-23 11:13:15 +01:00
|
|
|
setMouseTracking(false);
|
2025-11-23 18:53:22 +01:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:;
|
2025-11-23 11:13:15 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DrawingCanvasWidget::mouseMoveEvent(QMouseEvent *event)
|
|
|
|
|
{
|
|
|
|
|
QWidget::mouseMoveEvent(event);
|
2025-11-23 18:53:22 +01:00
|
|
|
|
|
|
|
|
if (m_currentlyDrawing)
|
|
|
|
|
setPixel(event->pos() / m_scale, *m_currentlyDrawing);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DrawingCanvasWidget::setPixel(QPoint pos, bool leftColor)
|
|
|
|
|
{
|
|
|
|
|
auto image = m_pixmap->toImage();
|
|
|
|
|
image.setPixelColor(pos.x(), pos.y(), leftColor ? m_leftButtonColor : m_rightButtonColor);
|
|
|
|
|
*m_pixmap = QPixmap::fromImage(image);
|
|
|
|
|
repaint();
|
|
|
|
|
emit changed();
|
2025-11-23 11:13:15 +01:00
|
|
|
}
|