const int infoWidth = width() / 2;
const int labelWidth = infoWidth - 2 * Spacing;
const int infoX = infoWidth;
- const int maxHeight = fontMetrics().height() * 5;
+ const int maxHeight = maxHeightPerLine();
- QRect boundingRect;
foreach (const MetaInfo& metaInfo, m_metaInfos) {
// draw label (e. g. "Date:")
painter.setPen(labelColor);
painter.setPen(infoColor);
painter.drawText(infoX, y, infoWidth, maxHeight,
Qt::AlignTop | Qt::AlignLeft | Qt::TextWordWrap,
- metaInfo.info,
- &boundingRect);
+ metaInfo.info);
- y += boundingRect.height() + Spacing;
+ y += requiredHeight(metaInfo);
}
}
+void MetaTextLabel::resizeEvent(QResizeEvent* event)
+{
+ QWidget::resizeEvent(event);
+
+ int minimumHeight = 0;
+ foreach (const MetaInfo& metaInfo, m_metaInfos) {
+ minimumHeight += requiredHeight(metaInfo);
+ }
+ setMinimumHeight(minimumHeight);
+}
+
int MetaTextLabel::requiredHeight(const MetaInfo& metaInfo) const
{
QTextOption textOption;
- textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
+ textOption.setWrapMode(QTextOption::WordWrap);
qreal height = 0;
const int leading = fontMetrics().leading();
}
textLayout.endLayout();
- return static_cast<int>(height) + Spacing;
+ int adjustedHeight = static_cast<int>(height);
+ if (adjustedHeight > maxHeightPerLine()) {
+ adjustedHeight = maxHeightPerLine();
+ }
+
+ return adjustedHeight + Spacing;
+}
+
+int MetaTextLabel::maxHeightPerLine() const
+{
+ return fontMetrics().height() * 100;
}
#include "metatextlabel.moc"
protected:
virtual void paintEvent(QPaintEvent* event);
+ virtual void resizeEvent(QResizeEvent* event);
private:
enum { Spacing = 2 };
* fit into the available width of the widget.
*/
int requiredHeight(const MetaInfo& metaInfo) const;
+
+ /**
+ * Returns the maximum height in pixels for the text of
+ * one added line. The returned value does not contain
+ * any additional spacing between texts.
+ */
+ int maxHeightPerLine() const;
};
#endif