volume-control.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "volume-control.hpp"
  2. #include "qt-wrappers.hpp"
  3. #include <QHBoxLayout>
  4. #include <QVBoxLayout>
  5. #include <QSlider>
  6. #include <QLabel>
  7. #include <string>
  8. using namespace std;
  9. void VolControl::OBSVolumeChanged(void *data, calldata_t calldata)
  10. {
  11. VolControl *volControl = static_cast<VolControl*>(data);
  12. int vol = (int)(calldata_float(calldata, "volume") * 100.0f + 0.5f);
  13. QMetaObject::invokeMethod(volControl, "VolumeChanged",
  14. Q_ARG(int, vol));
  15. }
  16. void VolControl::VolumeChanged(int vol)
  17. {
  18. signalChanged = false;
  19. slider->setValue(vol);
  20. signalChanged = true;
  21. }
  22. void VolControl::SliderChanged(int vol)
  23. {
  24. if (signalChanged) {
  25. signal_handler_disconnect(obs_source_signalhandler(source),
  26. "volume", OBSVolumeChanged, this);
  27. obs_source_setvolume(source, float(vol)*0.01f);
  28. signal_handler_connect(obs_source_signalhandler(source),
  29. "volume", OBSVolumeChanged, this);
  30. }
  31. volLabel->setText(QString::number(vol));
  32. }
  33. VolControl::VolControl(OBSSource source_)
  34. : source (source_),
  35. signalChanged (true)
  36. {
  37. QVBoxLayout *mainLayout = new QVBoxLayout();
  38. QHBoxLayout *textLayout = new QHBoxLayout();
  39. int vol = int(obs_source_getvolume(source) * 100.0f);
  40. nameLabel = new QLabel();
  41. volLabel = new QLabel();
  42. slider = new QSlider(Qt::Horizontal);
  43. QFont font = nameLabel->font();
  44. font.setPointSize(font.pointSize()-1);
  45. nameLabel->setText(obs_source_getname(source));
  46. nameLabel->setFont(font);
  47. volLabel->setText(QString::number(vol));
  48. volLabel->setFont(font);
  49. slider->setMinimum(0);
  50. slider->setMaximum(100);
  51. slider->setValue(vol);
  52. //slider->setMaximumHeight(16);
  53. textLayout->setContentsMargins(0, 0, 0, 0);
  54. textLayout->addWidget(nameLabel);
  55. textLayout->addWidget(volLabel);
  56. textLayout->setAlignment(nameLabel, Qt::AlignLeft);
  57. textLayout->setAlignment(volLabel, Qt::AlignRight);
  58. mainLayout->setContentsMargins(4, 4, 4, 4);
  59. mainLayout->setSpacing(2);
  60. mainLayout->addItem(textLayout);
  61. mainLayout->addWidget(slider);
  62. setLayout(mainLayout);
  63. signal_handler_connect(obs_source_signalhandler(source),
  64. "volume", OBSVolumeChanged, this);
  65. QWidget::connect(slider, SIGNAL(valueChanged(int)),
  66. this, SLOT(SliderChanged(int)));
  67. }
  68. VolControl::~VolControl()
  69. {
  70. signal_handler_disconnect(obs_source_signalhandler(source),
  71. "volume", OBSVolumeChanged, this);
  72. }