vcl/inc/qt5/QtInstanceMenuButton.hxx | 3 ++- vcl/qt5/QtInstanceMenuButton.cxx | 34 +++++++++++++++++++++++++++------- 2 files changed, 29 insertions(+), 8 deletions(-)
New commits: commit 5e3805934e882afe3b8a46902551b770616301a4 Author: Michael Weghorn <m.wegh...@posteo.de> AuthorDate: Mon Jan 27 22:06:29 2025 +0100 Commit: Michael Weghorn <m.wegh...@posteo.de> CommitDate: Tue Jan 28 08:19:52 2025 +0100 tdf#130857 qt weld: Address clazy warning (clazy-range-loop-detach) Address this clazy warning: .../libreoffice/vcl/qt5/QtInstanceMenuButton.cxx:154:5: c++11 range-loop might detach Qt container (QList) [clazy-range-loop-detach] Change-Id: I4348b2c66b54716a2442f5c3ccafafd66cf92333 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/180808 Tested-by: Jenkins Reviewed-by: Michael Weghorn <m.wegh...@posteo.de> diff --git a/vcl/qt5/QtInstanceMenuButton.cxx b/vcl/qt5/QtInstanceMenuButton.cxx index 617c02d3d980..6bbcd9cdcc9b 100644 --- a/vcl/qt5/QtInstanceMenuButton.cxx +++ b/vcl/qt5/QtInstanceMenuButton.cxx @@ -150,7 +150,7 @@ QMenu& QtInstanceMenuButton::getMenu() const QAction* QtInstanceMenuButton::getAction(const OUString& rIdent) const { - QList<QAction*> aActions = getMenu().actions(); + const QList<QAction*> aActions = getMenu().actions(); for (QAction* pAction : aActions) { if (pAction && pAction->objectName() == toQString(rIdent)) commit 12c6bf058cf88b6d4cfe6e0aff09cf183ea9e033 Author: Michael Weghorn <m.wegh...@posteo.de> AuthorDate: Mon Jan 27 21:46:17 2025 +0100 Commit: Michael Weghorn <m.wegh...@posteo.de> CommitDate: Tue Jan 28 08:19:46 2025 +0100 tdf#130857 qt weld: Support MenuButton separators + respect pos Implement QtInstanceMenuButton::insert_separator. Add a helper method QtInstanceMenuButton::insertAction that inserts the given action at the given position and use it from QtInstanceMenuButton::insert_item as well, which was so far only assigning the `nPos` local var, but not actually taking it into account to specify where to insert the new action. Use QWidget::insertAction [1] that allows to specify before which existing action the action should be inserted instead of always appending to the end. [1] https://doc.qt.io/qt-6/qwidget.html#insertAction Change-Id: I0acefb29f01b680b4efdf0ad6cea3bbf7f84c4b7 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/180807 Tested-by: Jenkins Reviewed-by: Michael Weghorn <m.wegh...@posteo.de> diff --git a/vcl/inc/qt5/QtInstanceMenuButton.hxx b/vcl/inc/qt5/QtInstanceMenuButton.hxx index 84959df71852..52b8af9c1839 100644 --- a/vcl/inc/qt5/QtInstanceMenuButton.hxx +++ b/vcl/inc/qt5/QtInstanceMenuButton.hxx @@ -27,7 +27,7 @@ public: const OUString* pIconName, VirtualDevice* pImageSurface, TriState eCheckRadioFalse) override; - virtual void insert_separator(int pos, const OUString& rId) override; + virtual void insert_separator(int nPos, const OUString& rId) override; virtual void remove_item(const OUString& rId) override; virtual void clear() override; virtual void set_item_sensitive(const OUString& rIdent, bool bSensitive) override; @@ -41,6 +41,7 @@ public: private: QMenu& getMenu() const; QAction* getAction(const OUString& rIdent) const; + void insertAction(QAction* pAction, int nPos); private Q_SLOTS: void handleButtonClicked(); diff --git a/vcl/qt5/QtInstanceMenuButton.cxx b/vcl/qt5/QtInstanceMenuButton.cxx index e68a7f656c90..617c02d3d980 100644 --- a/vcl/qt5/QtInstanceMenuButton.cxx +++ b/vcl/qt5/QtInstanceMenuButton.cxx @@ -40,22 +40,29 @@ void QtInstanceMenuButton::insert_item(int nPos, const OUString& rId, const OUSt (void)eCheckRadioFalse; GetQtInstance().RunInMainThread([&] { - if (nPos == -1) - nPos = getMenu().actions().count(); - - QAction* pAction = getMenu().addAction(vclToQtStringWithAccelerator(rStr)); + QAction* pAction = new QAction(vclToQtStringWithAccelerator(rStr), &getMenu()); pAction->setObjectName(toQString(rId)); if (pIconName) pAction->setIcon(loadQPixmapIcon(*pIconName)); else if (pImageSurface) pAction->setIcon(toQPixmap(*pImageSurface)); + + insertAction(pAction, nPos); }); } -void QtInstanceMenuButton::insert_separator(int, const OUString&) +void QtInstanceMenuButton::insert_separator(int nPos, const OUString& rId) { - assert(false && "Not implemented yet"); + SolarMutexGuard g; + + GetQtInstance().RunInMainThread([&] { + QAction* pAction = new QAction(&getMenu()); + pAction->setSeparator(true); + pAction->setObjectName(toQString(rId)); + + insertAction(pAction, nPos); + }); } void QtInstanceMenuButton::remove_item(const OUString& rId) @@ -153,6 +160,19 @@ QAction* QtInstanceMenuButton::getAction(const OUString& rIdent) const return nullptr; } +void QtInstanceMenuButton::insertAction(QAction* pAction, int nPos) +{ + SolarMutexGuard g; + + GetQtInstance().RunInMainThread([&] { + QAction* pNextAction = nullptr; + QList<QAction*> pActions = getMenu().actions(); + if (nPos >= 0 && nPos < pActions.count()) + pNextAction = pActions.at(nPos); + getMenu().insertAction(pNextAction, pAction); + }); +} + void QtInstanceMenuButton::handleButtonClicked() { if (m_pPopover)