svn path=/trunk/KDE/kdebase/apps/; revision=927537
MetaTextLabel::MetaTextLabel(QWidget* parent) :
QWidget(parent),
MetaTextLabel::MetaTextLabel(QWidget* parent) :
QWidget(parent),
m_metaInfos()
{
setFont(KGlobalSettings::smallestReadableFont());
m_metaInfos()
{
setFont(KGlobalSettings::smallestReadableFont());
}
MetaTextLabel::~MetaTextLabel()
}
MetaTextLabel::~MetaTextLabel()
void MetaTextLabel::clear()
{
void MetaTextLabel::clear()
{
m_metaInfos.clear();
update();
}
m_metaInfos.clear();
update();
}
MetaInfo metaInfo;
metaInfo.label = labelText;
metaInfo.info = infoText;
MetaInfo metaInfo;
metaInfo.label = labelText;
metaInfo.info = infoText;
m_metaInfos.append(metaInfo);
m_metaInfos.append(metaInfo);
-
- m_minimumHeight += requiredHeight(metaInfo);
- setMinimumHeight(m_minimumHeight);
-
+
+ setMinimumHeight(minimumHeight() + requiredHeight(metaInfo));
update();
}
void MetaTextLabel::paintEvent(QPaintEvent* event)
{
QWidget::paintEvent(event);
update();
}
void MetaTextLabel::paintEvent(QPaintEvent* event)
{
QWidget::paintEvent(event);
const QColor infoColor = palette().color(QPalette::Foreground);
QColor labelColor = infoColor;
labelColor.setAlpha(128);
const QColor infoColor = palette().color(QPalette::Foreground);
QColor labelColor = infoColor;
labelColor.setAlpha(128);
int y = 0;
const int infoWidth = width() / 2;
const int labelWidth = infoWidth - 2 * Spacing;
const int infoX = infoWidth;
const int maxHeight = fontMetrics().height() * 5;
int y = 0;
const int infoWidth = width() / 2;
const int labelWidth = infoWidth - 2 * Spacing;
const int infoX = infoWidth;
const int maxHeight = fontMetrics().height() * 5;
QRect boundingRect;
foreach (const MetaInfo& metaInfo, m_metaInfos) {
// draw label (e. g. "Date:")
QRect boundingRect;
foreach (const MetaInfo& metaInfo, m_metaInfos) {
// draw label (e. g. "Date:")
painter.drawText(0, y, labelWidth, maxHeight,
Qt::AlignTop | Qt::AlignRight | Qt::TextWordWrap,
metaInfo.label);
painter.drawText(0, y, labelWidth, maxHeight,
Qt::AlignTop | Qt::AlignRight | Qt::TextWordWrap,
metaInfo.label);
// draw information (e. g. "2008-11-09 20:12")
painter.setPen(infoColor);
painter.drawText(infoX, y, infoWidth, maxHeight,
Qt::AlignTop | Qt::AlignLeft | Qt::TextWordWrap,
metaInfo.info,
&boundingRect);
// draw information (e. g. "2008-11-09 20:12")
painter.setPen(infoColor);
painter.drawText(infoX, y, infoWidth, maxHeight,
Qt::AlignTop | Qt::AlignLeft | Qt::TextWordWrap,
metaInfo.info,
&boundingRect);
-
- y += boundingRect.height() + Spacing;
- }
-}
-void MetaTextLabel::resizeEvent(QResizeEvent* event)
-{
- QWidget::resizeEvent(event);
-
- m_minimumHeight = 0;
- foreach (const MetaInfo& metaInfo, m_metaInfos) {
- m_minimumHeight += requiredHeight(metaInfo);
+ y += boundingRect.height() + Spacing;
- setMinimumHeight(m_minimumHeight);
}
int MetaTextLabel::requiredHeight(const MetaInfo& metaInfo) const
{
QTextOption textOption;
textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
}
int MetaTextLabel::requiredHeight(const MetaInfo& metaInfo) const
{
QTextOption textOption;
textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
qreal height = 0;
const int leading = fontMetrics().leading();
const int availableWidth = width() / 2;
qreal height = 0;
const int leading = fontMetrics().leading();
const int availableWidth = width() / 2;
QTextLayout textLayout(metaInfo.info);
textLayout.setFont(font());
textLayout.setTextOption(textOption);
QTextLayout textLayout(metaInfo.info);
textLayout.setFont(font());
textLayout.setTextOption(textOption);
textLayout.beginLayout();
QTextLine line = textLayout.createLine();
while (line.isValid()) {
textLayout.beginLayout();
QTextLine line = textLayout.createLine();
while (line.isValid()) {
height += leading;
height += line.height();
line = textLayout.createLine();
height += leading;
height += line.height();
line = textLayout.createLine();
return static_cast<int>(height) + Spacing;
}
return static_cast<int>(height) + Spacing;
}
void clear();
void add(const QString& labelText, const QString& infoText);
void clear();
void add(const QString& labelText, const QString& infoText);
protected:
virtual void paintEvent(QPaintEvent* event);
protected:
virtual void paintEvent(QPaintEvent* event);
- virtual void resizeEvent(QResizeEvent* event);
private:
enum { Spacing = 2 };
private:
enum { Spacing = 2 };
struct MetaInfo
{
QString label;
QString info;
};
struct MetaInfo
{
QString label;
QString info;
};
QList<MetaInfo> m_metaInfos;
QList<MetaInfo> m_metaInfos;
/**
* Returns the required height in pixels for \a metaInfo to
* fit into the available width of the widget.
/**
* Returns the required height in pixels for \a metaInfo to
* fit into the available width of the widget.