VirtualBox

Changeset 50847 in vbox


Ignore:
Timestamp:
Mar 21, 2014 9:40:21 PM (11 years ago)
Author:
vboxsync
Message:

FE/Qt: UIGraphicsTextPane: Text-layout update optimization.

Location:
trunk/src/VBox/Frontends/VirtualBox/src/widgets/graphics
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/widgets/graphics/UIGraphicsTextPane.cpp

    r50846 r50847  
    3636}
    3737
     38UIGraphicsTextPane::~UIGraphicsTextPane()
     39{
     40    /* Clear text-layouts: */
     41    while (!m_leftList.isEmpty()) delete m_leftList.takeLast();
     42    while (!m_rightList.isEmpty()) delete m_rightList.takeLast();
     43}
     44
    3845void UIGraphicsTextPane::setText(const UITextTable &text)
    3946{
     
    6471    }
    6572
     73    /* Update text-layout: */
     74    updateTextLayout(true);
     75
    6676    /* Update minimum size-hint: */
    67     updateMinimumTextWidthHint();
    68     updateMinimumTextHeightHint();
    6977    updateGeometry();
    7078}
    7179
    72 void UIGraphicsTextPane::updateGeometry()
    73 {
    74     /* Discard cached minimum size-hint: */
    75     m_fMinimumSizeHintInvalidated = true;
    76 
    77     /* Call to base-class to notify layout if any: */
    78     QIGraphicsWidget::updateGeometry();
    79 
    80     /* And notify listeners which are not layouts: */
    81     emit sigGeometryChanged();
    82 }
    83 
    84 void UIGraphicsTextPane::updateMinimumTextWidthHint()
     80void UIGraphicsTextPane::updateTextLayout(bool fFull /* = false */)
    8581{
    8682    /* Prepare variables: */
    8783    QFontMetrics fm(font(), m_pPaintDevice);
    88 
    89     /* Search for the maximum line widths: */
    90     int iMaximumLeftLineWidth = 0;
    91     int iMaximumRightLineWidth = 0;
     84    int iMaximumTextWidth = (int)size().width() - 2 * m_iMargin - m_iSpacing;
     85
     86    /* Search for the maximum column widths: */
     87    int iMaximumLeftColumnWidth = 0;
     88    int iMaximumRightColumnWidth = 0;
    9289    bool fSingleColumnText = true;
    9390    foreach (const UITextTableLine &line, m_text)
     
    9895        QString strLeftLine = fRightColumnPresent ? line.first + ":" : line.first;
    9996        QString strRightLine = line.second;
    100         iMaximumLeftLineWidth = qMax(iMaximumLeftLineWidth, fm.width(strLeftLine));
    101         iMaximumRightLineWidth = qMax(iMaximumRightLineWidth, fm.width(strRightLine));
    102     }
    103     iMaximumLeftLineWidth += 1;
    104     iMaximumRightLineWidth += 1;
    105 
    106     /* Calculate minimum-text-width: */
    107     int iMinimumTextWidth = 0;
    108     if (fSingleColumnText)
    109     {
    110         /* Take into account only left column: */
    111         int iMinimumLeftColumnWidth = qMin(iMaximumLeftLineWidth, m_iMinimumTextColumnWidth);
    112         iMinimumTextWidth = iMinimumLeftColumnWidth;
    113     }
    114     else
    115     {
    116         /* Take into account both columns, but wrap only right one: */
    117         int iMinimumLeftColumnWidth = iMaximumLeftLineWidth;
    118         int iMinimumRightColumnWidth = qMin(iMaximumRightLineWidth, m_iMinimumTextColumnWidth);
    119         iMinimumTextWidth = iMinimumLeftColumnWidth + m_iSpacing + iMinimumRightColumnWidth;
    120     }
    121 
    122     /* Make sure something changed: */
    123     if (m_iMinimumTextWidth == iMinimumTextWidth)
    124         return;
    125 
    126     /* Remember new value: */
    127     m_iMinimumTextWidth = iMinimumTextWidth;
    128 }
    129 
    130 void UIGraphicsTextPane::updateMinimumTextHeightHint()
    131 {
    132     /* Prepare variables: */
    133     int iMaximumTextWidth = (int)size().width() - 2 * m_iMargin - m_iSpacing;
    134     QFontMetrics fm(font(), m_pPaintDevice);
    135 
    136     /* Search for the maximum line widths: */
    137     int iMaximumLeftLineWidth = 0;
    138     int iMaximumRightLineWidth = 0;
    139     bool fSingleColumnText = true;
    140     foreach (const UITextTableLine &line, m_text)
    141     {
    142         bool fRightColumnPresent = !line.second.isEmpty();
    143         if (fRightColumnPresent)
    144             fSingleColumnText = false;
    145         QString strFirstLine = fRightColumnPresent ? line.first + ":" : line.first;
    146         QString strSecondLine = line.second;
    147         iMaximumLeftLineWidth = qMax(iMaximumLeftLineWidth, fm.width(strFirstLine));
    148         iMaximumRightLineWidth = qMax(iMaximumRightLineWidth, fm.width(strSecondLine));
    149     }
    150     iMaximumLeftLineWidth += 1;
    151     iMaximumRightLineWidth += 1;
    152 
    153     /* Calculate column widths: */
     97        iMaximumLeftColumnWidth = qMax(iMaximumLeftColumnWidth, fm.width(strLeftLine));
     98        iMaximumRightColumnWidth = qMax(iMaximumRightColumnWidth, fm.width(strRightLine));
     99    }
     100    iMaximumLeftColumnWidth += 1;
     101    iMaximumRightColumnWidth += 1;
     102
     103    /* Calculate text attributes: */
    154104    int iLeftColumnWidth = 0;
    155105    int iRightColumnWidth = 0;
     106    /* Left column only: */
    156107    if (fSingleColumnText)
    157108    {
    158         /* Take into account only left column: */
     109        /* Full update? */
     110        if (fFull)
     111        {
     112            /* Minimum width for left column: */
     113            int iMinimumLeftColumnWidth = qMin(m_iMinimumTextColumnWidth, iMaximumLeftColumnWidth);
     114            /* Minimum width for whole text: */
     115            m_iMinimumTextWidth = iMinimumLeftColumnWidth;
     116        }
     117
     118        /* Current width for left column: */
    159119        iLeftColumnWidth = qMax(m_iMinimumTextColumnWidth, iMaximumTextWidth);
    160120    }
     121    /* Two columns: */
    161122    else
    162123    {
    163         /* Take into account both columns, but wrap only right one: */
    164         iLeftColumnWidth = iMaximumLeftLineWidth;
     124        /* Full update? */
     125        if (fFull)
     126        {
     127            /* Minimum width for left column: */
     128            int iMinimumLeftColumnWidth = iMaximumLeftColumnWidth;
     129            /* Minimum width for right column: */
     130            int iMinimumRightColumnWidth = qMin(m_iMinimumTextColumnWidth, iMaximumRightColumnWidth);
     131            /* Minimum width for whole text: */
     132            m_iMinimumTextWidth = iMinimumLeftColumnWidth + m_iSpacing + iMinimumRightColumnWidth;
     133        }
     134
     135        /* Current width for left column: */
     136        iLeftColumnWidth = iMaximumLeftColumnWidth;
     137        /* Current width for right column: */
    165138        iRightColumnWidth = iMaximumTextWidth - iLeftColumnWidth;
    166139    }
    167140
    168     /* Calculate minimum-text-height: */
    169     int iMinimumTextHeight = 0;
    170     foreach (const UITextTableLine line, m_text)
    171     {
    172         /* First layout: */
     141    /* Clear old text-layouts: */
     142    while (!m_leftList.isEmpty()) delete m_leftList.takeLast();
     143    while (!m_rightList.isEmpty()) delete m_rightList.takeLast();
     144
     145    /* Prepare new text-layouts: */
     146    int iTextX = m_iMargin;
     147    int iTextY = m_iMargin;
     148    /* Populate text-layouts: */
     149    m_iMinimumTextHeight = 0;
     150    foreach (const UITextTableLine &line, m_text)
     151    {
     152        /* Left layout: */
    173153        int iLeftColumnHeight = 0;
    174154        if (!line.first.isEmpty())
    175155        {
    176156            bool fRightColumnPresent = !line.second.isEmpty();
    177             QTextLayout *pTextLayout = buildTextLayout(font(), m_pPaintDevice,
    178                                                        fRightColumnPresent ? line.first + ":" : line.first,
    179                                                        iLeftColumnWidth, iLeftColumnHeight);
    180             delete pTextLayout;
    181         }
    182 
    183         /* Second layout: */
     157            m_leftList << buildTextLayout(font(), m_pPaintDevice,
     158                                          fRightColumnPresent ? line.first + ":" : line.first,
     159                                          iLeftColumnWidth, iLeftColumnHeight);
     160            m_leftList.last()->setPosition(QPointF(iTextX, iTextY));
     161        }
     162
     163        /* Right layout: */
    184164        int iRightColumnHeight = 0;
    185165        if (!line.second.isEmpty())
    186166        {
    187             QTextLayout *pTextLayout = buildTextLayout(font(), m_pPaintDevice, line.second,
    188                                                        iRightColumnWidth, iRightColumnHeight);
    189             delete pTextLayout;
    190         }
    191 
     167            m_rightList << buildTextLayout(font(), m_pPaintDevice,
     168                                           line.second,
     169                                           iRightColumnWidth, iRightColumnHeight);
     170            m_rightList.last()->setPosition(QPointF(iTextX + iLeftColumnWidth + m_iSpacing, iTextY));
     171        }
     172
     173        /* Maximum colum height? */
     174        int iMaximumColumnHeight = qMax(iLeftColumnHeight, iRightColumnHeight);
     175
     176        /* Indent Y: */
     177        iTextY += iMaximumColumnHeight;
    192178        /* Append summary text height: */
    193         iMinimumTextHeight += qMax(iLeftColumnHeight, iRightColumnHeight);
    194     }
    195 
    196     /* Make sure something changed: */
    197     if (m_iMinimumTextHeight == iMinimumTextHeight)
    198         return;
    199 
    200     /* Remember new value: */
    201     m_iMinimumTextHeight = iMinimumTextHeight;
     179        m_iMinimumTextHeight += iMaximumColumnHeight;
     180    }
     181}
     182
     183void UIGraphicsTextPane::updateGeometry()
     184{
     185    /* Discard cached minimum size-hint: */
     186    m_fMinimumSizeHintInvalidated = true;
     187
     188    /* Call to base-class to notify layout if any: */
     189    QIGraphicsWidget::updateGeometry();
     190
     191    /* And notify listeners which are not layouts: */
     192    emit sigGeometryChanged();
    202193}
    203194
     
    224215void UIGraphicsTextPane::resizeEvent(QGraphicsSceneResizeEvent*)
    225216{
    226     /* Update minimum height-hint: */
    227     updateMinimumTextHeightHint();
     217    /* Update text-layout: */
     218    updateTextLayout();
     219
     220    /* Update minimum size-hint: */
    228221    updateGeometry();
    229222}
     
    231224void UIGraphicsTextPane::paint(QPainter *pPainter, const QStyleOptionGraphicsItem*, QWidget*)
    232225{
    233     /* Prepare variables: */
    234     int iMaximumTextWidth = (int)size().width() - 2 * m_iMargin - m_iSpacing;
    235     QFontMetrics fm(font(), m_pPaintDevice);
    236 
    237     /* Where to paint? */
    238     int iTextX = m_iMargin;
    239     int iTextY = m_iMargin;
    240 
    241     /* Search for the maximum line widths: */
    242     int iMaximumLeftLineWidth = 0;
    243     int iMaximumRightLineWidth = 0;
    244     bool fSingleColumnText = true;
    245     foreach (const UITextTableLine line, m_text)
    246     {
    247         bool fRightColumnPresent = !line.second.isEmpty();
    248         if (fRightColumnPresent)
    249             fSingleColumnText = false;
    250         QString strFirstLine = fRightColumnPresent ? line.first + ":" : line.first;
    251         QString strSecondLine = line.second;
    252         iMaximumLeftLineWidth = qMax(iMaximumLeftLineWidth, fm.width(strFirstLine));
    253         iMaximumRightLineWidth = qMax(iMaximumRightLineWidth, fm.width(strSecondLine));
    254     }
    255     iMaximumLeftLineWidth += 1;
    256     iMaximumRightLineWidth += 1;
    257 
    258     /* Calculate column widths: */
    259     int iLeftColumnWidth = 0;
    260     int iRightColumnWidth = 0;
    261     if (fSingleColumnText)
    262     {
    263         /* Take into account only left column: */
    264         iLeftColumnWidth = qMax(m_iMinimumTextColumnWidth, iMaximumTextWidth);
    265     }
    266     else
    267     {
    268         /* Take into account both columns, but wrap only right one: */
    269         iLeftColumnWidth = iMaximumLeftLineWidth;
    270         iRightColumnWidth = iMaximumTextWidth - iLeftColumnWidth;
    271     }
    272 
    273     /* For each the line: */
    274     foreach (const UITextTableLine line, m_text)
    275     {
    276         /* First layout: */
    277         int iLeftColumnHeight = 0;
    278         if (!line.first.isEmpty())
    279         {
    280             bool fRightColumnPresent = !line.second.isEmpty();
    281             QTextLayout *pTextLayout = buildTextLayout(font(), m_pPaintDevice,
    282                                                        fRightColumnPresent ? line.first + ":" : line.first,
    283                                                        iLeftColumnWidth, iLeftColumnHeight);
    284             pTextLayout->draw(pPainter, QPointF(iTextX, iTextY));
    285             delete pTextLayout;
    286         }
    287 
    288         /* Second layout: */
    289         int iRightColumnHeight = 0;
    290         if (!line.second.isEmpty())
    291         {
    292             QTextLayout *pTextLayout = buildTextLayout(font(), m_pPaintDevice,
    293                                                        line.second, iRightColumnWidth, iRightColumnHeight);
    294             pTextLayout->draw(pPainter, QPointF(iTextX + iLeftColumnWidth + m_iSpacing, iTextY));
    295             delete pTextLayout;
    296         }
    297 
    298         /* Indent Y: */
    299         iTextY += qMax(iLeftColumnHeight, iRightColumnHeight);
    300     }
     226    /* Draw all the text-layouts: */
     227    foreach (QTextLayout *pTextLayout, m_leftList)
     228        pTextLayout->draw(pPainter, QPoint(0, 0));
     229    foreach (QTextLayout *pTextLayout, m_rightList)
     230        pTextLayout->draw(pPainter, QPoint(0, 0));
    301231}
    302232
  • trunk/src/VBox/Frontends/VirtualBox/src/widgets/graphics/UIGraphicsTextPane.h

    r50846 r50847  
    4343    /** Graphics text-pane constructor. */
    4444    UIGraphicsTextPane(QIGraphicsWidget *pParent, QPaintDevice *pPaintDevice);
     45    /** Graphics text-pane destructor. */
     46    ~UIGraphicsTextPane();
    4547
    4648    /** Returns whether contained text is empty. */
     
    5355private:
    5456
     57    /** Update text-layout. */
     58    void updateTextLayout(bool fFull = false);
     59
    5560    /** Notifies listeners about size-hint changes. */
    5661    void updateGeometry();
    57     /** Updates minimum text width hint. */
    58     void updateMinimumTextWidthHint();
    59     /** Updates minimum text height hint. */
    60     void updateMinimumTextHeightHint();
    6162    /** Returns the size-hint to constrain the content. */
    6263    QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
     
    9394    /** Contained text. */
    9495    UITextTable m_text;
     96    /** Left text-layout list. */
     97    QList<QTextLayout*> m_leftList;
     98    /** Right text-layout list. */
     99    QList<QTextLayout*> m_rightList;
    95100};
    96101
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette