ToolTip->ToolTipArea

This commit is contained in:
Marco Martin 2014-01-08 18:13:07 +01:00
parent 798b7665d4
commit 6a0525c1c3
6 changed files with 77 additions and 165 deletions

View File

@ -91,6 +91,12 @@ PlasmaComponents.Page {
Row { Row {
spacing: _s spacing: _s
PlasmaCore.ToolTipArea {
width: childrenRect.width
height: childrenRect.height
icon: "klipper"
mainText: "Fish sighted in the wild, in the wild, a fish was seen."
subText: "A mean-looking grouper swam by."
PlasmaCore.IconItem { PlasmaCore.IconItem {
id: akonadiIcon id: akonadiIcon
objectName: "akonadiIcon" objectName: "akonadiIcon"
@ -99,95 +105,53 @@ PlasmaComponents.Page {
height: _h height: _h
//anchors.horizontalCenter: parent.horizontalCenter //anchors.horizontalCenter: parent.horizontalCenter
Rectangle { color: "orange"; opacity: 0.3; anchors.fill: parent; } Rectangle { color: "orange"; opacity: 0.3; anchors.fill: parent; }
PlasmaCore.ToolTip {
anchors.fill: parent
target: akonadiIcon
icon: "klipper"
mainText: "Fish sighted in the wild, in the wild, a fish was seen."
subText: "A mean-looking grouper swam by."
} }
} }
PlasmaCore.ToolTipArea {
height: _h
width: height
image: bridgeimage.source
mainText: "Bridge"
subText: "Waalbrug."
Image { Image {
id: bridgeimage
objectName: "bridgeimage" objectName: "bridgeimage"
height: _h height: _h
width: height width: height
fillMode: Image.PreserveAspectFit fillMode: Image.PreserveAspectFit
source: "../images/bridge.jpg" source: "../images/bridge.jpg"
PlasmaCore.ToolTip {
target: parent
image: parent.source
mainText: "Bridge"
subText: "Waalbrug."
} }
} }
PlasmaCore.ToolTipArea {
width: childrenRect.width
height: childrenRect.height
mainItem: PlasmaComponents.Label {
text: "Nijmegen North Beach"
anchors.centerIn: parent
}
Image { Image {
objectName: "surfboardimage" objectName: "surfboardimage"
height: _h height: _h
width: height width: height
fillMode: Image.PreserveAspectFit fillMode: Image.PreserveAspectFit
source: "../images/surfboard.jpg" source: "../images/surfboard.jpg"
PlasmaCore.ToolTip {
anchors.fill: parent
image: parent.source
mainItem: PlasmaComponents.Label {
text: "Nijmegen North Beach"
anchors.centerIn: parent
}
//subText: "A surfboard on the beach. <br />The photo shows the Waal river's north beach, \ //subText: "A surfboard on the beach. <br />The photo shows the Waal river's north beach, \
//across the water from Nijmegen, Netherlands. It was taken during the summer festivals a few years back." //across the water from Nijmegen, Netherlands. It was taken during the summer festivals a few years back."
} }
} }
PlasmaCore.ToolTipArea {
width: childrenRect.width
height: childrenRect.height
mainText: "Tooltip on button"
PlasmaComponents.Button { PlasmaComponents.Button {
id: button
text: "Button" text: "Button"
iconSource: "call-start" iconSource: "call-start"
PlasmaCore.ToolTip {
target: parent
mainText: "Tooltip on button"
}
}
QtExtras.MouseEventListener {
id: task1
width: height
height: _h*2
hoverEnabled: true
onContainsMouseChanged: {
if (containsMouse) {
print("Setting target to task1");
tooltip.target = task1;
tooltip.mainText = "Bridge"
tooltip.subText = "in Nijmegen"
} else {
print("Setting target to null");
tooltip.target = null;
}
}
Image {
objectName: "bridgeimage1"
anchors.fill: parent
fillMode: Image.PreserveAspectFit
source: "../images/bridge.jpg"
MouseArea {
anchors.fill: parent
onClicked: {
print("bridgeimage1 clicked");
}
}
} }
} }
} }
} }
PlasmaCore.ToolTip {
id: tooltip
target: null
}
} }

View File

@ -85,7 +85,7 @@ void CoreBindingsPlugin::registerTypes(const char *uri)
qmlRegisterType<Plasma::SortFilterModel>(uri, 2, 0, "SortFilterModel"); qmlRegisterType<Plasma::SortFilterModel>(uri, 2, 0, "SortFilterModel");
qmlRegisterType<DialogProxy>(uri, 2, 0, "Dialog"); qmlRegisterType<DialogProxy>(uri, 2, 0, "Dialog");
qmlRegisterType<ToolTip>(uri, 2, 0, "ToolTip"); qmlRegisterType<ToolTip>(uri, 2, 0, "ToolTipArea");
qmlRegisterInterface<Plasma::Service>("Service"); qmlRegisterInterface<Plasma::Service>("Service");
qRegisterMetaType<Plasma::Service*>("Service"); qRegisterMetaType<Plasma::Service*>("Service");

View File

@ -90,7 +90,6 @@ Item {
PlasmaExtras.Heading { PlasmaExtras.Heading {
id: tooltipMaintext id: tooltipMaintext
level: 3 level: 3
width: parent.preferredTextWidth
wrapMode: Text.WordWrap wrapMode: Text.WordWrap
text: toolTip ? toolTip.mainText : "" text: toolTip ? toolTip.mainText : ""
anchors { anchors {

View File

@ -35,9 +35,11 @@ ToolTip::ToolTip(QQuickItem *parent)
m_showTimer = new QTimer(this); m_showTimer = new QTimer(this);
m_showTimer->setSingleShot(true); m_showTimer->setSingleShot(true);
connect(m_showTimer, &QTimer::timeout, [=]() { connect(m_showTimer, &QTimer::timeout, [=]() {
setVisible(true); showToolTip();
}); });
setAcceptHoverEvents(true); setAcceptHoverEvents(true);
setFiltersChildMouseEvents(true);
} }
ToolTip::~ToolTip() ToolTip::~ToolTip()
@ -58,40 +60,9 @@ void ToolTip::setMainItem(QQuickItem *mainItem)
} }
} }
QQuickItem *ToolTip::target() const void ToolTip::showToolTip()
{
if (m_target.data()) {
return m_target.data();
} else {
QQuickItem *qqi = qobject_cast<QQuickItem*>(parent());
return qqi;
}
}
void ToolTip::setTarget(QQuickItem *target)
{
if (m_target.data() == target) {
return;
}
m_target = target;
setParentItem(target);
property("anchors").value<QObject *>()->setProperty("fill", QVariant::fromValue(parentItem()));
emit targetChanged();
}
bool ToolTip::isVisible() const
{ {
ToolTipDialog *dlg = ToolTipDialog::instance(); ToolTipDialog *dlg = ToolTipDialog::instance();
return (dlg->mainItem() == mainItem() && mainItem() && mainItem()->isVisible());
}
void ToolTip::setVisible(const bool visible)
{
ToolTipDialog *dlg = ToolTipDialog::instance();
if (visible) {
if (!mainItem()) { if (!mainItem()) {
setMainItem(dlg->loadDefaultItem()); setMainItem(dlg->loadDefaultItem());
@ -107,11 +78,8 @@ void ToolTip::setVisible(const bool visible)
} }
dlg->setMainItem(mainItem()); dlg->setMainItem(mainItem());
dlg->setVisualParent(target());
dlg->setVisible(true); dlg->setVisible(true);
} else { dlg->setVisualParent(this);
dlg->setVisible(false);
}
} }
QString ToolTip::mainText() const QString ToolTip::mainText() const
@ -185,24 +153,20 @@ void ToolTip::setImage(const QVariant &image)
void ToolTip::hoverEnterEvent(QHoverEvent *event) void ToolTip::hoverEnterEvent(QHoverEvent *event)
{ {
if (ToolTipDialog::instance()->isVisible()) { if (ToolTipDialog::instance()->isVisible()) {
//FIXME: setVisible needs to be renamed in sync or something like that //FIXME: showToolTip needs to be renamed in sync or something like that
setVisible(true); showToolTip();
} else { } else {
m_showTimer->start(500); m_showTimer->start(500);
} }
//relay the event
if (window()) {
window()->sendEvent(target(), event);
}
} }
void ToolTip::hoverLeaveEvent(QHoverEvent *event) void ToolTip::hoverLeaveEvent(QHoverEvent *event)
{ {
m_showTimer->stop(); m_showTimer->stop();
//relay the event
if (window()) {
window()->sendEvent(target(), event);
}
} }
bool ToolTip::childMouseEventFilter(QQuickItem *item, QEvent *event)
{
return QQuickItem::childMouseEventFilter(item, event);
}

View File

@ -43,16 +43,6 @@ class ToolTip : public QQuickItem
*/ */
Q_PROPERTY(QQuickItem *mainItem READ mainItem WRITE setMainItem NOTIFY mainItemChanged) Q_PROPERTY(QQuickItem *mainItem READ mainItem WRITE setMainItem NOTIFY mainItemChanged)
/**
* The main QML item that will be displayed in the Dialog
*/
Q_PROPERTY(QQuickItem *target READ target WRITE setTarget NOTIFY targetChanged)
/**
* Visibility of the Dialog window. Doesn't have anything to do with the visibility of the mainItem.
*/
Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged)
/** /**
* The main text of this tooltip * The main text of this tooltip
*/ */
@ -81,11 +71,7 @@ public:
QQuickItem *mainItem() const; QQuickItem *mainItem() const;
void setMainItem(QQuickItem *mainItem); void setMainItem(QQuickItem *mainItem);
QQuickItem *target() const; void showToolTip();
void setTarget(QQuickItem *target);
bool isVisible() const;
void setVisible(const bool visible);
QString mainText() const; QString mainText() const;
void setMainText(const QString &mainText); void setMainText(const QString &mainText);
@ -100,12 +86,12 @@ public:
void setImage(const QVariant &image); void setImage(const QVariant &image);
protected: protected:
bool childMouseEventFilter(QQuickItem *item, QEvent *event);
void hoverEnterEvent(QHoverEvent *event); void hoverEnterEvent(QHoverEvent *event);
void hoverLeaveEvent(QHoverEvent *event); void hoverLeaveEvent(QHoverEvent *event);
Q_SIGNALS: Q_SIGNALS:
void mainItemChanged(); void mainItemChanged();
void targetChanged();
void visibleChanged(); void visibleChanged();
void mainTextChanged(); void mainTextChanged();
void subTextChanged(); void subTextChanged();
@ -114,7 +100,6 @@ Q_SIGNALS:
private: private:
QWeakPointer<QQuickItem> m_mainItem; QWeakPointer<QQuickItem> m_mainItem;
QWeakPointer<QQuickItem> m_target;
QTimer *m_showTimer; QTimer *m_showTimer;
QString m_mainText; QString m_mainText;
QString m_subText; QString m_subText;

View File

@ -60,7 +60,7 @@ QUrl PackageUrlInterceptor::intercept(const QUrl &path, QQmlAbstractUrlIntercept
if (path.scheme() == QStringLiteral("plasmapackage")) { if (path.scheme() == QStringLiteral("plasmapackage")) {
//FIXME: this is incorrect but works around a bug in qml in resolution of urls of qmldir files //FIXME: this is incorrect but works around a bug in qml in resolution of urls of qmldir files
if (type == QQmlAbstractUrlInterceptor::QmldirFile) { if (0&&type == QQmlAbstractUrlInterceptor::QmldirFile) {
return QUrl(m_package.filePath(0, path.path())); return QUrl(m_package.filePath(0, path.path()));
} else { } else {
return QUrl::fromLocalFile(m_package.filePath(0, path.path())); return QUrl::fromLocalFile(m_package.filePath(0, path.path()));