lhcolordialog.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include "lhcolordialog.h"
  2. #include <QTimer>
  3. #include <QLabel>
  4. #include <QDialogButtonBox>
  5. #include <QPushButton>
  6. LHColorDialog::LHColorDialog(Skin sk, const QString& qss, QWidget* parent) :
  7. QDialog(parent),
  8. IDropShadowable(this),
  9. m_pLayout(new QHBoxLayout),
  10. m_skinQss(qss)
  11. {
  12. //设置无边框属性
  13. setWindowFlag(Qt::FramelessWindowHint);
  14. //设置背景透明属性
  15. setAttribute(Qt::WA_TranslucentBackground, true);
  16. createColorDialog(sk);
  17. }
  18. // call this constructor
  19. LHColorDialog::LHColorDialog(Skin sk, const QString& qss, const QColor& initial, QWidget* parent) :
  20. QDialog(parent),
  21. IDropShadowable(this),
  22. m_pLayout(new QHBoxLayout),
  23. m_skinQss(qss)
  24. {
  25. //设置无边框属性
  26. setWindowFlags(Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnTopHint);
  27. //设置背景透明属性
  28. setAttribute(Qt::WA_TranslucentBackground, true);
  29. createColorDialog(sk, initial);
  30. }
  31. LHColorDialog::~LHColorDialog()
  32. {
  33. qInfo() << "LHColorDialog: destructor...";
  34. }
  35. void LHColorDialog::setStandardColor(int idx, const QColor &clr)
  36. {
  37. if (nullptr != m_pColorDlg) {
  38. m_pColorDlg->setStandardColor(idx, clr);
  39. }
  40. }
  41. void LHColorDialog::setCustomColor(int idx, const QColor &clr)
  42. {
  43. if (nullptr != m_pColorDlg) {
  44. m_pColorDlg->setCustomColor(idx, clr);
  45. }
  46. }
  47. void LHColorDialog::createColorDialog(Skin sk, const QColor& initial)
  48. {
  49. QHBoxLayout* pLay = new QHBoxLayout(this);
  50. QWidget* pMain = new QWidget(this);
  51. pMain->setLayout(m_pLayout);
  52. // XXX:加载样式表
  53. QFile* pf = nullptr;
  54. if (sk == emDARK) {
  55. pf = new QFile(":/skins/dark.qss", this);
  56. } else if (sk == emBRIGHT) {
  57. pf = new QFile(":/skins/bright.qss", this);
  58. }
  59. if (!pf && !m_skinQss.isEmpty()) {// 加载自定义样式
  60. pMain->setStyleSheet(m_skinQss);
  61. }
  62. if (pf && pf->open(QIODevice::ReadOnly)) {
  63. QString str(QString::fromUtf8(pf->readAll()));
  64. pMain->setStyleSheet(str);
  65. pf->close();
  66. }
  67. pLay->addWidget(pMain);
  68. pLay->setMargin(SHADOW_RADIUS);
  69. m_pColorDlg = new QColorDialog(initial, this);
  70. if (nullptr != m_pColorDlg) {
  71. customDialog();
  72. setButtonName();
  73. connect(m_pColorDlg, &QColorDialog::colorSelected, this, [this](const QColor& clr) {
  74. m_selColor = clr;
  75. });
  76. connect(m_pColorDlg, &QColorDialog::currentColorChanged, this, [this](const QColor& clr) {
  77. m_selColor = clr;
  78. });
  79. connect(m_pColorDlg, &QDialog::finished, this, [this](int code) {
  80. m_selColor = m_pColorDlg->selectedColor();
  81. if (code == QDialog::Accepted) {
  82. accept();
  83. } else {
  84. reject();
  85. }
  86. });
  87. m_pColorDlg->hide();
  88. m_pColorDlg->setOptions(QColorDialog::DontUseNativeDialog);
  89. m_pColorDlg->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
  90. m_pLayout->addWidget(m_pColorDlg);
  91. m_pLayout->setMargin(0);
  92. m_pColorDlg->show();
  93. m_pColorDlg->move(0, 0);
  94. pMain->resize(m_pColorDlg->size());
  95. this->resize(m_pColorDlg->width() + 2 * SHADOW_RADIUS, m_pColorDlg->height() + 2 * SHADOW_RADIUS);
  96. BoxShadow shadows = {0, 0, SHADOW_RADIUS, 0, QColor(0, 0, 0, 90), QSize(0, 0), QImage()};
  97. SetDropShadow(shadows, pMain->size());
  98. }
  99. setMinimumHeight(2 * SHADOW_RADIUS);
  100. setMinimumWidth(2 * SHADOW_RADIUS);
  101. }
  102. void LHColorDialog::setButtonName()
  103. {
  104. if (nullptr == m_pColorDlg) return;
  105. QList<QPushButton*> objs = m_pColorDlg->findChildren<QPushButton*>();
  106. for (auto& btn : objs) {
  107. if (btn->text() == "确定") {// Ok
  108. btn->setObjectName("btn_ok");
  109. } else if (btn->text() == "取消") {// Cancel
  110. btn->setObjectName("btn_cancel");
  111. }
  112. }
  113. }
  114. void LHColorDialog::customDialog()
  115. {
  116. if (nullptr == m_pColorDlg) return;
  117. auto pLay = m_pColorDlg->layout();
  118. if (!pLay) return;
  119. pLay->setMargin(16);// 设置窗口外层边距16px
  120. // 拉伸颜色图
  121. auto pTopLay = pLay->itemAt(0);
  122. if (pTopLay && pTopLay->layout()) {
  123. auto pTop = pTopLay->layout();
  124. auto pRightLay = pTop->itemAt(1);
  125. if (pRightLay && pRightLay->layout()) {
  126. auto pPickLay = pRightLay->layout()->itemAt(0);
  127. if (QBoxLayout* pPick = qobject_cast<QBoxLayout*>(pPickLay->layout())) {
  128. pPick->takeAt(pPick->count() - 1);// 拿掉弹簧
  129. auto cLay = pPick->itemAt(0)->layout();
  130. if (QWidget* pWdg = cLay->itemAt(1)->widget()) {
  131. pWdg->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
  132. }
  133. }
  134. }
  135. }
  136. // 调整按钮
  137. int nLay = pLay->count();
  138. auto subLay = pLay->itemAt(nLay - 1);
  139. if (subLay && subLay->widget()) {
  140. auto pWidget = subLay->widget();
  141. QDialogButtonBox* pbb = qobject_cast<QDialogButtonBox*>(pWidget);
  142. if (!pbb) return;
  143. pbb->clear();
  144. QPushButton* cancel = pbb->addButton("取消", QDialogButtonBox::NoRole);
  145. connect(cancel, &QPushButton::clicked, m_pColorDlg, &QColorDialog::reject);
  146. QPushButton* empty = pbb->addButton("", QDialogButtonBox::NoRole);
  147. empty->setObjectName("empty");
  148. empty->setVisible(false);
  149. empty->setEnabled(false);
  150. QPushButton* ok = pbb->addButton("确定", QDialogButtonBox::NoRole);
  151. connect(ok, &QPushButton::clicked, m_pColorDlg, &QColorDialog::accept);
  152. ok->setDefault(true);
  153. QPushButton* empty2 = pbb->addButton("", QDialogButtonBox::NoRole);
  154. empty2->setObjectName("empty");
  155. empty2->setVisible(false);
  156. empty2->setEnabled(false);
  157. auto pBtnLayout = pbb->layout();
  158. if (QBoxLayout* pboxlay = qobject_cast<QBoxLayout*>(pBtnLayout)) {
  159. pboxlay->setSpacing(8);
  160. }
  161. }
  162. }
  163. void LHColorDialog::mousePressEvent(QMouseEvent *event)
  164. {
  165. if (Qt::LeftButton == event->button()) {
  166. m_bMousePress = true;
  167. m_prePos = event->globalPos()-this->pos();
  168. }
  169. event->accept();
  170. }
  171. void LHColorDialog::mouseReleaseEvent(QMouseEvent *event)
  172. {
  173. m_bMousePress = false;
  174. event->accept();
  175. }
  176. void LHColorDialog::mouseMoveEvent(QMouseEvent *event)
  177. {
  178. if (m_bMousePress && (Qt::LeftButton & event->buttons())) {
  179. this->move(event->globalPos() - m_prePos);
  180. }
  181. event->accept();
  182. }