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 
8 MythRect::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  */
20 MythRect& 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;
37  m_offsetHeight = 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 
46 bool MythRect::operator== (const MythRect &other) const
47 {
48  return ((m_percentWidth == other.m_percentWidth) &&
49  (m_percentHeight == other.m_percentHeight) &&
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 
59 void MythRect::Reset(void)
60 {
61  m_parentArea.setRect(0, 0, 0, 0);
62 }
63 
64 void 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))
83  + m_offsetWidth;
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 
104 void 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 
139 void 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 
199 bool 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 
246 void 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 
256 void 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 
266 void 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 
277 void 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 
296 void 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 
309 void 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 
319 void 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 
329 QString 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 
345 QString 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 
361 QString 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 
377 QString 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 
393 QString 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()).arg(getY()).arg(getWidth()).arg(getHeight());
401 
402  return result;
403 }
404 
405 QRect MythRect::toQRect() const
406 {
407  return {x(),y(),width(),height()};
408 }
409 
411 
412 MythPoint::MythPoint(const QString &sX, const QString &sY)
413  : QPoint()
414 {
415  setX(sX);
416  setY(sY);
417 }
418 
419 /*
420  * An explicit assignment operator from QPoint to MythPoint. This
421  * takes the place of an implicitly converting from a QPoint to a
422  * MythPoint and then calling the compiler generated default
423  * assignment operator.
424  */
425 MythPoint& MythPoint::operator= (const QPoint& other)
426 {
427  if (this == &other)
428  return *this;
429 
430  // Set coords from the point.
431  QPoint::setX(other.x());
432  QPoint::setY(other.y());
433 
434  // Restore everything else to default.
435  m_percentX = 0.0F;
436  m_percentY = 0.0F;
437  m_offsetX = 0;
438  m_offsetY = 0;
439  m_needsUpdate = true;
440  m_parentArea = {};
441  m_valid = true;
442  return *this;
443 };
444 
445 void MythPoint::CalculatePoint(const QRect & parentArea)
446 {
447  if ((m_parentArea == parentArea && !m_needsUpdate) || !parentArea.isValid())
448  return;
449 
450  m_parentArea = parentArea;
451 
452  int X = x();
453  int Y = y();
454 
455  if (m_percentX > 0.0F)
456  X = (int) (m_percentX * (float)m_parentArea.width()) + m_offsetX;
457  if (m_percentY > 0.0F)
458  Y = (int) (m_percentY * (float)m_parentArea.height()) + m_offsetY;
459 
460  QPoint::setX(X);
461  QPoint::setY(Y);
462 
463  m_needsUpdate = false;
464  m_valid = true;
465 }
466 
467 void MythPoint::CalculatePoint(const MythRect & parentArea)
468 {
469  CalculatePoint(parentArea.toQRect());
470 }
471 
473 {
474  if (m_percentX == 0.0F)
475  QPoint::setX(GetMythMainWindow()->NormX(x()));
476 
477  if (m_percentY == 0.0F)
478  QPoint::setY(GetMythMainWindow()->NormY(y()));
479 
482 }
483 
489 bool MythPoint::parsePosition(float & percent, int & offset, int & absolute,
490  const QString &value)
491 {
492  /*
493  Position can be either an absolute, or a percentage with an
494  optional offset.
495 
496  720
497  -5
498  33%
499  75%+10
500  */
501 
502  percent = 0.0;
503  offset = 0;
504  absolute = 0;
505 
506  if (value.isEmpty())
507  return true;
508 
509  int number = 0;
510  char ch = ' ';
511  QString tmp(value); // QTextStream won't accept a const!
512  QTextStream is(&tmp);
513 
514  is >> number;
515  if (is.status() != QTextStream::Ok)
516  return true;
517 
518  is.skipWhiteSpace();
519  is >> ch;
520  if (ch != '%')
521  {
522  absolute = number;
523  return true;
524  }
525 
526  percent = static_cast<float>(number) / 100.0F;
527  is >> offset;
528  return false;
529 }
530 
531 void MythPoint::setX(const QString &sX)
532 {
533  int absoluteX = 0;
534 
535  if (parsePosition(m_percentX, m_offsetX, absoluteX, sX))
536  QPoint::setX(absoluteX);
537  else
538  m_needsUpdate = true;
539 }
540 
541 void MythPoint::setY(const QString &sY)
542 {
543  int absoluteY = 0;
544 
545  if (parsePosition(m_percentY, m_offsetY, absoluteY, sY))
546  QPoint::setY(absoluteY);
547  else
548  m_needsUpdate = true;
549 }
550 
551 QString MythPoint::getX(void) const
552 {
553  QString stringX;
554  if (m_percentX > 0.0F)
555  stringX = QString("%1%").arg((int)(m_percentX * 100));
556  else
557  stringX = QString("%1").arg(x() - m_offsetX);
558  if (m_offsetX != 0)
559  {
560  if (m_offsetX > 0)
561  stringX += '+';
562  stringX += QString("%1").arg(m_offsetX);
563  }
564  return stringX;
565 }
566 
567 QString MythPoint::getY(void) const
568 {
569  QString stringY;
570  if (m_percentY > 0.0F)
571  stringY = QString("%1%").arg((int)(m_percentY * 100));
572  else
573  stringY = QString("%1").arg(y() - m_offsetY);
574  if (m_offsetY != 0)
575  {
576  if (m_offsetY > 0)
577  stringY += '+';
578  stringY += QString("%1").arg(m_offsetY);
579  }
580  return stringY;
581 }
582 
583 QString MythPoint::toString(bool details) const
584 {
585  QString result = QString("(%1,%2)")
586  .arg(x()).arg(y());
587 
588  if (details)
589  result += QString(" [%1,%2]")
590  .arg(getX()).arg(getY());
591 
592  return result;
593 }
594 
595 QPoint MythPoint::toQPoint() const
596 {
597  return {x(),y()};
598 }
MythRect::getY
QString getY(void) const
Definition: mythrect.cpp:345
mythrect.h
MythMainWindow::NormX
int NormX(int x)
Definition: mythmainwindow.cpp:2236
MythRect::CalculateArea
void CalculateArea(const QRect &parentArea)
Definition: mythrect.cpp:64
MythPoint::m_valid
bool m_valid
Definition: mythrect.h:132
MythPoint::m_offsetX
int m_offsetX
Definition: mythrect.h:125
MythRect::toString
QString toString(bool details=false) const
Definition: mythrect.cpp:393
MythRect::m_offsetHeight
int m_offsetHeight
Definition: mythrect.h:74
MythRect::setRect
void setRect(const QString &sX, const QString &sY, const QString &sWidth, const QString &sHeight, const QString &baseRes=QString())
Definition: mythrect.cpp:139
MythRect::toQRect
QRect toQRect(void) const
Definition: mythrect.cpp:405
MythPoint::toString
QString toString(bool details=false) const
Definition: mythrect.cpp:583
MythPoint::toQPoint
QPoint toQPoint(void) const
Definition: mythrect.cpp:595
MythRect::Reset
void Reset(void)
Definition: mythrect.cpp:59
MythRect::parsePosition
static bool parsePosition(float &percent, int &offset, int &absolute, const QString &value, bool is_size)
parse the position
Definition: mythrect.cpp:199
MythRect::m_offsetY
int m_offsetY
Definition: mythrect.h:76
MythRect::m_percentX
float m_percentX
Definition: mythrect.h:71
MythRect::operator==
bool operator==(const MythRect &other) const
Definition: mythrect.cpp:46
MythRect::m_percentWidth
float m_percentWidth
Definition: mythrect.h:69
MythRect::m_percentHeight
float m_percentHeight
Definition: mythrect.h:70
MythRect
Wrapper around QRect allowing us to handle percentage and other relative values for areas in mythui.
Definition: mythrect.h:18
MythRect::getWidth
QString getWidth(void) const
Definition: mythrect.cpp:361
tmp
static guint32 * tmp
Definition: goom_core.cpp:30
MythRect::moveLeft
void moveLeft(const QString &sX)
Definition: mythrect.cpp:309
MythPoint::m_percentX
float m_percentX
Definition: mythrect.h:123
MythPoint::m_needsUpdate
bool m_needsUpdate
Definition: mythrect.h:128
MythPoint::MythPoint
MythPoint()
Definition: mythrect.h:92
MythRect::m_needsUpdate
bool m_needsUpdate
Definition: mythrect.h:78
MythUIHelper::GetBaseSize
QSize GetBaseSize(void) const
Definition: mythuihelper.cpp:1506
MythRect::moveTop
void moveTop(const QString &sY)
Definition: mythrect.cpp:319
MythPoint::CalculatePoint
void CalculatePoint(const QRect &parentArea)
Definition: mythrect.cpp:445
MythRect::m_percentY
float m_percentY
Definition: mythrect.h:72
MythPoint::setY
void setY(const QString &sY)
Definition: mythrect.cpp:541
MythRect::setHeight
void setHeight(const QString &sHeight)
Definition: mythrect.cpp:277
MythPoint::parsePosition
static bool parsePosition(float &percent, int &offset, int &absolute, const QString &value)
parse the position
Definition: mythrect.cpp:489
MythPoint::NormPoint
void NormPoint(void)
Definition: mythrect.cpp:472
MythRect::getX
QString getX(void) const
Definition: mythrect.cpp:329
MythPoint::m_offsetY
int m_offsetY
Definition: mythrect.h:126
MythRect::m_offsetX
int m_offsetX
Definition: mythrect.h:75
mythuihelper.h
MythRect::moveTopLeft
void moveTopLeft(const QPoint &point)
Definition: mythrect.cpp:296
MythRect::MythRect
MythRect()=default
MythPoint::operator=
MythPoint & operator=(const QPoint &other)
Definition: mythrect.cpp:425
MythRect::NormRect
void NormRect(void)
Definition: mythrect.cpp:109
MythRect::topLeft
MythPoint topLeft(void) const
Definition: mythrect.cpp:288
MythRect::setWidth
void setWidth(const QString &sWidth)
Definition: mythrect.cpp:266
MythRect::m_offsetWidth
int m_offsetWidth
Definition: mythrect.h:73
MythRect::setY
void setY(const QString &sY)
Definition: mythrect.cpp:256
MythRect::m_parentArea
QRect m_parentArea
Definition: mythrect.h:80
MythRect::operator=
MythRect & operator=(const QRect &other)
Definition: mythrect.cpp:20
MythPoint::m_percentY
float m_percentY
Definition: mythrect.h:124
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:107
MythPoint::m_parentArea
QRect m_parentArea
Definition: mythrect.h:130
MythPoint::setX
void setX(const QString &sX)
Definition: mythrect.cpp:531
MythPoint::getX
QString getX(void) const
Definition: mythrect.cpp:551
MythRect::getHeight
QString getHeight(void) const
Definition: mythrect.cpp:377
GetMythUI
MythUIHelper * GetMythUI()
Definition: mythuihelper.cpp:81
MythPoint::getY
QString getY(void) const
Definition: mythrect.cpp:567
mythmainwindow.h
MythMainWindow::NormY
int NormY(int y)
Definition: mythmainwindow.cpp:2241
MythRect::setX
void setX(const QString &sX)
Definition: mythrect.cpp:246
MythPoint
Wrapper around QPoint allowing us to handle percentage and other relative values for positioning in m...
Definition: mythrect.h:89