- // +------+
- // | Icon |
- // +------+
- //
- // Name role that
- // might get wrapped above
- // several lines.
- // Additional role 1
- // Additional role 2
-
- const QHash<QByteArray, QVariant> values = data();
-
- const KItemListStyleOption& option = styleOption();
- const qreal maxWidth = size().width() - 2 * option.margin;
- const qreal widgetHeight = size().height();
- const qreal fontHeight = option.fontMetrics.height();
-
- // Initialize properties for the "name" role. It will be used as anchor
- // for initializing the position of the other roles.
- m_text[Name].setText(KStringHandler::preProcessWrap(values["name"].toString()));
-
- // Calculate the number of lines required for the name and the required width
- int textLinesCountForName = 0;
- qreal requiredWidthForName = 0;
- QTextLine line;
-
- QTextLayout layout(m_text[Name].text(), option.font);
- layout.setTextOption(m_text[Name].textOption());
- layout.beginLayout();
- while ((line = layout.createLine()).isValid()) {
- line.setLineWidth(maxWidth);
- requiredWidthForName = qMax(requiredWidthForName, line.naturalTextWidth());
- ++textLinesCountForName;
- }
- layout.endLayout();
-
- // Use one line for each additional information
- int textLinesCount = textLinesCountForName;
- const int additionalRolesCount = qMax(visibleRoles().count() - 1, 0);
- textLinesCount += additionalRolesCount;
-
- m_text[Name].setTextWidth(maxWidth);
- m_textPos[Name] = QPointF(option.margin, widgetHeight - textLinesCount * fontHeight - option.margin);
- m_textRect = QRectF(option.margin + (maxWidth - requiredWidthForName) / 2,
- m_textPos[Name].y(),
- requiredWidthForName,
- textLinesCountForName * fontHeight);
-
- // Calculate the position for each additional information
- qreal y = m_textPos[Name].y() + textLinesCountForName * fontHeight;
- foreach (const QByteArray& role, m_sortedVisibleRoles) {
- const TextId textId = roleTextId(role);
- if (textId == Name) {
- continue;
- }
-
- const QString text = roleText(role, values);
- m_text[textId].setText(text);
-
- qreal requiredWidth = 0;
-
- QTextLayout layout(text, option.font);
- layout.setTextOption(m_text[textId].textOption());
- layout.beginLayout();
- QTextLine textLine = layout.createLine();
- if (textLine.isValid()) {
- textLine.setLineWidth(maxWidth);
- requiredWidth = textLine.naturalTextWidth();
- if (textLine.textLength() < text.length()) {
- // TODO: QFontMetrics::elidedText() works different regarding the given width
- // in comparison to QTextLine::setLineWidth(). It might happen that the text does
- // not get elided although it does not fit into the given width. As workaround
- // the margin is substracted.
- const QString elidedText = option.fontMetrics.elidedText(text, Qt::ElideRight, maxWidth - option.margin);
- m_text[textId].setText(elidedText);
- }
- }
- layout.endLayout();
-
- m_textPos[textId] = QPointF(option.margin, y);
- m_text[textId].setTextWidth(maxWidth);
-
- const QRectF textRect(option.margin + (maxWidth - requiredWidth) / 2, y, requiredWidth, fontHeight);
- m_textRect |= textRect;