• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

Plasma

  • plasma
  • widgets
label.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2008 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#include "label.h"
21
22#include <QApplication>
23#include <QDir>
24#include <QGraphicsSceneMouseEvent>
25#include <QLabel>
26#include <QMenu>
27#include <QPainter>
28#include <QStyleOptionGraphicsItem>
29
30#include <kcolorscheme.h>
31#include <kglobalsettings.h>
32#include <kmimetype.h>
33
34#include "private/themedwidgetinterface_p.h"
35#include "svg.h"
36#include "theme.h"
37
38namespace Plasma
39{
40
41class LabelPrivate : public ThemedWidgetInterface<Label>
42{
43public:
44 LabelPrivate(Label *label)
45 : ThemedWidgetInterface<Label>(label),
46 svg(0),
47 textSelectable(false),
48 hasLinks(false)
49 {
50 }
51
52 ~LabelPrivate()
53 {
54 delete svg;
55 }
56
57 void setPixmap()
58 {
59 if (imagePath.isEmpty()) {
60 delete svg;
61 svg = 0;
62 return;
63 }
64
65 KMimeType::Ptr mime = KMimeType::findByPath(absImagePath);
66 QPixmap pm(q->size().toSize());
67
68 if (mime->is("image/svg+xml") || mime->is("image/svg+xml-compressed")) {
69 if (!svg || svg->imagePath() != absImagePath) {
70 delete svg;
71 svg = new Svg();
72 svg->setImagePath(imagePath);
73 QObject::connect(svg, SIGNAL(repaintNeeded()), q, SLOT(setPixmap()));
74 }
75
76 QPainter p(&pm);
77 svg->paint(&p, pm.rect());
78 } else {
79 delete svg;
80 svg = 0;
81 pm = QPixmap(absImagePath);
82 }
83
84 static_cast<QLabel*>(q->widget())->setPixmap(pm);
85 }
86
87 QString imagePath;
88 QString absImagePath;
89 Svg *svg;
90 bool textSelectable : 1;
91 bool hasLinks : 1;
92};
93
94Label::Label(QGraphicsWidget *parent)
95 : QGraphicsProxyWidget(parent),
96 d(new LabelPrivate(this))
97{
98 QLabel *native = new QLabel;
99
100 native->setWindowFlags(native->windowFlags()|Qt::BypassGraphicsProxyWidget);
101 native->setAttribute(Qt::WA_NoSystemBackground);
102 native->setWordWrap(true);
103 native->setWindowIcon(QIcon());
104
105 connect(native, SIGNAL(linkActivated(QString)), this, SIGNAL(linkActivated(QString)));
106 connect(native, SIGNAL(linkHovered(QString)), this, SIGNAL(linkHovered(QString)));
107
108 d->setWidget(native);
109 d->initTheming();
110}
111
112Label::~Label()
113{
114 delete d;
115}
116
117void Label::setText(const QString &text)
118{
119 d->hasLinks = text.contains("<a ", Qt::CaseInsensitive);
120 static_cast<QLabel*>(widget())->setText(text);
121 updateGeometry();
122}
123
124QString Label::text() const
125{
126 return static_cast<QLabel*>(widget())->text();
127}
128
129void Label::setImage(const QString &path)
130{
131 if (d->imagePath == path) {
132 return;
133 }
134
135 delete d->svg;
136 d->svg = 0;
137 d->imagePath = path;
138
139 bool absolutePath = !path.isEmpty() &&
140 #ifdef Q_WS_WIN
141 !QDir::isRelativePath(path)
142 #else
143 (path[0] == '/' || path.startsWith(QLatin1String(":/")))
144 #endif
145 ;
146
147 if (absolutePath) {
148 d->absImagePath = path;
149 } else {
150 //TODO: package support
151 d->absImagePath = Theme::defaultTheme()->imagePath(path);
152 }
153
154 d->setPixmap();
155}
156
157QString Label::image() const
158{
159 return d->imagePath;
160}
161
162void Label::setScaledContents(bool scaled)
163{
164 static_cast<QLabel*>(widget())->setScaledContents(scaled);
165}
166
167bool Label::hasScaledContents() const
168{
169 return static_cast<QLabel*>(widget())->hasScaledContents();
170}
171
172void Label::setTextSelectable(bool enable)
173{
174 if (enable) {
175 nativeWidget()->setTextInteractionFlags(Qt::TextBrowserInteraction);
176 } else {
177 nativeWidget()->setTextInteractionFlags(Qt::LinksAccessibleByMouse | Qt::LinksAccessibleByKeyboard);
178 }
179
180 d->textSelectable = enable;
181}
182
183bool Label::textSelectable() const
184{
185 return d->textSelectable;
186}
187
188void Label::setAlignment(Qt::Alignment alignment)
189{
190 nativeWidget()->setAlignment(alignment);
191}
192
193Qt::Alignment Label::alignment() const
194{
195 return nativeWidget()->alignment();
196}
197
198void Label::setWordWrap(bool wrap)
199{
200 nativeWidget()->setWordWrap(wrap);
201}
202
203bool Label::wordWrap() const
204{
205 return nativeWidget()->wordWrap();
206}
207
208void Label::setStyleSheet(const QString &stylesheet)
209{
210 widget()->setStyleSheet(stylesheet);
211}
212
213QString Label::styleSheet()
214{
215 return widget()->styleSheet();
216}
217
218QLabel *Label::nativeWidget() const
219{
220 return static_cast<QLabel*>(widget());
221}
222
223void Label::dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data)
224{
225 Q_UNUSED(sourceName);
226
227 QStringList texts;
228 foreach (const QVariant &v, data) {
229 if (v.canConvert(QVariant::String)) {
230 texts << v.toString();
231 }
232 }
233
234 setText(texts.join(" "));
235}
236
237void Label::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
238{
239 if (d->textSelectable || d->hasLinks){
240 QContextMenuEvent contextMenuEvent(QContextMenuEvent::Reason(event->reason()),
241 event->pos().toPoint(), event->screenPos(), event->modifiers());
242 QApplication::sendEvent(nativeWidget(), &contextMenuEvent);
243 }else{
244 event->ignore();
245 }
246}
247
248void Label::resizeEvent(QGraphicsSceneResizeEvent *event)
249{
250 d->setPixmap();
251 QGraphicsProxyWidget::resizeEvent(event);
252}
253
254void Label::mousePressEvent(QGraphicsSceneMouseEvent *event)
255{
256 QGraphicsProxyWidget::mousePressEvent(event);
257 //FIXME: when QTextControl accept()s mouse press events (as of Qt 4.6.2, it processes them
258 //but never marks them as accepted) the following event->accept() can be removed
259 if (d->textSelectable || d->hasLinks) {
260 event->accept();
261 }
262}
263
264void Label::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
265{
266 if (d->textSelectable) {
267 QGraphicsProxyWidget::mouseMoveEvent(event);
268 }
269}
270
271void Label::paint(QPainter *painter,
272 const QStyleOptionGraphicsItem *option,
273 QWidget *widget)
274{
275 QLabel *native = nativeWidget();
276 QFontMetrics fm = native->font();
277
278 //indirect painting still used for fade out
279 if (native->wordWrap() || native->text().isEmpty() || size().width() >= fm.width(native->text())) {
280 QGraphicsProxyWidget::paint(painter, option, widget);
281 } else {
282 const int gradientLength = 25;
283 QPixmap buffer(contentsRect().size().toSize());
284 buffer.fill(Qt::transparent);
285
286 QPainter buffPainter(&buffer);
287
288 QGraphicsProxyWidget::paint(&buffPainter, option, widget);
289
290 QLinearGradient gr;
291
292 buffPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
293 buffPainter.setPen(Qt::NoPen);
294
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);
301
302 buffPainter.drawRect(QRect(gr.start().toPoint(), QSize(gradientLength, size().height())));
303 } else {
304 gr.setStart(0, 0);
305 gr.setFinalStop(gradientLength, 0);
306 gr.setColorAt(0, Qt::transparent);
307 gr.setColorAt(1, Qt::black);
308 buffPainter.setBrush(gr);
309
310 buffPainter.drawRect(QRect(0, 0, gradientLength, size().height()));
311 }
312
313 buffPainter.end();
314 painter->drawPixmap(contentsRect(), buffer, buffer.rect());
315 }
316}
317
318void Label::changeEvent(QEvent *event)
319{
320 d->changeEvent(event);
321 QGraphicsProxyWidget::changeEvent(event);
322}
323
324bool Label::event(QEvent *event)
325{
326 d->event(event);
327 return QGraphicsProxyWidget::event(event);
328}
329
330QVariant Label::itemChange(GraphicsItemChange change, const QVariant & value)
331{
332 if (change == QGraphicsItem::ItemCursorHasChanged) {
333 nativeWidget()->setCursor(cursor());
334 }
335
336 return QGraphicsWidget::itemChange(change, value);
337}
338
339QSizeF Label::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
340{
341 if (sizePolicy().verticalPolicy() == QSizePolicy::Fixed) {
342 return QGraphicsProxyWidget::sizeHint(Qt::PreferredSize, constraint);
343 } else {
344 return QGraphicsProxyWidget::sizeHint(which, constraint);
345 }
346}
347
348} // namespace Plasma
349
350#include <label.moc>
351
Plasma::DataEngine::Data
QHash< QString, QVariant > Data
Definition: dataengine.h:68
Plasma::Label::event
bool event(QEvent *event)
Definition: label.cpp:324
Plasma::Label::contextMenuEvent
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
Definition: label.cpp:237
Plasma::Label::hasScaledContents
bool hasScaledContents
Definition: label.h:48
Plasma::Label::alignment
Qt::Alignment alignment
Definition: label.h:47
Plasma::Label::linkActivated
void linkActivated(const QString &link)
Plasma::Label::setText
void setText(const QString &text)
Sets the display text for this Label.
Definition: label.cpp:117
Plasma::Label::linkHovered
void linkHovered(const QString &link)
Plasma::Label::text
QString text
Definition: label.h:45
Plasma::Label::nativeWidget
QLabel * nativeWidget
Definition: label.h:52
Plasma::Label::setScaledContents
void setScaledContents(bool scaled)
Scale or not the contents of the label to the label size.
Definition: label.cpp:162
Plasma::Theme::imagePath
Q_INVOKABLE QString imagePath(const QString &name) const
Retrieve the path for an SVG image in the current theme.
Definition: theme.cpp:794
Plasma::Theme::defaultTheme
static Theme * defaultTheme()
Singleton pattern accessor.
Definition: theme.cpp:544
QGraphicsProxyWidget
QGraphicsWidget
QStyleOptionGraphicsItem
QWidget
label.h
Plasma
Namespace for everything in libplasma.
Definition: abstractdialogmanager.cpp:25
svg.h
theme.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal