menu-radio-item.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #if defined(Hiro_MenuRadioItem)
  2. namespace hiro {
  3. auto pMenuRadioItem::construct() -> void {
  4. qtMenuRadioItem = new QtMenuRadioItem(*this);
  5. qtActionGroup = new QActionGroup(nullptr);
  6. qtMenuRadioItem->setCheckable(true);
  7. qtMenuRadioItem->setActionGroup(qtActionGroup);
  8. qtMenuRadioItem->setChecked(true);
  9. qtMenuRadioItem->connect(qtMenuRadioItem, SIGNAL(triggered()), SLOT(onActivate()));
  10. if(auto parent = _parentMenu()) {
  11. parent->qtMenu->addAction(qtMenuRadioItem);
  12. }
  13. if(auto parent = _parentPopupMenu()) {
  14. parent->qtPopupMenu->addAction(qtMenuRadioItem);
  15. }
  16. setGroup(state().group);
  17. _setState();
  18. }
  19. auto pMenuRadioItem::destruct() -> void {
  20. if(Application::state().quit) return; //TODO: hack
  21. delete qtMenuRadioItem;
  22. delete qtActionGroup;
  23. qtMenuRadioItem = nullptr;
  24. qtActionGroup = nullptr;
  25. }
  26. auto pMenuRadioItem::setChecked() -> void {
  27. _setState();
  28. }
  29. auto pMenuRadioItem::setGroup(sGroup group) -> void {
  30. bool first = true;
  31. if(auto& group = state().group) {
  32. for(auto& weak : group->state.objects) {
  33. if(auto object = weak.acquire()) {
  34. if(auto menuRadioItem = dynamic_cast<mMenuRadioItem*>(object.data())) {
  35. if(auto self = menuRadioItem->self()) {
  36. self->qtMenuRadioItem->setChecked(menuRadioItem->state.checked = first);
  37. first = false;
  38. }
  39. }
  40. }
  41. }
  42. }
  43. _setState();
  44. }
  45. auto pMenuRadioItem::setText(const string& text) -> void {
  46. _setState();
  47. }
  48. auto pMenuRadioItem::_setState() -> void {
  49. if(auto& group = state().group) {
  50. if(auto object = group->object(0)) {
  51. if(auto menuRadioItem = dynamic_cast<mMenuRadioItem*>(object.data())) {
  52. if(auto self = menuRadioItem->self()) {
  53. qtMenuRadioItem->setActionGroup(self->qtActionGroup);
  54. }
  55. }
  56. }
  57. }
  58. qtMenuRadioItem->setChecked(state().checked);
  59. qtMenuRadioItem->setText(QString::fromUtf8(state().text));
  60. }
  61. auto QtMenuRadioItem::onActivate() -> void {
  62. if(p.state().checked) return;
  63. p.self().setChecked();
  64. p.self().doActivate();
  65. }
  66. }
  67. #endif