[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
This commit is contained in:
Kai Uwe Broulik 2017-01-25 18:02:33 +01:00
parent c65edd7167
commit ceeb57d17f
3 changed files with 62 additions and 0 deletions

View File

@ -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()) {

View File

@ -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);

View File

@ -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
}
}
}
}