add more spacing
[personal-kdebase.git] / apps / plasma / applets / folderview / label.cpp
blobb3afebfd12452490c1e6521463d8575bacf922c5
1 /*
2 * Copyright © 2008 Fredrik Höglund <fredrik@kde.org>
3 * Copyright © 2008 Andrew Lake <jamboarder@yahoo.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 #include "label.h"
23 #include <QString>
24 #include <QPainter>
26 #include "plasma/paintutils.h"
28 Label::Label(QGraphicsWidget *parent)
29 : QGraphicsWidget(parent)
31 setMinimumHeight(QFontMetrics(font()).lineSpacing() + 4);
32 setMaximumHeight(QFontMetrics(font()).lineSpacing() + 4);
33 setCacheMode(DeviceCoordinateCache);
36 Label::~Label()
40 void Label::setText(const QString &text)
42 m_text = text;
43 update();
46 QString Label::text() const
48 return m_text;
51 void Label::setDrawShadow(bool on)
53 m_drawShadow = on;
54 update();
57 bool Label::drawShadow() const
59 return m_drawShadow;
62 void Label::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
64 Q_UNUSED(option)
65 Q_UNUSED(widget)
67 const QString text = QFontMetrics(font()).elidedText(m_text, Qt::ElideMiddle, contentsRect().width());
69 QColor shadowColor;
70 QPoint shadowOffset;
71 if (!m_drawShadow) {
72 shadowColor = Qt::transparent;
73 shadowOffset = QPoint();
74 } else if (qGray(palette().color(QPalette::Text).rgb()) > 192) {
75 shadowColor = Qt::black;
76 shadowOffset = QPoint(1, 1);
77 } else {
78 shadowColor = Qt::white;
79 shadowOffset = QPoint();
82 QPixmap titlePixmap = Plasma::PaintUtils::shadowText(text, palette().color(QPalette::Text),
83 shadowColor, shadowOffset);
84 painter->drawPixmap(contentsRect().topLeft(), titlePixmap);
86 int width = contentsRect().width();
88 //Draw underline
89 if (m_divider.width() != width) {
90 qreal fw = 1.0 / width * 20.0;
91 m_divider = QPixmap(width, 2);
92 m_divider.fill(Qt::transparent);
93 QLinearGradient g(0, 0, width, 0);
94 g.setColorAt(0, Qt::transparent);
95 g.setColorAt(fw, Qt::black);
96 g.setColorAt(1 - fw, Qt::black);
97 g.setColorAt(1, Qt::transparent);
98 QPainter p(&m_divider);
99 p.setCompositionMode(QPainter::CompositionMode_Source);
100 p.fillRect(0, 0, width, 1, QColor(0, 0, 0, 64));
101 p.fillRect(0, 1, width, 1, QColor(255, 255, 255, 64));
102 p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
103 p.fillRect(m_divider.rect(), g);
105 painter->drawPixmap(0, contentsRect().height() - 2, m_divider);
108 #include "label.moc"