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());
259 if (d->textSelectable || d->hasLinks) {
266 if (d->textSelectable) {
276 QFontMetrics fm = native->font();
279 if (native->wordWrap() || native->text().isEmpty() || size().width() >= fm.width(native->text())) {
282 const int gradientLength = 25;
283 QPixmap buffer(contentsRect().size().toSize());
284 buffer.fill(Qt::transparent);
286 QPainter buffPainter(&buffer);
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);
332 if (change == QGraphicsItem::ItemCursorHasChanged) {
341 if (sizePolicy().verticalPolicy() == QSizePolicy::Fixed) {