• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kde-runtime API Reference
  • KDE Home
  • Contact Us
 

PlasmaCore

  • sources
  • kde-4.14
  • kde-runtime
  • plasma
  • declarativeimports
  • core
tooltip.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Marco Martin <mart@kde.org>
3  * Copyright 2011 Artur Duque de Souza <asouza@kde.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Library General Public License as
7  * published by the Free Software Foundation; either version 2, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 #include "tooltip.h"
22 #include "declarativeitemcontainer_p.h"
23 
24 #include <QDeclarativeItem>
25 #include <QGraphicsObject>
26 #include <QGraphicsWidget>
27 #include <QGraphicsScene>
28 #include <QDebug>
29 #include <QTimer>
30 
31 #include <KDE/KIcon>
32 #include <KDE/KIconLoader>
33 #include <KDE/Plasma/ToolTipContent>
34 #include <KDE/Plasma/ToolTipManager>
35 
36 
37 ToolTipProxy::ToolTipProxy(QObject *parent)
38  : QObject(parent), m_mainText(""), m_subText(""), m_widget(0)
39 {
40  connect(this, SIGNAL(targetChanged()), this, SLOT(updateToolTip()));
41  connect(this, SIGNAL(mainTextChanged()), this, SLOT(updateToolTip()));
42  connect(this, SIGNAL(subTextChanged()), this, SLOT(updateToolTip()));
43  connect(this, SIGNAL(imageChanged()), this, SLOT(updateToolTip()));
44 }
45 
46 ToolTipProxy::~ToolTipProxy()
47 {
48 }
49 
50 QGraphicsObject *ToolTipProxy::target() const
51 {
52  return m_target.data();
53 }
54 
55 void ToolTipProxy::setTarget(QGraphicsObject *target)
56 {
57  if (m_target.data() != target) {
58  m_target = target;
59 
60  m_widget = qobject_cast<QGraphicsWidget*>(m_target.data());
61  if (!m_widget) {
62  // if this is called in Compenent.onCompleted we have to
63  // wait a loop for the item to be added to a scene
64  QTimer::singleShot(0, this, SLOT(syncTarget()));
65  return;
66  }
67  emit targetChanged();
68  }
69 }
70 
71 void ToolTipProxy::syncTarget()
72 {
73  if (!m_target) {
74  return;
75  }
76  // find the scene
77  QGraphicsScene *scene = m_target.data()->scene();
78  if (!scene) {
79  QObject *parent = m_target.data();
80  while ((parent = parent->parent())) {
81  QGraphicsObject *qo = qobject_cast<QGraphicsObject*>(parent);
82  if (qo && qo->scene()) {
83  scene = qo->scene();
84  scene->addItem(m_target.data());
85  break;
86  }
87  }
88  }
89 
90  QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(m_target.data());
91  if (!item) {
92  return;
93  }
94 
95  if (!m_declarativeItemContainer && scene) {
96  m_declarativeItemContainer = QWeakPointer<DeclarativeItemContainer>(new DeclarativeItemContainer());
97  m_declarativeItemContainer.data()->setObjectName("DIContainer");
98  scene->addItem(m_declarativeItemContainer.data());
99  }
100 
101  if (m_declarativeItemContainer) {
102  m_target.data()->setObjectName("Original Item");
103  m_declarativeItemContainer.data()->setDeclarativeItem(item, false);
104  m_declarativeItemContainer.data()->setAcceptHoverEvents(true);
105  m_declarativeItemContainer.data()->setParentItem(m_target.data());
106  m_widget = m_declarativeItemContainer.data();
107  emit targetChanged();
108  }
109 }
110 
111 QString ToolTipProxy::mainText() const
112 {
113  return m_mainText;
114 }
115 
116 void ToolTipProxy::setMainText(const QString &text)
117 {
118  if (text == m_mainText) {
119  return;
120  }
121 
122  m_mainText = text;
123  emit mainTextChanged();
124 }
125 
126 QString ToolTipProxy::subText() const
127 {
128  return m_subText;
129 }
130 
131 void ToolTipProxy::setSubText(const QString &text)
132 {
133  if (text == m_subText) {
134  return;
135  }
136 
137  m_subText = text;
138  emit subTextChanged();
139 }
140 
141 QVariant ToolTipProxy::image() const
142 {
143  return m_image;
144 }
145 
146 void ToolTipProxy::setImage(QVariant name)
147 {
148  if (name == m_image) {
149  return;
150  }
151 
152  m_image = name;
153  emit imageChanged();
154 }
155 
156 void ToolTipProxy::updateToolTip()
157 {
158  if (!m_widget) {
159  return;
160  }
161 
162  Plasma::ToolTipContent data;
163  data.setMainText(m_mainText);
164  data.setSubText(m_subText);
165 
166  // set image
167  switch (m_image.type()) {
168  case QVariant::String: {
169  QString name = m_image.toString();
170  if (!name.isEmpty()) {
171  KIcon icon(name);
172  if (!icon.isNull()) {
173  data.setImage(icon.pixmap(IconSize(KIconLoader::Desktop)));
174  }
175  }
176  break;
177  }
178 
179  case QVariant::Icon: {
180  QIcon icon = m_image.value<QIcon>();
181  data.setImage(icon);
182  break;
183  }
184 
185  case QVariant::Pixmap: {
186  QPixmap pixmap = m_image.value<QPixmap>();
187  data.setImage(pixmap);
188  break;
189  }
190 
191  default:
192  break;
193  }
194  Plasma::ToolTipManager::self()->setContent(m_widget, data);
195 }
196 
197 #include "tooltip.moc"
198 
DeclarativeItemContainer
Definition: declarativeitemcontainer_p.h:29
QGraphicsScene
ToolTipProxy::subText
QString subText() const
ToolTipProxy::subTextChanged
void subTextChanged()
ToolTipProxy::syncTarget
void syncTarget()
Definition: tooltip.cpp:71
ToolTipProxy::mainText
QString mainText() const
QVariant::value
T value() const
QWeakPointer::data
T * data() const
tooltip.h
QGraphicsItem::scene
QGraphicsScene * scene() const
QObject::name
const char * name() const
ToolTipProxy::target
QGraphicsObject * target() const
QObject
QString::isEmpty
bool isEmpty() const
QGraphicsWidget
ToolTipProxy::imageChanged
void imageChanged()
QString
ToolTipProxy::setMainText
void setMainText(const QString &text)
Definition: tooltip.cpp:116
ToolTipProxy::setTarget
void setTarget(QGraphicsObject *target)
Definition: tooltip.cpp:55
QGraphicsObject
QPixmap
ToolTipProxy::setSubText
void setSubText(const QString &text)
Definition: tooltip.cpp:131
declarativeitemcontainer_p.h
QWeakPointer< DeclarativeItemContainer >
ToolTipProxy::updateToolTip
void updateToolTip()
Definition: tooltip.cpp:156
ToolTipProxy::targetChanged
void targetChanged()
QDeclarativeItem
ToolTipProxy::image
QVariant image() const
ToolTipProxy::setImage
void setImage(QVariant name)
Definition: tooltip.cpp:146
ToolTipProxy::mainTextChanged
void mainTextChanged()
QVariant::type
Type type() const
QGraphicsScene::addItem
void addItem(QGraphicsItem *item)
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::parent
QObject * parent() const
ToolTipProxy::ToolTipProxy
ToolTipProxy(QObject *parent=0)
Definition: tooltip.cpp:37
ToolTipProxy::~ToolTipProxy
~ToolTipProxy()
Definition: tooltip.cpp:46
QVariant::toString
QString toString() const
QIcon
QTimer::singleShot
singleShot
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:08:28 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

PlasmaCore

Skip menu "PlasmaCore"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kde-runtime API Reference

Skip menu "kde-runtime API Reference"
  • KCMShell
  • KNotify
  • Plasma Runtime
  •     PlasmaCore
  •     DragAndDrop
  •     PlasmaComponents
  •     PlasmaExtraComponents
  •     QtExtraComponents

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