22#include <QApplication>
24#include <QGraphicsSceneMouseEvent>
28#include <QStyleOptionGraphicsItem>
30#include <kcolorscheme.h>
31#include <kglobalsettings.h>
34#include "private/themedwidgetinterface_p.h"
41class LabelPrivate :
public ThemedWidgetInterface<Label>
44 LabelPrivate(Label *label)
45 : ThemedWidgetInterface<Label>(label),
47 textSelectable(false),
59 if (imagePath.isEmpty()) {
65 KMimeType::Ptr mime = KMimeType::findByPath(absImagePath);
66 QPixmap pm(q->size().toSize());
68 if (mime->is(
"image/svg+xml") || mime->is(
"image/svg+xml-compressed")) {
69 if (!svg || svg->imagePath() != absImagePath) {
72 svg->setImagePath(imagePath);
73 QObject::connect(svg, SIGNAL(repaintNeeded()), q, SLOT(setPixmap()));
77 svg->paint(&p, pm.rect());
81 pm = QPixmap(absImagePath);
84 static_cast<QLabel*
>(q->widget())->setPixmap(pm);
90 bool textSelectable : 1;
96 d(new LabelPrivate(this))
98 QLabel *native =
new QLabel;
100 native->setWindowFlags(native->windowFlags()|Qt::BypassGraphicsProxyWidget);
101 native->setAttribute(Qt::WA_NoSystemBackground);
102 native->setWordWrap(
true);
103 native->setWindowIcon(QIcon());
108 d->setWidget(native);
117void Label::setText(
const QString &text)
119 d->hasLinks =
text.contains(
"<a ", Qt::CaseInsensitive);
124QString Label::text()
const
126 return static_cast<QLabel*
>(widget())->
text();
129void Label::setImage(
const QString &path)
131 if (d->imagePath == path) {
139 bool absolutePath = !path.isEmpty() &&
141 !QDir::isRelativePath(path)
143 (path[0] ==
'/' || path.startsWith(QLatin1String(
":/")))
148 d->absImagePath = path;
157QString Label::image()
const
162void Label::setScaledContents(
bool scaled)
167bool Label::hasScaledContents()
const
172void Label::setTextSelectable(
bool enable)
175 nativeWidget()->setTextInteractionFlags(Qt::TextBrowserInteraction);
177 nativeWidget()->setTextInteractionFlags(Qt::LinksAccessibleByMouse | Qt::LinksAccessibleByKeyboard);
180 d->textSelectable = enable;
183bool Label::textSelectable()
const
185 return d->textSelectable;
188void Label::setAlignment(Qt::Alignment alignment)
193Qt::Alignment Label::alignment()
const
198void Label::setWordWrap(
bool wrap)
203bool Label::wordWrap()
const
208void Label::setStyleSheet(
const QString &stylesheet)
210 widget()->setStyleSheet(stylesheet);
213QString Label::styleSheet()
215 return widget()->styleSheet();
218QLabel *Label::nativeWidget()
const
220 return static_cast<QLabel*
>(widget());
225 Q_UNUSED(sourceName);
228 foreach (
const QVariant &v, data) {
229 if (v.canConvert(QVariant::String)) {
230 texts << v.toString();
237void Label::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
239 if (d->textSelectable || d->hasLinks){
248void Label::resizeEvent(QGraphicsSceneResizeEvent *event)
251 QGraphicsProxyWidget::resizeEvent(
event);
254void Label::mousePressEvent(QGraphicsSceneMouseEvent *event)
256 QGraphicsProxyWidget::mousePressEvent(
event);
259 if (d->textSelectable || d->hasLinks) {
264void Label::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
266 if (d->textSelectable) {
267 QGraphicsProxyWidget::mouseMoveEvent(
event);
271void Label::paint(QPainter *painter,
276 QFontMetrics fm = native->font();
279 if (native->wordWrap() || native->text().isEmpty() || size().width() >= fm.width(native->text())) {
280 QGraphicsProxyWidget::paint(painter, option, widget);
282 const int gradientLength = 25;
283 QPixmap buffer(contentsRect().size().toSize());
284 buffer.fill(Qt::transparent);
286 QPainter buffPainter(&buffer);
288 QGraphicsProxyWidget::paint(&buffPainter, option, widget);
292 buffPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
293 buffPainter.setPen(Qt::NoPen);
295 if (option->direction == Qt::LeftToRight) {
296 gr.setStart(size().width()-gradientLength, 0);
297 gr.setFinalStop(size().width(), 0);
298 gr.setColorAt(0, Qt::black);
299 gr.setColorAt(1, Qt::transparent);
300 buffPainter.setBrush(gr);
302 buffPainter.drawRect(QRect(gr.start().toPoint(), QSize(gradientLength, size().height())));
305 gr.setFinalStop(gradientLength, 0);
306 gr.setColorAt(0, Qt::transparent);
307 gr.setColorAt(1, Qt::black);
308 buffPainter.setBrush(gr);
310 buffPainter.drawRect(QRect(0, 0, gradientLength, size().height()));
314 painter->drawPixmap(contentsRect(), buffer, buffer.rect());
318void Label::changeEvent(QEvent *event)
320 d->changeEvent(
event);
321 QGraphicsProxyWidget::changeEvent(
event);
324bool Label::event(QEvent *event)
327 return QGraphicsProxyWidget::event(
event);
330QVariant Label::itemChange(GraphicsItemChange change,
const QVariant & value)
332 if (change == QGraphicsItem::ItemCursorHasChanged) {
336 return QGraphicsWidget::itemChange(change, value);
339QSizeF Label::sizeHint(Qt::SizeHint which,
const QSizeF &constraint)
const
341 if (sizePolicy().verticalPolicy() == QSizePolicy::Fixed) {
342 return QGraphicsProxyWidget::sizeHint(Qt::PreferredSize, constraint);
344 return QGraphicsProxyWidget::sizeHint(which, constraint);
QHash< QString, QVariant > Data
bool event(QEvent *event)
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
void linkActivated(const QString &link)
void setText(const QString &text)
Sets the display text for this Label.
void linkHovered(const QString &link)
void setScaledContents(bool scaled)
Scale or not the contents of the label to the label size.
Q_INVOKABLE QString imagePath(const QString &name) const
Retrieve the path for an SVG image in the current theme.
static Theme * defaultTheme()
Singleton pattern accessor.
Namespace for everything in libplasma.