]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/metatextlabel.cpp
SVN_SILENT: coding style fix
[dolphin.git] / src / panels / information / metatextlabel.cpp
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 ***************************************************************************/
19
20 #include "metatextlabel.h"
21
22 #include <kglobalsettings.h>
23 #include <klocale.h>
24
25 #include <QPainter>
26 #include <QTextLayout>
27 #include <QTextLine>
28 #include <kdebug.h>
29
30 MetaTextLabel::MetaTextLabel(QWidget* parent) :
31 QWidget(parent),
32 m_metaInfos()
33 {
34 setFont(KGlobalSettings::smallestReadableFont());
35 setMinimumHeight(0);
36 }
37
38 MetaTextLabel::~MetaTextLabel()
39 {
40 }
41
42 void MetaTextLabel::clear()
43 {
44 setMinimumHeight(0);
45 m_metaInfos.clear();
46 update();
47 }
48
49 void MetaTextLabel::add(const QString& labelText, const QString& infoText)
50 {
51 MetaInfo metaInfo;
52 metaInfo.label = labelText;
53 metaInfo.info = infoText;
54 m_metaInfos.append(metaInfo);
55
56 setMinimumHeight(minimumHeight() + requiredHeight(metaInfo));
57 update();
58 }
59
60 void MetaTextLabel::paintEvent(QPaintEvent* event)
61 {
62 QWidget::paintEvent(event);
63
64 QPainter painter(this);
65
66 const QColor infoColor = palette().color(QPalette::Foreground);
67 QColor labelColor = infoColor;
68 labelColor.setAlpha(128);
69
70 int y = 0;
71 const int infoWidth = width() / 2;
72 const int labelWidth = infoWidth - 2 * Spacing;
73 const int infoX = infoWidth;
74 const int maxHeight = maxHeightPerLine();
75
76 foreach (const MetaInfo& metaInfo, m_metaInfos) {
77 // draw label (e. g. "Date:")
78 painter.setPen(labelColor);
79 painter.drawText(0, y, labelWidth, maxHeight,
80 Qt::AlignTop | Qt::AlignRight | Qt::TextWordWrap,
81 metaInfo.label);
82
83 // draw information (e. g. "2008-11-09 20:12")
84 painter.setPen(infoColor);
85 painter.drawText(infoX, y, infoWidth, maxHeight,
86 Qt::AlignTop | Qt::AlignLeft | Qt::TextWordWrap,
87 metaInfo.info);
88
89 y += requiredHeight(metaInfo);
90 }
91 }
92
93 void MetaTextLabel::resizeEvent(QResizeEvent* event)
94 {
95 QWidget::resizeEvent(event);
96
97 int minimumHeight = 0;
98 foreach (const MetaInfo& metaInfo, m_metaInfos) {
99 minimumHeight += requiredHeight(metaInfo);
100 }
101 setMinimumHeight(minimumHeight);
102 }
103
104 int MetaTextLabel::requiredHeight(const MetaInfo& metaInfo) const
105 {
106 QTextOption textOption;
107 textOption.setWrapMode(QTextOption::WordWrap);
108
109 qreal height = 0;
110 const int leading = fontMetrics().leading();
111 const int availableWidth = width() / 2;
112
113 QTextLayout textLayout(metaInfo.info);
114 textLayout.setFont(font());
115 textLayout.setTextOption(textOption);
116
117 textLayout.beginLayout();
118 QTextLine line = textLayout.createLine();
119 while (line.isValid()) {
120 line.setLineWidth(availableWidth);
121 height += leading;
122 height += line.height();
123 line = textLayout.createLine();
124 }
125 textLayout.endLayout();
126
127 int adjustedHeight = static_cast<int>(height);
128 if (adjustedHeight > maxHeightPerLine()) {
129 adjustedHeight = maxHeightPerLine();
130 }
131
132 return adjustedHeight + Spacing;
133 }
134
135 int MetaTextLabel::maxHeightPerLine() const
136 {
137 return fontMetrics().height() * 100;
138 }
139
140 #include "metatextlabel.moc"