QML Bindings for Plasma::ToolTips

Implementation of a proxy to make it easy to declare tooltips in QML,
that will use Plasma::ToolTipManager to display tooltips in any item
that is declared in QML.

The syntax is:

import org.kde.plasma.core 0.1 as PlasmaCore

PlasmaCore.ToolTip {
    target: id_of_the_target
    mainText: "hello"
    subText: "world"
    image: "konqueror"
}

The syntax looks pretty straightforward and simple. "id_of_the_target"
is the id of the element which will show the tooltip in case it's hovered.

If the target is a QGraphicsWidget, the code path is really simple but if
it's a QDeclarativeItem, then we create a wrapper (that is a QGraphicsWidget)
and setup that in a way that we set this wrapper as the target on Plasma's
API. For libplasma2 we may want to change this so it's more generic (and
also put this together with the components).

Right now there is two open issues for me:

 1) we should use "image" or "icon"? Allowing the use of a image that is
a string that identifies the icon for KIconLoader seems simpler and more
beautiful to me, but it would be nice to specify a random QPixmap. Besides
being able to use the bindings for QIcon it doesn't look very good. Marco,
do you have any thoughts on this issue?

 2) for touch oriented UIs, the tooltip as it is right now is not very
useful. Is there any "touch" scenario where we may want to use them?

CCMAIL:mart@kde.org
Signed-off-by: Artur Duque de Souza <asouza@kde.org>
This commit is contained in:
Artur Duque de Souza 2011-04-27 19:33:29 -03:00
parent 436fd7e762
commit f1e61647b6
2 changed files with 244 additions and 0 deletions

View File

@ -0,0 +1,168 @@
/***************************************************************************
* Copyright 2011 Marco Martin <mart@kde.org> *
* Copyright 2011 Artur Duque de Souza <asouza@kde.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
***************************************************************************/
#include "tooltip.h"
#include "declarativeitemcontainer_p.h"
#include <QDeclarativeItem>
#include <QGraphicsObject>
#include <QGraphicsWidget>
#include <QGraphicsScene>
#include <QDebug>
#include <QTimer>
#include <KIcon>
#include <KIconLoader>
#include <Plasma/ToolTipContent>
#include <Plasma/ToolTipManager>
ToolTipProxy::ToolTipProxy(QObject *parent)
: QObject(parent), m_mainText(""), m_subText(""),
m_declarativeItemContainer(0), m_widget(0)
{
connect(this, SIGNAL(targetChanged()), this, SLOT(updateToolTip()));
connect(this, SIGNAL(mainTextChanged()), this, SLOT(updateToolTip()));
connect(this, SIGNAL(subTextChanged()), this, SLOT(updateToolTip()));
connect(this, SIGNAL(imageChanged()), this, SLOT(updateToolTip()));
}
ToolTipProxy::~ToolTipProxy()
{
delete m_declarativeItemContainer;
}
QGraphicsObject *ToolTipProxy::target() const
{
return m_target.data();
}
void ToolTipProxy::setTarget(QGraphicsObject *target)
{
if (m_target.data() != target) {
m_target = target;
m_widget = qobject_cast<QGraphicsWidget*>(m_target.data());
if (!m_widget) {
// if this is called in Compenent.onCompleted we have to
// wait a loop for the item to be added to a scene
QTimer::singleShot(0, this, SLOT(syncTarget()));
return;
}
emit targetChanged();
}
}
void ToolTipProxy::syncTarget()
{
// find the scene
QGraphicsScene *scene = m_target.data()->scene();
if (!scene) {
QObject *parent = m_target.data();
while ((parent = parent->parent())) {
QGraphicsObject *qo = qobject_cast<QGraphicsObject*>(parent);
if (qo) {
scene = qo->scene();
scene->addItem(m_target.data());
break;
}
}
}
QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(m_target.data());
if (!item) {
return;
}
if (!m_declarativeItemContainer) {
m_declarativeItemContainer = new DeclarativeItemContainer();
m_declarativeItemContainer->setObjectName("DIContainer");
scene->addItem(m_declarativeItemContainer);
}
m_target.data()->setObjectName("Original Item");
m_declarativeItemContainer->setDeclarativeItem(item, false);
m_declarativeItemContainer->setAcceptHoverEvents(true);
m_declarativeItemContainer->setParentItem(m_target.data());
m_widget = m_declarativeItemContainer;
emit targetChanged();
}
QString ToolTipProxy::mainText() const
{
return m_mainText;
}
void ToolTipProxy::setMainText(const QString &text)
{
if (text == m_mainText) {
return;
}
m_mainText = text;
emit mainTextChanged();
}
QString ToolTipProxy::subText() const
{
return m_subText;
}
void ToolTipProxy::setSubText(const QString &text)
{
if (text == m_subText) {
return;
}
m_subText = text;
emit subTextChanged();
}
// ###TODO: SHOULD BE PIXMAP OR QICON??
QString ToolTipProxy::image() const
{
return m_image;
}
void ToolTipProxy::setImage(const QString &name)
{
if (name == m_image) {
return;
}
m_image = name;
emit imageChanged();
}
void ToolTipProxy::updateToolTip()
{
if (!m_widget) {
return;
}
Plasma::ToolTipContent data;
data.setMainText(m_mainText);
data.setSubText(m_subText);
data.setImage(KIcon(m_image).pixmap(IconSize(KIconLoader::Desktop)));
Plasma::ToolTipManager::self()->setContent(m_widget, data);
}
#include "tooltip.moc"

View File

@ -0,0 +1,76 @@
/***************************************************************************
* Copyright 2011 Marco Martin <mart@kde.org> *
* Copyright 2011 Artur Duque de Souza <asouza@kde.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
***************************************************************************/
#ifndef TOOLTIP_PROXY_P
#define TOOLTIP_PROXY_P
#include <QObject>
#include <QWeakPointer>
class QGraphicsObject;
class QGraphicsWidget;
class DeclarativeItemContainer;
class ToolTipProxy : public QObject
{
Q_OBJECT
Q_PROPERTY(QGraphicsObject *target READ target WRITE setTarget NOTIFY targetChanged)
Q_PROPERTY(QString mainText READ mainText WRITE setMainText NOTIFY mainTextChanged)
Q_PROPERTY(QString subText READ subText WRITE setSubText NOTIFY subTextChanged)
Q_PROPERTY(QString image READ image WRITE setImage NOTIFY imageChanged)
public:
ToolTipProxy(QObject *parent = 0);
~ToolTipProxy();
QGraphicsObject *target() const;
void setTarget(QGraphicsObject *target);
QString mainText() const;
void setMainText(const QString &text);
QString subText() const;
void setSubText(const QString &text);
// SHOULD BE PIXMAP OR QICON
QString image() const;
void setImage(const QString &name);
Q_SIGNALS:
void targetChanged();
void mainTextChanged();
void subTextChanged();
void imageChanged();
protected Q_SLOTS:
void syncTarget();
void updateToolTip();
private:
QString m_mainText;
QString m_subText;
QString m_image;
DeclarativeItemContainer *m_declarativeItemContainer;
QGraphicsWidget *m_widget;
QWeakPointer<QGraphicsObject> m_target;
};
#endif