OBSToggleSwitch.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 "OBSToggleSwitch.hpp"
  15. static QColor blendColors(const QColor &color1, const QColor &color2, float ratio)
  16. {
  17. int r = color1.red() * (1 - ratio) + color2.red() * ratio;
  18. int g = color1.green() * (1 - ratio) + color2.green() * ratio;
  19. int b = color1.blue() * (1 - ratio) + color2.blue() * ratio;
  20. return QColor(r, g, b, 255);
  21. }
  22. OBSToggleSwitch::OBSToggleSwitch(QWidget *parent)
  23. : QAbstractButton(parent),
  24. animHandle(new QPropertyAnimation(this, "xpos", this)),
  25. animBgColor(new QPropertyAnimation(this, "blend", this)),
  26. OBSIdianUtils(this)
  27. {
  28. offPos = rect().width() / 2 - 18;
  29. onPos = rect().width() / 2 + 18;
  30. xPos = offPos;
  31. margin = 3;
  32. setCheckable(true);
  33. setAccessibleName("ToggleSwitch");
  34. installEventFilter(this);
  35. connect(this, &OBSToggleSwitch::clicked, this, &OBSToggleSwitch::onClicked);
  36. connect(animHandle, &QVariantAnimation::valueChanged, this, &OBSToggleSwitch::updateBackgroundColor);
  37. connect(animBgColor, &QVariantAnimation::valueChanged, this, &OBSToggleSwitch::updateBackgroundColor);
  38. }
  39. OBSToggleSwitch::OBSToggleSwitch(bool defaultState, QWidget *parent) : OBSToggleSwitch(parent)
  40. {
  41. setChecked(defaultState);
  42. if (defaultState) {
  43. xPos = onPos;
  44. }
  45. }
  46. void OBSToggleSwitch::animateHandlePosition()
  47. {
  48. animHandle->setStartValue(xPos);
  49. int endPos = onPos;
  50. if ((!isDelayed() && !isChecked()) || (isDelayed() && !pendingStatus))
  51. endPos = offPos;
  52. animHandle->setEndValue(endPos);
  53. animHandle->setDuration(120);
  54. animHandle->start();
  55. }
  56. void OBSToggleSwitch::updateBackgroundColor()
  57. {
  58. QColor offColor = underMouse() ? backgroundInactiveHover : backgroundInactive;
  59. QColor onColor = underMouse() ? backgroundActiveHover : backgroundActive;
  60. if (!isDelayed()) {
  61. int offset = isChecked() ? 0 : offPos;
  62. blend = (float)(xPos - offset) / (float)(onPos);
  63. }
  64. QColor bg = blendColors(offColor, onColor, blend);
  65. if (!isEnabled())
  66. bg = backgroundInactive;
  67. setStyleSheet("background: " + bg.name());
  68. }
  69. void OBSToggleSwitch::changeEvent(QEvent *event)
  70. {
  71. if (event->type() == QEvent::EnabledChange) {
  72. OBSIdianUtils::toggleClass("disabled", !isEnabled());
  73. updateBackgroundColor();
  74. }
  75. }
  76. void OBSToggleSwitch::paintEvent(QPaintEvent *)
  77. {
  78. QStyleOptionButton opt;
  79. opt.initFrom(this);
  80. QPainter p(this);
  81. bool showChecked = isChecked();
  82. if (isDelayed()) {
  83. showChecked = pendingStatus;
  84. }
  85. opt.state.setFlag(QStyle::State_On, showChecked);
  86. opt.state.setFlag(QStyle::State_Off, !showChecked);
  87. opt.state.setFlag(QStyle::State_Sunken, true);
  88. style()->drawPrimitive(QStyle::PE_PanelButtonCommand, &opt, &p, this);
  89. p.setRenderHint(QPainter::Antialiasing, true);
  90. p.setBrush(handleColor);
  91. p.drawEllipse(QRectF(xPos, margin, handleSize, handleSize));
  92. }
  93. void OBSToggleSwitch::showEvent(QShowEvent *e)
  94. {
  95. margin = (rect().height() - handleSize) / 2;
  96. offPos = margin;
  97. onPos = rect().width() - handleSize - margin;
  98. xPos = isChecked() ? onPos : offPos;
  99. updateBackgroundColor();
  100. style()->polish(this);
  101. QAbstractButton::showEvent(e);
  102. }
  103. void OBSToggleSwitch::click()
  104. {
  105. if (!isDelayed())
  106. QAbstractButton::click();
  107. if (isChecked() == pendingStatus)
  108. setPending(!isChecked());
  109. }
  110. void OBSToggleSwitch::onClicked(bool checked)
  111. {
  112. if (delayed)
  113. return;
  114. setPending(checked);
  115. }
  116. void OBSToggleSwitch::setStatus(bool status)
  117. {
  118. if (status == isChecked() && status == pendingStatus)
  119. return;
  120. pendingStatus = status;
  121. setChecked(status);
  122. if (isChecked()) {
  123. animBgColor->setStartValue(0.0f);
  124. animBgColor->setEndValue(1.0f);
  125. } else {
  126. animBgColor->setStartValue(1.0f);
  127. animBgColor->setEndValue(0.0f);
  128. }
  129. animBgColor->setEasingCurve(QEasingCurve::InOutCubic);
  130. animBgColor->setDuration(240);
  131. animBgColor->start();
  132. }
  133. void OBSToggleSwitch::setPending(bool pending)
  134. {
  135. pendingStatus = pending;
  136. animateHandlePosition();
  137. if (!isDelayed())
  138. return;
  139. if (pending) {
  140. emit pendingChecked();
  141. } else {
  142. emit pendingUnchecked();
  143. }
  144. }
  145. void OBSToggleSwitch::setDelayed(bool state)
  146. {
  147. delayed = state;
  148. pendingStatus = isChecked();
  149. }
  150. void OBSToggleSwitch::enterEvent(QEnterEvent *e)
  151. {
  152. setCursor(Qt::PointingHandCursor);
  153. updateBackgroundColor();
  154. QAbstractButton::enterEvent(e);
  155. }
  156. void OBSToggleSwitch::leaveEvent(QEvent *e)
  157. {
  158. updateBackgroundColor();
  159. QAbstractButton::leaveEvent(e);
  160. }
  161. void OBSToggleSwitch::keyReleaseEvent(QKeyEvent *e)
  162. {
  163. if (!isDelayed()) {
  164. QAbstractButton::keyReleaseEvent(e);
  165. return;
  166. }
  167. if (e->key() != Qt::Key_Space) {
  168. return;
  169. }
  170. click();
  171. }
  172. void OBSToggleSwitch::mouseReleaseEvent(QMouseEvent *e)
  173. {
  174. if (!isDelayed()) {
  175. QAbstractButton::mouseReleaseEvent(e);
  176. return;
  177. }
  178. if (e->button() != Qt::LeftButton) {
  179. return;
  180. }
  181. click();
  182. }
  183. QSize OBSToggleSwitch::sizeHint() const
  184. {
  185. return QSize(2 * handleSize, handleSize);
  186. }