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