00001
00002 #include <QTextStream>
00003
00004 #include "mythrect.h"
00005 #include "mythmainwindow.h"
00006
00007 MythRect::MythRect()
00008 : QRect()
00009 {
00010 Init();
00011 }
00012
00013 MythRect::MythRect(int x, int y, int width, int height)
00014 : QRect(x, y, width, height)
00015 {
00016 Init();
00017 }
00018
00019 MythRect::MythRect(const QString &sX, const QString &sY, const QString &sWidth,
00020 const QString &sHeight)
00021 : QRect()
00022 {
00023 Init();
00024 setRect(sX,sY,sWidth,sHeight);
00025 }
00026
00027 MythRect::MythRect(QRect rect)
00028 : QRect(rect)
00029 {
00030 Init();
00031 }
00032
00033 bool MythRect::operator== (const MythRect &other) const
00034 {
00035 return ((m_percentWidth == other.m_percentWidth) &&
00036 (m_percentHeight == other.m_percentHeight) &&
00037 (m_percentX == other.m_percentX) &&
00038 (m_percentY == other.m_percentY) &&
00039 (m_offsetWidth == other.m_offsetWidth) &&
00040 (m_offsetHeight == other.m_offsetHeight) &&
00041 (m_offsetX == other.m_offsetX) &&
00042 (m_offsetY == other.m_offsetY) &&
00043 (QRect)(*this) == (QRect)other);
00044 }
00045
00046 void MythRect::Init(void)
00047 {
00048 m_needsUpdate = true;
00049 m_percentWidth = m_percentHeight = m_percentX = m_percentY = 0.0;
00050 m_offsetWidth = m_offsetHeight = m_offsetX = m_offsetY = 0;
00051 }
00052
00053 void MythRect::Reset(void)
00054 {
00055 m_parentArea.setRect(0, 0, 0, 0);
00056 }
00057
00058 void MythRect::CalculateArea(const MythRect & parentArea)
00059 {
00060 QRect area = parentArea.toQRect();
00061 if ((m_parentArea == area && !m_needsUpdate) || !parentArea.isValid())
00062 return;
00063
00064 m_parentArea = area;
00065
00066 int w = width();
00067 int h = height();
00068 int X = x();
00069 int Y = y();
00070
00071 if (m_percentX > 0.0)
00072 X = (int) (m_percentX * (float)m_parentArea.width()) + m_offsetX;
00073 if (m_percentY > 0.0)
00074 Y = (int) (m_percentY * (float)m_parentArea.height()) + m_offsetY;
00075 if (m_percentWidth > 0.0)
00076 w = (int) (m_percentWidth * (float)(m_parentArea.width() - X))
00077 + m_offsetWidth;
00078 else if (m_offsetWidth != 0)
00079 w = m_parentArea.width() - X + m_offsetWidth;
00080 if (m_percentHeight > 0.0)
00081 h = (int) (m_percentHeight * (float)(m_parentArea.height() - Y))
00082 + m_offsetHeight;
00083 else if (m_offsetHeight != 0)
00084 h = m_parentArea.height() - Y + m_offsetHeight;
00085
00086 QRect::setRect(X,Y,w,h);
00087
00088 m_needsUpdate = false;
00089 }
00090
00091 void MythRect::NormRect(void)
00092 {
00093
00094 if (m_percentWidth == 0.0)
00095 QRect::setWidth(GetMythMainWindow()->NormX(width()));
00096
00097 if (m_percentHeight == 0.0)
00098 QRect::setHeight(GetMythMainWindow()->NormY(height()));
00099
00100 int X = 0;
00101 if (m_percentX == 0.0)
00102 X = GetMythMainWindow()->NormX(x());
00103
00104 int Y = 0;
00105 if (m_percentY == 0.0)
00106 Y = GetMythMainWindow()->NormY(y());
00107
00108 m_offsetX = GetMythMainWindow()->NormX(m_offsetX);
00109 m_offsetY = GetMythMainWindow()->NormY(m_offsetY);
00110 m_offsetWidth = GetMythMainWindow()->NormX(m_offsetWidth);
00111 m_offsetHeight = GetMythMainWindow()->NormY(m_offsetHeight);
00112
00113 QRect::moveTopLeft(QPoint(X,Y));
00114
00115 normalized();
00116 }
00117
00118 void MythRect::setRect(const QString &sX, const QString &sY,
00119 const QString &sWidth, const QString &sHeight)
00120 {
00121 setX(sX);
00122 setY(sY);
00123 setWidth(sWidth);
00124 setHeight(sHeight);
00125 }
00126
00132 bool MythRect::parsePosition(float & percent, int & offset, int & absolute,
00133 const QString &value, bool is_size)
00134 {
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 percent = 0.0;
00146 offset = 0;
00147 absolute = 0;
00148
00149 if (value.isEmpty())
00150 return true;
00151
00152 int number;
00153 char ch;
00154 QString tmp(value);
00155 QTextStream is(&tmp);
00156
00157 is >> number;
00158 if (is.status() != QTextStream::Ok)
00159 return true;
00160
00161 is.skipWhiteSpace();
00162 is >> ch;
00163 if (ch != '%')
00164 {
00165 if (is_size && number < 0)
00166 {
00167 offset = number;
00168 return false;
00169 }
00170 absolute = number;
00171 return true;
00172 }
00173
00174 percent = static_cast<float>(number) / 100.0;
00175 is >> offset;
00176 return false;
00177 }
00178
00179 void MythRect::setX(const QString &sX)
00180 {
00181 int absoluteX;
00182
00183 if (parsePosition(m_percentX, m_offsetX, absoluteX, sX, false))
00184 QRect::setX(absoluteX);
00185 else
00186 m_needsUpdate = true;
00187 }
00188
00189 void MythRect::setY(const QString &sY)
00190 {
00191 int absoluteY;
00192
00193 if (parsePosition(m_percentY, m_offsetY, absoluteY, sY, false))
00194 QRect::setY(absoluteY);
00195 else
00196 m_needsUpdate = true;
00197 }
00198
00199 void MythRect::setWidth(const QString &sWidth)
00200 {
00201 int absoluteWidth;
00202
00203 if (parsePosition(m_percentWidth, m_offsetWidth, absoluteWidth,
00204 sWidth, true))
00205 QRect::setWidth(absoluteWidth);
00206 else
00207 m_needsUpdate = true;
00208 }
00209
00210 void MythRect::setHeight(const QString &sHeight)
00211 {
00212 int absoluteHeight;
00213
00214 if (parsePosition(m_percentHeight, m_offsetHeight, absoluteHeight,
00215 sHeight, true))
00216 QRect::setHeight(absoluteHeight);
00217 else
00218 m_needsUpdate = true;
00219 }
00220
00221 MythPoint MythRect::topLeft(void) const
00222 {
00223 MythPoint point;
00224 point.setX(getX());
00225 point.setY(getY());
00226 return point;
00227 }
00228
00229 void MythRect::moveTopLeft(const MythPoint &point)
00230 {
00231 moveLeft(point.getX());
00232 moveTop(point.getY());
00233 }
00234
00235 void MythRect::moveLeft(const QString &sX)
00236 {
00237 int absoluteX;
00238
00239 if (parsePosition(m_percentX, m_offsetX, absoluteX, sX, false))
00240 QRect::moveLeft(absoluteX);
00241 else
00242 m_needsUpdate = true;
00243 }
00244
00245 void MythRect::moveTop(const QString &sY)
00246 {
00247 int absoluteY;
00248
00249 if (parsePosition(m_percentY, m_offsetY, absoluteY, sY, false))
00250 QRect::moveTop(absoluteY);
00251 else
00252 m_needsUpdate = true;
00253 }
00254
00255 QString MythRect::getX(void) const
00256 {
00257 QString stringX;
00258 if (m_percentX > 0.0)
00259 stringX = QString("%1%").arg((int)(m_percentX * 100));
00260 else
00261 stringX = QString("%1").arg(x() - m_offsetX);
00262 if (m_offsetX != 0)
00263 {
00264 if (m_offsetX > 0)
00265 stringX += '+';
00266 stringX += QString("%1").arg(m_offsetX);
00267 }
00268 return stringX;
00269 }
00270
00271 QString MythRect::getY(void) const
00272 {
00273 QString stringY;
00274 if (m_percentY > 0.0)
00275 stringY = QString("%1%").arg((int)(m_percentY * 100));
00276 else
00277 stringY = QString("%1").arg(y() - m_offsetY);
00278 if (m_offsetY != 0)
00279 {
00280 if (m_offsetY > 0)
00281 stringY += '+';
00282 stringY += QString("%1").arg(m_offsetY);
00283 }
00284 return stringY;
00285 }
00286
00287 QString MythRect::getWidth(void) const
00288 {
00289 QString stringWidth;
00290 if (m_percentWidth > 0.0)
00291 stringWidth = QString("%1%").arg((int)(m_percentWidth * 100));
00292 else
00293 stringWidth = QString("%1").arg(width() - m_offsetWidth);
00294 if (m_offsetWidth != 0)
00295 {
00296 if (m_offsetWidth > 0)
00297 stringWidth += '+';
00298 stringWidth += QString("%1").arg(m_offsetWidth);
00299 }
00300 return stringWidth;
00301 }
00302
00303 QString MythRect::getHeight(void) const
00304 {
00305 QString stringHeight;
00306 if (m_percentHeight > 0.0)
00307 stringHeight = QString("%1%").arg((int)(m_percentHeight * 100));
00308 else
00309 stringHeight = QString("%1").arg(height() - m_offsetHeight);
00310 if (m_offsetHeight != 0)
00311 {
00312 if (m_offsetHeight > 0)
00313 stringHeight += '+';
00314 stringHeight += QString("%1").arg(m_offsetHeight);
00315 }
00316 return stringHeight;
00317 }
00318
00319 QString MythRect::toString(bool details) const
00320 {
00321 QString result = QString("(%1,%2,%3,%4)")
00322 .arg(x()).arg(y()).arg(width()).arg(height());
00323
00324 if (details)
00325 result += QString(" [%1,%2,%3,%4]")
00326 .arg(getX()).arg(getY()).arg(getWidth()).arg(getHeight());
00327
00328 return result;
00329 }
00330
00331 QRect MythRect::toQRect() const
00332 {
00333 return QRect(x(),y(),width(),height());
00334 }
00335
00337
00338 MythPoint::MythPoint()
00339 : QPoint()
00340 {
00341 Init();
00342 valid = false;
00343 }
00344
00345 MythPoint::MythPoint(int x, int y)
00346 : QPoint(x, y)
00347 {
00348 Init();
00349 }
00350
00351 MythPoint::MythPoint(const QString &sX, const QString &sY)
00352 : QPoint()
00353 {
00354 Init();
00355 setX(sX);
00356 setY(sY);
00357 }
00358
00359 MythPoint::MythPoint(QPoint point)
00360 : QPoint(point)
00361 {
00362 Init();
00363 }
00364
00365 void MythPoint::Init()
00366 {
00367 m_needsUpdate = true;
00368 m_percentX = m_percentY = 0.0;
00369 m_offsetX = m_offsetY = 0;
00370 valid = true;
00371 }
00372
00373 void MythPoint::CalculatePoint(const MythRect & parentArea)
00374 {
00375 QRect area = parentArea.toQRect();
00376 if ((m_parentArea == area && !m_needsUpdate) || !parentArea.isValid())
00377 return;
00378
00379 m_parentArea = area;
00380
00381 int X = x();
00382 int Y = y();
00383
00384 if (m_percentX > 0.0)
00385 X = (int) (m_percentX * (float)m_parentArea.width()) + m_offsetX;
00386 if (m_percentY > 0.0)
00387 Y = (int) (m_percentY * (float)m_parentArea.height()) + m_offsetY;
00388
00389 QPoint::setX(X);
00390 QPoint::setY(Y);
00391
00392 m_needsUpdate = false;
00393 valid = true;
00394 }
00395
00396 void MythPoint::NormPoint(void)
00397 {
00398 if (m_percentX == 0.0)
00399 QPoint::setX(GetMythMainWindow()->NormX(x()));
00400
00401 if (m_percentY == 0.0)
00402 QPoint::setY(GetMythMainWindow()->NormY(y()));
00403
00404 m_offsetX = GetMythMainWindow()->NormX(m_offsetX);
00405 m_offsetY = GetMythMainWindow()->NormY(m_offsetY);
00406 }
00407
00413 bool MythPoint::parsePosition(float & percent, int & offset, int & absolute,
00414 const QString &value)
00415 {
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426 percent = 0.0;
00427 offset = 0;
00428 absolute = 0;
00429
00430 if (value.isEmpty())
00431 return true;
00432
00433 int number;
00434 char ch;
00435 QString tmp(value);
00436 QTextStream is(&tmp);
00437
00438 is >> number;
00439 if (is.status() != QTextStream::Ok)
00440 return true;
00441
00442 is.skipWhiteSpace();
00443 is >> ch;
00444 if (ch != '%')
00445 {
00446 absolute = number;
00447 return true;
00448 }
00449
00450 percent = static_cast<float>(number) / 100.0;
00451 is >> offset;
00452 return false;
00453 }
00454
00455 void MythPoint::setX(const QString &sX)
00456 {
00457 int absoluteX;
00458
00459 if (parsePosition(m_percentX, m_offsetX, absoluteX, sX))
00460 QPoint::setX(absoluteX);
00461 else
00462 m_needsUpdate = true;
00463 }
00464
00465 void MythPoint::setY(const QString &sY)
00466 {
00467 int absoluteY;
00468
00469 if (parsePosition(m_percentY, m_offsetY, absoluteY, sY))
00470 QPoint::setY(absoluteY);
00471 else
00472 m_needsUpdate = true;
00473 }
00474
00475 QString MythPoint::getX(void) const
00476 {
00477 QString stringX;
00478 if (m_percentX > 0.0)
00479 stringX = QString("%1%").arg((int)(m_percentX * 100));
00480 else
00481 stringX = QString("%1").arg(x() - m_offsetX);
00482 if (m_offsetX != 0)
00483 {
00484 if (m_offsetX > 0)
00485 stringX += '+';
00486 stringX += QString("%1").arg(m_offsetX);
00487 }
00488 return stringX;
00489 }
00490
00491 QString MythPoint::getY(void) const
00492 {
00493 QString stringY;
00494 if (m_percentY > 0.0)
00495 stringY = QString("%1%").arg((int)(m_percentY * 100));
00496 else
00497 stringY = QString("%1").arg(y() - m_offsetY);
00498 if (m_offsetY != 0)
00499 {
00500 if (m_offsetY > 0)
00501 stringY += '+';
00502 stringY += QString("%1").arg(m_offsetY);
00503 }
00504 return stringY;
00505 }
00506
00507 QString MythPoint::toString(bool details) const
00508 {
00509 QString result = QString("(%1,%2)")
00510 .arg(x()).arg(y());
00511
00512 if (details)
00513 result += QString(" [%1,%2]")
00514 .arg(getX()).arg(getY());
00515
00516 return result;
00517 }
00518
00519 QPoint MythPoint::toQPoint() const
00520 {
00521 return QPoint(x(),y());
00522 }
00523