CustomColorSettingCard.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "CustomColorSettingCard.h"
  2. #include "Widgets/Button.h"
  3. #include "Layout/VBoxLayout.h"
  4. #include "DialogBox/ColorDialog.h"
  5. #include <QButtonGroup>
  6. CustomColorSettingCard::CustomColorSettingCard(const QVariant &configItem, FluentIconBase *icon, const QString &title,
  7. const QString &content, QWidget *parent)
  8. : ExpandGroupSettingCard(icon, title, content, parent)
  9. {
  10. // TODO: 这里是配置项,未完成
  11. m_configItem = configItem;
  12. m_defaultColor = QColor(configItem.value<QColor>());
  13. m_customColor = QColor(configItem.value<QColor>());
  14. m_choiceLabel = new QLabel(this);
  15. m_radioWidget = new QWidget(view());
  16. m_radioLayout = new QVBoxLayout(m_radioWidget);
  17. m_defaultRadioButton = new RadioButton(tr("Default color"), m_radioWidget);
  18. m_customRadioButton = new RadioButton(tr("Custom color"), m_radioWidget);
  19. m_buttonGroup = new QButtonGroup(this);
  20. m_customColorWidget = new QWidget(view());
  21. m_customColorLayout = new QHBoxLayout(m_customColorWidget);
  22. m_customLabel = new QLabel(tr("Custom color"), m_customColorWidget);
  23. m_chooseColorButton = new QPushButton(tr("Choose color"), m_customColorWidget);
  24. initWidget();
  25. }
  26. void CustomColorSettingCard::initWidget()
  27. {
  28. initLayout();
  29. if (m_defaultColor != m_customColor) {
  30. m_customRadioButton->setChecked(true);
  31. m_chooseColorButton->setEnabled(true);
  32. } else {
  33. m_defaultRadioButton->setChecked(true);
  34. m_chooseColorButton->setEnabled(false);
  35. }
  36. m_choiceLabel->setText(m_buttonGroup->checkedButton()->text());
  37. m_choiceLabel->adjustSize();
  38. m_chooseColorButton->setObjectName("chooseColorButton");
  39. connect(m_buttonGroup, QOverload<QAbstractButton *>::of(&QButtonGroup::buttonClicked), this,
  40. &CustomColorSettingCard::onRadioButtonClicked);
  41. connect(m_chooseColorButton, &QPushButton::clicked, this, &CustomColorSettingCard::showColorDialog);
  42. }
  43. void CustomColorSettingCard::initLayout()
  44. {
  45. addWidget(m_choiceLabel);
  46. m_radioLayout->setSpacing(19);
  47. m_radioLayout->setAlignment(Qt::AlignTop);
  48. m_radioLayout->setContentsMargins(48, 18, 0, 18);
  49. m_buttonGroup->addButton(m_customRadioButton);
  50. m_buttonGroup->addButton(m_defaultRadioButton);
  51. m_radioLayout->addWidget(m_customRadioButton);
  52. m_radioLayout->addWidget(m_defaultRadioButton);
  53. m_radioLayout->setSizeConstraint(QVBoxLayout::SetMinimumSize);
  54. m_customColorLayout->setContentsMargins(48, 18, 44, 18);
  55. m_customColorLayout->addWidget(m_customLabel, 0, Qt::AlignLeft);
  56. m_customColorLayout->addWidget(m_chooseColorButton, 0, Qt::AlignRight);
  57. m_customColorLayout->setSizeConstraint(QHBoxLayout::SetMinimumSize);
  58. viewLayout->setSpacing(0);
  59. viewLayout->setContentsMargins(0, 0, 0, 0);
  60. addGroupWidget(m_radioWidget);
  61. addGroupWidget(m_customColorWidget);
  62. adjustViewSize();
  63. }
  64. void CustomColorSettingCard::onRadioButtonClicked(QAbstractButton *button)
  65. {
  66. if (button->text() == m_choiceLabel->text()) {
  67. return;
  68. }
  69. m_choiceLabel->setText(button->text());
  70. m_choiceLabel->adjustSize();
  71. if (button == m_defaultRadioButton) {
  72. m_chooseColorButton->setDisabled(true);
  73. // qconfig.set(self.configItem, self.defaultColor)
  74. if (m_defaultColor != m_customColor) {
  75. emit colorChanged(m_defaultColor);
  76. }
  77. } else {
  78. m_chooseColorButton->setDisabled(false);
  79. // qconfig.set(self.configItem, self.customColor)
  80. if (m_defaultColor != m_customColor) {
  81. emit colorChanged(m_customColor);
  82. }
  83. }
  84. }
  85. void CustomColorSettingCard::showColorDialog()
  86. {
  87. QScopedPointer<ColorDialog> w(new ColorDialog(m_defaultColor, tr("Choose color"), this->window()));
  88. w->updateStyle();
  89. connect(w.data(), &ColorDialog::colorChanged, this, &CustomColorSettingCard::onCustomColorChanged);
  90. w->exec();
  91. }
  92. void CustomColorSettingCard::onCustomColorChanged(const QColor &color)
  93. {
  94. // qconfig.set(self.configItem, color)
  95. m_customColor = color;
  96. emit colorChanged(color);
  97. }