VirtualBox

Changeset 59643 in vbox


Ignore:
Timestamp:
Feb 12, 2016 10:43:21 AM (9 years ago)
Author:
vboxsync
Message:

FE/Qt: ​​​bugref:8088: Rework session-information window: UIInformationItem classes cleanup.

Location:
trunk/src/VBox/Frontends/VirtualBox/src/runtime/information
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/runtime/information/UIInformationItem.cpp

    r59484 r59643  
    2121
    2222/* Qt includes: */
    23 # include <QDebug>
    2423# include <QPainter>
    2524# include <QApplication>
     25# include <QAbstractTextDocumentLayout>
     26# include <QTextDocument>
    2627
    2728/* GUI includes: */
     
    3233UIInformationItem::UIInformationItem(QObject *pParent)
    3334    : QStyledItemDelegate(pParent)
    34     , m_fontMetrics(QApplication::fontMetrics())
    35     , m_iMinimumLeftColumnWidth(300)
    36     , m_textpaneheight(20)
    3735{
     36    m_pTextDocument = new QTextDocument(this);
    3837}
    3938
    40 void UIInformationItem::setIcon(const QIcon &icon) const
     39void UIInformationItem::setIcon(const QString &strIcon) const
    4140{
    42     /* Cache icon: */
    43     if (icon.isNull())
    44     {
    45         /* No icon provided: */
    46         m_pixmapSize = QSize();
    47         m_pixmap = QPixmap();
    48     }
    49     else
    50     {
    51         /* Determine default the icon size: */
    52         const QStyle *pStyle = QApplication::style();
    53         const int iIconMetric = pStyle->pixelMetric(QStyle::PM_SmallIconSize);
    54         m_pixmapSize = QSize(iIconMetric, iIconMetric);
    55         /* Acquire the icon of the corresponding size: */
    56         m_pixmap = icon.pixmap(m_pixmapSize);
    57     }
     41    m_strIcon = strIcon;
     42    updateTextLayout();
    5843}
    5944
     
    6247    /* Cache name: */
    6348    m_strName = strName;
    64     m_nameSize = QSize(m_fontMetrics.width(m_strName), m_fontMetrics.height());
     49    updateTextLayout();
    6550}
    6651
     
    10489void UIInformationItem::paint(QPainter *pPainter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    10590{
    106     /* Initialize some necessary variables: */
    107     const int iMargin = 2;
    108     const int iSpacing = 5;
     91    pPainter->save();
     92    updateData(index);
    10993
    110     /* Item rect: */
    111     const QRect optionRect = option.rect;
    112     /* Palette: */
    113     const QPalette pal = option.palette;
    114     /* Font metrics: */
    115     const QFontMetrics fm = option.fontMetrics;
     94    QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &option, pPainter);
     95    pPainter->resetTransform();
     96    pPainter->translate(option.rect.topLeft());
    11697
    117     const int iPositionX = optionRect.topLeft().x() + iMargin;
    118     const int iPositionY = optionRect.topLeft().y() + iMargin;
    119 
    120     /* Set Name: */
    121     m_strName = index.data().toString();
    122     m_nameSize = QSize(fm.width(m_strName), fm.height());
    123 
    124     /* Set Icon: */
    125     setIcon(QIcon(index.data(Qt::DecorationRole).toString()));
    126 
    127     /* Paint pixmap: */
    128     const int iElementPixmapX = iMargin + iPositionX;
    129     const int iElementPixmapY = iPositionY;
    130     pPainter->drawPixmap(QRect(QPoint(iElementPixmapX, iElementPixmapY), m_pixmapSize), m_pixmap);
    131 
    132     /* Paint name: */
    133     const int iNameX = iElementPixmapX + m_pixmapSize.width() + iSpacing;
    134     const int iNameY = optionRect.topLeft().y();
    135     QFont namefont = QApplication::font();
    136     namefont.setWeight(QFont::Bold);
    137     QPoint namePos(iNameX, iNameY);
    138     namePos += QPoint(0, fm.ascent());
    139     const QColor buttonTextColor = pal.color(QPalette::Active, QPalette::ButtonText);
    140     pPainter->save();
    141     pPainter->setFont(namefont);
    142     pPainter->setPen(buttonTextColor);
    143     pPainter->drawText(namePos, m_strName);
     98    m_pTextDocument->drawContents(pPainter);
    14499    pPainter->restore();
    145     setText(index.data(Qt::UserRole+1).value<UITextTable>());
    146 
    147     /* Layout text-pane: */
    148     const int iTextPaneY = qMax(m_pixmapSize.height(), m_nameSize.height()) + iPositionY;
    149     /* Draw all the text-layouts: */
    150     foreach (QTextLayout *pTextLayout, m_leftList)
    151         pTextLayout->draw(pPainter, QPoint(iNameX, iTextPaneY));
    152     foreach (QTextLayout *pTextLayout, m_rightList)
    153         pTextLayout->draw(pPainter, QPoint(iNameX, iTextPaneY));
    154 
    155     m_textpaneheight = m_leftList.count() * fm.lineSpacing();
    156100}
    157101
    158 /*QSize UIInformationItem::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
     102QSize UIInformationItem::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    159103{
    160     const int iWidth = m_nameSize.width() + m_pixmapSize.width() + 10 + m_textpanewidth;
    161     const int iHeight = qMax(m_pixmapSize.height(), m_nameSize.height())+ m_textpaneheight +20;
    162     return QSize(iWidth, iHeight);
    163 }*/
     104    updateData(index);
     105    return m_pTextDocument->size().toSize();
     106}
    164107
    165 QTextLayout* UIInformationItem::buildTextLayout(const QString &strText) const
     108void UIInformationItem::updateData(const QModelIndex &index) const
    166109{
    167     /* Create layout; */
    168     QTextLayout *pTextLayout = new QTextLayout(strText);
     110    setName(index.data().toString());
     111    setIcon(index.data(Qt::DecorationRole).toString());
     112    setText(index.data(Qt::UserRole+1).value<UITextTable>());
     113    m_type = index.data(Qt::UserRole+2).value<InformationElementType>();
     114}
    169115
    170     /* Configure layout: */
    171     QTextOption textOption;
    172     textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
    173     pTextLayout->setTextOption(textOption);
    174 
    175     /* Build layout: */
    176     pTextLayout->beginLayout();
    177     while (1)
    178     {
    179         QTextLine line = pTextLayout->createLine();
    180         if (!line.isValid())
    181             break;
    182     }
    183     pTextLayout->endLayout();
    184 
    185     /* Return layout: */
    186     return pTextLayout;
     116QString UIInformationItem::htmlData()
     117{
     118    return m_pTextDocument->toHtml();
    187119}
    188120
    189121void UIInformationItem::updateTextLayout() const
    190122{
    191     /* Initialize text attributes: */
    192     int iSpacing = 20;
     123    /* Details templates */
     124    static const char *sSectionBoldTpl =
     125        "<tr><td width=22 rowspan=%1 align=left><img width=16 height=16 src='%2'></td>"
     126            "<td><b><nobr>%3</nobr></b></td></tr>"
     127            "%4";
     128    static const char *sSectionItemTpl2 =
     129        "<tr><td width=300><nobr>%1</nobr></td><td/><td>%2</td></tr>";
    193130
    194     /* Clear old text-layouts: */
    195     while (!m_leftList.isEmpty()) delete m_leftList.takeLast();
    196     while (!m_rightList.isEmpty()) delete m_rightList.takeLast();
     131    const QString &sectionTpl = sSectionBoldTpl;
    197132
    198     /* Prepare new text-layouts: */
    199     /* Left layout: */
    200     int iTextY = 0;
    201     foreach (const UITextTableLine &line, m_text)
     133    /* Compose details report: */
     134    QString report;
    202135    {
    203         if (!line.first.isEmpty())
    204         {
    205             m_leftList << buildTextLayout(line.first + ":");
    206             m_leftList.last()->setPosition(QPointF(0, iTextY));
    207         }
    208         /* Indent Y: */
    209         iTextY += m_fontMetrics.lineSpacing() + 1;
    210         /* Re-calculate maximum width: */
    211         m_iMinimumLeftColumnWidth = qMax(m_iMinimumLeftColumnWidth, m_fontMetrics.width(line.first));
     136        QString item;
     137        foreach (const UITextTableLine &line, m_text)
     138            item = item + QString(sSectionItemTpl2).arg(line.first, line.second);
     139
     140        report = sectionTpl
     141                  .arg(m_text.count() + 1) /* rows */
     142                  .arg(m_strIcon, /* icon */
     143                       m_strName, /* title */
     144                       item); /* items */
    212145    }
    213146
    214     /* Right layout: */
    215     iTextY = 0;
    216     foreach (const UITextTableLine &line, m_text)
    217     {
    218         if (!line.second.isEmpty())
    219         {
    220             m_rightList << buildTextLayout(line.second);
    221             m_rightList.last()->setPosition(QPointF(0 + m_iMinimumLeftColumnWidth + iSpacing, iTextY));
    222             m_textpanewidth = qMax(0 + m_iMinimumLeftColumnWidth + iSpacing, m_textpanewidth);
    223         }
    224 
    225         /* Indent Y: */
    226         iTextY += m_fontMetrics.lineSpacing() + 1;
    227     }
     147    /* Set html data: */
     148    m_pTextDocument->setHtml(report);
    228149}
    229150
  • trunk/src/VBox/Frontends/VirtualBox/src/runtime/information/UIInformationItem.h

    r59484 r59643  
    3232/* Forward declarations: */
    3333class QTextLayout;
     34class QTextDocument;
    3435
    3536/* Typedefs: */
     
    5152
    5253    /** Defines the icon of information-item as @a icon. */
    53     void setIcon(const QIcon &icon) const;
     54    void setIcon(const QString &icon) const;
    5455
    5556    /** Defines the name of information-item as @a strName. */
     
    6566
    6667    /** Size-hint calculation routine. */
    67     //QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
     68    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
     69
     70    /** Updates data for the item with @a index. */
     71    void updateData(const QModelIndex &index) const;
     72
     73    /** returns html data. */
     74    QString htmlData();
    6875
    6976private:
    70     /** Builds text-layout using text @a strText. */
    71     QTextLayout* buildTextLayout(const QString &strText) const;
    72 
    7377    /** Updates text-layout. */
    7478    void updateTextLayout() const;
    7579
    7680    /** Holds the pixmap of information-item. */
    77     mutable QPixmap m_pixmap;
     81    mutable QString m_strIcon;
    7882
    7983    /** Holds the name of information-item. */
    8084    mutable QString m_strName;
    8185
    82     /** Holds the size of pixmap of information-item. */
    83     mutable QSize m_pixmapSize;
    84 
    85     /** Holds the size of pixmap of information-item. */
    86     mutable QSize m_nameSize;
    87 
    88     /** Holds the font-metrics. */
    89     QFontMetrics m_fontMetrics;
    90     /** Holds the minimum-width. */
    91     mutable int m_iMinimumLeftColumnWidth;
     86    /** Holds the text-data of information-item. */
     87    mutable UITextTable m_text;
    9288
    9389    /** Holds the text-data of information-item. */
    94     mutable UITextTable m_text;
    95     /** Holds the left text-layout list. */
    96     mutable QList<QTextLayout*> m_leftList;
    97     /** Holds the right text-layout list. */
    98     mutable QList<QTextLayout*> m_rightList;
     90    mutable InformationElementType m_type;
    9991
    100     /** Holds the width of text-pane. */
    101     mutable int m_textpanewidth;
    102     /** Holds the height of text-pane. */
    103     mutable int m_textpaneheight;
     92    /** Holds the instance of text-dcoument we create. */
     93    QTextDocument *m_pTextDocument;
    10494};
    10595
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