2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-31 06:26:18 +00:00

Refactor Ui::Radiobutton. Add Ui::Radioenum<Enum>.

Now group of Ui::Radiobutton instances share Ui::RadiobuttonGroup.
All value management is done through the group instance, not through
separate radio buttons. Also a template for groups over enums added.
This commit is contained in:
John Preston
2017-03-19 00:06:10 +03:00
parent 0a40bf2071
commit 12cbf78191
17 changed files with 247 additions and 170 deletions

View File

@@ -91,6 +91,8 @@ public:
}
void setValue(int value);
private:
friend class Radiobutton;
void registerButton(Radiobutton *button) {
if (!base::contains(_buttons, button)) {
_buttons.push_back(button);
@@ -100,11 +102,6 @@ public:
_buttons.erase(std::remove(_buttons.begin(), _buttons.end(), button), _buttons.end());
}
private:
friend class Radiobutton;
void registerButton(Radiobutton *button);
void unregisterButton(Radiobutton *button);
int _value = 0;
bool _hasValue = false;
base::lambda<void(int value)> _changedCallback;
@@ -116,9 +113,6 @@ class Radiobutton : public RippleButton {
public:
Radiobutton(QWidget *parent, const std::shared_ptr<RadiobuttonGroup> &group, int value, const QString &text, const style::Checkbox &st = st::defaultCheckbox);
RadiobuttonGroup *group() const {
return _group.get();
}
QMargins getMargins() const override {
return _st.margin;
}
@@ -152,4 +146,48 @@ private:
};
template <typename Enum>
class Radioenum;
template <typename Enum>
class RadioenumGroup {
public:
RadioenumGroup() = default;
RadioenumGroup(Enum value) : _group(static_cast<int>(value)) {
}
template <typename Callback>
void setChangedCallback(Callback &&callback) {
_group.setChangedCallback([callback](int value) {
callback(static_cast<Enum>(value));
});
}
bool hasValue() const {
return _group.hasValue();
}
Enum value() const {
return static_cast<Enum>(_group.value());
}
void setValue(Enum value) {
_group.setValue(static_cast<int>(value));
}
private:
template <typename OtherEnum>
friend class Radioenum;
RadiobuttonGroup _group;
};
template <typename Enum>
class Radioenum : public Radiobutton {
public:
Radioenum(QWidget *parent, const std::shared_ptr<RadioenumGroup<Enum>> &group, Enum value, const QString &text, const style::Checkbox &st = st::defaultCheckbox)
: Radiobutton(parent, std::shared_ptr<RadiobuttonGroup>(group, &group->_group), static_cast<int>(value), text, st) {
}
};
} // namespace Ui