Added status labels in image editor and implemented flood fill
This commit is contained in:
@@ -3,21 +3,41 @@
|
||||
#include <QPixmap>
|
||||
#include <QPainter>
|
||||
#include <QMouseEvent>
|
||||
#include <QQueue>
|
||||
#include <QDebug>
|
||||
|
||||
void DrawingCanvasWidget::setPixmap(QPixmap &pixmap)
|
||||
DrawingCanvasWidget::DrawingCanvasWidget(QWidget *parent) :
|
||||
QWidget{parent}
|
||||
{
|
||||
m_pixmap = &pixmap;
|
||||
setFixedSize(m_pixmap->size() * m_scale);
|
||||
setMouseTracking(true);
|
||||
}
|
||||
|
||||
void DrawingCanvasWidget::setMode(Mode mode)
|
||||
{
|
||||
qDebug() << mode;
|
||||
if (m_mode != mode)
|
||||
emit modeChanged(m_mode = mode);
|
||||
// TODO cursor
|
||||
}
|
||||
|
||||
QPixmap DrawingCanvasWidget::pixmap() const
|
||||
{
|
||||
return QPixmap::fromImage(m_image);
|
||||
}
|
||||
|
||||
void DrawingCanvasWidget::setPixmap(const QPixmap &pixmap)
|
||||
{
|
||||
m_image = pixmap.toImage();
|
||||
setFixedSize(m_image.size() * m_zoom);
|
||||
update();
|
||||
}
|
||||
|
||||
void DrawingCanvasWidget::setScale(float scale)
|
||||
void DrawingCanvasWidget::setScale(float zoom)
|
||||
{
|
||||
if (m_scale == scale)
|
||||
if (m_zoom == zoom)
|
||||
return;
|
||||
emit scaleChanged(m_scale = scale);
|
||||
if (m_pixmap)
|
||||
setFixedSize(m_pixmap->size() * m_scale);
|
||||
emit zoomChanged(m_zoom = zoom);
|
||||
setFixedSize(m_image.size() * m_zoom);
|
||||
update();
|
||||
}
|
||||
|
||||
@@ -37,11 +57,7 @@ void DrawingCanvasWidget::setRightButtonColor(const QColor &rightButtonColor)
|
||||
|
||||
void DrawingCanvasWidget::paintEvent(QPaintEvent *ev)
|
||||
{
|
||||
if (!m_pixmap)
|
||||
{
|
||||
QWidget::paintEvent(ev);
|
||||
return;
|
||||
}
|
||||
QWidget::paintEvent(ev);
|
||||
|
||||
QBrush brush;
|
||||
{
|
||||
@@ -65,23 +81,57 @@ void DrawingCanvasWidget::paintEvent(QPaintEvent *ev)
|
||||
painter.setBrush(std::move(brush));
|
||||
painter.drawRect(rect());
|
||||
|
||||
painter.drawPixmap(rect(), *m_pixmap);
|
||||
painter.drawImage(rect(), m_image);
|
||||
}
|
||||
|
||||
void DrawingCanvasWidget::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
QWidget::mousePressEvent(event);
|
||||
|
||||
auto zoomdPos = event->pos() / m_zoom;
|
||||
auto left = event->button() == Qt::LeftButton;
|
||||
|
||||
switch (event->button())
|
||||
{
|
||||
case Qt::LeftButton:
|
||||
case Qt::RightButton:
|
||||
{
|
||||
m_currentlyDrawing = event->button() == Qt::LeftButton;
|
||||
|
||||
setMouseTracking(true);
|
||||
|
||||
setPixel(event->pos() / m_scale, event->button() == Qt::LeftButton);
|
||||
{
|
||||
switch (m_mode)
|
||||
{
|
||||
case Mode::Draw:
|
||||
m_currentlyDrawing = left;
|
||||
setPixel(zoomdPos, left);
|
||||
break;
|
||||
case Mode::Spray:
|
||||
break;
|
||||
case Mode::Erase:
|
||||
break;
|
||||
case Mode::Pick:
|
||||
break;
|
||||
case Mode::Line:
|
||||
break;
|
||||
case Mode::Polygon:
|
||||
break;
|
||||
case Mode::Rectangle:
|
||||
break;
|
||||
case Mode::Ellipse:
|
||||
break;
|
||||
case Mode::RoundedRectangle:
|
||||
break;
|
||||
case Mode::SelectRegion:
|
||||
break;
|
||||
case Mode::SelectWand:
|
||||
break;
|
||||
case Mode::SelectSpray:
|
||||
break;
|
||||
case Mode::Text:
|
||||
break;
|
||||
case Mode::Fill:
|
||||
floodFill(zoomdPos, left);
|
||||
break;
|
||||
case Mode::Replace:
|
||||
break;
|
||||
};
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -100,8 +150,6 @@ void DrawingCanvasWidget::mouseReleaseEvent(QMouseEvent *event)
|
||||
{
|
||||
m_currentlyDrawing = std::nullopt;
|
||||
|
||||
setMouseTracking(false);
|
||||
|
||||
break;
|
||||
}
|
||||
default:;
|
||||
@@ -112,15 +160,57 @@ void DrawingCanvasWidget::mouseMoveEvent(QMouseEvent *event)
|
||||
{
|
||||
QWidget::mouseMoveEvent(event);
|
||||
|
||||
auto zoomdPos = event->pos() / m_zoom;
|
||||
emit cursorMoved(zoomdPos);
|
||||
|
||||
if (m_currentlyDrawing)
|
||||
setPixel(event->pos() / m_scale, *m_currentlyDrawing);
|
||||
setPixel(zoomdPos, *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);
|
||||
if (!m_image.rect().contains(pos))
|
||||
return;
|
||||
|
||||
m_image.setPixelColor(pos.x(), pos.y(), leftColor ? m_leftButtonColor : m_rightButtonColor);
|
||||
|
||||
repaint();
|
||||
emit changed();
|
||||
}
|
||||
|
||||
void DrawingCanvasWidget::floodFill(QPoint pos, bool leftColor)
|
||||
{
|
||||
if (!m_image.rect().contains(pos))
|
||||
return;
|
||||
|
||||
const QColor targetColor = m_image.pixelColor(pos);
|
||||
const QColor replacementColor = leftColor ? m_leftButtonColor : m_rightButtonColor;
|
||||
|
||||
if (targetColor == replacementColor)
|
||||
return;
|
||||
|
||||
QQueue<QPoint> queue;
|
||||
queue.enqueue(pos);
|
||||
|
||||
while (!queue.isEmpty())
|
||||
{
|
||||
QPoint p = queue.dequeue();
|
||||
|
||||
if (m_image.pixelColor(p) != targetColor)
|
||||
continue;
|
||||
|
||||
m_image.setPixelColor(p, replacementColor);
|
||||
|
||||
if (p.x() > 0)
|
||||
queue.enqueue(QPoint(p.x() - 1, p.y()));
|
||||
if (p.x() < m_image.width() - 1)
|
||||
queue.enqueue(QPoint(p.x() + 1, p.y()));
|
||||
if (p.y() > 0)
|
||||
queue.enqueue(QPoint(p.x(), p.y() - 1));
|
||||
if (p.y() < m_image.height() - 1)
|
||||
queue.enqueue(QPoint(p.x(), p.y() + 1));
|
||||
}
|
||||
|
||||
repaint();
|
||||
emit changed();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user