]>
cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/metatextlabel.cpp
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
;
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
);
66 m_metaInfos
.append(metaInfo
);
69 setMinimumHeight(minimumHeight() + requiredHeight(metaInfo
));
73 void MetaTextLabel::paintEvent(QPaintEvent
* event
)
75 QWidget::paintEvent(event
);
77 QPainter
painter(this);
79 const QColor infoColor
= palette().color(QPalette::Foreground
);
80 QColor labelColor
= infoColor
;
81 labelColor
.setAlpha(128);
84 const int infoWidth
= infoTextWidth();
85 const int labelWidth
= labelTextWidth();
86 const int infoX
= infoWidth
;
87 const int maxHeight
= maxHeightPerLine();
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
,
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
,
102 y
+= requiredHeight(metaInfo
);
106 void MetaTextLabel::resizeEvent(QResizeEvent
* event
)
108 QWidget::resizeEvent(event
);
110 int minimumHeight
= 0;
111 foreach (const MetaInfo
& metaInfo
, m_metaInfos
) {
112 minimumHeight
+= requiredHeight(metaInfo
);
114 setMinimumHeight(minimumHeight
);
117 int MetaTextLabel::requiredHeight(const MetaInfo
& info
) const
119 const int labelTextHeight
= requiredHeight(info
.label
, labelTextWidth());
120 const int infoTextHeight
= requiredHeight(info
.info
, infoTextWidth());
121 return qMax(labelTextHeight
, infoTextHeight
);
124 int MetaTextLabel::requiredHeight(const QString
& text
, int width
) const
126 QTextOption textOption
;
127 textOption
.setWrapMode(QTextOption::WordWrap
);
130 const int leading
= fontMetrics().leading();
132 QTextLayout
textLayout(text
);
133 textLayout
.setFont(font());
134 textLayout
.setTextOption(textOption
);
136 textLayout
.beginLayout();
137 QTextLine line
= textLayout
.createLine();
138 while (line
.isValid()) {
139 line
.setLineWidth(width
);
141 height
+= line
.height();
142 line
= textLayout
.createLine();
144 textLayout
.endLayout();
146 int adjustedHeight
= static_cast<int>(height
);
147 if (adjustedHeight
> maxHeightPerLine()) {
148 adjustedHeight
= maxHeightPerLine();
151 return adjustedHeight
+ Spacing
;
154 int MetaTextLabel::maxHeightPerLine() const
156 return fontMetrics().height() * 100;
159 #include "metatextlabel.moc"