Add rotation and scale to overlay API
This commit is contained in:
parent
67bec313a5
commit
ad5eea2293
4 changed files with 140 additions and 30 deletions
|
@ -44,6 +44,17 @@ public:
|
||||||
Q_INVOKABLE int getOpacity(int layer = 0);
|
Q_INVOKABLE int getOpacity(int layer = 0);
|
||||||
Q_INVOKABLE void setOpacity(int opacity, int layer);
|
Q_INVOKABLE void setOpacity(int opacity, int layer);
|
||||||
Q_INVOKABLE void setOpacity(int opacity);
|
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 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 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);
|
Q_INVOKABLE void addPath(QList<int> x, QList<int> y, QString borderColor = "#000000", QString fillColor = "transparent", int layer = 0);
|
||||||
|
|
|
@ -11,7 +11,7 @@ class OverlayItem {
|
||||||
public:
|
public:
|
||||||
OverlayItem() {}
|
OverlayItem() {}
|
||||||
virtual ~OverlayItem() {};
|
virtual ~OverlayItem() {};
|
||||||
virtual void render(QPainter *, int, int) {};
|
virtual void render(QPainter *) {};
|
||||||
};
|
};
|
||||||
|
|
||||||
class OverlayText : public OverlayItem {
|
class OverlayText : public OverlayItem {
|
||||||
|
@ -25,7 +25,7 @@ public:
|
||||||
this->fontSize = fontSize;
|
this->fontSize = fontSize;
|
||||||
}
|
}
|
||||||
~OverlayText() {}
|
~OverlayText() {}
|
||||||
virtual void render(QPainter *painter, int x, int y);
|
virtual void render(QPainter *painter);
|
||||||
private:
|
private:
|
||||||
const QStaticText text;
|
const QStaticText text;
|
||||||
int x;
|
int x;
|
||||||
|
@ -37,17 +37,13 @@ private:
|
||||||
class OverlayPath : public OverlayItem {
|
class OverlayPath : public OverlayItem {
|
||||||
public:
|
public:
|
||||||
OverlayPath(QPainterPath path, QColor borderColor, QColor fillColor) {
|
OverlayPath(QPainterPath path, QColor borderColor, QColor fillColor) {
|
||||||
this->prevX = 0;
|
|
||||||
this->prevY = 0;
|
|
||||||
this->path = path;
|
this->path = path;
|
||||||
this->borderColor = borderColor;
|
this->borderColor = borderColor;
|
||||||
this->fillColor = fillColor;
|
this->fillColor = fillColor;
|
||||||
}
|
}
|
||||||
~OverlayPath() {}
|
~OverlayPath() {}
|
||||||
virtual void render(QPainter *painter, int x, int y);
|
virtual void render(QPainter *painter);
|
||||||
private:
|
private:
|
||||||
int prevX;
|
|
||||||
int prevY;
|
|
||||||
QPainterPath path;
|
QPainterPath path;
|
||||||
QColor borderColor;
|
QColor borderColor;
|
||||||
QColor fillColor;
|
QColor fillColor;
|
||||||
|
@ -61,7 +57,7 @@ public:
|
||||||
this->image = image;
|
this->image = image;
|
||||||
}
|
}
|
||||||
~OverlayImage() {}
|
~OverlayImage() {}
|
||||||
virtual void render(QPainter *painter, int x, int y);
|
virtual void render(QPainter *painter);
|
||||||
private:
|
private:
|
||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
|
@ -74,6 +70,9 @@ public:
|
||||||
Overlay() {
|
Overlay() {
|
||||||
this->x = 0;
|
this->x = 0;
|
||||||
this->y = 0;
|
this->y = 0;
|
||||||
|
this->rotation = 0;
|
||||||
|
this->hScale = 1.0;
|
||||||
|
this->vScale = 1.0;
|
||||||
this->hidden = false;
|
this->hidden = false;
|
||||||
this->opacity = 1.0;
|
this->opacity = 1.0;
|
||||||
this->clippingRect = nullptr;
|
this->clippingRect = nullptr;
|
||||||
|
@ -89,6 +88,13 @@ public:
|
||||||
int getY();
|
int getY();
|
||||||
void setX(int x);
|
void setX(int x);
|
||||||
void setY(int y);
|
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 setClippingRect(QRectF rect);
|
||||||
void clearClippingRect();
|
void clearClippingRect();
|
||||||
void setPosition(int x, int y);
|
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 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, 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 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:
|
private:
|
||||||
QColor getColor(QString colorStr);
|
QColor getColor(QString colorStr);
|
||||||
QList<OverlayItem*> items;
|
QList<OverlayItem*> items;
|
||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
|
int rotation;
|
||||||
|
qreal hScale;
|
||||||
|
qreal vScale;
|
||||||
bool hidden;
|
bool hidden;
|
||||||
qreal opacity;
|
qreal opacity;
|
||||||
QRectF *clippingRect;
|
QRectF *clippingRect;
|
||||||
|
|
|
@ -147,6 +147,66 @@ void MapView::setOpacity(int opacity) {
|
||||||
this->scene()->update();
|
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) {
|
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->getOverlay(layer)->addText(text, x, y, color, fontSize);
|
||||||
this->scene()->update();
|
this->scene()->update();
|
||||||
|
|
|
@ -2,47 +2,49 @@
|
||||||
#include "scripting.h"
|
#include "scripting.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
void OverlayText::render(QPainter *painter, int x, int y) {
|
void OverlayText::render(QPainter *painter) {
|
||||||
QFont font = painter->font();
|
QFont font = painter->font();
|
||||||
font.setPixelSize(this->fontSize);
|
font.setPixelSize(this->fontSize);
|
||||||
painter->setFont(font);
|
painter->setFont(font);
|
||||||
painter->setPen(this->color);
|
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) {
|
void OverlayPath::render(QPainter *painter) {
|
||||||
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;
|
|
||||||
painter->setPen(this->borderColor);
|
painter->setPen(this->borderColor);
|
||||||
painter->drawPath(this->path);
|
painter->drawPath(this->path);
|
||||||
painter->fillPath(this->path, this->fillColor);
|
painter->fillPath(this->path, this->fillColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OverlayImage::render(QPainter *painter, int x, int y) {
|
void OverlayImage::render(QPainter *painter) {
|
||||||
painter->drawImage(this->x + x, this->y + y, this->image);
|
painter->drawImage(this->x, this->y, this->image);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Overlay::renderItems(QPainter *painter) {
|
void Overlay::renderItems(QPainter *painter) {
|
||||||
if (this->hidden) return;
|
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) {
|
if (this->clippingRect) {
|
||||||
painter->setClipping(true);
|
painter->setClipping(true);
|
||||||
painter->setClipRect(*this->clippingRect);
|
painter->setClipRect(*this->clippingRect);
|
||||||
}
|
}
|
||||||
|
|
||||||
qreal oldOpacity = painter->opacity();
|
|
||||||
painter->setOpacity(this->opacity);
|
painter->setOpacity(this->opacity);
|
||||||
for (auto item : this->items)
|
for (auto item : this->items)
|
||||||
item->render(painter, this->x, this->y);
|
item->render(painter);
|
||||||
painter->setOpacity(oldOpacity);
|
|
||||||
|
|
||||||
if (this->clippingRect) {
|
painter->restore();
|
||||||
painter->setClipping(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Overlay::clearItems() {
|
void Overlay::clearItems() {
|
||||||
|
@ -92,6 +94,34 @@ void Overlay::setY(int y) {
|
||||||
this->y = 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) {
|
void Overlay::setClippingRect(QRectF rect) {
|
||||||
if (this->clippingRect) {
|
if (this->clippingRect) {
|
||||||
delete this->clippingRect;
|
delete this->clippingRect;
|
||||||
|
@ -143,18 +173,18 @@ bool Overlay::addRect(int x, int y, int width, int height, QString borderColorSt
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Overlay::addPath(QList<int> x, QList<int> y, QString borderColorStr, QString fillColorStr) {
|
bool Overlay::addPath(QList<int> xCoords, QList<int> yCoords, QString borderColorStr, QString fillColorStr) {
|
||||||
int numPoints = qMin(x.length(), y.length());
|
int numPoints = qMin(xCoords.length(), yCoords.length());
|
||||||
if (numPoints < 2) {
|
if (numPoints < 2) {
|
||||||
logError("Overlay path must have at least two points.");
|
logError("Overlay path must have at least two points.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QPainterPath path;
|
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++)
|
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)));
|
this->items.append(new OverlayPath(path, getColor(borderColorStr), getColor(fillColorStr)));
|
||||||
return true;
|
return true;
|
||||||
|
|
Loading…
Reference in a new issue