MythTV master
mythrect.cpp
Go to the documentation of this file.
1
2#include <QTextStream>
3
4#include "mythrect.h"
5#include "mythmainwindow.h"
6#include "mythuihelper.h"
7
8MythRect::MythRect(const QString &sX, const QString &sY, const QString &sWidth,
9 const QString &sHeight, const QString &baseRes)
10{
11 setRect(sX,sY,sWidth,sHeight,baseRes);
12}
13
14/*
15 * An explicit assignment operator from QRect to MythRect. This
16 * takes the place of an implicitly converting from a QRect to a
17 * MythRect and then calling the compiler generated default
18 * assignment operator.
19 */
20MythRect& MythRect::operator= (const QRect other)
21{
22 if (this == &other)
23 return *this;
24
25 // Set coords from the point.
26 QRect::setX(other.x());
27 QRect::setY(other.y());
28 QRect::setWidth(other.width());
29 QRect::setHeight(other.height());
30
31 // Restore everything else to default.
32 m_percentWidth = 0.0F;
33 m_percentHeight = 0.0F;
34 m_percentX = 0.0F;
35 m_percentY = 0.0F;
36 m_offsetWidth = 0;
38 m_offsetX = 0;
39 m_offsetY = 0;
40 m_needsUpdate = true;
41 m_parentArea = {0,0,0,0};
42
43 return *this;
44};
45
46bool MythRect::operator== (const MythRect &other) const
47{
48 return ((m_percentWidth == other.m_percentWidth) &&
50 (m_percentX == other.m_percentX) &&
51 (m_percentY == other.m_percentY) &&
52 (m_offsetWidth == other.m_offsetWidth) &&
53 (m_offsetHeight == other.m_offsetHeight) &&
54 (m_offsetX == other.m_offsetX) &&
55 (m_offsetY == other.m_offsetY) &&
56 (QRect)(*this) == (QRect)other);
57}
58
60{
61 m_parentArea.setRect(0, 0, 0, 0);
62}
63
64void MythRect::CalculateArea(const QRect parentArea)
65{
66 if ((m_parentArea == parentArea && !m_needsUpdate) || !parentArea.isValid())
67 return;
68
69 m_parentArea = parentArea;
70
71 int w = width();
72 int h = height();
73 int X = x();
74 int Y = y();
75
76 if (m_percentX > 0.0F)
77 X = (int) (m_percentX * (float)m_parentArea.width()) + m_offsetX;
78 if (m_percentY > 0.0F)
79 Y = (int) (m_percentY * (float)m_parentArea.height()) + m_offsetY;
80 if (m_percentWidth > 0.0F)
81 {
82 w = (int) (m_percentWidth * (float)(m_parentArea.width() - X))
84 }
85 else if (m_offsetWidth != 0)
86 {
87 w = m_parentArea.width() - X + m_offsetWidth;
88 }
89 if (m_percentHeight > 0.0F)
90 {
91 h = (int) (m_percentHeight * (float)(m_parentArea.height() - Y))
93 }
94 else if (m_offsetHeight != 0)
95 {
96 h = m_parentArea.height() - Y + m_offsetHeight;
97 }
98
99 QRect::setRect(X,Y,w,h);
100
101 m_needsUpdate = false;
102}
103
104void MythRect::CalculateArea(const MythRect & parentArea)
105{
106 CalculateArea(parentArea.toQRect());
107}
108
110{
111
112 if (m_percentWidth == 0.0F)
113 QRect::setWidth(GetMythMainWindow()->NormX(width()));
114
115 if (m_percentHeight == 0.0F)
116 QRect::setHeight(GetMythMainWindow()->NormY(height()));
117
118 int X = 0;
119 if (m_percentX == 0.0F)
120 X = GetMythMainWindow()->NormX(x());
121
122 int Y = 0;
123 if (m_percentY == 0.0F)
124 Y = GetMythMainWindow()->NormY(y());
125
130
131 QRect::moveTopLeft(QPoint(X,Y));
132
133 // This would apply the result of normalized to the rect but causes
134 // problems with theme display
135 // QRect tr = normalized();
136 // setRect(tr.x(), tr.y(), tr.width(), tr.height());
137}
138
139void MythRect::setRect(const QString &sX, const QString &sY,
140 const QString &sWidth, const QString &sHeight,
141 const QString &rectBaseRes)
142{
143
144 // cater for an extra paramater on area and similar tags
145 // for base resolution.
146
147 QString vX = sX;
148 QString vY = sY;
149 QString vWidth = sWidth;
150 QString vHeight = sHeight;
151 if (!rectBaseRes.isEmpty())
152 {
153 QStringList res = rectBaseRes.split('x');
154 if (res.size() == 2)
155 {
156 QSize themeBaseSize = GetMythUI()->GetBaseSize();
157 int rectBaseWidth = res[0].toInt();
158 int rectBaseHeight = res[1].toInt();
159 if (rectBaseWidth > 0 && rectBaseHeight > 0)
160 {
161 int iX = sX.toInt();
162 if (iX > 0)
163 {
164 iX = iX * themeBaseSize.width() / rectBaseWidth;
165 vX = QString::number(iX);
166 }
167 int iY = sY.toInt();
168 if (iY > 0)
169 {
170 iY = iY * themeBaseSize.height() / rectBaseHeight;
171 vY = QString::number(iY);
172 }
173 int iWidth = sWidth.toInt();
174 if (iWidth > 0)
175 {
176 iWidth = iWidth * themeBaseSize.width() / rectBaseWidth;
177 vWidth = QString::number(iWidth);
178 }
179 int iHeight = sHeight.toInt();
180 if (iHeight > 0)
181 {
182 iHeight = iHeight * themeBaseSize.height() / rectBaseHeight;
183 vHeight = QString::number(iHeight);
184 }
185 }
186 }
187 }
188 setX(vX);
189 setY(vY);
190 setWidth(vWidth);
191 setHeight(vHeight);
192}
193
199bool MythRect::parsePosition(float & percent, int & offset, int & absolute,
200 const QString &value, bool is_size)
201{
202 /*
203 Position can be either an absolute, or a percentage with an
204 optional offset.
205
206 720
207 -5
208 33%
209 75%+10
210 */
211
212 percent = 0.0;
213 offset = 0;
214 absolute = 0;
215
216 if (value.isEmpty())
217 return true;
218
219 int number = 0;
220 char ch = ' ';
221 QString tmp(value); // QTextStream won't accept a const!
222 QTextStream is(&tmp);
223
224 is >> number;
225 if (is.status() != QTextStream::Ok)
226 return true;
227
228 is.skipWhiteSpace();
229 is >> ch;
230 if (ch != '%')
231 {
232 if (is_size && number < 0)
233 {
234 offset = number;
235 return false;
236 }
237 absolute = number;
238 return true;
239 }
240
241 percent = static_cast<float>(number) / 100.0F;
242 is >> offset;
243 return false;
244}
245
246void MythRect::setX(const QString &sX)
247{
248 int absoluteX = 0;
249
250 if (parsePosition(m_percentX, m_offsetX, absoluteX, sX, false))
251 QRect::setX(absoluteX);
252 else
253 m_needsUpdate = true;
254}
255
256void MythRect::setY(const QString &sY)
257{
258 int absoluteY = 0;
259
260 if (parsePosition(m_percentY, m_offsetY, absoluteY, sY, false))
261 QRect::setY(absoluteY);
262 else
263 m_needsUpdate = true;
264}
265
266void MythRect::setWidth(const QString &sWidth)
267{
268 int absoluteWidth = 0;
269
270 if (parsePosition(m_percentWidth, m_offsetWidth, absoluteWidth,
271 sWidth, true))
272 QRect::setWidth(absoluteWidth);
273 else
274 m_needsUpdate = true;
275}
276
277void MythRect::setHeight(const QString &sHeight)
278{
279 int absoluteHeight = 0;
280
281 if (parsePosition(m_percentHeight, m_offsetHeight, absoluteHeight,
282 sHeight, true))
283 QRect::setHeight(absoluteHeight);
284 else
285 m_needsUpdate = true;
286}
287
289{
290 MythPoint point;
291 point.setX(getX());
292 point.setY(getY());
293 return point;
294}
295
296void MythRect::moveTopLeft(const QPoint point)
297{
298 // No need to convert coordinates to strings and back since there
299 // is no percent scaling on a raw QPoint.
300 QRect::moveTopLeft(point);
301}
302
304{
305 moveLeft(point.getX());
306 moveTop(point.getY());
307}
308
309void MythRect::moveLeft(const QString &sX)
310{
311 int absoluteX = 0;
312
313 if (parsePosition(m_percentX, m_offsetX, absoluteX, sX, false))
314 QRect::moveLeft(absoluteX);
315 else // Move left to the absolute pos specified by the percentage/offset
316 m_needsUpdate = true;
317}
318
319void MythRect::moveTop(const QString &sY)
320{
321 int absoluteY = 0;
322
323 if (parsePosition(m_percentY, m_offsetY, absoluteY, sY, false))
324 QRect::moveTop(absoluteY);
325 else // Move top to the absolute pos specified by the percentage/offset
326 m_needsUpdate = true;
327}
328
329QString MythRect::getX(void) const
330{
331 QString stringX;
332 if (m_percentX > 0.0F)
333 stringX = QString("%1%").arg((int)(m_percentX * 100));
334 else
335 stringX = QString("%1").arg(x() - m_offsetX);
336 if (m_offsetX != 0)
337 {
338 if (m_offsetX > 0)
339 stringX += '+';
340 stringX += QString("%1").arg(m_offsetX);
341 }
342 return stringX;
343}
344
345QString MythRect::getY(void) const
346{
347 QString stringY;
348 if (m_percentY > 0.0F)
349 stringY = QString("%1%").arg((int)(m_percentY * 100));
350 else
351 stringY = QString("%1").arg(y() - m_offsetY);
352 if (m_offsetY != 0)
353 {
354 if (m_offsetY > 0)
355 stringY += '+';
356 stringY += QString("%1").arg(m_offsetY);
357 }
358 return stringY;
359}
360
361QString MythRect::getWidth(void) const
362{
363 QString stringWidth;
364 if (m_percentWidth > 0.0F)
365 stringWidth = QString("%1%").arg((int)(m_percentWidth * 100));
366 else
367 stringWidth = QString("%1").arg(width() - m_offsetWidth);
368 if (m_offsetWidth != 0)
369 {
370 if (m_offsetWidth > 0)
371 stringWidth += '+';
372 stringWidth += QString("%1").arg(m_offsetWidth);
373 }
374 return stringWidth;
375}
376
377QString MythRect::getHeight(void) const
378{
379 QString stringHeight;
380 if (m_percentHeight > 0.0F)
381 stringHeight = QString("%1%").arg((int)(m_percentHeight * 100));
382 else
383 stringHeight = QString("%1").arg(height() - m_offsetHeight);
384 if (m_offsetHeight != 0)
385 {
386 if (m_offsetHeight > 0)
387 stringHeight += '+';
388 stringHeight += QString("%1").arg(m_offsetHeight);
389 }
390 return stringHeight;
391}
392
393QString MythRect::toString(bool details) const
394{
395 QString result = QString("(%1,%2,%3,%4)")
396 .arg(x()).arg(y()).arg(width()).arg(height());
397
398 if (details)
399 result += QString(" [%1,%2,%3,%4]")
400 .arg(getX(), getY(), getWidth(), getHeight());
401
402 return result;
403}
404
405QRect MythRect::toQRect() const
406{
407 return {x(),y(),width(),height()};
408}
409
411
412MythPoint::MythPoint(const QString &sX, const QString &sY)
413{
414 setX(sX);
415 setY(sY);
416}
417
418/*
419 * An explicit assignment operator from QPoint to MythPoint. This
420 * takes the place of an implicitly converting from a QPoint to a
421 * MythPoint and then calling the compiler generated default
422 * assignment operator.
423 */
425{
426 if (this == &other)
427 return *this;
428
429 // Set coords from the point.
430 QPoint::setX(other.x());
431 QPoint::setY(other.y());
432
433 // Restore everything else to default.
434 m_percentX = 0.0F;
435 m_percentY = 0.0F;
436 m_offsetX = 0;
437 m_offsetY = 0;
438 m_needsUpdate = true;
439 m_parentArea = {};
440 m_valid = true;
441 return *this;
442};
443
444void MythPoint::CalculatePoint(const QRect parentArea)
445{
446 if ((m_parentArea == parentArea && !m_needsUpdate) || !parentArea.isValid())
447 return;
448
449 m_parentArea = parentArea;
450
451 int X = x();
452 int Y = y();
453
454 if (m_percentX > 0.0F)
455 X = (int) (m_percentX * (float)m_parentArea.width()) + m_offsetX;
456 if (m_percentY > 0.0F)
457 Y = (int) (m_percentY * (float)m_parentArea.height()) + m_offsetY;
458
459 QPoint::setX(X);
460 QPoint::setY(Y);
461
462 m_needsUpdate = false;
463 m_valid = true;
464}
465
466void MythPoint::CalculatePoint(const MythRect & parentArea)
467{
468 CalculatePoint(parentArea.toQRect());
469}
470
472{
473 if (m_percentX == 0.0F)
474 QPoint::setX(GetMythMainWindow()->NormX(x()));
475
476 if (m_percentY == 0.0F)
477 QPoint::setY(GetMythMainWindow()->NormY(y()));
478
481}
482
488bool MythPoint::parsePosition(float & percent, int & offset, int & absolute,
489 const QString &value)
490{
491 /*
492 Position can be either an absolute, or a percentage with an
493 optional offset.
494
495 720
496 -5
497 33%
498 75%+10
499 */
500
501 percent = 0.0;
502 offset = 0;
503 absolute = 0;
504
505 if (value.isEmpty())
506 return true;
507
508 int number = 0;
509 char ch = ' ';
510 QString tmp(value); // QTextStream won't accept a const!
511 QTextStream is(&tmp);
512
513 is >> number;
514 if (is.status() != QTextStream::Ok)
515 return true;
516
517 is.skipWhiteSpace();
518 is >> ch;
519 if (ch != '%')
520 {
521 absolute = number;
522 return true;
523 }
524
525 percent = static_cast<float>(number) / 100.0F;
526 is >> offset;
527 return false;
528}
529
530void MythPoint::setX(const QString &sX)
531{
532 int absoluteX = 0;
533
534 if (parsePosition(m_percentX, m_offsetX, absoluteX, sX))
535 QPoint::setX(absoluteX);
536 else
537 m_needsUpdate = true;
538}
539
540void MythPoint::setY(const QString &sY)
541{
542 int absoluteY = 0;
543
544 if (parsePosition(m_percentY, m_offsetY, absoluteY, sY))
545 QPoint::setY(absoluteY);
546 else
547 m_needsUpdate = true;
548}
549
550QString MythPoint::getX(void) const
551{
552 QString stringX;
553 if (m_percentX > 0.0F)
554 stringX = QString("%1%").arg((int)(m_percentX * 100));
555 else
556 stringX = QString("%1").arg(x() - m_offsetX);
557 if (m_offsetX != 0)
558 {
559 if (m_offsetX > 0)
560 stringX += '+';
561 stringX += QString("%1").arg(m_offsetX);
562 }
563 return stringX;
564}
565
566QString MythPoint::getY(void) const
567{
568 QString stringY;
569 if (m_percentY > 0.0F)
570 stringY = QString("%1%").arg((int)(m_percentY * 100));
571 else
572 stringY = QString("%1").arg(y() - m_offsetY);
573 if (m_offsetY != 0)
574 {
575 if (m_offsetY > 0)
576 stringY += '+';
577 stringY += QString("%1").arg(m_offsetY);
578 }
579 return stringY;
580}
581
582QString MythPoint::toString(bool details) const
583{
584 QString result = QString("(%1,%2)")
585 .arg(x()).arg(y());
586
587 if (details)
588 result += QString(" [%1,%2]")
589 .arg(getX(), getY());
590
591 return result;
592}
593
595{
596 return {x(),y()};
597}
Wrapper around QPoint allowing us to handle percentage and other relative values for positioning in m...
Definition: mythrect.h:89
void CalculatePoint(QRect parentArea)
Definition: mythrect.cpp:444
void setY(const QString &sY)
Definition: mythrect.cpp:540
float m_percentX
Definition: mythrect.h:123
QRect m_parentArea
Definition: mythrect.h:130
QString getX(void) const
Definition: mythrect.cpp:550
QString toString(bool details=false) const
Definition: mythrect.cpp:582
MythPoint()
Definition: mythrect.h:92
static bool parsePosition(float &percent, int &offset, int &absolute, const QString &value)
parse the position
Definition: mythrect.cpp:488
float m_percentY
Definition: mythrect.h:124
bool m_valid
Definition: mythrect.h:132
MythPoint & operator=(QPoint other)
Definition: mythrect.cpp:424
void NormPoint(void)
Definition: mythrect.cpp:471
bool m_needsUpdate
Definition: mythrect.h:128
void setX(const QString &sX)
Definition: mythrect.cpp:530
int m_offsetY
Definition: mythrect.h:126
int m_offsetX
Definition: mythrect.h:125
QPoint toQPoint(void) const
Definition: mythrect.cpp:594
QString getY(void) const
Definition: mythrect.cpp:566
Wrapper around QRect allowing us to handle percentage and other relative values for areas in mythui.
Definition: mythrect.h:18
void setRect(const QString &sX, const QString &sY, const QString &sWidth, const QString &sHeight, const QString &baseRes=QString())
Definition: mythrect.cpp:139
void NormRect(void)
Definition: mythrect.cpp:109
int m_offsetX
Definition: mythrect.h:75
MythPoint topLeft(void) const
Definition: mythrect.cpp:288
QRect m_parentArea
Definition: mythrect.h:80
MythRect & operator=(QRect other)
Definition: mythrect.cpp:20
int m_offsetWidth
Definition: mythrect.h:73
float m_percentHeight
Definition: mythrect.h:70
void setY(const QString &sY)
Definition: mythrect.cpp:256
float m_percentY
Definition: mythrect.h:72
int m_offsetHeight
Definition: mythrect.h:74
void moveTop(const QString &sY)
Definition: mythrect.cpp:319
bool m_needsUpdate
Definition: mythrect.h:78
void moveLeft(const QString &sX)
Definition: mythrect.cpp:309
void setX(const QString &sX)
Definition: mythrect.cpp:246
void setWidth(const QString &sWidth)
Definition: mythrect.cpp:266
QString toString(bool details=false) const
Definition: mythrect.cpp:393
QString getHeight(void) const
Definition: mythrect.cpp:377
void Reset(void)
Definition: mythrect.cpp:59
MythRect()=default
void setHeight(const QString &sHeight)
Definition: mythrect.cpp:277
bool operator==(const MythRect &other) const
Definition: mythrect.cpp:46
float m_percentX
Definition: mythrect.h:71
QString getY(void) const
Definition: mythrect.cpp:345
void CalculateArea(QRect parentArea)
Definition: mythrect.cpp:64
static bool parsePosition(float &percent, int &offset, int &absolute, const QString &value, bool is_size)
parse the position
Definition: mythrect.cpp:199
float m_percentWidth
Definition: mythrect.h:69
void moveTopLeft(QPoint point)
Definition: mythrect.cpp:296
QString getX(void) const
Definition: mythrect.cpp:329
int m_offsetY
Definition: mythrect.h:76
QRect toQRect(void) const
Definition: mythrect.cpp:405
QString getWidth(void) const
Definition: mythrect.cpp:361
int NormX(int X) const
int NormY(int Y) const
QSize GetBaseSize() const
static guint32 * tmp
Definition: goom_core.cpp:26
MythMainWindow * GetMythMainWindow(void)
MythUIHelper * GetMythUI()