ToggleSwitch.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /******************************************************************************
  2. Copyright (C) 2023 by Dennis Sädtler <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <Idian/ToggleSwitch.hpp>
  15. #include <Idian/moc_ToggleSwitch.cpp>
  16. using idian::ToggleSwitch;
  17. namespace {
  18. QColor blendColors(const QColor &color1, const QColor &color2, float ratio)
  19. {
  20. int r = color1.red() * (1 - ratio) + color2.red() * ratio;
  21. int g = color1.green() * (1 - ratio) + color2.green() * ratio;
  22. int b = color1.blue() * (1 - ratio) + color2.blue() * ratio;
  23. return QColor(r, g, b, 255);
  24. }
  25. } // namespace
  26. ToggleSwitch::ToggleSwitch(QWidget *parent)
  27. : QAbstractButton(parent),
  28. animHandle(new QPropertyAnimation(this, "xpos", this)),
  29. animBgColor(new QPropertyAnimation(this, "blend", this)),
  30. OBSIdianUtils(this)
  31. {
  32. offPos = rect().width() / 2 - 18;
  33. onPos = rect().width() / 2 + 18;
  34. xPos = offPos;
  35. margin = 3;
  36. setCheckable(true);
  37. setAccessibleName("ToggleSwitch");
  38. installEventFilter(this);
  39. connect(this, &ToggleSwitch::clicked, this, &ToggleSwitch::onClicked);
  40. connect(animHandle, &QVariantAnimation::valueChanged, this, &ToggleSwitch::updateBackgroundColor);
  41. connect(animBgColor, &QVariantAnimation::valueChanged, this, &ToggleSwitch::updateBackgroundColor);
  42. }
  43. ToggleSwitch::ToggleSwitch(bool defaultState, QWidget *parent) : ToggleSwitch(parent)
  44. {
  45. setChecked(defaultState);
  46. if (defaultState) {
  47. xPos = onPos;
  48. }
  49. }
  50. void ToggleSwitch::animateHandlePosition()
  51. {
  52. animHandle->setStartValue(xPos);
  53. int endPos = onPos;
  54. if ((!isDelayed() && !isChecked()) || (isDelayed() && !pendingStatus))
  55. endPos = offPos;
  56. animHandle->setEndValue(endPos);
  57. animHandle->setDuration(120);
  58. animHandle->start();
  59. }
  60. void ToggleSwitch::updateBackgroundColor()
  61. {
  62. QColor offColor = underMouse() ? backgroundInactiveHover : backgroundInactive;
  63. QColor onColor = underMouse() ? backgroundActiveHover : backgroundActive;
  64. if (!isDelayed()) {
  65. int offset = isChecked() ? 0 : offPos;
  66. blend = (float)(xPos - offset) / (float)(onPos);
  67. }
  68. QColor bg = blendColors(offColor, onColor, blend);
  69. if (!isEnabled())
  70. bg = backgroundInactive;
  71. setStyleSheet("background: " + bg.name());
  72. }
  73. void ToggleSwitch::changeEvent(QEvent *event)
  74. {
  75. if (event->type() == QEvent::EnabledChange) {
  76. OBSIdianUtils::toggleClass("disabled", !isEnabled());
  77. updateBackgroundColor();
  78. }
  79. }
  80. void ToggleSwitch::paintEvent(QPaintEvent *)
  81. {
  82. QStyleOptionButton opt;
  83. opt.initFrom(this);
  84. QPainter p(this);
  85. bool showChecked = isChecked();
  86. if (isDelayed()) {
  87. showChecked = pendingStatus;
  88. }
  89. opt.state.setFlag(QStyle::State_On, showChecked);
  90. opt.state.setFlag(QStyle::State_Off, !showChecked);
  91. opt.state.setFlag(QStyle::State_Sunken, true);
  92. style()->drawPrimitive(QStyle::PE_PanelButtonCommand, &opt, &p, this);
  93. p.setRenderHint(QPainter::Antialiasing, true);
  94. p.setBrush(handleColor);
  95. p.drawEllipse(QRectF(xPos, margin, handleSize, handleSize));
  96. }
  97. void ToggleSwitch::showEvent(QShowEvent *e)
  98. {
  99. margin = (rect().height() - handleSize) / 2;
  100. offPos = margin;
  101. onPos = rect().width() - handleSize - margin;
  102. xPos = isChecked() ? onPos : offPos;
  103. updateBackgroundColor();
  104. style()->polish(this);
  105. QAbstractButton::showEvent(e);
  106. }
  107. void ToggleSwitch::click()
  108. {
  109. if (!isDelayed())
  110. QAbstractButton::click();
  111. if (isChecked() == pendingStatus)
  112. setPending(!isChecked());
  113. }
  114. void ToggleSwitch::onClicked(bool checked)
  115. {
  116. if (delayed)
  117. return;
  118. setPending(checked);
  119. }
  120. void ToggleSwitch::setStatus(bool status)
  121. {
  122. if (status == isChecked() && status == pendingStatus)
  123. return;
  124. pendingStatus = status;
  125. setChecked(status);
  126. if (isChecked()) {
  127. animBgColor->setStartValue(0.0f);
  128. animBgColor->setEndValue(1.0f);
  129. } else {
  130. animBgColor->setStartValue(1.0f);
  131. animBgColor->setEndValue(0.0f);
  132. }
  133. animBgColor->setEasingCurve(QEasingCurve::InOutCubic);
  134. animBgColor->setDuration(240);
  135. animBgColor->start();
  136. }
  137. void ToggleSwitch::setPending(bool pending)
  138. {
  139. pendingStatus = pending;
  140. animateHandlePosition();
  141. if (!isDelayed())
  142. return;
  143. if (pending) {
  144. emit pendingChecked();
  145. } else {
  146. emit pendingUnchecked();
  147. }
  148. }
  149. void ToggleSwitch::setDelayed(bool state)
  150. {
  151. delayed = state;
  152. pendingStatus = isChecked();
  153. }
  154. void ToggleSwitch::enterEvent(QEnterEvent *e)
  155. {
  156. setCursor(Qt::PointingHandCursor);
  157. updateBackgroundColor();
  158. QAbstractButton::enterEvent(e);
  159. }
  160. void ToggleSwitch::leaveEvent(QEvent *e)
  161. {
  162. updateBackgroundColor();
  163. QAbstractButton::leaveEvent(e);
  164. }
  165. void ToggleSwitch::keyReleaseEvent(QKeyEvent *e)
  166. {
  167. if (!isDelayed()) {
  168. QAbstractButton::keyReleaseEvent(e);
  169. return;
  170. }
  171. if (e->key() != Qt::Key_Space) {
  172. return;
  173. }
  174. click();
  175. }
  176. void ToggleSwitch::mouseReleaseEvent(QMouseEvent *e)
  177. {
  178. if (!isDelayed()) {
  179. QAbstractButton::mouseReleaseEvent(e);
  180. return;
  181. }
  182. if (e->button() != Qt::LeftButton) {
  183. return;
  184. }
  185. click();
  186. }
  187. QSize ToggleSwitch::sizeHint() const
  188. {
  189. return QSize(2 * handleSize, handleSize);
  190. }