add more spacing
[personal-kdebase.git] / apps / dolphin / src / panels / information / metatextlabel.cpp
blob66f12db9033367d80d4f72d4014dd154e70c576d
1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
3 * *
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. *
8 * *
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. *
13 * *
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>
23 #include <klocale.h>
25 #include <QPainter>
26 #include <QTextLayout>
27 #include <QTextLine>
28 #include <kdebug.h>
30 MetaTextLabel::MetaTextLabel(QWidget* parent) :
31 QWidget(parent),
32 m_minimumHeight(0),
33 m_metaInfos()
35 setFont(KGlobalSettings::smallestReadableFont());
38 MetaTextLabel::~MetaTextLabel()
42 void MetaTextLabel::clear()
44 m_minimumHeight = 0;
45 m_metaInfos.clear();
46 update();
49 void MetaTextLabel::add(const QString& labelText, const QString& infoText)
51 MetaInfo metaInfo;
52 metaInfo.label = labelText;
53 metaInfo.info = infoText;
55 m_metaInfos.append(metaInfo);
57 m_minimumHeight += requiredHeight(metaInfo);
58 setMinimumHeight(m_minimumHeight);
60 update();
63 void MetaTextLabel::paintEvent(QPaintEvent* event)
65 QWidget::paintEvent(event);
67 QPainter painter(this);
69 const QColor infoColor = palette().color(QPalette::Foreground);
70 QColor labelColor = infoColor;
71 labelColor.setAlpha(128);
73 int y = 0;
74 const int infoWidth = width() / 2;
75 const int labelWidth = infoWidth - 2 * Spacing;
76 const int infoX = infoWidth;
77 const int maxHeight = fontMetrics().height() * 5;
79 QRect boundingRect;
80 foreach (const MetaInfo& metaInfo, m_metaInfos) {
81 // draw label (e. g. "Date:")
82 painter.setPen(labelColor);
83 painter.drawText(0, y, labelWidth, maxHeight,
84 Qt::AlignTop | Qt::AlignRight | Qt::TextWordWrap,
85 metaInfo.label);
87 // draw information (e. g. "2008-11-09 20:12")
88 painter.setPen(infoColor);
89 painter.drawText(infoX, y, infoWidth, maxHeight,
90 Qt::AlignTop | Qt::AlignLeft | Qt::TextWordWrap,
91 metaInfo.info,
92 &boundingRect);
94 y += boundingRect.height() + Spacing;
98 void MetaTextLabel::resizeEvent(QResizeEvent* event)
100 QWidget::resizeEvent(event);
102 m_minimumHeight = 0;
103 foreach (const MetaInfo& metaInfo, m_metaInfos) {
104 m_minimumHeight += requiredHeight(metaInfo);
106 setMinimumHeight(m_minimumHeight);
109 int MetaTextLabel::requiredHeight(const MetaInfo& metaInfo) const
111 QTextOption textOption;
112 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
114 qreal height = 0;
115 const int leading = fontMetrics().leading();
116 const int availableWidth = width() / 2;
118 QTextLayout textLayout(metaInfo.info);
119 textLayout.setFont(font());
120 textLayout.setTextOption(textOption);
122 textLayout.beginLayout();
123 QTextLine line = textLayout.createLine();
124 while (line.isValid()) {
125 line.setLineWidth(availableWidth);
126 height += leading;
127 height += line.height();
128 line = textLayout.createLine();
130 textLayout.endLayout();
132 return static_cast<int>(height) + Spacing;
135 #include "metatextlabel.moc"