Add rotation and scale to overlay API

This commit is contained in:
GriffinR 2022-10-11 17:21:44 -04:00 committed by Marcus Huderle
parent 67bec313a5
commit ad5eea2293
4 changed files with 140 additions and 30 deletions

View file

@ -44,6 +44,17 @@ public:
Q_INVOKABLE int getOpacity(int layer = 0);
Q_INVOKABLE void setOpacity(int opacity, int layer);
Q_INVOKABLE void setOpacity(int opacity);
Q_INVOKABLE int getHorizontalScale(int layer);
Q_INVOKABLE int getVerticalScale(int layer);
Q_INVOKABLE void setHorizontalScale(int scale, int layer);
Q_INVOKABLE void setHorizontalScale(int scale);
Q_INVOKABLE void setVerticalScale(int scale, int layer);
Q_INVOKABLE void setVerticalScale(int scale);
Q_INVOKABLE int getRotation(int layer);
Q_INVOKABLE void setRotation(int rotation, int layer);
Q_INVOKABLE void setRotation(int rotation);
Q_INVOKABLE void rotate(int degrees, int layer);
Q_INVOKABLE void rotate(int degrees);
Q_INVOKABLE void addText(QString text, int x, int y, QString color = "#000000", int fontSize = 12, int layer = 0);
Q_INVOKABLE void addRect(int x, int y, int width, int height, QString borderColor = "#000000", QString fillColor = "transparent", int rounding = 0, int layer = 0);
Q_INVOKABLE void addPath(QList<int> x, QList<int> y, QString borderColor = "#000000", QString fillColor = "transparent", int layer = 0);

View file

@ -11,7 +11,7 @@ class OverlayItem {
public:
OverlayItem() {}
virtual ~OverlayItem() {};
virtual void render(QPainter *, int, int) {};
virtual void render(QPainter *) {};
};
class OverlayText : public OverlayItem {
@ -25,7 +25,7 @@ public:
this->fontSize = fontSize;
}
~OverlayText() {}
virtual void render(QPainter *painter, int x, int y);
virtual void render(QPainter *painter);
private:
const QStaticText text;
int x;
@ -37,17 +37,13 @@ private:
class OverlayPath : public OverlayItem {
public:
OverlayPath(QPainterPath path, QColor borderColor, QColor fillColor) {
this->prevX = 0;
this->prevY = 0;
this->path = path;
this->borderColor = borderColor;
this->fillColor = fillColor;
}
~OverlayPath() {}
virtual void render(QPainter *painter, int x, int y);
virtual void render(QPainter *painter);
private:
int prevX;
int prevY;
QPainterPath path;
QColor borderColor;
QColor fillColor;
@ -61,7 +57,7 @@ public:
this->image = image;
}
~OverlayImage() {}
virtual void render(QPainter *painter, int x, int y);
virtual void render(QPainter *painter);
private:
int x;
int y;
@ -74,6 +70,9 @@ public:
Overlay() {
this->x = 0;
this->y = 0;
this->rotation = 0;
this->hScale = 1.0;
this->vScale = 1.0;
this->hidden = false;
this->opacity = 1.0;
this->clippingRect = nullptr;
@ -89,6 +88,13 @@ public:
int getY();
void setX(int x);
void setY(int y);
int getHScale();
int getVScale();
void setHScale(int scale);
void setVScale(int scale);
int getRotation();
void setRotation(int rotation);
void rotate(int degrees);
void setClippingRect(QRectF rect);
void clearClippingRect();
void setPosition(int x, int y);
@ -100,12 +106,15 @@ public:
bool addRect(int x, int y, int width, int height, QString borderColorStr, QString fillColorStr, int rounding);
bool addImage(int x, int y, QString filepath, bool useCache = true, int width = -1, int height = -1, int xOffset = 0, int yOffset = 0, qreal hScale = 1, qreal vScale = 1, QList<QRgb> palette = QList<QRgb>(), bool setTransparency = false);
bool addImage(int x, int y, QImage image);
bool addPath(QList<int> x, QList<int> y, QString borderColorStr, QString fillColorStr);
bool addPath(QList<int> xCoords, QList<int> yCoords, QString borderColorStr, QString fillColorStr);
private:
QColor getColor(QString colorStr);
QList<OverlayItem*> items;
int x;
int y;
int rotation;
qreal hScale;
qreal vScale;
bool hidden;
qreal opacity;
QRectF *clippingRect;

View file

@ -147,6 +147,66 @@ void MapView::setOpacity(int opacity) {
this->scene()->update();
}
int MapView::getHorizontalScale(int layer) {
return this->getOverlay(layer)->getHScale();
}
int MapView::getVerticalScale(int layer) {
return this->getOverlay(layer)->getVScale();
}
void MapView::setHorizontalScale(int scale, int layer) {
this->getOverlay(layer)->setHScale(scale);
this->scene()->update();
}
// Overload. No layer provided, set horizontal scale of all layers
void MapView::setHorizontalScale(int scale) {
foreach (Overlay * layer, this->overlayMap)
layer->setHScale(scale);
this->scene()->update();
}
void MapView::setVerticalScale(int scale, int layer) {
this->getOverlay(layer)->setVScale(scale);
this->scene()->update();
}
// Overload. No layer provided, set vertical scale of all layers
void MapView::setVerticalScale(int scale) {
foreach (Overlay * layer, this->overlayMap)
layer->setVScale(scale);
this->scene()->update();
}
int MapView::getRotation(int layer) {
return this->getOverlay(layer)->getRotation();
}
void MapView::setRotation(int rotation, int layer) {
this->getOverlay(layer)->setRotation(rotation);
this->scene()->update();
}
// Overload. No layer provided, set rotation of all layers
void MapView::setRotation(int rotation) {
foreach (Overlay * layer, this->overlayMap)
layer->setRotation(rotation);
this->scene()->update();
}
void MapView::rotate(int degrees, int layer) {
this->getOverlay(layer)->rotate(degrees);
this->scene()->update();
}
// Overload. No layer provided, rotate all layers
void MapView::rotate(int degrees) {
foreach (Overlay * layer, this->overlayMap)
layer->rotate(degrees);
this->scene()->update();
}
void MapView::addText(QString text, int x, int y, QString color, int fontSize, int layer) {
this->getOverlay(layer)->addText(text, x, y, color, fontSize);
this->scene()->update();

View file

@ -2,47 +2,49 @@
#include "scripting.h"
#include "log.h"
void OverlayText::render(QPainter *painter, int x, int y) {
void OverlayText::render(QPainter *painter) {
QFont font = painter->font();
font.setPixelSize(this->fontSize);
painter->setFont(font);
painter->setPen(this->color);
painter->drawStaticText(this->x + x, this->y + y, this->text);
painter->drawStaticText(this->x, this->y, this->text);
}
void OverlayPath::render(QPainter *painter, int x, int y) {
if (x != this->prevX || y != this->prevY) {
// Overlay has moved since the path was last drawn
path.translate(x - prevX, y - prevY);
}
this->prevX = x;
this->prevY = y;
void OverlayPath::render(QPainter *painter) {
painter->setPen(this->borderColor);
painter->drawPath(this->path);
painter->fillPath(this->path, this->fillColor);
}
void OverlayImage::render(QPainter *painter, int x, int y) {
painter->drawImage(this->x + x, this->y + y, this->image);
void OverlayImage::render(QPainter *painter) {
painter->drawImage(this->x, this->y, this->image);
}
void Overlay::renderItems(QPainter *painter) {
if (this->hidden) return;
painter->save();
QTransform transform = painter->transform();
transform.translate(this->x, this->y);
transform.rotate(this->rotation);
transform.scale(this->hScale, this->vScale);
painter->setTransform(transform);
/*painter->translate(this->x, this->y);
painter->rotate(this->rotation);
painter->scale(this->hScale, this->vScale);*/
if (this->clippingRect) {
painter->setClipping(true);
painter->setClipRect(*this->clippingRect);
}
qreal oldOpacity = painter->opacity();
painter->setOpacity(this->opacity);
for (auto item : this->items)
item->render(painter, this->x, this->y);
painter->setOpacity(oldOpacity);
item->render(painter);
if (this->clippingRect) {
painter->setClipping(false);
}
painter->restore();
}
void Overlay::clearItems() {
@ -92,6 +94,34 @@ void Overlay::setY(int y) {
this->y = y;
}
int Overlay::getHScale() {
return this->hScale;
}
int Overlay::getVScale() {
return this->vScale;
}
void Overlay::setHScale(int scale) {
this->hScale = scale;
}
void Overlay::setVScale(int scale) {
this->vScale = scale;
}
int Overlay::getRotation() {
return this->rotation;
}
void Overlay::setRotation(int rotation) {
this->rotation = rotation;
}
void Overlay::rotate(int degrees) {
this->rotation += degrees;
}
void Overlay::setClippingRect(QRectF rect) {
if (this->clippingRect) {
delete this->clippingRect;
@ -143,18 +173,18 @@ bool Overlay::addRect(int x, int y, int width, int height, QString borderColorSt
return true;
}
bool Overlay::addPath(QList<int> x, QList<int> y, QString borderColorStr, QString fillColorStr) {
int numPoints = qMin(x.length(), y.length());
bool Overlay::addPath(QList<int> xCoords, QList<int> yCoords, QString borderColorStr, QString fillColorStr) {
int numPoints = qMin(xCoords.length(), yCoords.length());
if (numPoints < 2) {
logError("Overlay path must have at least two points.");
return false;
}
QPainterPath path;
path.moveTo(x.at(0), y.at(0));
path.moveTo(xCoords.at(0), yCoords.at(0));
for (int i = 1; i < numPoints; i++)
path.lineTo(x.at(i), y.at(i));
path.lineTo(xCoords.at(i), yCoords.at(i));
this->items.append(new OverlayPath(path, getColor(borderColorStr), getColor(fillColorStr)));
return true;