1#include "overlaypainter.h"
3#include "layercamera.h"
8Q_CONSTEXPR
static int LEFT_ANGLE_OFFSET = 90;
9Q_CONSTEXPR
static int RIGHT_ANGLE_OFFSET = -90;
10Q_CONSTEXPR
static int HANDLE_WIDTH = 12;
12OverlayPainter::OverlayPainter()
16void OverlayPainter::initializePainter(
QPainter& painter)
20 pen.setCosmetic(
true);
28 mCameraLayer = cameraLayer;
31void OverlayPainter::setViewTransform(
const QTransform view)
33 mViewTransform = view;
36void OverlayPainter::paint(
QPainter &painter,
const QRect& viewport)
38 if (mCameraLayer ==
nullptr) {
return; }
41 initializePainter(painter);
43 QTransform camTransform = mCameraLayer->getViewAtFrame(mOptions.nFrameIndex);
44 QRect cameraRect = mCameraLayer->getViewRect();
45 Camera* camera = mCameraLayer->getLastCameraAtFrame(mOptions.nFrameIndex, 0);
47 if (camera ==
nullptr) {
return; }
53 paintOverlayCenter(painter, camTransform, cameraRect);
57 paintOverlayThirds(painter, camTransform, cameraRect);
59 if (mOptions.bGoldenRatio)
61 paintOverlayGolden(painter, camTransform, cameraRect);
63 if (mOptions.bSafeArea)
65 paintOverlaySafeAreas(painter, *camera, camTransform, cameraRect);
69 if (mOptions.bPerspective1)
71 paintOverlayPerspectiveOnePoint(painter, mappedViewport, camTransform);
73 if (mOptions.bPerspective2 || mOptions.bPerspective3)
75 paintOverlayPerspectiveTwoPoints(painter, mappedViewport, *camera, camTransform);
77 if (mOptions.bPerspective3)
79 paintOverlayPerspectiveThreePoints(painter, mappedViewport, *camera, camTransform);
90void OverlayPainter::paintOverlayCenter(
QPainter &painter,
const QTransform& camTransform,
const QRect& camRect)
const
98 dashes << 10 << space << 10 << space << 10 << space;
99 pen.setDashPattern(dashes);
100 pen.setCosmetic(
true);
110 painter.
drawLine(
QLineF(centerTop, centerBottom).pointAt(0.4).toPoint(),
111 QLineF(centerTop, centerBottom).pointAt(0.6).toPoint());
112 painter.
drawLine(
QLineF(centerLeft, centerRight).pointAt(0.4).toPoint(),
113 QLineF(centerLeft, centerRight).pointAt(0.6).toPoint());
118void OverlayPainter::paintOverlayThirds(
QPainter &painter,
const QTransform& camTransform,
const QRect& camRect)
const
126 dashes << 10 << space << 10 << space << 10 << space;
127 pen.setDashPattern(dashes);
128 pen.setCosmetic(
true);
138 painter.
drawLine(topLine.pointAt(0.333).toPoint(), bottomLine.pointAt(0.333));
139 painter.
drawLine(topLine.pointAt(0.667).toPoint(), bottomLine.pointAt(0.667));
140 painter.
drawLine(leftLine.pointAt(0.333).toPoint(), rightLine.pointAt(0.333));
141 painter.
drawLine(leftLine.pointAt(0.667).toPoint(), rightLine.pointAt(0.667));
146void OverlayPainter::paintOverlayGolden(
QPainter &painter,
const QTransform& camTransform,
const QRect& camRect)
const
154 dashes << 10 << space << 10 << space << 10 << space;
155 pen.setDashPattern(dashes);
156 pen.setCosmetic(
true);
166 painter.
drawLine(topLine.pointAt(0.382).toPoint(), bottomLine.pointAt(0.382));
167 painter.
drawLine(topLine.pointAt(0.618).toPoint(), bottomLine.pointAt(0.618));
168 painter.
drawLine(leftLine.pointAt(0.382).toPoint(), rightLine.pointAt(0.382));
169 painter.
drawLine(leftLine.pointAt(0.618).toPoint(), rightLine.pointAt(0.618));
174void OverlayPainter::paintOverlaySafeAreas(
QPainter &painter,
const Camera& camera,
const QTransform& camTransform,
const QRect& camRect)
const
181 dashes << 10 << space << 10 << space << 10 << space;
182 pen.setDashPattern(dashes);
183 pen.setCosmetic(
true);
189 QLineF topLeftCrossLine(poly.
at(0), poly.
at(2));
190 QLineF bottomLeftCrossLine(poly.
at(3), poly.
at(1));
192 if (mOptions.bActionSafe)
194 int action = mOptions.nActionSafe;
195 painter.
drawLine(topLeftCrossLine.pointAt((action / 2.0) / 100.0).toPoint(),
196 bottomLeftCrossLine.pointAt((100 - (action / 2.0)) / 100).toPoint());
197 painter.
drawLine(topLeftCrossLine.pointAt((action / 2.0) / 100.0).toPoint(),
198 bottomLeftCrossLine.pointAt((action / 2.0) / 100).toPoint());
199 painter.
drawLine(topLeftCrossLine.pointAt((100 - (action / 2.0)) / 100.0).toPoint(),
200 bottomLeftCrossLine.pointAt((100 - (action / 2.0)) / 100).toPoint());
201 painter.
drawLine(topLeftCrossLine.pointAt((100 - (action / 2.0)) / 100.0).toPoint(),
202 bottomLeftCrossLine.pointAt((action / 2.0) / 100).toPoint());
204 if (mOptions.bShowSafeAreaHelperText)
207 QPointF topLeft = topLeftCrossLine.pointAt((action / 2.0) / 100 ).
toPoint();
215 painter.
drawText(
QPoint(), tr(
"Safe Action area %1 %").arg(action));
219 if (mOptions.bTitleSafe)
221 int title = mOptions.nTitleSafe;
222 painter.
drawLine(topLeftCrossLine.pointAt((title / 2.0) / 100.0).toPoint(),
223 bottomLeftCrossLine.pointAt((100 - (title / 2.0)) / 100).toPoint());
224 painter.
drawLine(topLeftCrossLine.pointAt((title / 2.0) / 100.0).toPoint(),
225 bottomLeftCrossLine.pointAt((title / 2.0) / 100).toPoint());
226 painter.
drawLine(topLeftCrossLine.pointAt((100 - (title / 2.0)) / 100.0).toPoint(),
227 bottomLeftCrossLine.pointAt((100 - (title / 2.0)) / 100).toPoint());
228 painter.
drawLine(topLeftCrossLine.pointAt((100 - (title / 2.0)) / 100.0).toPoint(),
229 bottomLeftCrossLine.pointAt((title / 2.0) / 100).toPoint());
231 if (mOptions.bShowSafeAreaHelperText)
233 QPointF bottomLeft = bottomLeftCrossLine.pointAt((title / 2.0) / 100);
242 painter.
drawText(
QPoint(), tr(
"Safe Title area %1 %").arg(title));
249void OverlayPainter::paintOverlayPerspectiveOnePoint(
QPainter& painter,
const QRect& viewport,
const QTransform& camTransform)
const
251 qreal degrees =
static_cast<qreal
>(mOptions.nOverlayAngle);
252 if (degrees == 7.0) { degrees = 7.5; }
255 QLineF angleLine(singlePoint.
x(), singlePoint.
y(), singlePoint.
x() + 1, singlePoint.
y());
258 for (qreal angle = 0; angle < 180; angle += degrees)
260 angleLine.setAngle(angle);
261 lines.
append(clipLine(angleLine, viewport, -qInf(), qInf()));
268 if (mOptions.bShowHandle) {
269 singlePoint = mViewTransform.
map(singlePoint);
274 painter.
drawEllipse(
QRectF(singlePoint.
x()-HANDLE_WIDTH*.5, singlePoint.
y()-HANDLE_WIDTH*.5, HANDLE_WIDTH, HANDLE_WIDTH));
280void OverlayPainter::paintOverlayPerspectiveTwoPoints(
QPainter& painter,
const QRect& viewport,
const Camera& camera,
const QTransform& camTransform)
const
282 qreal degrees =
static_cast<qreal
>(mOptions.nOverlayAngle);
283 if (degrees == 7.0) { degrees = 7.5; }
287 QLineF angleLineLeft(leftPoint.
x(), leftPoint.
y(), leftPoint.
x() + 1, leftPoint.
y());
288 QLineF angleLineRight(rightPoint.
x(), rightPoint.
y(), rightPoint.
x() + 1, rightPoint.
y());
291 for (qreal angle = 0; angle <= 180; angle += degrees)
293 angleLineLeft.setAngle(LEFT_ANGLE_OFFSET - angle + camera.rotation());
294 angleLineRight.setAngle(RIGHT_ANGLE_OFFSET - angle + camera.rotation());
295 lines.
append(clipLine(angleLineLeft, viewport, 0, qInf()));
296 lines.
append(clipLine(angleLineRight, viewport, 0, qInf()));
303 if (mOptions.bShowHandle) {
304 leftPoint = mViewTransform.
map(leftPoint);
305 rightPoint = mViewTransform.
map(rightPoint);
310 painter.
drawEllipse(
QRectF(leftPoint.
x()-HANDLE_WIDTH*.5, leftPoint.
y()-HANDLE_WIDTH*.5, HANDLE_WIDTH,HANDLE_WIDTH));
311 painter.
drawEllipse(
QRectF(rightPoint.
x()-HANDLE_WIDTH*.5, rightPoint.
y()-HANDLE_WIDTH*.5, HANDLE_WIDTH, HANDLE_WIDTH));
317void OverlayPainter::paintOverlayPerspectiveThreePoints(
QPainter& painter,
const QRect& viewport,
const Camera& camera,
const QTransform& camTransform)
const
319 qreal degrees =
static_cast<qreal
>(mOptions.nOverlayAngle);
320 if (degrees == 7.0) { degrees = 7.5; }
323 QLineF angleLine(middlePoint.
x(), middlePoint.
y(), middlePoint.
x() + 1, middlePoint.
y());
325 const int middleAngleOffset = mOptions.mLeftPerspPoint.
y() < mOptions.mMiddlePerspPoint.
y() ? 180 : 0;
327 for (qreal angle = 0; angle <= 180; angle += degrees)
329 angleLine.setAngle(middleAngleOffset - angle + camera.rotation());
330 lines.
append(clipLine(angleLine, viewport, 0, qInf()));
337 if (mOptions.bShowHandle) {
338 middlePoint = mViewTransform.
map(middlePoint);
343 painter.
drawEllipse(
QRectF(middlePoint.
x()-HANDLE_WIDTH*.5, middlePoint.
y()-HANDLE_WIDTH*.5, HANDLE_WIDTH,HANDLE_WIDTH));
349void OverlayPainter::paintGrid(
QPainter& painter)
const
352 int gridSizeW = mOptions.nGridSizeW;
353 int gridSizeH = mOptions.nGridSizeH;
358 int left = round100(boundingRect.
left(), gridSizeW) - gridSizeW;
359 int right = round100(boundingRect.
right(), gridSizeW) + gridSizeW;
360 int top = round100(boundingRect.
top(), gridSizeH) - gridSizeH;
361 int bottom = round100(boundingRect.
bottom(), gridSizeH) + gridSizeH;
364 pen.setCosmetic(
true);
369 for (
int x = left; x <
right; x += gridSizeW)
371 painter.
drawLine(x, top, x, bottom);
375 for (
int y = top; y < bottom; y += gridSizeH)
377 painter.
drawLine(left, y, right, y);
383int OverlayPainter::round100(
double f,
int gridSize)
const
385 return static_cast<int>(f) / gridSize * gridSize;
QPointF pointAt(qreal t) const const
CompositionMode_Difference
void drawEllipse(const QRectF &rectangle)
void drawLine(const QLineF &line)
void drawLines(const QLineF *lines, int lineCount)
void drawText(const QPointF &position, const QString &text)
void setBrush(const QBrush &brush)
void setCompositionMode(QPainter::CompositionMode mode)
void setPen(const QColor &color)
void setRenderHint(QPainter::RenderHint hint, bool on)
void setWorldMatrixEnabled(bool enable)
QRect viewport() const const
const QColor & color(QPalette::ColorGroup group, QPalette::ColorRole role) const const
QPoint toPoint() const const
qreal bottom() const const
qreal right() const const
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)
void append(const T &value)
const T & at(int i) const const