MenuManager: modernize for loops
This commit is contained in:
committed by
Chris Hennes
parent
fc40c22ecc
commit
5610d226c3
@@ -71,9 +71,9 @@ MenuItem* MenuItem::findItem(const std::string& name)
|
||||
return this;
|
||||
}
|
||||
else {
|
||||
for (QList<MenuItem*>::Iterator it = _items.begin(); it != _items.end(); ++it) {
|
||||
if ((*it)->_name == name) {
|
||||
return *it;
|
||||
for (auto& item : _items) {
|
||||
if (item->_name == name) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -83,15 +83,15 @@ MenuItem* MenuItem::findItem(const std::string& name)
|
||||
|
||||
MenuItem* MenuItem::findParentOf(const std::string& name)
|
||||
{
|
||||
for (QList<MenuItem*>::Iterator it = _items.begin(); it != _items.end(); ++it) {
|
||||
if ((*it)->_name == name) {
|
||||
for (auto& item : _items) {
|
||||
if (item->_name == name) {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
for (QList<MenuItem*>::Iterator it = _items.begin(); it != _items.end(); ++it) {
|
||||
if ((*it)->findParentOf(name)) {
|
||||
return *it;
|
||||
for (auto& item : _items) {
|
||||
if (item->findParentOf(name)) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,10 +103,9 @@ MenuItem* MenuItem::copy() const
|
||||
auto root = new MenuItem;
|
||||
root->setCommand(command());
|
||||
|
||||
QList<MenuItem*> items = getItems();
|
||||
for (QList<MenuItem*>::Iterator it = items.begin(); it != items.end(); ++it)
|
||||
for (auto& item : _items)
|
||||
{
|
||||
root->appendItem((*it)->copy());
|
||||
root->appendItem(item->copy());
|
||||
}
|
||||
|
||||
return root;
|
||||
@@ -152,8 +151,8 @@ void MenuItem::removeItem(MenuItem* item)
|
||||
|
||||
void MenuItem::clear()
|
||||
{
|
||||
for (QList<MenuItem*>::Iterator it = _items.begin(); it != _items.end(); ++it) {
|
||||
delete *it;
|
||||
for (auto& item : _items) {
|
||||
delete item;
|
||||
}
|
||||
_items.clear();
|
||||
}
|
||||
@@ -246,22 +245,21 @@ void MenuManager::setup(MenuItem* menuItems) const
|
||||
menuBar->clear();
|
||||
#endif
|
||||
|
||||
QList<MenuItem*> items = menuItems->getItems();
|
||||
QList<QAction*> actions = menuBar->actions();
|
||||
for (QList<MenuItem*>::Iterator it = items.begin(); it != items.end(); ++it)
|
||||
for (auto& item : menuItems->getItems())
|
||||
{
|
||||
// search for the menu action
|
||||
QAction* action = findAction(actions, QString::fromLatin1((*it)->command().c_str()));
|
||||
QAction* action = findAction(actions, QString::fromLatin1(item->command().c_str()));
|
||||
if (!action) {
|
||||
// There must be not more than one separator in the menu bar, so
|
||||
// we can safely remove it if available and append it at the end
|
||||
if ((*it)->command() == "Separator") {
|
||||
if (item->command() == "Separator") {
|
||||
action = menuBar->addSeparator();
|
||||
action->setObjectName(QLatin1String("Separator"));
|
||||
}
|
||||
else {
|
||||
// create a new menu
|
||||
std::string menuName = (*it)->command();
|
||||
std::string menuName = item->command();
|
||||
QMenu* menu = menuBar->addMenu(
|
||||
QApplication::translate("Workbench", menuName.c_str()));
|
||||
action = menu->menuAction();
|
||||
@@ -270,7 +268,7 @@ void MenuManager::setup(MenuItem* menuItems) const
|
||||
}
|
||||
|
||||
// set the menu user data
|
||||
action->setData(QString::fromLatin1((*it)->command().c_str()));
|
||||
action->setData(QString::fromLatin1(item->command().c_str()));
|
||||
}
|
||||
else {
|
||||
// put the menu at the end
|
||||
@@ -283,13 +281,13 @@ void MenuManager::setup(MenuItem* menuItems) const
|
||||
|
||||
// flll up the menu
|
||||
if (!action->isSeparator()) {
|
||||
setup(*it, action->menu());
|
||||
setup(item, action->menu());
|
||||
}
|
||||
}
|
||||
|
||||
// hide all menus which we don't need for the moment
|
||||
for (QList<QAction*>::Iterator it = actions.begin(); it != actions.end(); ++it) {
|
||||
(*it)->setVisible(false);
|
||||
for (auto& action : actions) {
|
||||
action->setVisible(false);
|
||||
}
|
||||
|
||||
// enable update again
|
||||
@@ -299,42 +297,39 @@ void MenuManager::setup(MenuItem* menuItems) const
|
||||
void MenuManager::setup(MenuItem* item, QMenu* menu) const
|
||||
{
|
||||
CommandManager& mgr = Application::Instance->commandManager();
|
||||
QList<MenuItem*> items = item->getItems();
|
||||
QList<QAction*> actions = menu->actions();
|
||||
for (QList<MenuItem*>::Iterator it = items.begin(); it != items.end(); ++it) {
|
||||
for (auto& item : item->getItems()) {
|
||||
// search for the menu item
|
||||
QList<QAction*> used_actions = findActions(actions, QString::fromLatin1((*it)->command().c_str()));
|
||||
QList<QAction*> used_actions = findActions(actions, QString::fromLatin1(item->command().c_str()));
|
||||
if (used_actions.isEmpty()) {
|
||||
if ((*it)->command() == "Separator") {
|
||||
if (item->command() == "Separator") {
|
||||
QAction* action = menu->addSeparator();
|
||||
action->setObjectName(QLatin1String("Separator"));
|
||||
// set the menu user data
|
||||
action->setData(QLatin1String("Separator"));
|
||||
used_actions.append(action);
|
||||
}
|
||||
else {
|
||||
if ((*it)->hasItems()) {
|
||||
if (item->hasItems()) {
|
||||
// Creste a submenu
|
||||
std::string menuName = (*it)->command();
|
||||
QMenu* submenu = menu->addMenu(
|
||||
QApplication::translate("Workbench", menuName.c_str()));
|
||||
std::string menuName = item->command();
|
||||
QMenu* submenu = menu->addMenu(QApplication::translate("Workbench", menuName.c_str()));
|
||||
QAction* action = submenu->menuAction();
|
||||
submenu->setObjectName(QString::fromLatin1((*it)->command().c_str()));
|
||||
action->setObjectName(QString::fromLatin1((*it)->command().c_str()));
|
||||
submenu->setObjectName(QString::fromLatin1(item->command().c_str()));
|
||||
action->setObjectName(QString::fromLatin1(item->command().c_str()));
|
||||
// set the menu user data
|
||||
action->setData(QString::fromLatin1((*it)->command().c_str()));
|
||||
action->setData(QString::fromLatin1(item->command().c_str()));
|
||||
used_actions.append(action);
|
||||
}
|
||||
else {
|
||||
// A command can have more than one QAction
|
||||
int count = menu->actions().count();
|
||||
// Check if action was added successfully
|
||||
if (mgr.addTo((*it)->command().c_str(), menu)) {
|
||||
if (mgr.addTo(item->command().c_str(), menu)) {
|
||||
QList<QAction*> acts = menu->actions();
|
||||
for (int i=count; i < acts.count(); i++) {
|
||||
QAction* act = acts[i];
|
||||
// set the menu user data
|
||||
act->setData(QString::fromLatin1((*it)->command().c_str()));
|
||||
act->setData(QString::fromLatin1(item->command().c_str()));
|
||||
used_actions.append(act);
|
||||
}
|
||||
}
|
||||
@@ -342,34 +337,33 @@ void MenuManager::setup(MenuItem* item, QMenu* menu) const
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (QList<QAction*>::Iterator it = used_actions.begin(); it != used_actions.end(); ++it) {
|
||||
for (auto& action : used_actions) {
|
||||
// put the menu item at the end
|
||||
menu->removeAction(*it);
|
||||
menu->addAction(*it);
|
||||
int index = actions.indexOf(*it);
|
||||
menu->removeAction(action);
|
||||
menu->addAction(action);
|
||||
int index = actions.indexOf(action);
|
||||
actions.removeAt(index);
|
||||
}
|
||||
}
|
||||
|
||||
// fill up the submenu
|
||||
if ((*it)->hasItems()) {
|
||||
setup(*it, used_actions.front()->menu());
|
||||
if (item->hasItems()) {
|
||||
setup(item, used_actions.front()->menu());
|
||||
}
|
||||
}
|
||||
|
||||
// remove all menu items which we don't need for the moment
|
||||
for (QList<QAction*>::Iterator it = actions.begin(); it != actions.end(); ++it) {
|
||||
menu->removeAction(*it);
|
||||
for (auto& action : actions) {
|
||||
menu->removeAction(action);
|
||||
}
|
||||
}
|
||||
|
||||
void MenuManager::retranslate() const
|
||||
{
|
||||
QMenuBar* menuBar = getMainWindow()->menuBar();
|
||||
QList<QAction*> actions = menuBar->actions();
|
||||
for (QList<QAction*>::Iterator it = actions.begin(); it != actions.end(); ++it) {
|
||||
if ((*it)->menu()) {
|
||||
retranslate((*it)->menu());
|
||||
for (auto& action : menuBar->actions()) {
|
||||
if (action->menu()) {
|
||||
retranslate(action->menu());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -395,19 +389,18 @@ void MenuManager::retranslate(QMenu* menu) const
|
||||
QApplication::translate("Workbench",
|
||||
(const char*)menuName));
|
||||
}
|
||||
QList<QAction*> actions = menu->actions();
|
||||
for (QList<QAction*>::Iterator it = actions.begin(); it != actions.end(); ++it) {
|
||||
if ((*it)->menu()) {
|
||||
retranslate((*it)->menu());
|
||||
for (auto& action : menu->actions()) {
|
||||
if (action->menu()) {
|
||||
retranslate(action->menu());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QAction* MenuManager::findAction(const QList<QAction*>& acts, const QString& item) const
|
||||
{
|
||||
for (QList<QAction*>::ConstIterator it = acts.begin(); it != acts.end(); ++it) {
|
||||
if ((*it)->data().toString() == item) {
|
||||
return *it;
|
||||
for (auto& action : acts) {
|
||||
if (action->data().toString() == item) {
|
||||
return action;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -422,9 +415,9 @@ QList<QAction*> MenuManager::findActions(const QList<QAction*>& acts, const QStr
|
||||
// name.
|
||||
bool first_match = false;
|
||||
QList<QAction*> used;
|
||||
for (QList<QAction*>::ConstIterator it = acts.begin(); it != acts.end(); ++it) {
|
||||
if ((*it)->data().toString() == item) {
|
||||
used.append(*it);
|
||||
for (auto& action : acts) {
|
||||
if (action->data().toString() == item) {
|
||||
used.append(action);
|
||||
first_match = true;
|
||||
// get only one separator per request
|
||||
if (item == QLatin1String("Separator")) {
|
||||
|
||||
Reference in New Issue
Block a user