Showing the changes to interface/src/Menu.cpp (1 of 3 files). Show all
Unified · Split
| @@ -1286,63 +1286,162 @@ QString Menu::replaceLastOccurrence(QChar search, QChar replace, QString string) · 1285 unmodified lines | ||
| 1286 | 1286 | return string; |
| 1287 | 1287 | } |
| 1288 | 1288 | |
| 1289 | −void Menu::addTopMenu(const QString& menu) { | |
| 1290 | − addMenu(menu); | |
| 1289 | +QAction* Menu::getActionFromName(const QString& menuName, QMenu* menu) { | |
| 1290 | + QList<QAction*> menuActions; | |
| 1291 | + if (menu) { | |
| 1292 | + menuActions = menu->actions(); | |
| 1293 | + } else { | |
| 1294 | + menuActions = actions(); | |
| 1295 | + } | |
| 1296 | + | |
| 1297 | + foreach (QAction* menuAction, menuActions) { | |
| 1298 | + if (menuName == menuAction->text()) { | |
| 1299 | + return menuAction; | |
| 1300 | + } | |
| 1301 | + } | |
| 1302 | + return NULL; | |
| 1303 | +} | |
| 1291 | 1304 | |
| 1292 | − QMenuBar::repaint(); | |
| 1305 | +QMenu* Menu::getSubMenuFromName(const QString& menuName, QMenu* menu) { | |
| 1306 | + QAction* action = getActionFromName(menuName, menu); | |
| 1307 | + if (action) { | |
| 1308 | + return action->menu(); | |
| 1309 | + } | |
| 1310 | + return NULL; | |
| 1311 | +} | |
| 1312 | + | |
| 1313 | +QMenu* Menu::getMenuParent(const QString& menuName, QString& finalMenuPart) { | |
| 1314 | + QStringList menuTree = menuName.split(">"); | |
| 1315 | + QMenu* parent = NULL; | |
| 1316 | + QMenu* menu = NULL; | |
| 1317 | + foreach (QString menuTreePart, menuTree) { | |
| 1318 | + parent = menu; | |
| 1319 | + finalMenuPart = menuTreePart.trimmed(); | |
| 1320 | + menu = getSubMenuFromName(finalMenuPart, parent); | |
| 1321 | + if (!menu) { | |
| 1322 | + break; | |
| 1323 | + } | |
| 1324 | + } | |
| 1325 | + return parent; | |
| 1326 | +} | |
| 1327 | + | |
| 1328 | +QMenu* Menu::getMenu(const QString& menuName) { | |
| 1329 | + QStringList menuTree = menuName.split(">"); | |
| 1330 | + QMenu* parent = NULL; | |
| 1331 | + QMenu* menu = NULL; | |
| 1332 | + int item = 0; | |
| 1333 | + foreach (QString menuTreePart, menuTree) { | |
| 1334 | + menu = getSubMenuFromName(menuTreePart.trimmed(), parent); | |
| 1335 | + if (!menu) { | |
| 1336 | + break; | |
| 1337 | + } | |
| 1338 | + parent = menu; | |
| 1339 | + item++; | |
| 1340 | + } | |
| 1341 | + return menu; | |
| 1342 | +} | |
| 1293 | 1343 | |
| 1294 | − QList<QAction*> topLevelMenus = actions(); | |
| 1295 | − foreach (QAction* topLevelMenuAction, topLevelMenus) { | |
| 1296 | − qDebug() << "menu:" << topLevelMenuAction->text(); | |
| 1344 | +QAction* Menu::getMenuAction(const QString& menuName) { | |
| 1345 | + QStringList menuTree = menuName.split(">"); | |
| 1346 | + QMenu* parent = NULL; | |
| 1347 | + QAction* action = NULL; | |
| 1348 | + foreach (QString menuTreePart, menuTree) { | |
| 1349 | + action = getActionFromName(menuTreePart.trimmed(), parent); | |
| 1350 | + if (!action) { | |
| 1351 | + break; | |
| 1352 | + } | |
| 1353 | + parent = action->menu(); | |
| 1297 | 1354 | } |
| 1355 | + return action; | |
| 1298 | 1356 | } |
| 1299 | 1357 | |
| 1300 | −void Menu::removeTopMenu(const QString& menu) { | |
| 1301 | − QList<QAction*> topLevelMenus = actions(); | |
| 1302 | − foreach (QAction* topLevelMenuAction, topLevelMenus) { | |
| 1303 | − if (topLevelMenuAction->text() == menu) { | |
| 1304 | − QMenuBar::removeAction(topLevelMenuAction); | |
| 1358 | +QMenu* Menu::addMenu(const QString& menuName) { | |
| 1359 | + QStringList menuTree = menuName.split(">"); | |
| 1360 | + QMenu* addTo = NULL; | |
| 1361 | + QMenu* menu = NULL; | |
| 1362 | + foreach (QString menuTreePart, menuTree) { | |
| 1363 | + menu = getSubMenuFromName(menuTreePart.trimmed(), addTo); | |
| 1364 | + if (!menu) { | |
| 1365 | + if (!addTo) { | |
| 1366 | + menu = QMenuBar::addMenu(menuTreePart.trimmed()); | |
| 1367 | + } else { | |
| 1368 | + menu = addTo->addMenu(menuTreePart.trimmed()); | |
| 1369 | + } | |
| 1305 | 1370 | } |
| 1371 | + addTo = menu; | |
| 1306 | 1372 | } |
| 1373 | + | |
| 1374 | + QMenuBar::repaint(); | |
| 1375 | + return menu; | |
| 1307 | 1376 | } |
| 1308 | 1377 | |
| 1309 | −void Menu::addMenuItem(const QString& menu, const QString& menuitem) { | |
| 1310 | − KeyEvent noKey; | |
| 1311 | − addMenuItem(menu, menuitem, noKey); | |
| 1378 | +void Menu::removeMenu(const QString& menuName) { | |
| 1379 | + QAction* action = getMenuAction(menuName); | |
| 1380 | + | |
| 1381 | + // only proceed if the menu actually exists | |
| 1382 | + if (action) { | |
| 1383 | + QString finalMenuPart; | |
| 1384 | + QMenu* parent = getMenuParent(menuName, finalMenuPart); | |
| 1385 | + | |
| 1386 | + if (parent) { | |
| 1387 | + removeAction(parent, finalMenuPart); | |
| 1388 | + } else { | |
| 1389 | + QMenuBar::removeAction(action); | |
| 1390 | + } | |
| 1391 | + | |
| 1392 | + QMenuBar::repaint(); | |
| 1393 | + } | |
| 1312 | 1394 | } |
| 1313 | 1395 | |
| 1314 | −void Menu::addMenuItem(const QString& menu, const QString& menuitem, const KeyEvent& shortcutKey) { | |
| 1315 | − QKeySequence shortcut(shortcutKey); | |
| 1316 | − addMenuItem(menu, menuitem, shortcut); | |
| 1396 | +void Menu::addMenuItem(const QString& menuName, const QString& menuitem, bool checkable, bool checked) { | |
| 1397 | + QMenu* menuObj = getMenu(menuName); | |
| 1398 | + if (menuObj) { | |
| 1399 | + if (checkable) { | |
| 1400 | + addCheckableActionToQMenuAndActionHash(menuObj, menuitem, 0, checked, | |
| 1401 | + MenuScriptingInterface::getInstance(), SLOT(menuItemTriggered())); | |
| 1402 | + } else { | |
| 1403 | + addActionToQMenuAndActionHash(menuObj, menuitem, 0, | |
| 1404 | + MenuScriptingInterface::getInstance(), SLOT(menuItemTriggered())); | |
| 1405 | + } | |
| 1406 | + QMenuBar::repaint(); | |
| 1407 | + } | |
| 1317 | 1408 | } |
| 1318 | 1409 | |
| 1319 | −void Menu::addMenuItem(const QString& menu, const QString& menuitem, const QString& shortcutKey) { | |
| 1410 | +void Menu::addMenuItem(const QString& menuName, const QString& menuitem, const KeyEvent& shortcutKey, bool checkable, bool checked) { | |
| 1320 | 1411 | QKeySequence shortcut(shortcutKey); |
| 1321 | − addMenuItem(menu, menuitem, shortcut); | |
| 1412 | + addMenuItem(menuName, menuitem, shortcut, checkable, checked); | |
| 1322 | 1413 | } |
| 1323 | 1414 | |
| 1324 | −void Menu::addMenuItem(const QString& menu, const QString& menuitem, const QKeySequence& shortcutKey) { | |
| 1325 | − QList<QAction*> topLevelMenus = actions(); | |
| 1326 | − foreach (QAction* topLevelMenuAction, topLevelMenus) { | |
| 1327 | − if (topLevelMenuAction->text() == menu) { | |
| 1328 | − // add the menu item here... | |
| 1329 | − QMenu* menuObj = topLevelMenuAction->menu(); | |
| 1330 | − if (menuObj) { | |
| 1331 | − QShortcut* shortcut = new QShortcut(shortcutKey, this); | |
| 1332 | − QAction* menuItemAction = addActionToQMenuAndActionHash(menuObj, menuitem, shortcutKey, | |
| 1333 | − MenuScriptingInterface::getInstance(), SLOT(menuItemTriggered())); | |
| 1415 | +void Menu::addMenuItem(const QString& menuName, const QString& menuitem, const QString& shortcutKey, bool checkable, bool checked) { | |
| 1416 | + QKeySequence shortcut(shortcutKey); | |
| 1417 | + addMenuItem(menuName, menuitem, shortcut, checkable, checked); | |
| 1418 | +} | |
| 1334 | 1419 | |
| 1335 | − connect(shortcut, SIGNAL(activated()), menuItemAction, SLOT(trigger())); | |
| 1336 | − } | |
| 1420 | +void Menu::addMenuItem(const QString& menuName, const QString& menuitem, const QKeySequence& shortcutKey, bool checkable, bool checked) { | |
| 1421 | + QMenu* menuObj = getMenu(menuName); | |
| 1422 | + if (menuObj) { | |
| 1423 | + QShortcut* shortcut = new QShortcut(shortcutKey, this); | |
| 1424 | + QAction* menuItemAction; | |
| 1425 | + if (checkable) { | |
| 1426 | + menuItemAction = addCheckableActionToQMenuAndActionHash(menuObj, menuitem, shortcutKey, checked, | |
| 1427 | + MenuScriptingInterface::getInstance(), SLOT(menuItemTriggered())); | |
| 1428 | + } else { | |
| 1429 | + menuItemAction = addActionToQMenuAndActionHash(menuObj, menuitem, shortcutKey, | |
| 1430 | + MenuScriptingInterface::getInstance(), SLOT(menuItemTriggered())); | |
| 1337 | 1431 | } |
| 1432 | + connect(shortcut, SIGNAL(activated()), menuItemAction, SLOT(trigger())); | |
| 1433 | + QMenuBar::repaint(); | |
| 1338 | 1434 | } |
| 1339 | − QMenuBar::repaint(); | |
| 1340 | 1435 | } |
| 1341 | 1436 | |
| 1342 | 1437 | void Menu::removeMenuItem(const QString& menu, const QString& menuitem) { |
| 1438 | + QMenu* menuObj = getMenu(menu); | |
| 1439 | + if (menuObj) { | |
| 1440 | + removeAction(menuObj, menuitem); | |
| 1441 | + } | |
| 1442 | + QMenuBar::repaint(); | |
| 1343 | 1443 | }; |
| 1344 | 1444 | |
| 1345 | − | |
| 1346 | 1445 | MenuScriptingInterface* MenuScriptingInterface::_instance = NULL; |
| 1347 | 1446 | QMutex MenuScriptingInterface::_instanceMutex; |
| 1348 | 1447 | |
| @@ -1373,34 +1472,64 @@ void MenuScriptingInterface::menuItemTriggered() { · 24 unmodified lines | ||
| 1373 | 1472 | } |
| 1374 | 1473 | } |
| 1375 | 1474 | |
| 1376 | − | |
| 1377 | − | |
| 1378 | −void MenuScriptingInterface::addTopMenu(const QString& menu) { | |
| 1379 | − QMetaObject::invokeMethod(Menu::getInstance(), "addTopMenu", Q_ARG(const QString&, menu)); | |
| 1475 | +void MenuScriptingInterface::addMenu(const QString& menu) { | |
| 1476 | + QMetaObject::invokeMethod(Menu::getInstance(), "addMenu", Q_ARG(const QString&, menu)); | |
| 1380 | 1477 | } |
| 1381 | 1478 | |
| 1382 | −void MenuScriptingInterface::removeTopMenu(const QString& menu) { | |
| 1383 | − QMetaObject::invokeMethod(Menu::getInstance(), "removeTopMenu", Q_ARG(const QString&, menu)); | |
| 1479 | +void MenuScriptingInterface::removeMenu(const QString& menu) { | |
| 1480 | + QMetaObject::invokeMethod(Menu::getInstance(), "removeMenu", Q_ARG(const QString&, menu)); | |
| 1384 | 1481 | } |
| 1385 | 1482 | |
| 1386 | 1483 | void MenuScriptingInterface::addMenuItemWithKeyEvent(const QString& menu, const QString& menuitem, const KeyEvent& shortcutKey) { |
| 1387 | − QMetaObject::invokeMethod(Menu::getInstance(), "addMenuItem", | |
| 1484 | + QMetaObject::invokeMethod(Menu::getInstance(), "addMenuItem", | |
| 1388 | 1485 | Q_ARG(const QString&, menu), |
| 1389 | 1486 | Q_ARG(const QString&, menuitem), |
| 1390 | − Q_ARG(const KeyEvent&, shortcutKey)); | |
| 1487 | + Q_ARG(const KeyEvent&, shortcutKey), | |
| 1488 | + Q_ARG(bool, false), | |
| 1489 | + Q_ARG(bool, false)); | |
| 1391 | 1490 | } |
| 1392 | 1491 | |
| 1393 | 1492 | void MenuScriptingInterface::addMenuItem(const QString& menu, const QString& menuitem, const QString& shortcutKey) { |
| 1394 | 1493 | QMetaObject::invokeMethod(Menu::getInstance(), "addMenuItem", |
| 1395 | 1494 | Q_ARG(const QString&, menu), |
| 1396 | 1495 | Q_ARG(const QString&, menuitem), |
| 1397 | − Q_ARG(const QString&, shortcutKey)); | |
| 1496 | + Q_ARG(const QString&, shortcutKey), | |
| 1497 | + Q_ARG(bool, false), | |
| 1498 | + Q_ARG(bool, false)); | |
| 1398 | 1499 | } |
| 1399 | 1500 | |
| 1400 | 1501 | void MenuScriptingInterface::addMenuItem(const QString& menu, const QString& menuitem) { |
| 1401 | 1502 | QMetaObject::invokeMethod(Menu::getInstance(), "addMenuItem", |
| 1402 | 1503 | Q_ARG(const QString&, menu), |
| 1403 | − Q_ARG(const QString&, menuitem)); | |
| 1504 | + Q_ARG(const QString&, menuitem), | |
| 1505 | + Q_ARG(bool, false), | |
| 1506 | + Q_ARG(bool, false)); | |
| 1507 | +} | |
| 1508 | + | |
| 1509 | +void MenuScriptingInterface::addCheckableMenuItemWithKeyEvent(const QString& menu, const QString& menuitem, const KeyEvent& shortcutKey, bool checked) { | |
| 1510 | + QMetaObject::invokeMethod(Menu::getInstance(), "addMenuItem", | |
| 1511 | + Q_ARG(const QString&, menu), | |
| 1512 | + Q_ARG(const QString&, menuitem), | |
| 1513 | + Q_ARG(const KeyEvent&, shortcutKey), | |
| 1514 | + Q_ARG(bool, true), | |
| 1515 | + Q_ARG(bool, checked)); | |
| 1516 | +} | |
| 1517 | + | |
| 1518 | +void MenuScriptingInterface::addCheckableMenuItem(const QString& menu, const QString& menuitem, const QString& shortcutKey, bool checked) { | |
| 1519 | + QMetaObject::invokeMethod(Menu::getInstance(), "addMenuItem", | |
| 1520 | + Q_ARG(const QString&, menu), | |
| 1521 | + Q_ARG(const QString&, menuitem), | |
| 1522 | + Q_ARG(const QString&, shortcutKey), | |
| 1523 | + Q_ARG(bool, true), | |
| 1524 | + Q_ARG(bool, checked)); | |
| 1525 | +} | |
| 1526 | + | |
| 1527 | +void MenuScriptingInterface::addCheckableMenuItem(const QString& menu, const QString& menuitem, bool checked) { | |
| 1528 | + QMetaObject::invokeMethod(Menu::getInstance(), "addMenuItem", | |
| 1529 | + Q_ARG(const QString&, menu), | |
| 1530 | + Q_ARG(const QString&, menuitem), | |
| 1531 | + Q_ARG(bool, true), | |
| 1532 | + Q_ARG(bool, checked)); | |
| 1404 | 1533 | } |
| 1405 | 1534 | |
| 1406 | 1535 | void MenuScriptingInterface::removeMenuItem(const QString& menu, const QString& menuitem) { |
| @@ -1408,3 +1537,11 @@ void MenuScriptingInterface::removeMenuItem(const QString& menu, const QString& · 1 unmodified line | ||
| 1408 | 1537 | Q_ARG(const QString&, menu), |
| 1409 | 1538 | Q_ARG(const QString&, menuitem)); |
| 1410 | 1539 | }; |
| 1540 | + | |
| 1541 | +bool MenuScriptingInterface::isOptionChecked(const QString& menuOption) { | |
| 1542 | + return Menu::getInstance()->isOptionChecked(menuOption); | |
| 1543 | +} | |
| 1544 | + | |
| 1545 | +void MenuScriptingInterface::setIsOptionChecked(const QString& menuOption, bool isChecked) { | |
| 1546 | + return Menu::getInstance()->setIsOptionChecked(menuOption, isChecked); | |
| 1547 | +} | |