OptionsSettingCard.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "OptionsSettingCard.h"
  2. #include "Layout/VBoxLayout.h"
  3. #include "Widgets/Button.h"
  4. #include <QButtonGroup>
  5. OptionsSettingCard::OptionsSettingCard(const QVariant &configItem, const QString &title, const QString &content,
  6. const QStringList &texts, QWidget *parent)
  7. : ExpandSettingCard(NEWFLICON(FluentIcon, FOLDER_ADD), title, content, parent),
  8. m_configItem(configItem),
  9. m_configName(configItem.toString()),
  10. m_texts(texts)
  11. {
  12. m_choiceLabel = new QLabel(this);
  13. m_buttonGroup = new QButtonGroup(this);
  14. addWidget(m_choiceLabel);
  15. // create buttons
  16. viewLayout->setSpacing(19);
  17. viewLayout->setContentsMargins(48, 18, 0, 18);
  18. for (int i = 0; i < m_texts.count(); ++i) {
  19. RadioButton *button = new RadioButton(m_texts.at(i), view());
  20. m_buttonGroup->addButton(button);
  21. viewLayout->addWidget(button);
  22. // button->setProperty(m_configName, option);
  23. }
  24. adjustViewSize();
  25. setValue(configItem);
  26. // configItem.valueChanged.connect(self.setValue)
  27. connect(m_buttonGroup, QOverload<QAbstractButton *>::of(&QButtonGroup::buttonClicked), this,
  28. &OptionsSettingCard::onButtonClicked);
  29. }
  30. void OptionsSettingCard::setValue(const QVariant &value)
  31. {
  32. // qconfig.set(self.configItem, value)
  33. for (auto button : viewLayout->widgets()) {
  34. // isChecked = button.property(self.configName) == value
  35. bool isChecked = false;
  36. RadioButton *rb = static_cast<RadioButton *>(button);
  37. rb->setChecked(isChecked);
  38. if (isChecked) {
  39. m_choiceLabel->setText(rb->text());
  40. m_choiceLabel->adjustSize();
  41. }
  42. }
  43. }
  44. void OptionsSettingCard::onButtonClicked(QAbstractButton *button)
  45. {
  46. if (button->text() == m_choiceLabel->text()) {
  47. return;
  48. }
  49. // value = button.property(self.configName)
  50. // qconfig.set(self.configItem, value)
  51. m_choiceLabel->setText(button->text());
  52. m_choiceLabel->adjustSize();
  53. emit optionChanged(m_configItem);
  54. }