radio-button.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #if defined(Hiro_RadioButton)
  2. namespace hiro {
  3. auto pRadioButton::construct() -> void {
  4. qtWidget = qtRadioButton = new QtRadioButton(*this);
  5. qtRadioButton->setCheckable(true);
  6. qtRadioButton->setToolButtonStyle(Qt::ToolButtonTextOnly);
  7. qtRadioButton->connect(qtRadioButton, SIGNAL(toggled(bool)), SLOT(onActivate()));
  8. pWidget::construct();
  9. setGroup(state().group);
  10. _setState();
  11. }
  12. auto pRadioButton::destruct() -> void {
  13. if(qtRadioButton) delete qtRadioButton;
  14. qtWidget = qtRadioButton = nullptr;
  15. }
  16. auto pRadioButton::minimumSize() const -> Size {
  17. auto size = pFont::size(qtWidget->font(), state().text);
  18. if(state().orientation == Orientation::Horizontal) {
  19. size.setWidth(size.width() + state().icon.width());
  20. size.setHeight(max(size.height(), state().icon.height()));
  21. }
  22. if(state().orientation == Orientation::Vertical) {
  23. size.setWidth(max(size.width(), state().icon.width()));
  24. size.setHeight(size.height() + state().icon.height());
  25. }
  26. return {size.width() + 20, size.height() + 12};
  27. }
  28. auto pRadioButton::setBordered(bool bordered) -> void {
  29. }
  30. auto pRadioButton::setChecked() -> void {
  31. _setState();
  32. }
  33. auto pRadioButton::setGroup(sGroup group) -> void {
  34. bool first = true;
  35. if(auto& group = state().group) {
  36. group->self()->lock();
  37. for(auto& weak : group->state.objects) {
  38. if(auto object = weak.acquire()) {
  39. if(auto radioButton = dynamic_cast<mRadioButton*>(object.data())) {
  40. if(auto self = radioButton->self()) {
  41. self->qtRadioButton->setChecked(radioButton->state.checked = first);
  42. first = false;
  43. }
  44. }
  45. }
  46. }
  47. group->self()->unlock();
  48. }
  49. }
  50. auto pRadioButton::setIcon(const image& icon) -> void {
  51. _setState();
  52. }
  53. auto pRadioButton::setOrientation(Orientation orientation) -> void {
  54. _setState();
  55. }
  56. auto pRadioButton::setText(const string& text) -> void {
  57. _setState();
  58. }
  59. auto pRadioButton::_setState() -> void {
  60. if(auto& group = state().group) {
  61. group->self()->lock();
  62. for(auto& weak : group->state.objects) {
  63. if(auto object = weak.acquire()) {
  64. if(auto radioButton = dynamic_cast<mRadioButton*>(object.data())) {
  65. if(auto self = radioButton->self()) {
  66. self->qtRadioButton->setChecked(radioButton->state.checked);
  67. }
  68. }
  69. }
  70. }
  71. group->self()->unlock();
  72. }
  73. qtRadioButton->setIconSize(QSize(state().icon.width(), state().icon.height()));
  74. qtRadioButton->setIcon(CreateIcon(state().icon));
  75. qtRadioButton->setStyleSheet("text-align: top;");
  76. switch(state().orientation) {
  77. case Orientation::Horizontal: qtRadioButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); break;
  78. case Orientation::Vertical: qtRadioButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); break;
  79. }
  80. qtRadioButton->setText(QString::fromUtf8(state().text));
  81. }
  82. auto QtRadioButton::onActivate() -> void {
  83. if(auto& group = p.state().group) {
  84. if(!group->self()->locked()) {
  85. bool wasChecked = p.state().checked;
  86. p.self().setChecked();
  87. if(!wasChecked) p.self().doActivate();
  88. }
  89. }
  90. }
  91. }
  92. #endif