From bd9cc5c12c022c6c368b51eb0d28bd6b8f8ef0ee Mon Sep 17 00:00:00 2001 From: Adriano Rezende Date: Tue, 3 Jul 2012 12:40:41 +0200 Subject: Fix ToolTip and improve ButtonBehavior Change-Id: I099aa93a8ee28c5e7e76ef8e316449266acbadd2 Reviewed-by: Jens Bache-Wiig --- components/Button.qml | 8 --- components/CheckBox.qml | 2 +- components/ToolButton.qml | 8 --- components/private/BasicButton.qml | 24 ++++----- components/private/ButtonBehavior.qml | 39 +++++---------- src/qquickcomponentsprivate.cpp | 63 ++++++++++++++++++++++++ src/qquickcomponentsprivate.h | 58 ++++++++++++++++++++++ src/qstyleplugin.cpp | 12 ++++- src/qtooltiparea.cpp | 91 ----------------------------------- src/qtooltiparea.h | 77 ----------------------------- src/src.pro | 4 +- 11 files changed, 160 insertions(+), 226 deletions(-) create mode 100644 src/qquickcomponentsprivate.cpp create mode 100644 src/qquickcomponentsprivate.h delete mode 100644 src/qtooltiparea.cpp delete mode 100644 src/qtooltiparea.h diff --git a/components/Button.qml b/components/Button.qml index 19dd394a..10908b7f 100644 --- a/components/Button.qml +++ b/components/Button.qml @@ -45,7 +45,6 @@ import QtDesktop 0.2 BasicButton { id: button - property alias containsMouse: tooltip.containsMouse property bool defaultbutton: false property string styleHint property string text @@ -55,13 +54,6 @@ BasicButton { Keys.onSpacePressed:animateClick() - TooltipArea { - // Note this will eat hover events - id: tooltip - anchors.fill: parent - text: button.tooltip - } - delegate: StyleItem { id: styleitem anchors.fill: parent diff --git a/components/CheckBox.qml b/components/CheckBox.qml index a97e5052..c4c152de 100644 --- a/components/CheckBox.qml +++ b/components/CheckBox.qml @@ -47,7 +47,7 @@ FocusScope { signal clicked - property alias pressed: behavior.pressed + property alias pressed: behavior.effectivePressed property alias checked: behavior.checked property alias containsMouse: behavior.containsMouse diff --git a/components/ToolButton.qml b/components/ToolButton.qml index 5952a6f4..55dd7bbc 100644 --- a/components/ToolButton.qml +++ b/components/ToolButton.qml @@ -45,19 +45,11 @@ import "private" as Private Private.BasicButton { id:button - property alias containsMouse: tooltip.containsMouse property string iconName property string styleHint property url iconSource property string text - TooltipArea { - // Note this will eat hover events - id: tooltip - anchors.fill: parent - text: button.tooltip - } - delegate: StyleItem { id: styleitem anchors.fill: parent diff --git a/components/private/BasicButton.qml b/components/private/BasicButton.qml index 2427060c..daa17adf 100644 --- a/components/private/BasicButton.qml +++ b/components/private/BasicButton.qml @@ -39,12 +39,13 @@ ****************************************************************************/ import QtQuick 2.0 +import QtDesktop.Internal 0.2 as Internal Item { id: button signal clicked - property alias pressed: behavior.pressed + property alias pressed: behavior.effectivePressed property alias containsMouse: behavior.containsMouse property alias checkable: behavior.checkable // button toggles between checked and !checked property alias checked: behavior.checked @@ -67,15 +68,15 @@ Item { implicitHeight: loader.item.implicitHeight function animateClick() { - behavior.pressed = true - behavior.clicked() + behavior.keyPressed = true + button.clicked() animateClickTimer.start() } Timer { id: animateClickTimer interval: 250 - onTriggered: behavior.pressed = false + onTriggered: behavior.keyPressed = false } Loader { @@ -90,13 +91,14 @@ Item { id: behavior anchors.fill: parent onClicked: button.clicked() - onPressedChanged: if (activeFocusOnPress) button.focus = true - onMouseMoved: {tiptimer.restart()} - Timer{ - id: tiptimer - interval:1000 - running:containsMouse && tooltip.length - onTriggered: button.toolTipTriggered() + onExited: Internal.hideToolTip() + onCanceled: Internal.hideToolTip() + onPressed: if (activeFocusOnPress) button.focus = true + + Timer { + interval: 1000 + running: containsMouse && !pressed && tooltip.length + onTriggered: Internal.showToolTip(behavior, Qt.point(behavior.mouseX, behavior.mouseY), tooltip) } } diff --git a/components/private/ButtonBehavior.qml b/components/private/ButtonBehavior.qml index 4942d62f..fdf0c79c 100644 --- a/components/private/ButtonBehavior.qml +++ b/components/private/ButtonBehavior.qml @@ -40,34 +40,21 @@ import QtQuick 2.0 -Item { - id: behavior - - signal clicked - property bool pressed: false // Can't be alias of mouseArea.pressed because the latter is read-only - property alias containsMouse: mouseArea.containsMouse +MouseArea { property bool checkable: false property bool checked: false - property bool triState: false - signal mouseMoved + property bool keyPressed: false + property bool effectivePressed: pressed && containsMouse || keyPressed + + hoverEnabled: true + + onCheckableChanged: { + if (!checkable) + checked = false; + } - onCheckableChanged: { if(!checkable) checked = false } - MouseArea { - id: mouseArea - anchors.fill: parent - hoverEnabled: true - onPositionChanged: behavior.mouseMoved() - onPressed: behavior.pressed = true // needed when hover is enabled - onEntered: if(pressed && enabled) behavior.pressed = true - onExited: behavior.pressed = false - onCanceled: behavior.pressed = false // mouse stolen e.g. by Flickable - onReleased: { - if(behavior.pressed && behavior.enabled) { // No click if release outside area - behavior.pressed = false - if(behavior.checkable) - behavior.checked = !behavior.checked; - behavior.clicked() - } - } + onReleased: { + if (checkable && containsMouse) + checked = !checked; } } diff --git a/src/qquickcomponentsprivate.cpp b/src/qquickcomponentsprivate.cpp new file mode 100644 index 00000000..2f424ca3 --- /dev/null +++ b/src/qquickcomponentsprivate.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Components project. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickcomponentsprivate.h" +#include +#include + + +QQuickComponentsPrivate::QQuickComponentsPrivate(QObject *parent) + : QObject(parent) +{ + +} + +void QQuickComponentsPrivate::showToolTip(QQuickItem *item, const QPointF &pos, const QString &str) +{ + if (!item || !item->canvas()) + return; + + QToolTip::showText(item->canvas()->mapToGlobal(item->mapToScene(pos).toPoint()), str); +} + +void QQuickComponentsPrivate::hideToolTip() +{ + QToolTip::hideText(); +} diff --git a/src/qquickcomponentsprivate.h b/src/qquickcomponentsprivate.h new file mode 100644 index 00000000..2f9c9ecf --- /dev/null +++ b/src/qquickcomponentsprivate.h @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Components project. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKCOMPONENTSPRIVATE_H +#define QQUICKCOMPONENTSPRIVATE_H + +#include +#include + +class QQuickComponentsPrivate : public QObject +{ + Q_OBJECT + +public: + QQuickComponentsPrivate(QObject *parent = 0); + + Q_INVOKABLE void showToolTip(QQuickItem *item, const QPointF &pos, const QString &text); + Q_INVOKABLE void hideToolTip(); +}; + +#endif diff --git a/src/qstyleplugin.cpp b/src/qstyleplugin.cpp index e375d253..4cf7a825 100644 --- a/src/qstyleplugin.cpp +++ b/src/qstyleplugin.cpp @@ -49,9 +49,9 @@ #include "qdesktopitem.h" #include "qwheelarea.h" #include "qcursorarea.h" -#include "qtooltiparea.h" #include "qtsplitterbase.h" #include "qquicklinearlayout.h" +#include "qquickcomponentsprivate.h" #include #include @@ -79,12 +79,20 @@ public: } }; +QObject *registerPrivateModule(QQmlEngine *engine, QJSEngine *jsEngine) +{ + Q_UNUSED(engine); + Q_UNUSED(jsEngine); + return new QQuickComponentsPrivate(); +} void StylePlugin::registerTypes(const char *uri) { + qmlRegisterModuleApi(QByteArray(uri) + ".Internal", + 0, 2, registerPrivateModule); + qmlRegisterType(uri, 0, 2, "StyleItem"); qmlRegisterType(uri, 0, 2, "CursorArea"); - qmlRegisterType(uri, 0, 2, "TooltipArea"); qmlRegisterType(uri, 0, 2, "RangeModel"); qmlRegisterType(uri, 0, 2, "WheelArea"); diff --git a/src/qtooltiparea.cpp b/src/qtooltiparea.cpp deleted file mode 100644 index 9b97d8fe..00000000 --- a/src/qtooltiparea.cpp +++ /dev/null @@ -1,91 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the Qt Components project. -** -** $QT_BEGIN_LICENSE:BSD$ -** You may use this file under the terms of the BSD license as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor -** the names of its contributors may be used to endorse or promote -** products derived from this software without specific prior written -** permission. -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qtooltiparea.h" -#include -#include -#include - -QTooltipArea::QTooltipArea(QQuickItem *parent) : - QQuickItem(parent), - m_containsMouse(false) -{ - setAcceptHoverEvents(true); - connect(&m_tiptimer, SIGNAL(timeout()), this, SLOT(timeout())); -} - -void QTooltipArea::setText(const QString &t) -{ - if (t != m_text) { - m_text = t; - emit textChanged(); - } -} - -void QTooltipArea::showToolTip(const QString &str) const -{ - Q_UNUSED(str); - //QToolTip::showText(cursor().pos(), str); -} - -void QTooltipArea::hoverEnterEvent(QHoverEvent *event) - -{ - Q_UNUSED(event); - m_tiptimer.start(1000); - - m_containsMouse = true; - emit containsMouseChanged(); - //QQuickItem::hoverEnterEvent(event); -} - -void QTooltipArea::hoverLeaveEvent(QHoverEvent *event) -{ - Q_UNUSED(event); - m_tiptimer.stop(); - m_containsMouse = false; - emit containsMouseChanged(); - //QQuickItem::hoverLeaveEvent(event); -} - -void QTooltipArea::timeout() -{ - showToolTip(m_text); -} diff --git a/src/qtooltiparea.h b/src/qtooltiparea.h deleted file mode 100644 index 5352ab94..00000000 --- a/src/qtooltiparea.h +++ /dev/null @@ -1,77 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the Qt Components project. -** -** $QT_BEGIN_LICENSE:BSD$ -** You may use this file under the terms of the BSD license as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor -** the names of its contributors may be used to endorse or promote -** products derived from this software without specific prior written -** permission. -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QTOOLTIPAREA_H -#define QTOOLTIPAREA_H - -#include -#include -#include - -class QTooltipArea : public QQuickItem -{ - Q_OBJECT - Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) - Q_PROPERTY(bool containsMouse READ containsMouse NOTIFY containsMouseChanged) - -public: - QTooltipArea(QQuickItem *parent = 0); - void setText(const QString &t); - QString text() const {return m_text;} - bool containsMouse() const {return m_containsMouse;} - void showToolTip(const QString &str) const; - void hoverEnterEvent(QHoverEvent *event); - void hoverLeaveEvent(QHoverEvent *event); - -public slots: - void timeout(); - -signals: - void textChanged(); - void containsMouseChanged(); - -private: - - QTimer m_tiptimer; - QString m_text; - bool m_containsMouse; -}; - -#endif // QTOOLTIPAREA_H diff --git a/src/src.pro b/src/src.pro index 353ab5cc..54fe2cc3 100644 --- a/src/src.pro +++ b/src/src.pro @@ -25,7 +25,7 @@ HEADERS += qtmenu.h \ qquicklayoutengine_p.h \ qquicklayout.h \ qquicklinearlayout.h \ - qtooltiparea.h \ + qquickcomponentsprivate.h \ qtsplitterbase.h SOURCES += qtmenu.cpp \ @@ -39,10 +39,10 @@ SOURCES += qtmenu.cpp \ qdesktopitem.cpp \ qtoplevelwindow.cpp \ qcursorarea.cpp \ - qtooltiparea.cpp \ qquicklayout.cpp \ qquicklayoutengine.cpp \ qquicklinearlayout.cpp \ + qquickcomponentsprivate.cpp \ qtsplitterbase.cpp TARGETPATH = QtDesktop/plugin -- cgit v1.2.1