Added status labels in image editor and implemented flood fill

This commit is contained in:
2025-11-29 19:36:21 +01:00
parent fc25dbf528
commit 103d780e8c
9 changed files with 284 additions and 83 deletions
+2 -2
View File
@@ -35,7 +35,7 @@ EditSpriteDialog::EditSpriteDialog(const std::vector<QPixmap> &pixmaps, const QS
connect(m_ui->actionNew, &QAction::triggered, this, &EditSpriteDialog::newSprite);
connect(m_ui->listView, &QListView::doubleClicked, this, &EditSpriteDialog::doubleClicked);
connect(m_ui->listView, &QAbstractItemView::activated, this, &EditSpriteDialog::activated);
}
EditSpriteDialog::~EditSpriteDialog() = default;
@@ -99,7 +99,7 @@ void EditSpriteDialog::newSprite()
}
}
void EditSpriteDialog::doubleClicked(const QModelIndex &index)
void EditSpriteDialog::activated(const QModelIndex &index)
{
if (!index.isValid())
{
+1 -1
View File
@@ -25,7 +25,7 @@ public:
private slots:
void newSprite();
void doubleClicked(const QModelIndex &index);
void activated(const QModelIndex &index);
void changed();
+94 -20
View File
@@ -10,8 +10,11 @@
ImageEditorDialog::ImageEditorDialog(const QPixmap &pixmap, const QString &title, QWidget *parent) :
QDialog{parent},
m_ui{std::make_unique<Ui::ImageEditorDialog>()},
m_pixmap{pixmap},
m_title{title}
m_title{title},
m_posLabel{*new QLabel(tr("(%0,%1)").arg(0).arg(0))},
m_zoomLabel{*new QLabel},
m_sizeLabel{*new QLabel},
m_memoryLabel{*new QLabel}
{
m_ui->setupUi(this);
@@ -54,21 +57,25 @@ ImageEditorDialog::ImageEditorDialog(const QPixmap &pixmap, const QString &title
{
auto group = new QButtonGroup{this};
group->setExclusive(true);
group->addButton(m_ui->toolButtonDraw);
group->addButton(m_ui->toolButtonSpray);
group->addButton(m_ui->toolButtonErase);
group->addButton(m_ui->toolButtonPick);
group->addButton(m_ui->toolButtonLine);
group->addButton(m_ui->toolButtonPolygon);
group->addButton(m_ui->toolButtonRectangle);
group->addButton(m_ui->toolButtonEllipse);
group->addButton(m_ui->toolButtonRoundedRectangle);
group->addButton(m_ui->toolButtonSelectRegion);
group->addButton(m_ui->toolButtonSelectWand);
group->addButton(m_ui->toolButtonSelectSpray);
group->addButton(m_ui->toolButtonText);
group->addButton(m_ui->toolButtonFill);
group->addButton(m_ui->toolButtonReplace);
group->addButton(m_ui->toolButtonDraw, std::to_underlying(DrawingCanvasWidget::Mode::Draw));
group->addButton(m_ui->toolButtonSpray, std::to_underlying(DrawingCanvasWidget::Mode::Spray));
group->addButton(m_ui->toolButtonErase, std::to_underlying(DrawingCanvasWidget::Mode::Erase));
group->addButton(m_ui->toolButtonPick, std::to_underlying(DrawingCanvasWidget::Mode::Pick));
group->addButton(m_ui->toolButtonLine, std::to_underlying(DrawingCanvasWidget::Mode::Line));
group->addButton(m_ui->toolButtonPolygon, std::to_underlying(DrawingCanvasWidget::Mode::Polygon));
group->addButton(m_ui->toolButtonRectangle, std::to_underlying(DrawingCanvasWidget::Mode::Rectangle));
group->addButton(m_ui->toolButtonEllipse, std::to_underlying(DrawingCanvasWidget::Mode::Ellipse));
group->addButton(m_ui->toolButtonRoundedRectangle, std::to_underlying(DrawingCanvasWidget::Mode::RoundedRectangle));
group->addButton(m_ui->toolButtonSelectRegion, std::to_underlying(DrawingCanvasWidget::Mode::SelectRegion));
group->addButton(m_ui->toolButtonSelectWand, std::to_underlying(DrawingCanvasWidget::Mode::SelectWand));
group->addButton(m_ui->toolButtonSelectSpray, std::to_underlying(DrawingCanvasWidget::Mode::SelectSpray));
group->addButton(m_ui->toolButtonText, std::to_underlying(DrawingCanvasWidget::Mode::Text));
group->addButton(m_ui->toolButtonFill, std::to_underlying(DrawingCanvasWidget::Mode::Fill));
group->addButton(m_ui->toolButtonReplace, std::to_underlying(DrawingCanvasWidget::Mode::Replace));
if (auto button = group->button(std::to_underlying(m_ui->canvas->mode())))
button->click();
connect(group, &QButtonGroup::idClicked,
m_ui->canvas, [canvas=m_ui->canvas](int id){ canvas->setMode(DrawingCanvasWidget::Mode(id)); });
}
{
@@ -96,16 +103,39 @@ ImageEditorDialog::ImageEditorDialog(const QPixmap &pixmap, const QString &title
group->addButton(m_ui->toolButtonAlignRight);
}
m_ui->toolButtonDraw->click();
updateLeftButtonColor(m_ui->canvas->leftButtonColor());
updateRightButtonColor(m_ui->canvas->rightButtonColor());
m_ui->canvas->setPixmap(m_pixmap);
m_ui->canvas->setPixmap(pixmap);
connect(m_ui->canvas, &DrawingCanvasWidget::cursorMoved,
this, &ImageEditorDialog::cursorMoved);
connect(m_ui->canvas, &DrawingCanvasWidget::modeChanged,
this, &ImageEditorDialog::modeChanged);
modeChanged(m_ui->canvas->mode());
connect(m_ui->canvas, &DrawingCanvasWidget::zoomChanged,
this, &ImageEditorDialog::zoomChanged);
zoomChanged(m_ui->canvas->zoom());
updateSize(pixmap);
for (QLabel *label : { &m_posLabel, &m_zoomLabel, &m_sizeLabel, &m_memoryLabel })
{
label->setFrameShape(QFrame::Panel);
label->setFrameShadow(QFrame::Sunken);
m_ui->statusBar->addPermanentWidget(label);
}
}
ImageEditorDialog::~ImageEditorDialog() = default;
QPixmap ImageEditorDialog::pixmap() const
{
return m_ui->canvas->pixmap();
}
void ImageEditorDialog::accept()
{
// TODO
@@ -191,6 +221,44 @@ void ImageEditorDialog::updateRightButtonColor(const QColor &rightButtonColor)
frame->setPalette(palette);
}
void ImageEditorDialog::modeChanged(DrawingCanvasWidget::Mode mode)
{
QString msg;
switch (mode)
{
case DrawingCanvasWidget::Mode::Draw: msg = tr("Paint with the mouse. <Shift> for hor/vert"); break;
case DrawingCanvasWidget::Mode::Spray: msg = tr("Spray with the mouse. <Shift> for hor/vert"); break;
case DrawingCanvasWidget::Mode::Erase: msg = tr("Erase with the mouse (based on transparency)"); break;
case DrawingCanvasWidget::Mode::Pick: msg = tr("Pick a color with the mouse"); break;
case DrawingCanvasWidget::Mode::Line: msg = tr("Draw a line. <Shift> for diagonal"); break;
case DrawingCanvasWidget::Mode::Polygon: msg = tr("Draw a polygon. <Esc> to end"); break;
case DrawingCanvasWidget::Mode::Rectangle: msg = tr("Draw a rectangle. <Shift> for square"); break;
case DrawingCanvasWidget::Mode::Ellipse: msg = tr("Draw an ellipse. <Shift> for circle"); break;
case DrawingCanvasWidget::Mode::RoundedRectangle: msg = tr("Draw a rounded rectangle. <Shift> for square"); break;
case DrawingCanvasWidget::Mode::SelectRegion: msg = tr("Select a region with the mouse"); break;
case DrawingCanvasWidget::Mode::SelectWand: msg = tr("Click to select with magic wand"); break;
case DrawingCanvasWidget::Mode::SelectSpray: msg = tr("Spray selection with the mouse"); break;
case DrawingCanvasWidget::Mode::Text: msg = tr("Click the mouse to start a new text"); break;
case DrawingCanvasWidget::Mode::Fill: msg = tr("Flood filling a region"); break;
case DrawingCanvasWidget::Mode::Replace: msg = tr("Click on a pixel whose color to change"); break;
};
if (msg.isNull())
qWarning() << "unknown mode" << mode;
m_ui->statusBar->showMessage(msg);
}
void ImageEditorDialog::cursorMoved(const QPoint &pos)
{
m_posLabel.setText(tr("(%0,%1)").arg(pos.x()).arg(pos.y()));
}
void ImageEditorDialog::zoomChanged(float zoom)
{
m_zoomLabel.setText(tr("Zoom: %0%").arg(zoom * 100.f, 0, 'f', 0));
}
void ImageEditorDialog::updateTitle()
{
setWindowTitle(tr("%0%1")
@@ -198,3 +266,9 @@ void ImageEditorDialog::updateTitle()
.arg(m_unsavedChanges ? tr("*") : QString{})
);
}
void ImageEditorDialog::updateSize(const QPixmap &pixmap)
{
m_sizeLabel.setText(tr("Size: %0 x %1").arg(pixmap.width()).arg(pixmap.height()));
m_memoryLabel.setText(tr("Memory: %0 KB").arg(pixmap.width() * pixmap.height() * 4 / 1000));
}
+13 -2
View File
@@ -6,6 +6,9 @@
#include <memory>
#include "widgets/drawingcanvaswidget.h"
class QLabel;
namespace Ui { class ImageEditorDialog; }
class ImageEditorDialog : public QDialog
@@ -16,7 +19,7 @@ public:
explicit ImageEditorDialog(const QPixmap &pixmap, const QString &title, QWidget *parent = nullptr);
~ImageEditorDialog();
const QPixmap &pixmap() const { return m_pixmap; }
QPixmap pixmap() const;
void accept() override;
void reject() override;
@@ -28,14 +31,22 @@ private slots:
void selectRightButtonColor();
void updateLeftButtonColor(const QColor &leftButtonColor);
void updateRightButtonColor(const QColor &rightButtonColor);
void modeChanged(DrawingCanvasWidget::Mode mode);
void cursorMoved(const QPoint &pos);
void zoomChanged(float zoom);
private:
void updateTitle();
void updateSize(const QPixmap &pixmap);
const std::unique_ptr<Ui::ImageEditorDialog> m_ui;
QPixmap m_pixmap;
const QString m_title;
QLabel &m_posLabel;
QLabel &m_zoomLabel;
QLabel &m_sizeLabel;
QLabel &m_memoryLabel;
bool m_unsavedChanges{};
};
+15 -15
View File
@@ -143,8 +143,8 @@ MainWindow::MainWindow(const QString &filePath, QWidget *parent) :
connect(m_ui->treeView, &QTreeView::customContextMenuRequested,
this, &MainWindow::contextMenuRequested);
connect(m_ui->treeView, &QTreeView::doubleClicked,
this, &MainWindow::doubleClicked);
connect(m_ui->treeView, &QTreeView::activated,
this, &MainWindow::activated);
connect(m_ui->treeView->selectionModel(), &QItemSelectionModel::currentChanged,
this, &MainWindow::selectionChanged);
@@ -342,7 +342,7 @@ void MainWindow::contextMenuRequested(const QPoint &pos)
menu.exec(m_ui->treeView->viewport()->mapToGlobal(pos));
}
void MainWindow::doubleClicked(const QModelIndex &index)
void MainWindow::activated(const QModelIndex &index)
{
if (m_projectTreeModel->nodeType(index) == ProjectTreeModel::NodeType::Root)
{
@@ -354,15 +354,15 @@ void MainWindow::doubleClicked(const QModelIndex &index)
showExtensionPackages();
return;
}
if (doubleClickedFor<Sprite>(index)) return;
if (doubleClickedFor<Sound>(index)) return;
if (doubleClickedFor<Background>(index)) return;
if (doubleClickedFor<Path>(index)) return;
if (doubleClickedFor<Script>(index)) return;
if (doubleClickedFor<Font>(index)) return;
if (doubleClickedFor<TimeLine>(index)) return;
if (doubleClickedFor<Object>(index)) return;
if (doubleClickedFor<Room>(index)) return;
if (activatedFor<Sprite>(index)) return;
if (activatedFor<Sound>(index)) return;
if (activatedFor<Background>(index)) return;
if (activatedFor<Path>(index)) return;
if (activatedFor<Script>(index)) return;
if (activatedFor<Font>(index)) return;
if (activatedFor<TimeLine>(index)) return;
if (activatedFor<Object>(index)) return;
if (activatedFor<Room>(index)) return;
}
void MainWindow::selectionChanged(const QModelIndex &index)
@@ -698,7 +698,7 @@ void MainWindow::showProperties()
return;
}
doubleClicked(index);
activated(index);
}
void MainWindow::findResource()
@@ -801,7 +801,7 @@ void MainWindow::rowsInserted(const QModelIndex &parent, int first, int last)
m_ui->treeView->expand(parent);
const auto index = m_projectTreeModel->index(first, 0, parent);
m_ui->treeView->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
doubleClicked(index);
activated(index);
}
void MainWindow::rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
@@ -981,7 +981,7 @@ void MainWindow::openOrActivateWindow(QMdiSubWindow * &ptr, Targs &&...args)
}
template<typename T>
bool MainWindow::doubleClickedFor(const QModelIndex &index)
bool MainWindow::activatedFor(const QModelIndex &index)
{
if (m_projectTreeModel->nodeType(index) != ProjectTreeModel::nodeTypeFor<T>())
return false;
+2 -2
View File
@@ -33,7 +33,7 @@ protected:
private slots:
void contextMenuRequested(const QPoint &pos);
void doubleClicked(const QModelIndex &index);
void activated(const QModelIndex &index);
void selectionChanged(const QModelIndex &index);
void modelErrorOccured(const QString &message);
void changed();
@@ -86,7 +86,7 @@ private:
void openOrActivateWindow(QMdiSubWindow * &ptr, Targs &&...args);
template<typename T>
bool doubleClickedFor(const QModelIndex &index);
bool activatedFor(const QModelIndex &index);
template<typename T>
bool rowsAboutToBeRemovedFor(const QModelIndex &parent, int first, int last);
+116 -26
View File
@@ -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();
}
+37 -11
View File
@@ -8,20 +8,40 @@ class DrawingCanvasWidget : public QWidget
{
Q_OBJECT
Q_PROPERTY(float scale READ scale WRITE setScale NOTIFY scaleChanged FINAL)
Q_PROPERTY(float zoom READ zoom WRITE setScale NOTIFY zoomChanged FINAL)
Q_PROPERTY(QColor leftButtonColor READ leftButtonColor WRITE setLeftButtonColor NOTIFY leftButtonColorChanged FINAL)
Q_PROPERTY(QColor rightButtonColor READ rightButtonColor WRITE setRightButtonColor NOTIFY rightButtonColorChanged FINAL)
public:
using QWidget::QWidget;
explicit DrawingCanvasWidget(QWidget *parent = nullptr);
QPixmap *pixmap() { return m_pixmap; }
const QPixmap *pixmap() const { return m_pixmap; }
void setPixmap(QPixmap &pixmap);
enum class Mode {
Draw,
Spray,
Erase,
Pick,
Line,
Polygon,
Rectangle,
Ellipse,
RoundedRectangle,
SelectRegion,
SelectWand,
SelectSpray,
Text,
Fill,
Replace,
};
Q_ENUM(Mode)
Q_PROPERTY(Mode mode READ mode WRITE setMode NOTIFY modeChanged FINAL)
Mode mode() const { return m_mode; }
void setMode(Mode mode);
float scale() const { return m_scale; }
void setScale(float scale);
QPixmap pixmap() const;
void setPixmap(const QPixmap &pixmap);
float zoom() const { return m_zoom; }
void setScale(float zoom);
QColor leftButtonColor() const { return m_leftButtonColor; }
void setLeftButtonColor(const QColor &LeftButtonColor);
@@ -31,7 +51,10 @@ public:
signals:
void changed();
void scaleChanged(float scale);
void cursorMoved(const QPoint &pos);
void modeChanged(DrawingCanvasWidget::Mode mode);
void zoomChanged(float zoom);
void leftButtonColorChanged(const QColor &LeftButtonColor);
void rightButtonColorChanged(const QColor &rightButtonColor);
@@ -43,10 +66,13 @@ protected:
private:
void setPixel(QPoint pos, bool leftColor);
void floodFill(QPoint pos, bool leftColor);
QPixmap *m_pixmap{};
Mode m_mode{Mode::Draw};
float m_scale{1.f};
QImage m_image;
float m_zoom{1.f};
QColor m_leftButtonColor{Qt::white};
QColor m_rightButtonColor{Qt::black};
+4 -4
View File
@@ -17,13 +17,13 @@ void ZoomableScrollArea::wheelEvent(QWheelEvent *event)
if (!w)
return;
auto scale = w->scale();
auto zoom = w->zoom();
if (event->angleDelta().y() > 0)
scale *= 2;
zoom *= 2;
else
scale /= 2;
zoom /= 2;
w->setScale(scale);
w->setScale(zoom);
event->accept();
}