SettingCard.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #include "SettingCard.h"
  2. #include "Widgets/IconWidget.h"
  3. #include "Widgets/SwitchButton.h"
  4. #include "Widgets/Slider.h"
  5. #include "Widgets/Button.h"
  6. #include "Widgets/ComboBox.h"
  7. #include "QFluentWidgets.h"
  8. #include "DialogBox/ColorDialog.h"
  9. #include <QPainter>
  10. SettingCard::SettingCard(FluentIconBase *ficon, const QString &title, const QString &content, QWidget *parent)
  11. : QFrame(parent), m_ficon(ficon)
  12. {
  13. m_iconLabel = new IconWidget(ficon, this);
  14. m_titleLabel = new QLabel(title, this);
  15. m_contentLabel = new QLabel(content, this);
  16. hBoxLayout = new QHBoxLayout();
  17. vBoxLayout = new QVBoxLayout();
  18. if (content.isEmpty()) {
  19. m_contentLabel->hide();
  20. }
  21. hBoxLayout->setSpacing(0);
  22. hBoxLayout->setContentsMargins(16, 0, 0, 0);
  23. hBoxLayout->setAlignment(Qt::AlignVCenter);
  24. vBoxLayout->setSpacing(0);
  25. vBoxLayout->setContentsMargins(0, 0, 0, 0);
  26. vBoxLayout->setAlignment(Qt::AlignVCenter);
  27. hBoxLayout->addWidget(m_iconLabel, 0, Qt::AlignLeft);
  28. hBoxLayout->addSpacing(16);
  29. hBoxLayout->addLayout(vBoxLayout);
  30. vBoxLayout->addWidget(m_titleLabel, 0, Qt::AlignLeft);
  31. vBoxLayout->addWidget(m_contentLabel, 0, Qt::AlignLeft);
  32. hBoxLayout->addSpacing(16);
  33. hBoxLayout->addStretch(1);
  34. setLayout(hBoxLayout);
  35. m_contentLabel->setObjectName("contentLabel");
  36. FluentStyleSheet::apply("SETTING_CARD", this);
  37. }
  38. void SettingCard::setTitle(const QString &title)
  39. {
  40. m_titleLabel->setText(title);
  41. }
  42. void SettingCard::setContent(const QString &content)
  43. {
  44. m_contentLabel->setText(content);
  45. m_contentLabel->setVisible(!content.isEmpty());
  46. }
  47. FluentIconBase *SettingCard::ficon() const
  48. {
  49. return m_ficon.data();
  50. }
  51. void SettingCard::setValue(const QVariant &) { }
  52. SwitchSettingCard::SwitchSettingCard(FluentIconBase *ficon, const QString &title, const QString &content,
  53. Qfw::ConfigItem *configItem, QWidget *parent)
  54. : SettingCard(ficon, title, content, parent), m_configItem(configItem)
  55. {
  56. m_switchButton = new SwitchButton(tr("Off"), this, IndicatorPosition::RIGHT);
  57. if (configItem) {
  58. setValue(configItem->value());
  59. connect(configItem, &Qfw::ConfigItem::valueChanged, this, &SwitchSettingCard::setValue);
  60. }
  61. // add switch button to layout
  62. hBoxLayout->addWidget(m_switchButton, 0, Qt::AlignRight);
  63. hBoxLayout->addSpacing(16);
  64. connect(m_switchButton, &SwitchButton::checkedChanged, this, &SwitchSettingCard::onCheckedChanged);
  65. }
  66. void SwitchSettingCard::setValue(const QVariant &value)
  67. {
  68. // 设计同步到配置文件里面去
  69. // if self.configItem:
  70. // qconfig.set(self.configItem, isChecked)
  71. m_switchButton->setChecked(value.toBool());
  72. if (value.toBool()) {
  73. m_switchButton->setText(tr("On"));
  74. } else {
  75. m_switchButton->setText(tr("Off"));
  76. }
  77. }
  78. bool SwitchSettingCard::isChecked() const
  79. {
  80. return m_switchButton->isChecked();
  81. }
  82. void SwitchSettingCard::onCheckedChanged(bool isChecked)
  83. {
  84. setValue(isChecked);
  85. emit checkedChanged(isChecked);
  86. }
  87. RangeSettingCard::RangeSettingCard(int min, int max, int value, FluentIconBase *ficon, const QString &title,
  88. const QString &content, QWidget *parent)
  89. : SettingCard(ficon, title, content, parent), m_min(min), m_max(max)
  90. {
  91. m_slider = new Slider(Qt::Horizontal, this);
  92. m_valueLabel = new QLabel(this);
  93. m_slider->setMinimumWidth(268);
  94. m_slider->setSingleStep(1);
  95. m_slider->setRange(m_min, m_max);
  96. m_slider->setValue(value);
  97. m_valueLabel->setNum(value);
  98. hBoxLayout->addStretch(1);
  99. hBoxLayout->addWidget(m_valueLabel, 0, Qt::AlignRight);
  100. hBoxLayout->addSpacing(6);
  101. hBoxLayout->addWidget(m_slider, 0, Qt::AlignRight);
  102. hBoxLayout->addSpacing(16);
  103. m_valueLabel->setObjectName("valueLabel");
  104. // TODO 配置文件修改也能调整这里
  105. connect(m_slider, &Slider::valueChanged, this, &RangeSettingCard::onValueChanged);
  106. }
  107. void RangeSettingCard::setValue(const QVariant &value)
  108. {
  109. // TODO 更新配置文件,记录修改
  110. if (m_slider->value() != value.toInt()) {
  111. m_slider->setValue(value.toInt());
  112. }
  113. m_valueLabel->setNum(value.toInt());
  114. m_valueLabel->adjustSize();
  115. }
  116. void RangeSettingCard::onValueChanged(int value)
  117. {
  118. setValue(value);
  119. emit valueChanged(value);
  120. }
  121. PushSettingCard::PushSettingCard(const QString &text, FluentIconBase *ficon, const QString &title,
  122. const QString &content, QWidget *parent)
  123. : SettingCard(ficon, title, content, parent)
  124. {
  125. button = new QPushButton(text, this);
  126. hBoxLayout->addWidget(button, 0, Qt::AlignRight);
  127. hBoxLayout->addSpacing(16);
  128. connect(button, &QPushButton::clicked, this, &PushSettingCard::clicked);
  129. }
  130. PrimaryPushSettingCard::PrimaryPushSettingCard(const QString &text, FluentIconBase *ficon, const QString &title,
  131. const QString &content, QWidget *parent)
  132. : PushSettingCard(text, ficon, title, content, parent)
  133. {
  134. button->setObjectName("primaryButton");
  135. }
  136. HyperlinkCard::HyperlinkCard(const QString &url, const QString &text, FluentIconBase *ficon, const QString &title,
  137. const QString &content, QWidget *parent)
  138. : SettingCard(ficon, title, content, parent)
  139. {
  140. linkButton = new HyperlinkButton(url, text, this);
  141. hBoxLayout->addWidget(linkButton, 0, Qt::AlignRight);
  142. hBoxLayout->addSpacing(16);
  143. }
  144. ColorPickerButton::ColorPickerButton(const QColor &color, const QString &title, QWidget *parent)
  145. : QPushButton(parent), m_title(title)
  146. {
  147. setFixedSize(96, 32);
  148. setAttribute(Qt::WA_TranslucentBackground);
  149. setColor(color);
  150. setCursor(Qt::PointingHandCursor);
  151. connect(this, &ColorPickerButton::clicked, this, &ColorPickerButton::showColorDialog);
  152. }
  153. void ColorPickerButton::setColor(const QColor &color)
  154. {
  155. m_color = color;
  156. update();
  157. }
  158. void ColorPickerButton::showColorDialog()
  159. {
  160. ColorDialog *w = new ColorDialog(m_color, tr("Choose") + " " + m_title, this->window());
  161. w->updateStyle();
  162. connect(w, &ColorDialog::colorChanged, this, &ColorPickerButton::onColorChanged);
  163. w->exec();
  164. w->deleteLater();
  165. }
  166. void ColorPickerButton::onColorChanged(const QColor &color)
  167. {
  168. setColor(color);
  169. emit colorChanged(color);
  170. }
  171. void ColorPickerButton::paintEvent(QPaintEvent * /*event*/)
  172. {
  173. QPainter painter(this);
  174. painter.setRenderHints(QPainter::Antialiasing);
  175. auto pc = QFWIns.isDarkTheme() ? QColor(255, 255, 255, 10) : QColor(234, 234, 234);
  176. painter.setPen(pc);
  177. painter.setBrush(m_color);
  178. painter.drawRoundedRect(rect().adjusted(1, 1, -1, -1), 5, 5);
  179. }
  180. ColorSettingCard::ColorSettingCard(const QColor &color, FluentIconBase *ficon, const QString &title,
  181. const QString &content, QWidget *parent)
  182. : SettingCard(ficon, title, content, parent), m_color(color)
  183. {
  184. m_colorPicker = new ColorPickerButton(color, title, this);
  185. hBoxLayout->addWidget(m_colorPicker, 0, Qt::AlignRight);
  186. hBoxLayout->addSpacing(16);
  187. connect(m_colorPicker, &ColorPickerButton::colorChanged, this, &ColorSettingCard::onColorChanged);
  188. }
  189. void ColorSettingCard::setValue(const QVariant &value)
  190. {
  191. m_colorPicker->setColor(value.value<QColor>());
  192. /// TODO: 存起来值
  193. }
  194. void ColorSettingCard::onColorChanged(const QColor &color)
  195. {
  196. /// TODO: 存起来值
  197. emit colorChanged(color);
  198. }
  199. ComboBoxSettingCard::ComboBoxSettingCard(const QString &defaultText, const QHash<QString, QVariant> &option,
  200. FluentIconBase *ficon, const QString &title, const QString &content,
  201. QWidget *parent)
  202. : SettingCard(ficon, title, content, parent), m_options(option)
  203. {
  204. m_comboBox = new ComboBox(this);
  205. hBoxLayout->addWidget(m_comboBox, 0, Qt::AlignRight);
  206. hBoxLayout->addSpacing(16);
  207. // QHashIterator<QVariant, QString> i(option);
  208. // while (i.hasNext()) {
  209. // i.next();
  210. // m_comboBox->addItem(i.value(), i.key());
  211. // }
  212. // m_comboBox->setCurrentText(defaultText);
  213. // connect(m_comboBox, &ComboBox::currentIndexChanged, this, &ComboBoxSettingCard::onCurrentIndexChanged);
  214. }
  215. void ComboBoxSettingCard::setValue(const QVariant &value)
  216. {
  217. if (!m_options.values().contains(value)) {
  218. return;
  219. }
  220. // m_comboBox->setCurrentText(m_options[value]);
  221. /// TODO: 存起来值
  222. }
  223. void ComboBoxSettingCard::onCurrentIndexChanged(int index)
  224. {
  225. /// TODO: 存起来值
  226. }