diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/imports/controls/Menu.qml | 2 | ||||
-rw-r--r-- | src/imports/controls/fusion/Menu.qml | 2 | ||||
-rw-r--r-- | src/imports/controls/material/Menu.qml | 2 | ||||
-rw-r--r-- | src/imports/controls/universal/Menu.qml | 2 | ||||
-rw-r--r-- | src/quicktemplates2/qquickmenu.cpp | 80 | ||||
-rw-r--r-- | src/quicktemplates2/qquickmenu_p.h | 6 | ||||
-rw-r--r-- | src/quicktemplates2/qquickmenu_p_p.h | 4 |
7 files changed, 97 insertions, 1 deletions
diff --git a/src/imports/controls/Menu.qml b/src/imports/controls/Menu.qml index 9e4ce902..31c9230a 100644 --- a/src/imports/controls/Menu.qml +++ b/src/imports/controls/Menu.qml @@ -49,6 +49,8 @@ T.Menu { margins: 0 + delegate: MenuItem { } + contentItem: ListView { implicitHeight: contentHeight model: control.contentModel diff --git a/src/imports/controls/fusion/Menu.qml b/src/imports/controls/fusion/Menu.qml index 9343afea..3fd4caa2 100644 --- a/src/imports/controls/fusion/Menu.qml +++ b/src/imports/controls/fusion/Menu.qml @@ -52,6 +52,8 @@ T.Menu { margins: 0 padding: 1 + delegate: MenuItem { } + contentItem: ListView { implicitHeight: contentHeight model: control.contentModel diff --git a/src/imports/controls/material/Menu.qml b/src/imports/controls/material/Menu.qml index 9767eed0..1d0d368a 100644 --- a/src/imports/controls/material/Menu.qml +++ b/src/imports/controls/material/Menu.qml @@ -56,6 +56,8 @@ T.Menu { transformOrigin: Item.Top + delegate: MenuItem { } + enter: Transition { // grow_fade_in NumberAnimation { property: "scale"; from: 0.9; to: 1.0; easing.type: Easing.OutQuint; duration: 220 } diff --git a/src/imports/controls/universal/Menu.qml b/src/imports/controls/universal/Menu.qml index 60dc8904..d7a27563 100644 --- a/src/imports/controls/universal/Menu.qml +++ b/src/imports/controls/universal/Menu.qml @@ -49,6 +49,8 @@ T.Menu { margins: 0 + delegate: MenuItem { } + contentItem: ListView { implicitHeight: contentHeight model: control.contentModel diff --git a/src/quicktemplates2/qquickmenu.cpp b/src/quicktemplates2/qquickmenu.cpp index 726a0fca..63d5d00c 100644 --- a/src/quicktemplates2/qquickmenu.cpp +++ b/src/quicktemplates2/qquickmenu.cpp @@ -39,11 +39,14 @@ #include "qquickmenuitem_p.h" #include "qquickcontrol_p_p.h" #include "qquickpopupitem_p_p.h" +#include "qquickaction_p.h" #include <QtGui/qevent.h> #include <QtGui/qcursor.h> #include <QtGui/qpa/qplatformintegration.h> #include <QtGui/private/qguiapplication_p.h> +#include <QtQml/qqmlcontext.h> +#include <QtQml/qqmlcomponent.h> #include <QtQml/private/qqmlengine_p.h> #include <QtQml/private/qv4scopedvalue_p.h> #include <QtQml/private/qv4variantobject_p.h> @@ -129,6 +132,17 @@ QT_BEGIN_NAMESPACE } \endcode + Since QtQuick.Controls 2.3 (Qt 5.10), it is also possible to declare + Action objects inside Menu: + + \code + Menu { + Action { text: "Cut" } + Action { text: "Copy" } + Action { text: "Paste" } + } + \endcode + Typically, menu items are statically declared as children of the menu, but Menu also provides API to \l {addItem}{add}, \l {insertItem}{insert}, \l {moveItem}{move} and \l {removeItem}{remove} items dynamically. The @@ -143,7 +157,8 @@ QT_BEGIN_NAMESPACE QQuickMenuPrivate::QQuickMenuPrivate() : contentItem(nullptr), - contentModel(nullptr) + contentModel(nullptr), + delegate(nullptr) { Q_Q(QQuickMenu); contentModel = new QQmlObjectModel(q); @@ -196,6 +211,30 @@ void QQuickMenuPrivate::removeItem(int index, QQuickItem *item) } } +QQuickItem *QQuickMenuPrivate::createItem(QQuickAction *action) +{ + Q_Q(QQuickMenu); + if (!delegate) + return nullptr; + + QQmlContext *creationContext = delegate->creationContext(); + if (!creationContext) + creationContext = qmlContext(q); + QQmlContext *context = new QQmlContext(creationContext, q); + context->setContextObject(q); + + QObject *object = delegate->beginCreate(context); + if (QQuickItem *item = qobject_cast<QQuickItem *>(object)) { + if (QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton*>(object)) + button->setAction(action); + delegate->completeCreate(); + return item; + } + + delete object; + return nullptr; +} + void QQuickMenuPrivate::resizeItem(QQuickItem *item) { if (!item || !contentItem) @@ -293,7 +332,13 @@ void QQuickMenuPrivate::contentData_append(QQmlListProperty<QObject> *prop, QObj { QQuickMenuPrivate *p = static_cast<QQuickMenuPrivate *>(prop->data); QQuickMenu *q = static_cast<QQuickMenu *>(prop->object); + QQuickItem *item = qobject_cast<QQuickItem *>(obj); + if (!item) { + if (QQuickAction *action = qobject_cast<QQuickAction *>(obj)) + item = p->createItem(action); + } + if (item) { if (QQuickItemPrivate::get(item)->isTransparentForPositioner()) { QQuickItemPrivate::get(item)->addItemChangeListener(p, QQuickItemPrivate::SiblingOrder); @@ -493,6 +538,39 @@ void QQuickMenu::setTitle(QString &title) /*! \since QtQuick.Controls 2.3 (Qt 5.10) + \qmlproperty Component QtQuick.Controls::Menu::delegate + + This property holds the component that is used to create items + to present actions. + + \code + Menu { + Action { text: "Cut" } + Action { text: "Copy" } + Action { text: "Paste" } + } + \endcode + + \sa Action +*/ +QQmlComponent *QQuickMenu::delegate() const +{ + Q_D(const QQuickMenu); + return d->delegate; +} + +void QQuickMenu::setDelegate(QQmlComponent *delegate) +{ + Q_D(QQuickMenu); + if (d->delegate == delegate) + return; + + d->delegate = delegate; + emit delegateChanged(); +} + +/*! + \since QtQuick.Controls 2.3 (Qt 5.10) \qmlmethod void QtQuick.Controls::Menu::popup(MenuItem item = null) Opens the menu at the mouse cursor on desktop platforms that have a mouse cursor diff --git a/src/quicktemplates2/qquickmenu_p.h b/src/quicktemplates2/qquickmenu_p.h index 868daaa3..bfb1d605 100644 --- a/src/quicktemplates2/qquickmenu_p.h +++ b/src/quicktemplates2/qquickmenu_p.h @@ -55,6 +55,7 @@ QT_BEGIN_NAMESPACE +class QQmlComponent; class QQuickMenuItem; class QQuickMenuPrivate; @@ -64,6 +65,7 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickMenu : public QQuickPopup Q_PROPERTY(QVariant contentModel READ contentModel CONSTANT FINAL) Q_PROPERTY(QQmlListProperty<QObject> contentData READ contentData FINAL) Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged) + Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged FINAL REVISION 3) Q_CLASSINFO("DefaultProperty", "contentData") public: @@ -81,6 +83,9 @@ public: QString title() const; void setTitle(QString &title); + QQmlComponent *delegate() const; + void setDelegate(QQmlComponent *delegate); + Q_REVISION(3) Q_INVOKABLE void popup(QQmlV4Function *args); protected: @@ -91,6 +96,7 @@ protected: Q_SIGNALS: void titleChanged(); + Q_REVISION(3) void delegateChanged(); protected: QPalette defaultPalette() const override; diff --git a/src/quicktemplates2/qquickmenu_p_p.h b/src/quicktemplates2/qquickmenu_p_p.h index 504bc74d..e78076ad 100644 --- a/src/quicktemplates2/qquickmenu_p_p.h +++ b/src/quicktemplates2/qquickmenu_p_p.h @@ -55,6 +55,8 @@ QT_BEGIN_NAMESPACE +class QQuickAction; +class QQmlComponent; class QQmlObjectModel; class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickMenuPrivate : public QQuickPopupPrivate @@ -68,6 +70,7 @@ public: void insertItem(int index, QQuickItem *item); void moveItem(int from, int to); void removeItem(int index, QQuickItem *item); + QQuickItem *createItem(QQuickAction *action); void resizeItem(QQuickItem *item); void resizeItems(); @@ -92,6 +95,7 @@ public: QQuickItem *contentItem; // TODO: cleanup QVector<QObject *> contentData; QQmlObjectModel *contentModel; + QQmlComponent *delegate; QString title; }; |