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