22 #include <QApplication> 24 #include <QGraphicsSceneMouseEvent> 28 #include <QStyleOptionGraphicsItem> 30 #include <kcolorscheme.h> 31 #include <kglobalsettings.h> 32 #include <kmimetype.h> 34 #include "private/themedwidgetinterface_p.h" 41 class 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);
119 d->hasLinks = text.contains(
"<a ", Qt::CaseInsensitive);
120 static_cast<QLabel*
>(widget())->
setText(text);
126 return static_cast<QLabel*
>(widget())->
text();
131 if (d->imagePath == path) {
139 bool absolutePath = !path.isEmpty() &&
141 !QDir::isRelativePath(path)
143 (path[0] ==
'/' || path.startsWith(QLatin1String(
":/")))
148 d->absImagePath = path;
175 nativeWidget()->setTextInteractionFlags(Qt::TextBrowserInteraction);
177 nativeWidget()->setTextInteractionFlags(Qt::LinksAccessibleByMouse | Qt::LinksAccessibleByKeyboard);
180 d->textSelectable = enable;
185 return d->textSelectable;
210 widget()->setStyleSheet(stylesheet);
215 return widget()->styleSheet();
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();
239 if (d->textSelectable || d->hasLinks){
240 QContextMenuEvent
contextMenuEvent(QContextMenuEvent::Reason(event->reason()),
241 event->pos().toPoint(),
event->screenPos(),
event->modifiers());
251 QGraphicsProxyWidget::resizeEvent(event);
256 QGraphicsProxyWidget::mousePressEvent(event);
259 if (d->textSelectable || d->hasLinks) {
266 if (d->textSelectable) {
267 QGraphicsProxyWidget::mouseMoveEvent(event);
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());
320 d->changeEvent(event);
321 QGraphicsProxyWidget::changeEvent(event);
327 return QGraphicsProxyWidget::event(event);
332 if (change == QGraphicsItem::ItemCursorHasChanged) {
336 return QGraphicsWidget::itemChange(change, value);
341 if (sizePolicy().verticalPolicy() == QSizePolicy::Fixed) {
342 return QGraphicsProxyWidget::sizeHint(Qt::PreferredSize, constraint);
344 return QGraphicsProxyWidget::sizeHint(which, constraint);
Q_INVOKABLE QString imagePath(const QString &name) const
Retrieve the path for an SVG image in the current theme.
Label(QGraphicsWidget *parent=0)
Constructs a label with word wrap on by default.
void dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data)
void setText(const QString &text)
Sets the display text for this Label.
void changeEvent(QEvent *event)
void setWordWrap(bool wrap)
Sets if the text of the label can wrap in multiple lines.
void setScaledContents(bool scaled)
Scale or not the contents of the label to the label size.
void resizeEvent(QGraphicsSceneResizeEvent *event)
QHash< QString, QVariant > Data
void setImage(const QString &path)
Sets the path to an image to display.
Namespace for everything in libplasma.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
void setStyleSheet(const QString &stylesheet)
Sets the stylesheet used to control the visual display of this Label.
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
bool hasScaledContents() const
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
bool event(QEvent *event)
QLabel * nativeWidget() const
void linkHovered(const QString &link)
void mousePressEvent(QGraphicsSceneMouseEvent *event)
bool textSelectable() const
static Theme * defaultTheme()
Singleton pattern accessor.
void setAlignment(Qt::Alignment alignment)
Sets the alignment for the text.
void linkActivated(const QString &link)
Qt::Alignment alignment() const
QVariant itemChange(GraphicsItemChange change, const QVariant &value)
void setTextSelectable(bool enable)
Set if the text on the label can be selected with the mouse.