From 49e3dc41a682aa688fc019e328bab9f8e194e02a Mon Sep 17 00:00:00 2001 From: Laurent Rineau Date: Mon, 20 Jul 2020 17:26:43 +0200 Subject: [PATCH] Reimplement addActionToMenu with standard algorithms --- Polyhedron/demo/Polyhedron/MainWindow.cpp | 36 +++++++---------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/Polyhedron/demo/Polyhedron/MainWindow.cpp b/Polyhedron/demo/Polyhedron/MainWindow.cpp index 27d4717f955..6f9073421dd 100644 --- a/Polyhedron/demo/Polyhedron/MainWindow.cpp +++ b/Polyhedron/demo/Polyhedron/MainWindow.cpp @@ -409,32 +409,18 @@ MainWindow::MainWindow(const QStringList &keywords, bool verbose, QWidget* paren void addActionToMenu(QAction* action, QMenu* menu) { - bool added = false; - QString atxt = action->text().remove("&"); - if(atxt.isEmpty()) - return; - for(QAction* it : menu->actions()) - { - QString btxt = it->text().remove("&"); - int i = 0; - if(btxt.isEmpty()) - { - continue; - } - while(i < atxt.size() - && i < btxt.size() - && atxt[i] == btxt[i]) - ++i; - bool res = (i == atxt.size() || i == btxt.size() || atxt[i] < btxt[i]); - if (res) - { - menu->insertAction(it, action); - added = true; - break; - } - } - if(!added) + auto actions = menu->actions(); + auto it = std::lower_bound(actions.begin(), actions.end(), + action->text().remove("&"), + [](QAction* a, QString text) { + return a->text().remove("&").compare(text) < 0; + }); + if(it == actions.end()) { menu->addAction(action); + } + else { + menu->insertAction(*it, action); + } } //Recursive function that do a pass over a menu and its sub-menus(etc.) and hide them when they are empty