12 setMouseTracking(
true);
23 if (!loadedImage.load(fileName))
34 if (!loadedImage.load(fileName))
73 return _maskTransparentColor;
83 return _backgroundImage;
88 QImage i = _maskImage;
89 for (
int x = 0; x < i.width(); ++x)
91 for (
int y = 0; y < i.height(); ++y)
93 QColor color {i.pixel(x, y)};
94 if (color.alpha() == 0)
96 color = _maskTransparentColor;
102 i.setPixel(x, y, color.rgba());
110 return _backgroundImage.size();
120 _penColor = newColor;
121 _penColor.setAlpha(255);
127 _penColor.setGreen(g);
128 _penColor.setBlue(b);
129 _penColor.setAlpha(255);
134 _penColor.setRedF(r);
135 _penColor.setGreenF(g);
136 _penColor.setBlueF(b);
137 _penColor.setAlpha(255);
143 _maskTransparentColor = newColor;
144 _maskTransparentColor.setAlpha(255);
149 _maskTransparentColor.setRed(r);
150 _maskTransparentColor.setGreen(g);
151 _maskTransparentColor.setBlue(b);
152 _maskTransparentColor.setAlpha(255);
157 _maskTransparentColor.setRedF(r);
158 _maskTransparentColor.setGreenF(g);
159 _maskTransparentColor.setBlueF(b);
160 _maskTransparentColor.setAlpha(255);
165 _maskImage.fill(qRgba(0, 0, 0, 0));
186 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
189 resizeImage(&_maskImage, size, _maskTransparentColor);
200 _maskImage = i.copy();
207 _backgroundImage = i.copy();
209 _maskModified =
true;
215 if (event->button() == Qt::LeftButton)
217 _lastPoint =
event->pos();
218 drawOnMask(_lastPoint);
225 if ((event->buttons() & Qt::LeftButton) && _drawing)
227 drawOnMask(event->pos());
229 else if (_drawPenCircle)
231 int rad = (_penWidth / 2) + 2;
232 QPoint border{rad, rad};
233 update(QRect(event->pos() - border, event->pos() + border));
239 if (event->button() == Qt::LeftButton && _drawing)
241 drawOnMask(event->pos());
248 QPainter painter(
this);
249 QRect dirtyRect =
event->rect();
250 painter.drawImage(dirtyRect, _backgroundImage, dirtyRect);
251 painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
254 x <
std::min(dirtyRect.x() + dirtyRect.width(), _maskImage.width());
260 y <
std::min(dirtyRect.y() + dirtyRect.height(), _maskImage.height()) ;
264 QColor clr {_maskImage.pixel(x, y)};
266 clr.red() == _maskTransparentColor.red() &&
267 clr.green() == _maskTransparentColor.green() &&
268 clr.blue() == _maskTransparentColor.blue()
271 clr = QColor(0, 0, 0, 0);
273 else if (clr.alpha() != 0)
275 clr.setAlpha(_maskAlpha);
277 _maskImage.setPixel(x, y, clr.rgba());
280 painter.drawImage(dirtyRect, _maskImage, dirtyRect);
283 _maskModified =
false;
293 const auto pos = mapFromGlobal(QCursor::pos());
294 if (pos.x() >= 0 && pos.x() < width() && pos.y() >= 0 && pos.y() < height())
296 painter.setBrush(Qt::NoBrush);
301 painter.drawEllipse(QPointF(pos), _penWidth / 2.0, _penWidth / 2.0);
307 painter.drawEllipse(QPointF(pos), _penWidth / 2.0 - 2, _penWidth / 2.0 - 2);
313 void ImageMaskEditor::drawOnMask(
const QPoint& endPoint)
315 QPainter painter(&_maskImage);
316 if (_lastPoint == endPoint)
320 painter.setPen(Qt::NoPen);
321 painter.setBrush(_penColor);
322 const auto w = _penWidth / 2.0;
323 painter.drawEllipse(QPointF(_lastPoint), w, w);
327 painter.setPen(_penColor);
328 painter.drawPoint(_lastPoint);
333 painter.setPen(QPen(_penColor, _penWidth, Qt::SolidLine, Qt::RoundCap,
335 painter.drawLine(_lastPoint, endPoint);
338 int rad = (_penWidth / 2) + 2;
339 update(QRect(_lastPoint, endPoint).normalized()
340 .adjusted(-rad, -rad, +rad, +rad));
341 _lastPoint = endPoint;
342 _maskModified =
true;
347 if (image->size() == newSize)
352 QImage newImage(newSize, QImage::Format_ARGB32);
353 newImage.fill(fillcolor);
354 QPainter painter(&newImage);
355 painter.drawImage(QPoint(0, 0), *image);