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.
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
);
40 void Label::setText(const QString
&text
)
46 QString
Label::text() const
51 void Label::setDrawShadow(bool on
)
57 bool Label::drawShadow() const
62 void Label::paint(QPainter
*painter
, const QStyleOptionGraphicsItem
*option
, QWidget
*widget
)
67 const QString text
= QFontMetrics(font()).elidedText(m_text
, Qt::ElideMiddle
, contentsRect().width());
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);
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();
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
);