Changeset 59643 in vbox
- Timestamp:
- Feb 12, 2016 10:43:21 AM (9 years ago)
- 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 21 21 22 22 /* Qt includes: */ 23 # include <QDebug>24 23 # include <QPainter> 25 24 # include <QApplication> 25 # include <QAbstractTextDocumentLayout> 26 # include <QTextDocument> 26 27 27 28 /* GUI includes: */ … … 32 33 UIInformationItem::UIInformationItem(QObject *pParent) 33 34 : QStyledItemDelegate(pParent) 34 , m_fontMetrics(QApplication::fontMetrics())35 , m_iMinimumLeftColumnWidth(300)36 , m_textpaneheight(20)37 35 { 36 m_pTextDocument = new QTextDocument(this); 38 37 } 39 38 40 void UIInformationItem::setIcon(const Q Icon &icon) const39 void UIInformationItem::setIcon(const QString &strIcon) const 41 40 { 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(); 58 43 } 59 44 … … 62 47 /* Cache name: */ 63 48 m_strName = strName; 64 m_nameSize = QSize(m_fontMetrics.width(m_strName), m_fontMetrics.height());49 updateTextLayout(); 65 50 } 66 51 … … 104 89 void UIInformationItem::paint(QPainter *pPainter, const QStyleOptionViewItem &option, const QModelIndex &index) const 105 90 { 106 /* Initialize some necessary variables: */ 107 const int iMargin = 2; 108 const int iSpacing = 5; 91 pPainter->save(); 92 updateData(index); 109 93 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()); 116 97 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); 144 99 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();156 100 } 157 101 158 /*QSize UIInformationItem::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const102 QSize UIInformationItem::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const 159 103 { 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 } 164 107 165 QTextLayout* UIInformationItem::buildTextLayout(const QString &strText) const108 void UIInformationItem::updateData(const QModelIndex &index) const 166 109 { 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 } 169 115 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; 116 QString UIInformationItem::htmlData() 117 { 118 return m_pTextDocument->toHtml(); 187 119 } 188 120 189 121 void UIInformationItem::updateTextLayout() const 190 122 { 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>"; 193 130 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 §ionTpl = sSectionBoldTpl; 197 132 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; 202 135 { 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 */ 212 145 } 213 146 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); 228 149 } 229 150 -
trunk/src/VBox/Frontends/VirtualBox/src/runtime/information/UIInformationItem.h
r59484 r59643 32 32 /* Forward declarations: */ 33 33 class QTextLayout; 34 class QTextDocument; 34 35 35 36 /* Typedefs: */ … … 51 52 52 53 /** Defines the icon of information-item as @a icon. */ 53 void setIcon(const Q Icon&icon) const;54 void setIcon(const QString &icon) const; 54 55 55 56 /** Defines the name of information-item as @a strName. */ … … 65 66 66 67 /** 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(); 68 75 69 76 private: 70 /** Builds text-layout using text @a strText. */71 QTextLayout* buildTextLayout(const QString &strText) const;72 73 77 /** Updates text-layout. */ 74 78 void updateTextLayout() const; 75 79 76 80 /** Holds the pixmap of information-item. */ 77 mutable Q Pixmap m_pixmap;81 mutable QString m_strIcon; 78 82 79 83 /** Holds the name of information-item. */ 80 84 mutable QString m_strName; 81 85 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; 92 88 93 89 /** 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; 99 91 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; 104 94 }; 105 95
Note:
See TracChangeset
for help on using the changeset viewer.