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 @@ -1,382 +1,401 @@ /*************************************************************************** * Copyright 2011 Viranch Mehta * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #include "qmenu.h" #include #include #include #include #include #include "plasmacomponentsplugin.h" QMenuProxy::QMenuProxy(QObject *parent) : QObject(parent), m_menu(nullptr), m_status(DialogStatus::Closed), m_placement(Plasma::Types::LeftPosedTopAlignedPopup) { if (qobject_cast(QCoreApplication::instance())) { m_menu = new QMenu(0); connect(m_menu, &QMenu::triggered, this, &QMenuProxy::itemTriggered); connect(m_menu, &QMenu::aboutToHide, [ = ]() { m_status = DialogStatus::Closed; emit statusChanged(); }); } } QMenuProxy::~QMenuProxy() { delete m_menu; } QQmlListProperty QMenuProxy::content() { return QQmlListProperty(this, m_items); } int QMenuProxy::actionCount() const { return m_items.count(); } QMenuItem *QMenuProxy::action(int index) const { return m_items.at(index); } DialogStatus::Status QMenuProxy::status() const { return m_status; } QObject *QMenuProxy::visualParent() const { return m_visualParent.data(); } void QMenuProxy::setVisualParent(QObject *parent) { if (m_visualParent.data() == parent) { return; } //if the old parent was a QAction, disconnect the menu from it QAction *action = qobject_cast(m_visualParent.data()); if (action) { action->setMenu(0); m_menu->clear(); } //if parent is a QAction, become a submenu action = qobject_cast(parent); if (action) { action->setMenu(m_menu); m_menu->clear(); foreach (QMenuItem *item, m_items) { if (item->section()) { if (!item->isVisible()) { continue; } m_menu->addSection(item->text()); } else { m_menu->addAction(item->action()); } } m_menu->updateGeometry(); } m_visualParent = parent; emit visualParentChanged(); } QWindow *QMenuProxy::transientParent() { if (!m_menu) { return nullptr; } return m_menu->windowHandle()->transientParent(); } void QMenuProxy::setTransientParent(QWindow *parent) { if (parent == m_menu->windowHandle()->transientParent()) { return; } m_menu->windowHandle()->setTransientParent(parent); emit transientParentChanged(); } Plasma::Types::PopupPlacement QMenuProxy::placement() const { return m_placement; } void QMenuProxy::setPlacement(Plasma::Types::PopupPlacement placement) { if (m_placement != placement) { m_placement = placement; emit placementChanged(); } } int QMenuProxy::minimumWidth() const { return m_menu->minimumWidth(); } void QMenuProxy::setMinimumWidth(int width) { if (m_menu->minimumWidth() != width) { m_menu->setMinimumWidth(width); emit minimumWidthChanged(); } } +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()) { case QEvent::ChildAdded: { QChildEvent *ce = static_cast(event); QMenuItem *mi = qobject_cast(ce->child()); //FIXME: linear complexity here if (mi && !m_items.contains(mi)) { if (mi->separator()) { m_menu->addSection(mi->text()); } else { m_menu->addAction(mi->action()); } m_items << mi; } break; } case QEvent::ChildRemoved: { QChildEvent *ce = static_cast(event); QMenuItem *mi = qobject_cast(ce->child()); //FIXME: linear complexity here if (mi) { m_menu->removeAction(mi->action()); m_items.removeAll(mi); } break; } default: break; } return QObject::event(event); } void QMenuProxy::clearMenuItems() { qDeleteAll(m_items); m_items.clear(); } void QMenuProxy::addMenuItem(const QString &text) { QMenuItem *item = new QMenuItem(); item->setText(text); m_menu->addAction(item->action()); m_items << item; } void QMenuProxy::addMenuItem(QMenuItem *item, QMenuItem *before) { if (before) { if (m_items.contains(item)) { m_menu->removeAction(item->action()); m_items.removeAll(item); } m_menu->insertAction(before->action(), item->action()); const int index = m_items.indexOf(before); if (index != -1) { m_items.insert(index, item); } else { m_items << item; } } else if (!m_items.contains(item)) { m_menu->addAction(item->action()); m_items << item; } } void QMenuProxy::addSection(const QString &text) { m_menu->addSection(text); } void QMenuProxy::removeMenuItem(QMenuItem *item) { if (!item) { return; } m_menu->removeAction(item->action()); m_items.removeOne(item); } void QMenuProxy::itemTriggered(QAction *action) { QMenuItem *item = qobject_cast(action); if (item) { emit triggered(item); int index = m_items.indexOf(item); if (index > -1) { emit triggeredIndex(index); } } } void QMenuProxy::rebuildMenu() { m_menu->clear(); foreach (QMenuItem *item, m_items) { if (item->section()) { if (!item->isVisible()) { continue; } m_menu->addSection(item->text()); } else { m_menu->addAction(item->action()); } } m_menu->adjustSize(); } void QMenuProxy::open(int x, int y) { qDebug() << "Opening menu at" << x << y; QQuickItem *parentItem = nullptr; if (m_visualParent) { parentItem = qobject_cast(m_visualParent.data()); } else { parentItem = qobject_cast(parent()); } if (!parentItem) { return; } rebuildMenu(); QPointF pos = parentItem->mapToScene(QPointF(x, y)); if (parentItem->window() && parentItem->window()->screen()) { pos = parentItem->window()->mapToGlobal(pos.toPoint()); } openInternal(pos.toPoint()); } Q_INVOKABLE void QMenuProxy::openRelative() { QQuickItem *parentItem = nullptr; if (m_visualParent) { parentItem = qobject_cast(m_visualParent.data()); } else { parentItem = qobject_cast(parent()); } if (!parentItem) { return; } rebuildMenu(); QPointF pos; using namespace Plasma; switch(m_placement) { case Types::TopPosedLeftAlignedPopup: case Types::LeftPosedTopAlignedPopup: { pos = parentItem->mapToScene(QPointF(0, 0)); break; } case Types::TopPosedRightAlignedPopup: case Types::RightPosedTopAlignedPopup: { pos = parentItem->mapToScene(QPointF(parentItem->width(), 0)); break; } case Types::LeftPosedBottomAlignedPopup: case Types::BottomPosedLeftAlignedPopup: { pos = parentItem->mapToScene(QPointF(0, parentItem->height())); break; } case Types::BottomPosedRightAlignedPopup: case Types::RightPosedBottomAlignedPopup: { pos = parentItem->mapToScene(QPointF(parentItem->width(), parentItem->height())); break; } default: open(); return; } if (parentItem->window() && parentItem->window()->screen()) { pos = parentItem->window()->mapToGlobal(pos.toPoint()); } QScreen *screen = parentItem->window()->screen(); if (screen) { if (pos.x() + m_menu->width() > (screen->geometry().x() + screen->geometry().width())) { pos.setX(pos.x() - m_menu->width()); } if (pos.y() + m_menu->height() > (screen->geometry().y() + screen->geometry().height())) { pos.setY(pos.y() - m_menu->height()); } } openInternal(pos.toPoint()); } void QMenuProxy::openInternal(QPoint pos) { m_menu->popup(pos); m_status = DialogStatus::Open; emit statusChanged(); } void QMenuProxy::close() { m_menu->hide(); } 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 @@ -1,184 +1,196 @@ /*************************************************************************** * Copyright 2011 Viranch Mehta * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #ifndef QMENU_PROXY_H #define QMENU_PROXY_H #include #include #include #include "qmenuitem.h" #include "enums.h" #include "plasma.h" class QDeclarativeItem; /** * @class Menu * * An Item provides a menu for use in context specific situations. * You can specify the position for the menu to open by setting its visualParent. * MenuItems should be used to draw entries in the menu. * The open() function opens up the menu at the given visualParent. * * * Example usage: * @code * import org.kde.plasma.components 2.0 as PlasmaComponents * * [...] * PlasmaComponents.Menu { * id: menu * ... * PlasmaComponents.MenuItem { * text: "Delete" * onClicked: { * myListItem.remove(); * } * } * } * PlasmaComponents.Button { * id: btn * onClicked: { * menu.visualParent = btn * menu.open() * } * } * [...] * @endcode * */ class QMenuProxy : public QObject { Q_OBJECT Q_PROPERTY(QQmlListProperty content READ content CONSTANT) Q_CLASSINFO("DefaultProperty", "content") /** * This is a hint to the window manager that this window is a dialog or pop-up on behalf of the given window. */ Q_PROPERTY(QWindow *transientParent READ transientParent WRITE setTransientParent NOTIFY transientParentChanged) /** * the visualParent is used to position the menu. it can be an item on the scene, like a button (that will open the menu on clicked) or another menuitem (in this case this will be a submenu) */ Q_PROPERTY(QObject *visualParent READ visualParent WRITE setVisualParent NOTIFY visualParentChanged()) Q_PROPERTY(DialogStatus::Status status READ status NOTIFY statusChanged) /** * The default placement for the menu. */ Q_PROPERTY(Plasma::Types::PopupPlacement placement READ placement WRITE setPlacement NOTIFY placementChanged) /** * A minimum width for the menu. */ 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(); QQmlListProperty content(); int actionCount() const; QMenuItem *action(int) const; DialogStatus::Status status() const; QObject *visualParent() const; void setVisualParent(QObject *parent); QWindow *transientParent(); void setTransientParent(QWindow *parent); Plasma::Types::PopupPlacement placement() const; void setPlacement(Plasma::Types::PopupPlacement placement); 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 */ Q_INVOKABLE void open(int x = 0, int y = 0); /** * This opens the menu at the specified placement relative to the visualParent. */ Q_INVOKABLE void openRelative(); /** * This closes the menu */ Q_INVOKABLE void close(); /** * This removes all menuItems inside the menu */ Q_INVOKABLE void clearMenuItems(); /** * This adds a menu item from a String */ Q_INVOKABLE void addMenuItem(const QString &text); /** * This adds MenuItem 'item' to the menu before MenuItem 'before'. * If MenuItem 'before' is 0 or does not exist in the menu, 'item' * is appended to the menu instead. * If MenuItem 'item' already exists in the menu, it is removed and * inserted at the new position. */ Q_INVOKABLE void addMenuItem(QMenuItem *item, QMenuItem *before = nullptr); /** * This adds a section header with a string used as name for the section */ Q_INVOKABLE void addSection(const QString &text); /** * This removes MenuItem 'item' * * @since 5.27 */ Q_INVOKABLE void removeMenuItem(QMenuItem *item); protected: bool event(QEvent *event) Q_DECL_OVERRIDE; Q_SIGNALS: void statusChanged(); void visualParentChanged(); void transientParentChanged(); void placementChanged(); void minimumWidthChanged(); + void maximumWidthChanged(); void triggered(QMenuItem *item); void triggeredIndex(int index); private Q_SLOTS: void itemTriggered(QAction *item); private: void rebuildMenu(); void openInternal(QPoint pos); QList m_items; QMenu *m_menu; DialogStatus::Status m_status; QWeakPointer m_visualParent; Plasma::Types::PopupPlacement m_placement; }; #endif //QMENU_PROXY_H 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 @@ -1,72 +1,103 @@ import QtQuick 2.0 import org.kde.plasma.components 2.0 as PlasmaComponents Rectangle { width: 600 height: 200 color: "white" Flow { anchors.fill: parent anchors.margins: 20 spacing: 20 PlasmaComponents.Button { text: "Simple menu" onClicked: simpleMenu.open(0, height) PlasmaComponents.Menu { id: simpleMenu PlasmaComponents.MenuItem { text: "Hello" } PlasmaComponents.MenuItem { text: "This is just a simple" } PlasmaComponents.MenuItem { text: "Menu" } PlasmaComponents.MenuItem { text: "without separators" } PlasmaComponents.MenuItem { text: "and other stuff" } } } PlasmaComponents.Button { text: "Checkable menu items" onClicked: checkableMenu.open(0, height) PlasmaComponents.Menu { id: checkableMenu PlasmaComponents.MenuItem { text: "Apple"; checkable: true } PlasmaComponents.MenuItem { text: "Banana"; checkable: true } PlasmaComponents.MenuItem { text: "Orange"; checkable: true } } } PlasmaComponents.Button { text: "Icons" onClicked: iconsMenu.open(0, height) PlasmaComponents.Menu { id: iconsMenu PlasmaComponents.MenuItem { text: "Error"; icon: "dialog-error" } PlasmaComponents.MenuItem { text: "Warning"; icon: "dialog-warning" } PlasmaComponents.MenuItem { text: "Information"; icon: "dialog-information" } } } PlasmaComponents.Button { text: "Separators and sections" onClicked: sectionsMenu.open(0, height) PlasmaComponents.Menu { id: sectionsMenu PlasmaComponents.MenuItem { text: "A menu"; section: true } PlasmaComponents.MenuItem { text: "One entry" } PlasmaComponents.MenuItem { text: "Another entry" } PlasmaComponents.MenuItem { separator: true } PlasmaComponents.MenuItem { text: "One item" } 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 + } + } } }