OBSToggleSwitch.cpp 5.6 KB

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