From ceeb57d17fed3b7569b5e9ab92b771db8ce3e1a4 Mon Sep 17 00:00:00 2001 From: Kai Uwe Broulik Date: Wed, 25 Jan 2017 18:02:33 +0100 Subject: [PATCH] [PlasmaComponents Menu] Add maximumWidth property This allows to limit the maximum width of a menu. It can be useful for instance in task manager where long file names for recent documents will result in gigantic menus. The property has a RESET method (which is called if you assign "undefined") which will set it back to QWIDGETSIZE_MAX to remove the size restriction. Differential Revision: https://phabricator.kde.org/D4257 --- .../plasmacomponents/qmenu.cpp | 19 ++++++++++++ .../plasmacomponents/qmenu.h | 12 +++++++ tests/components/menu.qml | 31 +++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/src/declarativeimports/plasmacomponents/qmenu.cpp b/src/declarativeimports/plasmacomponents/qmenu.cpp index 9cd3ca682..35e42df24 100644 --- a/src/declarativeimports/plasmacomponents/qmenu.cpp +++ b/src/declarativeimports/plasmacomponents/qmenu.cpp @@ -153,6 +153,25 @@ void QMenuProxy::setMinimumWidth(int width) } } +int QMenuProxy::maximumWidth() const +{ + return m_menu->maximumWidth(); +} + +void QMenuProxy::setMaximumWidth(int width) +{ + if (m_menu->maximumWidth() != width) { + m_menu->setMaximumWidth(width); + + emit maximumWidthChanged(); + } +} + +void QMenuProxy::resetMaximumWidth() +{ + setMaximumWidth(QWIDGETSIZE_MAX); +} + bool QMenuProxy::event(QEvent *event) { switch (event->type()) { diff --git a/src/declarativeimports/plasmacomponents/qmenu.h b/src/declarativeimports/plasmacomponents/qmenu.h index 7b4360cc2..cde223e58 100644 --- a/src/declarativeimports/plasmacomponents/qmenu.h +++ b/src/declarativeimports/plasmacomponents/qmenu.h @@ -93,6 +93,13 @@ class QMenuProxy : public QObject */ Q_PROPERTY(int minimumWidth READ minimumWidth WRITE setMinimumWidth NOTIFY minimumWidthChanged) + /** + * A maximum width for the menu. + * + * @since 5.31 + */ + Q_PROPERTY(int maximumWidth READ maximumWidth WRITE setMaximumWidth RESET resetMaximumWidth NOTIFY maximumWidthChanged) + public: QMenuProxy(QObject *parent = 0); ~QMenuProxy(); @@ -114,6 +121,10 @@ public: int minimumWidth() const; void setMinimumWidth(int width); + int maximumWidth() const; + void setMaximumWidth(int maximumWidth); + void resetMaximumWidth(); + /** * This opens the menu at position x,y on the given visualParent. By default x and y are set to 0 */ @@ -163,6 +174,7 @@ Q_SIGNALS: void transientParentChanged(); void placementChanged(); void minimumWidthChanged(); + void maximumWidthChanged(); void triggered(QMenuItem *item); void triggeredIndex(int index); diff --git a/tests/components/menu.qml b/tests/components/menu.qml index 429cdb0cb..84f5a3b15 100644 --- a/tests/components/menu.qml +++ b/tests/components/menu.qml @@ -68,5 +68,36 @@ Rectangle { PlasmaComponents.MenuItem { text: "Another item" } } } + + Row { + spacing: units.smallSpacing + + PlasmaComponents.Button { + id: minMaxButton + text: "Fixed minimum and maximum width" + onClicked: minMaxMenu.open(0, height) + + PlasmaComponents.Menu { + id: minMaxMenu + + minimumWidth: minMaxButton.width + maximumWidth: limitMenuMaxWidth.checked ? minMaxButton.width : undefined // has a RESET property + + PlasmaComponents.MenuItem { text: "Hello" } + PlasmaComponents.MenuItem { text: "This is just a simple" } + PlasmaComponents.MenuItem { text: "Menu" } + PlasmaComponents.MenuItem { text: "with some very very long text in one item that will " + + "make the menu super huge if you don't do anything about it" } + PlasmaComponents.MenuItem { text: "and other stuff" } + } + } + + PlasmaComponents.CheckBox { + id: limitMenuMaxWidth + anchors.verticalCenter: parent.verticalCenter + text: "Limit maximum width" + checked: true + } + } } }