All Classes Namespaces Functions Variables Enumerations Properties Pages
overlaypainter.cpp
1 #include "overlaypainter.h"
2 #include <QDebug>
3 #include <QSettings>
4 
5 #include "pencildef.h"
6 
7 
8 Q_CONSTEXPR static int OVERLAY_SAFE_CENTER_CROSS_SIZE = 25;
9 Q_CONSTEXPR static qreal LINELENGTHFACTOR = 2.0;
10 Q_CONSTEXPR static int LR_DIFF = 10; // minimum difference for Left and Right point
11 Q_CONSTEXPR static int MID_DIFF = 2; // minimum difference for Middle point
12 Q_CONSTEXPR static int LEFTANGLEOFFSET = 90;
13 Q_CONSTEXPR static int RIGHTANGLEOFFSET = -90;
14 
15 
16 void initPerspectivePainter(QPainter& painter)
17 {
19  QPen pen(QColor(180, 220, 255));
20  pen.setCosmetic(true);
21  painter.setPen(pen);
22  painter.setWorldMatrixEnabled(true);
23  painter.setBrush(Qt::NoBrush);
24 }
25 
26 OverlayPainter::OverlayPainter()
27 {
28 }
29 
30 void OverlayPainter::setViewTransform(const QTransform view)
31 {
32  mViewTransform = view;
33 }
34 
35 void OverlayPainter::renderOverlays(QPainter &painter, MoveMode mode)
36 {
37  mMoveMode = mode;
38  painter.save();
39 
40  if (mOptions.bCenter)
41  {
42  painter.setWorldTransform(mViewTransform);
43  paintOverlayCenter(painter);
44  }
45  if (mOptions.bThirds)
46  {
47  painter.setWorldTransform(mViewTransform);
48  paintOverlayThirds(painter);
49  }
50  if (mOptions.bGoldenRatio)
51  {
52  painter.setWorldTransform(mViewTransform);
53  paintOverlayGolden(painter);
54  }
55  if (mOptions.bSafeArea)
56  {
57  painter.setWorldTransform(mViewTransform);
58  paintOverlaySafeAreas(painter);
59  }
60 
61  QSettings settings(PENCIL2D, PENCIL2D);
62  mOptions.nOverlayAngle = settings.value("OverlayAngle").toInt();
63 
64  if (mOptions.bPerspective1)
65  {
66  painter.setWorldTransform(mViewTransform);
67  paintOverlayPerspective1(painter);
68  }
69  if (mOptions.bPerspective2)
70  {
71  painter.setWorldTransform(mViewTransform);
72  paintOverlayPerspective2(painter);
73  }
74  if (mOptions.bPerspective3)
75  {
76  painter.setWorldTransform(mViewTransform);
77  paintOverlayPerspective3(painter);
78  }
79  painter.restore();
80 }
81 
82 void OverlayPainter::paintOverlayCenter(QPainter &painter)
83 {
84  QRect rect = mOptions.mRect;
85 
86  painter.save();
88 
89  QPen pen(Qt::DashLine);
90  qreal space = 10;
91  QVector<qreal> dashes;
92  dashes << 10 << space << 10 << space << 10 << space;
93  pen.setDashPattern(dashes);
94  pen.setCosmetic(true);
95  painter.setPen(pen);
96  painter.setWorldMatrixEnabled(!mOptions.mIsCamera);
97  painter.setBrush(Qt::NoBrush);
98  painter.setRenderHint(QPainter::Antialiasing, false);
99 
100  int offset = OVERLAY_SAFE_CENTER_CROSS_SIZE;
101  painter.drawLine(rect.center().x()-offset, rect.center().y(), rect.center().x()+offset, rect.center().y());
102  painter.drawLine(rect.center().x(), rect.center().y()-offset, rect.center().x(), rect.center().y()+offset);
103 
104  painter.restore();
105 }
106 
107 void OverlayPainter::paintOverlayThirds(QPainter &painter)
108 {
109  QRect rect = mOptions.mRect;
110 
111  painter.save();
113 
114  QPen pen(Qt::DashLine);
115  qreal space = 10;
116  QVector<qreal> dashes;
117  dashes << 10 << space << 10 << space << 10 << space;
118  pen.setDashPattern(dashes);
119  pen.setCosmetic(true);
120  painter.setPen(pen);
121  painter.setWorldMatrixEnabled(!mOptions.mIsCamera);
122  painter.setBrush(Qt::NoBrush);
123  QPainter::RenderHints previous_renderhints = painter.renderHints();
124  painter.setRenderHint(QPainter::Antialiasing, false);
125 
126  painter.drawLine(rect.x(), rect.y() + (rect.height()/3), rect.right(), rect.y() + (rect.height()/3));
127  painter.drawLine(rect.x(), rect.y() + (rect.height() * 2/3), rect.x() + rect.width(), rect.y() + (rect.height() * 2/3));
128  painter.drawLine(rect.x() + rect.width()/3, rect.y(), rect.x() + rect.width()/3, rect.y() + rect.height());
129  painter.drawLine(rect.x() + rect.width() *2/3, rect.y(), rect.x() + rect.width() *2/3, rect.y() + rect.height());
130 
131  painter.setRenderHints(previous_renderhints);
132  painter.restore();
133 }
134 
135 void OverlayPainter::paintOverlayGolden(QPainter &painter)
136 {
137  QRect rect = mOptions.mRect;
138 
139  painter.save();
141 
142  QPen pen(Qt::DashLine);
143  qreal space = 10;
144  QVector<qreal> dashes;
145  dashes << 10 << space << 10 << space << 10 << space;
146  pen.setDashPattern(dashes);
147  pen.setCosmetic(true);
148  painter.setPen(pen);
149  painter.setWorldMatrixEnabled(!mOptions.mIsCamera);
150  painter.setBrush(Qt::NoBrush);
151  QPainter::RenderHints previous_renderhints = painter.renderHints();
152  painter.setRenderHint(QPainter::Antialiasing, false);
153 
154  painter.drawLine(rect.x(), static_cast<int>(rect.y() + (rect.height() * 0.38)), rect.right(), static_cast<int>(rect.y() + (rect.height() * 0.38)));
155  painter.drawLine(rect.x(), static_cast<int>(rect.y() + (rect.height() * 0.62)), rect.x() + rect.width(), static_cast<int>(rect.y() + (rect.height() * 0.62)));
156  painter.drawLine(static_cast<int>(rect.x() + rect.width() * 0.38), rect.y(), static_cast<int>(rect.x() + rect.width() * 0.38), rect.bottom());
157  painter.drawLine(static_cast<int>(rect.x() + rect.width() * 0.62), rect.y(), static_cast<int>(rect.x() + rect.width() * 0.62), rect.bottom());
158 
159  painter.setRenderHints(previous_renderhints);
160  painter.restore();
161 }
162 
163 void OverlayPainter::paintOverlaySafeAreas(QPainter &painter)
164 {
165  QRect rect = mOptions.mRect;
166 
167  painter.save();
169  QPen pen(Qt::DashLine);
170  qreal space = 10;
171  QVector<qreal> dashes;
172  dashes << 10 << space << 10 << space << 10 << space;
173  pen.setDashPattern(dashes);
174  pen.setCosmetic(true);
175  painter.setPen(pen);
176  painter.setWorldMatrixEnabled(!mOptions.mIsCamera);
177  painter.setBrush(Qt::NoBrush);
178  QPainter::RenderHints previous_renderhints = painter.renderHints();
180 
181  if (mOptions.bActionSafe)
182  {
183  int action = mOptions.nActionSafe;
184  QRect safeAction(rect.x() + rect.width() * action / 200,
185  rect.y() + rect.height() * action/200,
186  rect.width() * (100 - action) / 100,
187  rect.height() * (100 - action) / 100);
188  painter.drawRect(safeAction);
189 
190  if (mOptions.bShowSafeAreaHelperText)
191  {
192  painter.drawText(safeAction.x(), safeAction.y() - 1, tr("Safe Action area %1 %").arg(action));
193  }
194  }
195  if (mOptions.bTitleSafe)
196  {
197  int title = mOptions.nTitleSafe;
198  QRect safeTitle(rect.x() + rect.width() * title / 200,
199  rect.y() + rect.height() * title / 200,
200  rect.width() * (100 - title) / 100,
201  rect.height() * (100 - title) / 100);
202  painter.drawRect(safeTitle);
203 
204  if (mOptions.bShowSafeAreaHelperText)
205  {
206  painter.drawText(safeTitle.x(), safeTitle.y()-1, tr("Safe Title area %1 %").arg(title));
207  }
208  }
209 
210  painter.setRenderHints(previous_renderhints);
211  painter.restore();
212 }
213 
214 void OverlayPainter::paintOverlayPerspective1(QPainter& painter)
215 {
216  QRect rect = mOptions.mRect;
217 
218  painter.save();
219  initPerspectivePainter(painter);
220  QPainter::RenderHints previous_renderhints = painter.renderHints();
221  painter.setRenderHint(QPainter::Antialiasing, false);
222 
223  qreal degrees = static_cast<qreal>(mOptions.nOverlayAngle);
224  if (degrees == 7.0) { degrees = 7.5; }
225  int repeats = static_cast<int>(360 / degrees);
226  QLineF angleLine;
227  if (mOptions.mSinglePerspPoint == QPointF(0, 0)) // TODO: bug in QT prevents
228  mOptions.mSinglePerspPoint = QPointF(0.1, 0.1); // point to be (0,0)...
229  angleLine.setP1(mOptions.mSinglePerspPoint);
230  QVector<QLineF> lines;
231  for (int i = 0; i < repeats; i++)
232  {
233  angleLine.setAngle(i * degrees);
234  angleLine.setLength(rect.width() * 2.0);
235  lines.append(angleLine);
236  }
237  painter.drawLines(lines);
238 
239  painter.setRenderHints(previous_renderhints);
240  painter.restore();
241 }
242 
243 void OverlayPainter::paintOverlayPerspective2(QPainter& painter)
244 {
245  QRect rect = mOptions.mRect;
246  painter.save();
247  initPerspectivePainter(painter);
248  QPainter::RenderHints previous_renderhints = painter.renderHints();
249  painter.setRenderHint(QPainter::Antialiasing, false);
250 
251  qreal degrees = static_cast<qreal>(mOptions.nOverlayAngle);
252  if (degrees == 7.0) { degrees = 7.5; }
253  int repeats = static_cast<int>(180 / degrees);
254 
255  if (mOptions.mLeftPerspPoint == QPointF(0.0, 0.0)) // TODO: bug in QT prevents
256  mOptions.mLeftPerspPoint = QPointF(0.1, 0.1); // point to be (0,0)...
257  if (mOptions.mRightPerspPoint == QPointF(0.0, 0.0)) // TODO: bug in QT prevents
258  mOptions.mRightPerspPoint = QPointF(0.1, 0.1); // point to be (0,0)...
259 
260  switch (mMoveMode) {
261  case MoveMode::PERSP_LEFT:
262  setRightPoint(QPoint(mOptions.mRightPerspPoint.x(), mOptions.mLeftPerspPoint.y()));
263  break;
264  case MoveMode::PERSP_RIGHT:
265  setLeftPoint(QPoint(mOptions.mLeftPerspPoint.x(), mOptions.mRightPerspPoint.y()));
266  break;
267  default:
268  break;
269  }
270 
271  QLineF angleLine;
272  angleLine.setAngle(LEFTANGLEOFFSET);
273  angleLine.setP1(mOptions.mLeftPerspPoint);
274  QVector<QLineF> lines;
275  for (int i = 0; i <= repeats; i++)
276  {
277  angleLine.setAngle(LEFTANGLEOFFSET - i * degrees);
278  angleLine.setLength(rect.width() * LINELENGTHFACTOR);
279  lines.append(angleLine);
280  }
281 
282  angleLine.setAngle(RIGHTANGLEOFFSET);
283  angleLine.setP1(mOptions.mRightPerspPoint);
284  for (int i = 0; i <= repeats; i++)
285  {
286  angleLine.setAngle(RIGHTANGLEOFFSET - i * degrees);
287  angleLine.setLength(rect.width() * LINELENGTHFACTOR);
288  lines.append(angleLine);
289  }
290  painter.drawLines(lines);
291 
292  painter.setRenderHints(previous_renderhints);
293  painter.restore();
294 }
295 
296 void OverlayPainter::paintOverlayPerspective3(QPainter& painter)
297 {
298  if (!mOptions.bPerspective2)
299  paintOverlayPerspective2(painter);
300 
301  QRect rect = mOptions.mRect;
302  painter.save();
303  initPerspectivePainter(painter);
304  QPainter::RenderHints previous_renderhints = painter.renderHints();
305  painter.setRenderHint(QPainter::Antialiasing, false);
306 
307  qreal degrees = static_cast<qreal>(mOptions.nOverlayAngle);
308  if (degrees == 7.0) { degrees = 7.5; }
309  int repeats = static_cast<int>(180 / degrees);
310 
311  if (mOptions.mMiddlePerspPoint == QPointF(0, 0)) // TODO: bug in QT prevents
312  mOptions.mMiddlePerspPoint = QPointF(0.1, 0.1); // point to be (0,0)...
313 
314  const int middleAngleOffset = mOptions.mLeftPerspPoint.y() < mOptions.mMiddlePerspPoint.y() ? 180 : 0;
315 
316  QLineF angleLine;
317  angleLine.setAngle(middleAngleOffset);
318  angleLine.setP1(mOptions.mMiddlePerspPoint);
319  QVector<QLineF> lines;
320  for (int i = 0; i <= repeats; i++)
321  {
322  angleLine.setAngle(middleAngleOffset - i * degrees);
323  angleLine.setLength(rect.width() * LINELENGTHFACTOR);
324  lines.append(angleLine);
325  }
326  painter.drawLines(lines);
327 
328  painter.setRenderHints(previous_renderhints);
329  painter.restore();
330 }
void setCompositionMode(QPainter::CompositionMode mode)
void setRenderHint(QPainter::RenderHint hint, bool on)
DashLine
void append(const T &value)
int right() const const
QPainter::RenderHints renderHints() const const
void save()
void drawLines(const QLineF *lines, int lineCount)
int height() const const
int x() const const
int y() const const
void drawLine(const QLineF &line)
int x() const const
int y() const const
void drawRect(const QRectF &rectangle)
qreal x() const const
qreal y() const const
void setPen(const QColor &color)
void setWorldTransform(const QTransform &matrix, bool combine)
void setAngle(qreal angle)
void setBrush(const QBrush &brush)
void drawText(const QPointF &position, const QString &text)
QPoint center() const const
CompositionMode_Difference
void setRenderHints(QPainter::RenderHints hints, bool on)
void restore()
int width() const const
void setWorldMatrixEnabled(bool enable)
int bottom() const const
void setP1(const QPointF &p1)
void setLength(qreal length)