ColorDialog.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "ColorDialog.h"
  2. #include "HuePanel.h"
  3. #include "Common/StyleSheet.h"
  4. #include "Widgets/ScrollArea.h"
  5. #include "Widgets/Button.h"
  6. #include <QApplication>
  7. #include <QIntValidator>
  8. #include <QPainter>
  9. #include <QRegExp>
  10. BrightnessSlider::BrightnessSlider(const QColor &color, QWidget *parent)
  11. : Slider(Qt::Horizontal, parent), m_color(color)
  12. {
  13. setRange(0, 255);
  14. setSingleStep(1);
  15. setColor(color);
  16. connect(this, &BrightnessSlider::valueChanged, this, &BrightnessSlider::onValueChanged);
  17. }
  18. void BrightnessSlider::setColor(const QColor &color)
  19. {
  20. m_color = color;
  21. setValue(color.value());
  22. QString qss = FluentStyleSheet::content("COLOR_DIALOG");
  23. qss = qss.replace("--slider-hue", QString::number(color.hue()));
  24. qss = qss.replace("--slider-saturation", QString::number(color.saturation()));
  25. setStyleSheet(qss);
  26. }
  27. void BrightnessSlider::onValueChanged(int value)
  28. {
  29. m_color.setHsv(m_color.hue(), m_color.saturation(), value);
  30. setColor(m_color);
  31. emit colorChanged(m_color);
  32. }
  33. ColorCard::ColorCard(const QColor &color, QWidget *parent) : QWidget(parent)
  34. {
  35. setFixedSize(44, 128);
  36. setColor(color);
  37. }
  38. void ColorCard::setColor(const QColor &color)
  39. {
  40. m_color = color;
  41. update();
  42. }
  43. void ColorCard::paintEvent(QPaintEvent * /*event*/)
  44. {
  45. QPainter painter(this);
  46. painter.setRenderHints(QPainter::Antialiasing);
  47. painter.setBrush(m_color);
  48. painter.setPen(QColor(0, 0, 0, 13));
  49. painter.drawRoundedRect(rect(), 4, 4);
  50. }
  51. ColorLineEdit::ColorLineEdit(const QString &value, QWidget *parent) : LineEdit(parent)
  52. {
  53. setText(value);
  54. setFixedSize(136, 33);
  55. setIsClearButtonEnabled(true);
  56. setValidator(new QIntValidator(0, 255, this));
  57. connect(this, &ColorLineEdit::textEdited, this, &ColorLineEdit::onTextEdited);
  58. }
  59. void ColorLineEdit::onTextEdited(const QString &text)
  60. {
  61. int pos = 0;
  62. QString input = text;
  63. QValidator::State state = validator()->validate(input, pos);
  64. if (state == QIntValidator::Acceptable) {
  65. emit valueChanged(text);
  66. }
  67. }
  68. HexColorLineEdit::HexColorLineEdit(const QColor &color, QWidget *parent) : ColorLineEdit(color.name().mid(1), parent)
  69. {
  70. setValidator(new QRegExpValidator(QRegExp("[A-Fa-f0-9]{6}"), this));
  71. setTextMargins(4, 0, 33, 0);
  72. prefixLabel = new QLabel("#", this);
  73. prefixLabel->move(7, 2);
  74. prefixLabel->setObjectName("prefixLabel");
  75. }
  76. void HexColorLineEdit::setColor(const QColor &color)
  77. {
  78. setText(color.name().mid(1));
  79. }
  80. ColorDialog::ColorDialog(const QColor &color, const QString &title, QWidget *parent) : MaskDialogBase(parent)
  81. {
  82. m_oldColor = color;
  83. m_color = color;
  84. m_scrollArea = new ScrollArea(this->widget);
  85. m_scrollWidget = new QWidget(m_scrollArea);
  86. m_buttonGroup = new QFrame(this->widget);
  87. m_yesButton = new PrimaryPushButton(tr("OK"), m_buttonGroup);
  88. m_cancelButton = new QPushButton(tr("Cancel"), m_buttonGroup);
  89. m_titleLabel = new QLabel(title, m_scrollWidget);
  90. m_huePanel = new HuePanel(color, m_scrollWidget);
  91. m_newColorCard = new ColorCard(color, m_scrollWidget);
  92. m_oldColorCard = new ColorCard(color, m_scrollWidget);
  93. m_brightSlider = new BrightnessSlider(color, m_scrollWidget);
  94. m_editLabel = new QLabel(tr("Edit Color"), m_scrollWidget);
  95. m_redLabel = new QLabel(tr("Red"), m_scrollWidget);
  96. m_greenLabel = new QLabel(tr("Green"), m_scrollWidget);
  97. m_blueLabel = new QLabel(tr("Blue"), m_scrollWidget);
  98. m_hexLineEdit = new HexColorLineEdit(color, m_scrollWidget);
  99. m_redLineEdit = new ColorLineEdit(QString::number(color.red()), m_scrollWidget);
  100. m_greenLineEdit = new ColorLineEdit(QString::number(color.green()), m_scrollWidget);
  101. m_blueLineEdit = new ColorLineEdit(QString::number(color.blue()), m_scrollWidget);
  102. m_vBoxLayout = new QVBoxLayout(this->widget);
  103. initWidget();
  104. }
  105. void ColorDialog::setColor(const QColor &color, bool movePicker)
  106. {
  107. m_color = QColor(color);
  108. m_brightSlider->setColor(color);
  109. m_newColorCard->setColor(color);
  110. m_hexLineEdit->setColor(color);
  111. m_redLineEdit->setText(QString::number(color.red()));
  112. m_blueLineEdit->setText(QString::number(color.blue()));
  113. m_greenLineEdit->setText(QString::number(color.green()));
  114. if (movePicker) {
  115. m_huePanel->setColor(color);
  116. }
  117. }
  118. void ColorDialog::updateStyle()
  119. {
  120. setStyle(QApplication::style());
  121. m_titleLabel->adjustSize();
  122. m_editLabel->adjustSize();
  123. }
  124. void ColorDialog::initWidget()
  125. {
  126. m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  127. m_scrollArea->setViewportMargins(48, 24, 0, 24);
  128. m_scrollArea->setWidget(m_scrollWidget);
  129. this->widget->setMaximumSize(488, 696);
  130. this->widget->resize(488, 696);
  131. m_scrollWidget->resize(440, 560);
  132. m_buttonGroup->setFixedSize(486, 81);
  133. m_yesButton->setFixedWidth(216);
  134. m_cancelButton->setFixedWidth(216);
  135. this->setShadowEffect(60, QPointF(0, 10), QColor(0, 0, 0, 80));
  136. this->setMaskColor(QColor(0, 0, 0, 76));
  137. setQss();
  138. initLayout();
  139. connectSignalToSlot();
  140. }
  141. void ColorDialog::setQss()
  142. {
  143. m_editLabel->setObjectName("editLabel");
  144. m_titleLabel->setObjectName("titleLabel");
  145. m_yesButton->setObjectName("yesButton");
  146. m_cancelButton->setObjectName("cancelButton");
  147. m_buttonGroup->setObjectName("buttonGroup");
  148. FluentStyleSheet::apply("COLOR_DIALOG", this);
  149. m_titleLabel->adjustSize();
  150. m_editLabel->adjustSize();
  151. }
  152. void ColorDialog::initLayout()
  153. {
  154. m_huePanel->move(0, 46);
  155. m_newColorCard->move(288, 46);
  156. m_oldColorCard->move(288, m_newColorCard->geometry().bottom() + 1);
  157. m_brightSlider->move(0, 324);
  158. m_editLabel->move(0, 381);
  159. m_redLineEdit->move(0, 426);
  160. m_greenLineEdit->move(0, 470);
  161. m_blueLineEdit->move(0, 515);
  162. m_redLabel->move(144, 434);
  163. m_greenLabel->move(144, 478);
  164. m_blueLabel->move(144, 524);
  165. m_hexLineEdit->move(196, 381);
  166. m_vBoxLayout->setSpacing(0);
  167. m_vBoxLayout->setAlignment(Qt::AlignTop);
  168. m_vBoxLayout->setContentsMargins(0, 0, 0, 0);
  169. m_vBoxLayout->addWidget(m_scrollArea, 1);
  170. m_vBoxLayout->addWidget(m_buttonGroup, 0, Qt::AlignBottom);
  171. m_yesButton->move(24, 25);
  172. m_cancelButton->move(250, 25);
  173. }
  174. void ColorDialog::connectSignalToSlot()
  175. {
  176. connect(m_cancelButton, &QPushButton::clicked, this, &ColorDialog::reject);
  177. connect(m_yesButton, &PrimaryPushButton::clicked, this, &ColorDialog::onYesButtonClicked);
  178. connect(m_huePanel, &HuePanel::colorChanged, this, &ColorDialog::onHueChanged);
  179. connect(m_brightSlider, &BrightnessSlider::colorChanged, this, &ColorDialog::onBrightnessChanged);
  180. connect(m_redLineEdit, &ColorLineEdit::valueChanged, this, &ColorDialog::onRedChanged);
  181. connect(m_greenLineEdit, &ColorLineEdit::valueChanged, this, &ColorDialog::onGreenChanged);
  182. connect(m_blueLineEdit, &ColorLineEdit::valueChanged, this, &ColorDialog::onBlueChanged);
  183. connect(m_hexLineEdit, &HexColorLineEdit::valueChanged, this, &ColorDialog::onHexColorChanged);
  184. }
  185. void ColorDialog::onYesButtonClicked()
  186. {
  187. this->accept();
  188. if (m_color != m_oldColor) {
  189. emit colorChanged(m_color);
  190. }
  191. }
  192. void ColorDialog::onHueChanged(const QColor &color)
  193. {
  194. m_color.setHsv(color.hue(), color.saturation(), m_color.value());
  195. setColor(m_color);
  196. }
  197. void ColorDialog::onBrightnessChanged(const QColor &color)
  198. {
  199. m_color.setHsv(m_color.hue(), m_color.saturation(), color.value());
  200. setColor(m_color, false);
  201. }
  202. void ColorDialog::onRedChanged(const QString &red)
  203. {
  204. m_color.setRed(red.toInt());
  205. setColor(m_color);
  206. }
  207. void ColorDialog::onGreenChanged(const QString &green)
  208. {
  209. m_color.setGreen(green.toInt());
  210. setColor(m_color);
  211. }
  212. void ColorDialog::onBlueChanged(const QString &blue)
  213. {
  214. m_color.setBlue(blue.toInt());
  215. setColor(m_color);
  216. }
  217. void ColorDialog::onHexColorChanged(const QString &value)
  218. {
  219. m_color.setNamedColor("#" + value);
  220. setColor(m_color);
  221. }