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

Plasma

  • sources
  • kde-4.12
  • kdelibs
  • 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 
38 namespace Plasma
39 {
40 
41 class LabelPrivate : public ThemedWidgetInterface<Label>
42 {
43 public:
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 
94 Label::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 
112 Label::~Label()
113 {
114  delete d;
115 }
116 
117 void 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 
124 QString Label::text() const
125 {
126  return static_cast<QLabel*>(widget())->text();
127 }
128 
129 void 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 
157 QString Label::image() const
158 {
159  return d->imagePath;
160 }
161 
162 void Label::setScaledContents(bool scaled)
163 {
164  static_cast<QLabel*>(widget())->setScaledContents(scaled);
165 }
166 
167 bool Label::hasScaledContents() const
168 {
169  return static_cast<QLabel*>(widget())->hasScaledContents();
170 }
171 
172 void 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 
183 bool Label::textSelectable() const
184 {
185  return d->textSelectable;
186 }
187 
188 void Label::setAlignment(Qt::Alignment alignment)
189 {
190  nativeWidget()->setAlignment(alignment);
191 }
192 
193 Qt::Alignment Label::alignment() const
194 {
195  return nativeWidget()->alignment();
196 }
197 
198 void Label::setWordWrap(bool wrap)
199 {
200  nativeWidget()->setWordWrap(wrap);
201 }
202 
203 bool Label::wordWrap() const
204 {
205  return nativeWidget()->wordWrap();
206 }
207 
208 void Label::setStyleSheet(const QString &stylesheet)
209 {
210  widget()->setStyleSheet(stylesheet);
211 }
212 
213 QString Label::styleSheet()
214 {
215  return widget()->styleSheet();
216 }
217 
218 QLabel *Label::nativeWidget() const
219 {
220  return static_cast<QLabel*>(widget());
221 }
222 
223 void 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 
237 void 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 
248 void Label::resizeEvent(QGraphicsSceneResizeEvent *event)
249 {
250  d->setPixmap();
251  QGraphicsProxyWidget::resizeEvent(event);
252 }
253 
254 void 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 
264 void Label::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
265 {
266  if (d->textSelectable) {
267  QGraphicsProxyWidget::mouseMoveEvent(event);
268  }
269 }
270 
271 void 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 
318 void Label::changeEvent(QEvent *event)
319 {
320  d->changeEvent(event);
321  QGraphicsProxyWidget::changeEvent(event);
322 }
323 
324 bool Label::event(QEvent *event)
325 {
326  d->event(event);
327  return QGraphicsProxyWidget::event(event);
328 }
329 
330 QVariant 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 
339 QSizeF 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::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::Label::image
QString image() const
Plasma::Label::Label
Label(QGraphicsWidget *parent=0)
Constructs a label with word wrap on by default.
Definition: label.cpp:94
Plasma::Label::dataUpdated
void dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data)
Definition: label.cpp:223
QWidget
Plasma::Label::setText
void setText(const QString &text)
Sets the display text for this Label.
Definition: label.cpp:117
Plasma::Label::changeEvent
void changeEvent(QEvent *event)
Definition: label.cpp:318
Plasma::Label::setWordWrap
void setWordWrap(bool wrap)
Sets if the text of the label can wrap in multiple lines.
Definition: label.cpp:198
theme.h
Plasma::Label::setScaledContents
void setScaledContents(bool scaled)
Scale or not the contents of the label to the label size.
Definition: label.cpp:162
Plasma::Label::resizeEvent
void resizeEvent(QGraphicsSceneResizeEvent *event)
Definition: label.cpp:248
Plasma::DataEngine::Data
QHash< QString, QVariant > Data
Definition: dataengine.h:68
Plasma::Label::setImage
void setImage(const QString &path)
Sets the path to an image to display.
Definition: label.cpp:129
Plasma::Label::styleSheet
QString styleSheet()
Plasma::Label::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Definition: label.cpp:271
Plasma::Label::setStyleSheet
void setStyleSheet(const QString &stylesheet)
Sets the stylesheet used to control the visual display of this Label.
Definition: label.cpp:208
Plasma::Label::contextMenuEvent
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
Definition: label.cpp:237
Plasma::Label::mouseMoveEvent
void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
Definition: label.cpp:264
Plasma::Label::hasScaledContents
bool hasScaledContents() const
Plasma::Label::sizeHint
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
Definition: label.cpp:339
QGraphicsProxyWidget
Plasma::Label::event
bool event(QEvent *event)
Definition: label.cpp:324
Plasma::Label::~Label
~Label()
Definition: label.cpp:112
Plasma::Label::nativeWidget
QLabel * nativeWidget() const
Plasma::Label::linkHovered
void linkHovered(const QString &link)
Plasma::Label::mousePressEvent
void mousePressEvent(QGraphicsSceneMouseEvent *event)
Definition: label.cpp:254
Plasma::Label::text
QString text() const
Plasma::Label::textSelectable
bool textSelectable() const
Plasma::Theme::defaultTheme
static Theme * defaultTheme()
Singleton pattern accessor.
Definition: theme.cpp:544
Plasma::Label::setAlignment
void setAlignment(Qt::Alignment alignment)
Sets the alignment for the text.
Definition: label.cpp:188
label.h
Plasma::Label::linkActivated
void linkActivated(const QString &link)
Plasma::Label::wordWrap
bool wordWrap() const
Plasma::Label::alignment
Qt::Alignment alignment() const
Plasma::Label::itemChange
QVariant itemChange(GraphicsItemChange change, const QVariant &value)
Definition: label.cpp:330
QStyleOptionGraphicsItem
svg.h
Plasma::Label::setTextSelectable
void setTextSelectable(bool enable)
Set if the text on the label can be selected with the mouse.
Definition: label.cpp:172
QGraphicsWidget
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:33 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



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