1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "metatextlabel.h"
22 #include <kglobalsettings.h>
26 #include <QTextLayout>
30 MetaTextLabel::MetaTextLabel(QWidget
* parent
) :
34 setFont(KGlobalSettings::smallestReadableFont());
38 MetaTextLabel::~MetaTextLabel()
42 void MetaTextLabel::clear()
49 void MetaTextLabel::add(const QString
& labelText
, const QString
& infoText
)
52 metaInfo
.label
= labelText
;
53 metaInfo
.info
= infoText
;
54 m_metaInfos
.append(metaInfo
);
56 setMinimumHeight(minimumHeight() + requiredHeight(metaInfo
));
60 void MetaTextLabel::paintEvent(QPaintEvent
* event
)
62 QWidget::paintEvent(event
);
64 QPainter
painter(this);
66 const QColor infoColor
= palette().color(QPalette::Foreground
);
67 QColor labelColor
= infoColor
;
68 labelColor
.setAlpha(128);
71 const int infoWidth
= width() / 2;
72 const int labelWidth
= infoWidth
- 2 * Spacing
;
73 const int infoX
= infoWidth
;
74 const int maxHeight
= fontMetrics().height() * 5;
77 foreach (const MetaInfo
& metaInfo
, m_metaInfos
) {
78 // draw label (e. g. "Date:")
79 painter
.setPen(labelColor
);
80 painter
.drawText(0, y
, labelWidth
, maxHeight
,
81 Qt::AlignTop
| Qt::AlignRight
| Qt::TextWordWrap
,
84 // draw information (e. g. "2008-11-09 20:12")
85 painter
.setPen(infoColor
);
86 painter
.drawText(infoX
, y
, infoWidth
, maxHeight
,
87 Qt::AlignTop
| Qt::AlignLeft
| Qt::TextWordWrap
,
91 y
+= boundingRect
.height() + Spacing
;
95 int MetaTextLabel::requiredHeight(const MetaInfo
& metaInfo
) const
97 QTextOption textOption
;
98 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
101 const int leading
= fontMetrics().leading();
102 const int availableWidth
= width() / 2;
104 QTextLayout
textLayout(metaInfo
.info
);
105 textLayout
.setFont(font());
106 textLayout
.setTextOption(textOption
);
108 textLayout
.beginLayout();
109 QTextLine line
= textLayout
.createLine();
110 while (line
.isValid()) {
111 line
.setLineWidth(availableWidth
);
113 height
+= line
.height();
114 line
= textLayout
.createLine();
116 textLayout
.endLayout();
118 return static_cast<int>(height
) + Spacing
;
121 #include "metatextlabel.moc"