Ticket #9295: mythtv-0.24-stretching-cutdown-code.diff
File mythtv-0.24-stretching-cutdown-code.diff, 6.6 KB (added by , 13 years ago) |
---|
-
libs/libmythui/mythuitext.h
74 74 bool GetCutDown(void) const { return m_Cutdown; } 75 75 void SetMultiLine(bool multiline); 76 76 bool GetMultiLine(void) const { return m_MultiLine; } 77 void SetStretch(bool stretch); 78 bool GetStretch(void) const { return m_Stretch; } 77 79 78 80 void SetArea(const MythRect &rect); 79 81 void SetPosition(const MythPoint &pos); … … 85 87 86 88 bool MakeNarrow(QRect &min_rect); 87 89 void FillCutMessage(void); 88 QString cutDown(const QString &data, MythFontProperties *font,90 bool IsTextFitting(const QString &text, MythFontProperties *font, 89 91 bool multiline = false); 90 92 91 93 int m_Justification; … … 100 102 101 103 bool m_Cutdown; 102 104 bool m_MultiLine; 105 bool m_Stretch; 106 int m_minStretch; 107 int m_maxStretch; 103 108 104 109 MythFontProperties* m_Font; 105 110 QMap<QString, MythFontProperties> m_FontStates; -
libs/libmythui/mythuitext.cpp
35 35 m_scrolling = false; 36 36 m_scrollDirection = ScrollLeft; 37 37 m_textCase = CaseNormal; 38 m_Stretch = false; 39 m_minStretch = 100; 40 m_maxStretch = 100; 38 41 39 42 m_FontStates.insert("default", MythFontProperties()); 40 43 *m_Font = m_FontStates["default"]; … … 59 62 m_scrolling = false; 60 63 m_scrollDirection = ScrollLeft; 61 64 m_textCase = CaseNormal; 65 m_Stretch = false; 66 m_minStretch = 100; 67 m_maxStretch = 100; 62 68 63 69 SetArea(displayRect); 64 70 m_FontStates.insert("default", font); … … 222 228 SetRedraw(); 223 229 } 224 230 231 void MythUIText::SetStretch(bool stretch) 232 { 233 m_Stretch = stretch; 234 FillCutMessage(); 235 SetRedraw(); 236 } 237 225 238 void MythUIText::SetArea(const MythRect &rect) 226 239 { 227 240 MythUIType::SetArea(rect); … … 380 393 } 381 394 } 382 395 396 bool MythUIText::IsTextFitting(const QString &text, MythFontProperties *font, bool multiline) { 397 if (multiline) { 398 int maxwidth = GetArea().width(); 399 int maxheight = GetArea().height(); 400 int justification = Qt::AlignLeft | Qt::TextWordWrap; 401 QFontMetrics fm(font->face()); 402 403 int height = fm.boundingRect(0, 0, maxwidth, maxheight, 404 justification, text 405 ).height(); 406 return height <= maxheight; 407 } else { 408 int maxwidth = GetArea().width(); 409 QFontMetrics fm = QFontMetrics(m_Font->face()); 410 int width=fm.width(text, text.length()); 411 return width <= maxwidth; 412 } 413 } 414 383 415 void MythUIText::FillCutMessage() 384 416 { 385 417 m_CutMessage.clear(); … … 445 477 SetDrawRectSize(m_MinArea.width(), m_MinArea.height()); 446 478 } 447 479 448 if (m_Cutdown) 449 m_CutMessage = cutDown(m_CutMessage, m_Font, m_MultiLine); 480 // If we played with stretch, we need to reset to 100% before continuing 481 // This is because sometime, the font may not resetted correctly when 482 // switching text with SetText or other calls. 483 int stretch = 100; 484 if (m_Stretch) 485 m_Font->AdjustStretch(100); 486 bool fit = IsTextFitting(m_CutMessage, m_Font, m_MultiLine); 487 488 if (m_Stretch) { 489 // Can we extend the text ? 490 while (fit && stretch < m_maxStretch) { 491 m_Font->AdjustStretch(++stretch); 492 fit = IsTextFitting(m_CutMessage, m_Font, m_MultiLine); 493 } 494 // Can we shrink the text ? 495 // This will correct the 'overextend' above as extend will always 496 // extend a little to much (loop until it doesn't fit). 497 while (!fit && stretch > m_minStretch) { 498 m_Font->AdjustStretch(--stretch); 499 fit = IsTextFitting(m_CutMessage, m_Font, m_MultiLine); 500 } 501 } 502 503 // Do we need to cut the text ? 504 if (!fit && m_Cutdown) { 505 QString dotdotdot = QString("..."); 506 for (int index=m_CutMessage.length() - 1; index>0 && !fit; index--) { 507 m_CutMessage.truncate(index); 508 m_CutMessage.append(dotdotdot); 509 fit = IsTextFitting(m_CutMessage, m_Font, m_MultiLine); 510 } 511 } 450 512 } 451 513 452 514 void MythUIText::Pulse(void) … … 540 602 SetRedraw(); 541 603 } 542 604 543 QString MythUIText::cutDown(const QString &data, MythFontProperties *font,544 bool multiline)545 {546 int length = data.length();547 if (length == 0)548 return data;549 550 int maxwidth = GetArea().width();551 int maxheight = GetArea().height();552 int justification = Qt::AlignLeft | Qt::TextWordWrap;553 QFontMetrics fm(font->face());554 555 int margin = length - 1;556 int index = 0;557 int diff = 0;558 559 while (margin > 0)560 {561 if (multiline)562 diff = maxheight - fm.boundingRect(0, 0, maxwidth, maxheight,563 justification,564 data.left(index + margin + 1)565 ).height();566 else567 diff = maxwidth - fm.width(data, index + margin + 1);568 if (diff >= 0)569 index += margin;570 571 margin /= 2;572 573 if (index + margin >= length - 1)574 margin = (length - 1) - index;575 }576 577 if (index < length - 1)578 {579 QString tmpStr(data);580 tmpStr.truncate(index);581 if (index >= 3)582 tmpStr.replace(index - 3, 3, "...");583 return tmpStr;584 }585 586 return data;587 588 }589 590 605 bool MythUIText::ParseElement( 591 606 const QString &filename, QDomElement &element, bool showWarnings) 592 607 { … … 721 736 m_textCase = CaseNormal; 722 737 FillCutMessage(); 723 738 } 739 else if (element.tagName() == "stretch") 740 { 741 QString stretch = getFirstText(element); 742 if (!stretch.isEmpty()) 743 m_Stretch = parseBool(stretch); 744 QString tmp = element.attribute("min"); 745 if (!tmp.isEmpty()) 746 m_minStretch = tmp.toInt(); 747 tmp = element.attribute("max"); 748 if (!tmp.isEmpty()) 749 m_maxStretch = tmp.toInt(); 750 } 724 751 else 725 752 { 726 753 return MythUIType::ParseElement(filename, element, showWarnings); … … 750 777 751 778 m_Cutdown = text->m_Cutdown; 752 779 m_MultiLine = text->m_MultiLine; 780 m_Stretch = text->m_Stretch; 781 m_minStretch = text->m_minStretch; 782 m_maxStretch = text->m_maxStretch; 753 783 754 784 QMutableMapIterator<QString, MythFontProperties> it(text->m_FontStates); 755 785 while (it.hasNext())