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