]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/metatextlabel.cpp
Fixed regression when refactoring the Information Panel: Don't forget to give a visua...
[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
55 // add the meta information alphabetically sorted
56 bool inserted = false;
57 const int count = m_metaInfos.size();
58 for (int i = 0; i < count; ++i) {
59 if (m_metaInfos[i].label > labelText) {
60 m_metaInfos.insert(i, metaInfo);
61 inserted = true;
62 break;
63 }
64 }
65 if (!inserted) {
66 m_metaInfos.append(metaInfo);
67 }
68
69 setMinimumHeight(minimumHeight() + requiredHeight(metaInfo));
70 update();
71 }
72
73 void MetaTextLabel::paintEvent(QPaintEvent* event)
74 {
75 QWidget::paintEvent(event);
76
77 QPainter painter(this);
78
79 const QColor infoColor = palette().color(QPalette::Foreground);
80 QColor labelColor = infoColor;
81 labelColor.setAlpha(128);
82
83 int y = 0;
84 const int infoWidth = infoTextWidth();
85 const int labelWidth = labelTextWidth();
86 const int infoX = infoWidth;
87 const int maxHeight = maxHeightPerLine();
88
89 foreach (const MetaInfo& metaInfo, m_metaInfos) {
90 // draw label (e. g. "Date:")
91 painter.setPen(labelColor);
92 painter.drawText(0, y, labelWidth, maxHeight,
93 Qt::AlignTop | Qt::AlignRight | Qt::TextWordWrap,
94 metaInfo.label);
95
96 // draw information (e. g. "2008-11-09 20:12")
97 painter.setPen(infoColor);
98 painter.drawText(infoX, y, infoWidth, maxHeight,
99 Qt::AlignTop | Qt::AlignLeft | Qt::TextWordWrap,
100 metaInfo.info);
101
102 y += requiredHeight(metaInfo);
103 }
104 }
105
106 void MetaTextLabel::resizeEvent(QResizeEvent* event)
107 {
108 QWidget::resizeEvent(event);
109
110 int minimumHeight = 0;
111 foreach (const MetaInfo& metaInfo, m_metaInfos) {
112 minimumHeight += requiredHeight(metaInfo);
113 }
114 setMinimumHeight(minimumHeight);
115 }
116
117 int MetaTextLabel::requiredHeight(const MetaInfo& info) const
118 {
119 const int labelTextHeight = requiredHeight(info.label, labelTextWidth());
120 const int infoTextHeight = requiredHeight(info.info, infoTextWidth());
121 return qMax(labelTextHeight, infoTextHeight);
122 }
123
124 int MetaTextLabel::requiredHeight(const QString& text, int width) const
125 {
126 QTextOption textOption;
127 textOption.setWrapMode(QTextOption::WordWrap);
128
129 qreal height = 0;
130 const int leading = fontMetrics().leading();
131
132 QTextLayout textLayout(text);
133 textLayout.setFont(font());
134 textLayout.setTextOption(textOption);
135
136 textLayout.beginLayout();
137 QTextLine line = textLayout.createLine();
138 while (line.isValid()) {
139 line.setLineWidth(width);
140 height += leading;
141 height += line.height();
142 line = textLayout.createLine();
143 }
144 textLayout.endLayout();
145
146 int adjustedHeight = static_cast<int>(height);
147 if (adjustedHeight > maxHeightPerLine()) {
148 adjustedHeight = maxHeightPerLine();
149 }
150
151 return adjustedHeight + Spacing;
152 }
153
154 int MetaTextLabel::maxHeightPerLine() const
155 {
156 return fontMetrics().height() * 100;
157 }
158
159 #include "metatextlabel.moc"